c7f0fbb33f57d82f7c5486e833d944db3c8010d8
[freeside.git] / FS / FS / part_export / broadband_shellcommands.pm
1 package FS::part_export::broadband_shellcommands;
2
3 use strict;
4 use vars qw(@ISA %info);
5 use Tie::IxHash;
6 use FS::part_export;
7
8 @ISA = qw(FS::part_export);
9
10 tie my %options, 'Tie::IxHash',
11   'user' => { label=>'Remote username', default=>'freeside' },
12   'insert' => { label=>'Insert command',
13                  default=>'php provision.php --mac=$mac_addr --plan=$plan_id --account=active',
14                },
15   'delete'  => { label=>'Delete command',
16                   default=>'',
17                 },
18   'suspend'  => { label=>'Suspension command',
19                   default=>'php provision.php --mac=$mac_addr --plan=$plan_id --account=suspend',
20                 },
21   'unsuspend'=> { label=>'Unsuspension command',
22                   default=>'',
23                 },
24   'uppercase_mac' => { label => 'Force MACs to uppercase', 
25                         type => 'checkbox', }
26 ;
27
28 %info = (
29   'svc'     => 'svc_broadband',
30   'desc'    => 'Run remote commands via SSH, for svc_broadband services',
31   'options' => \%options,
32   'notes'   => <<'END'
33 END
34 );
35
36
37 sub rebless { shift; }
38
39 sub _export_insert {
40   my($self) = shift;
41   $self->_export_command('insert', @_);
42 }
43
44 sub _export_delete {
45   my($self) = shift;
46   $self->_export_command('delete', @_);
47 }
48
49 sub _export_suspend {
50   my($self) = shift;
51   $self->_export_command('suspend', @_);
52 }
53
54 sub _export_unsuspend {
55   my($self) = shift;
56   $self->_export_command('unsuspend', @_);
57 }
58
59 sub _export_command {
60   my ( $self, $action, $svc_broadband) = (shift, shift, shift);
61   my $command = $self->option($action);
62   return '' if $command =~ /^\s*$/;
63
64   #set variable for the command
65   no strict 'vars';
66   {
67     no strict 'refs';
68     ${$_} = $svc_broadband->getfield($_) foreach $svc_broadband->fields;
69   }
70
71   if ( $self->option('uppercase_mac') ) {
72         $mac_addr = uc $mac_addr;
73   }
74
75   #done setting variables for the command
76
77   $self->shellcommands_queue( $svc_broadband->svcnum,
78     user         => $self->option('user')||'root',
79     host         => $self->machine,
80     command      => eval(qq("$command")),
81   );
82 }
83
84 sub _export_replace {
85     '';
86 }
87
88 #a good idea to queue anything that could fail or take any time
89 sub shellcommands_queue {
90   my( $self, $svcnum ) = (shift, shift);
91   my $queue = new FS::queue {
92     'svcnum' => $svcnum,
93     'job'    => "FS::part_export::broadband_shellcommands::ssh_cmd",
94   };
95   $queue->insert( @_ );
96 }
97
98 sub ssh_cmd { #subroutine, not method
99   use Net::OpenSSH;
100   my $opt = { @_ };
101   my $ssh = Net::OpenSSH->new($opt->{'user'}.'@'.$opt->{'host'});
102   die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error;
103   my ($output, $errput) = $ssh->capture2($opt->{'command'});
104   die "Error running SSH command: ". $ssh->error if $ssh->error;
105   die $errput if $errput;
106   die $output if $output;
107   '';
108 }
109