summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/broadband_shellcommands.pm
blob: d3e495c459e99c2ccdfaa1184eab9365d4a19d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package FS::part_export::broadband_shellcommands;

use strict;
use vars qw(@ISA %info);
use Tie::IxHash;
use FS::part_export;

@ISA = qw(FS::part_export);

tie my %options, 'Tie::IxHash',
  'user'          => { label   => 'Remote username',
                       default => 'freeside' },
  'insert'        => { label   => 'Insert command',
                       default => 'php provision.php --mac=$mac_addr --plan=$plan_id --account=active',
                     },
  'delete'        => { label   => 'Delete command',
                       default  => '',
                     },
  'replace'       => { label   => 'Modification command',
                       default => '',
                     },
  'suspend'       => { label   => 'Suspension command',
                       default => 'php provision.php --mac=$mac_addr --plan=$plan_id --account=suspend',
                     },
  'unsuspend'     => { label   => 'Unsuspension command',
                       default => '',
                     },
  'uppercase_mac' => { label   => 'Force MACs to uppercase', 
                       type   => 'checkbox',
                     }
;

%info = (
  'svc'     => 'svc_broadband',
  'desc'    => 'Run remote commands via SSH, for svc_broadband services',
  'options' => \%options,
  'notes'   => <<'END'
Run remote commands via SSH, for broadband services.
<BR><BR>
All fields in svc_broadband are available for interpolation, as well as pkgnum, locationnum and custnum (prefixed with <code>new_</code> or <code>old_</code>
for replace operations).
END
);


sub rebless { shift; }

sub _export_insert {
  my($self) = shift;
  $self->_export_command('insert', @_);
}

sub _export_delete {
  my($self) = shift;
  $self->_export_command('delete', @_);
}

sub _export_suspend {
  my($self) = shift;
  $self->_export_command('suspend', @_);
}

sub _export_unsuspend {
  my($self) = shift;
  $self->_export_command('unsuspend', @_);
}

sub _export_command {
  my ( $self, $action, $svc_broadband) = (shift, shift, shift);
  my $command = $self->option($action);
  return '' if $command =~ /^\s*$/;

  my $command_string = $self->_export_subvars( $svc_broadband, $command );

  $self->shellcommands_queue( $svc_broadband->svcnum,
    user    => $self->option('user')||'root',
    host    => $self->machine,
    command => $command_string,
  );
}

sub _export_subvars {
  my( $self, $svc_broadband, $command ) = @_;

  no strict 'vars';
  {
    no strict 'refs';
    ${$_} = $svc_broadband->getfield($_) foreach $svc_broadband->fields;
  }

  $mac_addr = uc $mac_addr
    if $self->option('uppercase_mac');

  my $cust_pkg = $svc_broadband->cust_svc->cust_pkg;
  $pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
  $locationnum = $cust_pkg ? $cust_pkg->locationnum : '';
  $custnum = $cust_pkg ? $cust_pkg->custnum : '';

  eval(qq("$command"));
}

sub _export_replace {
  my($self, $new, $old ) = (shift, shift, shift);
  my $command = $self->option('replace');

  my $command_string = $self->_export_subvars_replace( $new, $old, $command );

  $self->shellcommands_queue( $new->svcnum,
    user    => $self->option('user')||'root',
    host    => $self->machine,
    command => $command_string,
  );
}

sub _export_subvars_replace {
  my( $self, $new, $old, $command ) = @_;

  no strict 'vars';
  {
    no strict 'refs';
    ${"old_$_"} = $old->getfield($_) foreach $old->fields;
    ${"new_$_"} = $new->getfield($_) foreach $new->fields;
  }

  if ( $self->option('uppercase_mac') ) {
    $old_mac_addr = uc $old_mac_addr;
    $new_mac_addr = uc $new_mac_addr;
  }

  my $old_cust_pkg = $old->cust_svc->cust_pkg;
  my $new_cust_pkg = $new->cust_svc->cust_pkg;
  $old_pkgnum = $old_cust_pkg ? $old_cust_pkg->pkgnum : '';
  $old_locationnum = $old_cust_pkg ? $old_cust_pkg->locationnum : '';
  $old_custnum = $old_cust_pkg ? $old_cust_pkg->custnum : '';
  $new_pkgnum = $new_cust_pkg ? $new_cust_pkg->pkgnum : '';
  $new_locationnum = $new_cust_pkg ? $new_cust_pkg->locationnum : '';
  $new_custnum = $new_cust_pkg ? $new_cust_pkg->custnum : '';

  eval(qq("$command"));
}


#a good idea to queue anything that could fail or take any time
sub shellcommands_queue {
  my( $self, $svcnum ) = (shift, shift);
  my $queue = new FS::queue {
    'svcnum' => $svcnum,
    'job'    => "FS::part_export::broadband_shellcommands::ssh_cmd",
  };
  $queue->insert( @_ );
}

sub ssh_cmd { #subroutine, not method
  use Net::OpenSSH;
  my $opt = { @_ };
  my $ssh = Net::OpenSSH->new($opt->{'user'}.'@'.$opt->{'host'});
  die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error;
  my ($output, $errput) = $ssh->capture2($opt->{'command'});
  die "Error running SSH command: ". $ssh->error if $ssh->error;
  die $errput if $errput;
  die $output if $output;
  '';
}

1;