1 package FS::part_export::broadband_shellcommands;
4 use vars qw(@ISA %info);
8 @ISA = qw(FS::part_export);
10 tie my %options, 'Tie::IxHash',
11 'user' => { label => 'Remote username',
12 default => 'freeside' },
13 'insert' => { label => 'Insert command',
14 default => 'php provision.php --mac=$mac_addr --plan=$plan_id --account=active',
16 'delete' => { label => 'Delete command',
19 'replace' => { label => 'Modification command',
22 'suspend' => { label => 'Suspension command',
23 default => 'php provision.php --mac=$mac_addr --plan=$plan_id --account=suspend',
25 'unsuspend' => { label => 'Unsuspension command',
28 'uppercase_mac' => { label => 'Force MACs to uppercase',
34 'svc' => 'svc_broadband',
35 'desc' => 'Run remote commands via SSH, for svc_broadband services',
36 'options' => \%options,
38 Run remote commands via SSH, for broadband services.
40 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>
41 for replace operations).
46 sub rebless { shift; }
50 $self->_export_command('insert', @_);
55 $self->_export_command('delete', @_);
60 $self->_export_command('suspend', @_);
63 sub _export_unsuspend {
65 $self->_export_command('unsuspend', @_);
69 my ( $self, $action, $svc_broadband) = (shift, shift, shift);
70 my $command = $self->option($action);
71 return '' if $command =~ /^\s*$/;
73 #set variables for the command
77 ${$_} = $svc_broadband->getfield($_) foreach $svc_broadband->fields;
80 $mac_addr = uc $mac_addr
81 if $self->option('uppercase_mac');
83 my $cust_pkg = $svc_broadband->cust_svc->cust_pkg;
84 $pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
85 $locationnum = $cust_pkg ? $cust_pkg->locationnum : '';
86 $custnum = $cust_pkg ? $cust_pkg->custnum : '';
88 #done setting variables for the command
90 $self->shellcommands_queue( $svc_broadband->svcnum,
91 user => $self->option('user')||'root',
92 host => $self->machine,
93 command => eval(qq("$command")),
98 my($self, $new, $old ) = (shift, shift, shift);
99 my $command = $self->option('replace');
101 #set variable for the command
105 ${"old_$_"} = $old->getfield($_) foreach $old->fields;
106 ${"new_$_"} = $new->getfield($_) foreach $new->fields;
109 if ( $self->option('uppercase_mac') ) {
110 $old_mac_addr = uc $old_mac_addr;
111 $new_mac_addr = uc $new_mac_addr;
114 my $old_cust_pkg = $old->cust_svc->cust_pkg;
115 my $new_cust_pkg = $new->cust_svc->cust_pkg;
116 $old_pkgnum = $old_cust_pkg ? $old_cust_pkg->pkgnum : '';
117 $old_locationnum = $old_cust_pkg ? $old_cust_pkg->locationnum : '';
118 $old_custnum = $old_cust_pkg ? $old_cust_pkg->custnum : '';
119 $new_pkgnum = $new_cust_pkg ? $new_cust_pkg->pkgnum : '';
120 $new_locationnum = $new_cust_pkg ? $new_cust_pkg->locationnum : '';
121 $new_custnum = $new_cust_pkg ? $new_cust_pkg->custnum : '';
123 #done setting variables for the command
125 $self->shellcommands_queue( $new->svcnum,
126 user => $self->option('user')||'root',
127 host => $self->machine,
128 command => eval(qq("$command")),
132 #a good idea to queue anything that could fail or take any time
133 sub shellcommands_queue {
134 my( $self, $svcnum ) = (shift, shift);
135 my $queue = new FS::queue {
137 'job' => "FS::part_export::broadband_shellcommands::ssh_cmd",
139 $queue->insert( @_ );
142 sub ssh_cmd { #subroutine, not method
145 my $ssh = Net::OpenSSH->new($opt->{'user'}.'@'.$opt->{'host'});
146 die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error;
147 my ($output, $errput) = $ssh->capture2($opt->{'command'});
148 die "Error running SSH command: ". $ssh->error if $ssh->error;
149 die $errput if $errput;
150 die $output if $output;