add pkgnum var to phone_shellcommands export, RT#20725
[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="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Administration:SSH_Keys">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 --directdid 1$phonenum --sip-secret $sip_password --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 = "build_exten.php --modify --exten $new_phonenum --directdid 1$new_phonenum --sip-secret $new_sip_password --name $new_cust_name --vm-password $new_pin && /usr/share/asterisk/bin/module_admin reload";
39       this.form.suspend.value = "";
40       this.form.unsuspend.value = "";
41     '> (Important note: Reduce freeside-queued "max_kids" to 1 when using FreePBX integration)
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>$sip_password</code> - SIP secret (quoted for the shell)
50   <LI><code>$pin</code> - Personal identification number
51   <LI><code>$cust_name</code> - Customer name (quoted for the shell)
52   <LI><code>$pkgnum</code> - Internal package number
53 </UL>
54 END
55 );
56
57 sub rebless { shift; }
58
59 sub _export_insert {
60   my($self) = shift;
61   $self->_export_command('useradd', @_);
62 }
63
64 sub _export_delete {
65   my($self) = shift;
66   $self->_export_command('userdel', @_);
67 }
68
69 sub _export_suspend {
70   my($self) = shift;
71   $self->_export_command('suspend', @_);
72 }
73
74 sub _export_unsuspend {
75   my($self) = shift;
76   $self->_export_command('unsuspend', @_);
77 }
78
79 sub _export_command {
80   my ( $self, $action, $svc_phone) = (shift, shift, shift);
81   my $command = $self->option($action);
82   return '' if $command =~ /^\s*$/;
83
84   #set variable for the command
85   no strict 'vars';
86   {
87     no strict 'refs';
88     ${$_} = $svc_phone->getfield($_) foreach $svc_phone->fields;
89   }
90   my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
91   my $pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
92   my $cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
93   $cust_name = shell_quote $cust_name;
94   my $sip_password = shell_quote $svc_phone->sip_password;
95   #done setting variables for the command
96
97   $self->shellcommands_queue( $svc_phone->svcnum,
98     user         => $self->option('user')||'root',
99     host         => $self->machine,
100     command      => eval(qq("$command")),
101   );
102 }
103
104 sub _export_replace {
105   my($self, $new, $old ) = (shift, shift, shift);
106   my $command = $self->option('usermod');
107   
108   #set variable for the command
109   no strict 'vars';
110   {
111     no strict 'refs';
112     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
113     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
114   }
115
116   my $old_cust_pkg = $old->cust_svc->cust_pkg;
117   my $old_pkgnum = $old_cust_pkg ? $old_cust_pkg->pkgnum : '';
118   my $cust_pkg = $new->cust_svc->cust_pkg;
119   my $new_pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
120   my $new_cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
121   $new_cust_name = shell_quote $new_cust_name;
122   #done setting variables for the command
123
124   $self->shellcommands_queue( $new->svcnum,
125     user         => $self->option('user')||'root',
126     host         => $self->machine,
127     command      => eval(qq("$command")),
128   );
129 }
130
131 #a good idea to queue anything that could fail or take any time
132 sub shellcommands_queue {
133   my( $self, $svcnum ) = (shift, shift);
134   my $queue = new FS::queue {
135     'svcnum' => $svcnum,
136     'job'    => "FS::part_export::phone_shellcommands::ssh_cmd",
137   };
138   $queue->insert( @_ );
139 }
140
141 sub ssh_cmd { #subroutine, not method
142   use Net::SSH '0.08';
143   &Net::SSH::ssh_cmd( { @_ } );
144 }
145
146 1;