reload after adding extensions
[freeside.git] / FS / FS / part_export / phone_shellcommands.pm
1 package FS::part_export::phone_shellcommands;
2
3 use strict;
4 use vars qw(@ISA %info);
5 use Tie::IxHash;
6 use String::ShellQuote;
7 use FS::part_export;
8
9 @ISA = qw(FS::part_export);
10
11 #TODO
12 #- modify command (get something from freepbx for changing PINs)
13 #- suspension/unsuspension
14
15 tie my %options, 'Tie::IxHash',
16   'user'      => { label=>'Remote username', default=>'root', },
17   'useradd'   => { label=>'Insert command', }, 
18   'userdel'   => { label=>'Delete command', }, 
19   'usermod'   => { label=>'Modify command', }, 
20   'suspend'   => { label=>'Suspension command', }, 
21   'unsuspend' => { label=>'Unsuspension command', }, 
22 ;
23
24 %info = (
25   'svc'     => 'svc_phone',
26   'desc'    => 'Run remote commands via SSH, for phone numbers',
27   'options' => \%options,
28   'notes'   => <<'END'
29 Run remote commands via SSH, for phone numbers.  You will need to
30 <a href="../docs/ssh.html">setup SSH for unattended operation</a>.
31 <BR><BR>Use these buttons for some useful presets:
32 <UL>
33   <LI>
34     <INPUT TYPE="button" VALUE="FreePBX (build_exten CLI module needed)" onClick='
35       this.form.user.value = "root";
36       this.form.useradd.value = "build_exten.php --create --exten $phonenum --name $cust_name --vm-password $pin && /usr/share/asterisk/bin/module_admin reload";
37       this.form.userdel.value = "build_exten.php --delete --exten $phonenum && /usr/share/asterisk/bin/module_admin reload";
38       this.form.usermod.value = "";
39       this.form.suspend.value = "";
40       this.form.unsuspend.value = "";
41     '>
42 </UL>
43
44 The following variables are available for interpolation (prefixed with new_ or
45 old_ for replace operations):
46 <UL>
47   <LI><code>$countrycode</code> - Country code
48   <LI><code>$phonenum</code> - Phone number
49   <LI><code>$pin</code> - Personal identification number
50   <LI><code>$cust_name</code> - Customer name
51 </UL>
52 END
53 );
54
55 sub rebless { shift; }
56
57 sub _export_insert {
58   my($self) = shift;
59   $self->_export_command('useradd', @_);
60 }
61
62 sub _export_delete {
63   my($self) = shift;
64   $self->_export_command('userdel', @_);
65 }
66
67 sub _export_suspend {
68   my($self) = shift;
69   $self->_export_command('suspend', @_);
70 }
71
72 sub _export_unsuspend {
73   my($self) = shift;
74   $self->_export_command('unsuspend', @_);
75 }
76
77 sub _export_command {
78   my ( $self, $action, $svc_phone) = (shift, shift, shift);
79   my $command = $self->option($action);
80   return '' if $command =~ /^\s*$/;
81
82   #set variable for the command
83   no strict 'vars';
84   {
85     no strict 'refs';
86     ${$_} = $svc_phone->getfield($_) foreach $svc_phone->fields;
87   }
88   my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
89   my $cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
90   $cust_name = shell_quote $cust_name;
91   #done setting variables for the command
92
93   $self->shellcommands_queue( $svc_phone->svcnum,
94     user         => $self->option('user')||'root',
95     host         => $self->machine,
96     command      => eval(qq("$command")),
97   );
98 }
99
100 sub _export_replace {
101   my($self, $new, $old ) = (shift, shift, shift);
102   my $command = $self->option('usermod');
103   
104   #set variable for the command
105   no strict 'vars';
106   {
107     no strict 'refs';
108     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
109     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
110   }
111
112   my $cust_pkg = $new->cust_svc->cust_pkg;
113   my $new_cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
114   $new_cust_name = shell_quote $new_cust_name;
115   #done setting variables for the command
116
117   $self->shellcommands_queue( $new->svcnum,
118     user         => $self->option('user')||'root',
119     host         => $self->machine,
120     command      => eval(qq("$command")),
121   );
122 }
123
124 #a good idea to queue anything that could fail or take any time
125 sub shellcommands_queue {
126   my( $self, $svcnum ) = (shift, shift);
127   my $queue = new FS::queue {
128     'svcnum' => $svcnum,
129     'job'    => "FS::part_export::phone_shellcommands::ssh_cmd",
130   };
131   $queue->insert( @_ );
132 }
133
134 sub ssh_cmd { #subroutine, not method
135   use Net::SSH '0.08';
136   &Net::SSH::ssh_cmd( { @_ } );
137 }
138