RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / part_export / external_shellcommands.pm
1 package FS::part_export::external_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 tie my %options, 'Tie::IxHash',
12   'user'      => { label=>'Remote username', default=>'root', },
13   'useradd'   => { label=>'Insert command', }, 
14   'userdel'   => { label=>'Delete command', }, 
15   'usermod'   => { label=>'Modify command', }, 
16   'suspend'   => { label=>'Suspension command', }, 
17   'unsuspend' => { label=>'Unsuspension command', }, 
18 ;
19
20 %info = (
21   'svc'     => 'svc_external',
22   'desc'    => 'Run remote commands via SSH, for external Service',
23   'options' => \%options,
24   'notes'   => <<'END'
25 Run remote commands via SSH, for .  You will need to
26 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Administration:SSH_Keys">setup SSH for unattended operation</a>.
27 <BR>
28 The following variables are available for interpolation (prefixed with new_ or
29 old_ for replace operations):
30 <UL>
31   <LI><code>$id</code>
32   <LI><code>$title</code>
33   <LI><code>$pkgnum</code>
34   <LI><code>$custnum</code>
35 </UL>
36 END
37 );
38
39 sub rebless { shift; }
40
41 sub _export_insert {
42   my($self) = shift;
43   $self->_export_command('useradd', @_);
44 }
45
46 sub _export_delete {
47   my($self) = shift;
48   $self->_export_command('userdel', @_);
49 }
50
51 sub _export_suspend {
52   my($self) = shift;
53   $self->_export_command('suspend', @_);
54 }
55
56 sub _export_unsuspend {
57   my($self) = shift;
58   $self->_export_command('unsuspend', @_);
59 }
60
61 sub _export_command {
62   my ( $self, $action, $svc_external) = (shift, shift, shift);
63   my $command = $self->option($action);
64   return '' if $command =~ /^\s*$/;
65
66   #set variable for the command
67   no strict 'vars';
68   {
69     no strict 'refs';
70     ${$_} = $svc_external->getfield($_) foreach $svc_external->fields;
71   }
72   my $cust_pkg = $svc_external->cust_svc->cust_pkg;
73   my $cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
74   my $title = shell_quote $svc_external->title;
75   my $pkgnum = shell_quote $cust_pkg->pkgnum;
76   my $custnum = shell_quote $cust_pkg->custnum;
77   #done setting variables for the command
78
79   $self->shellcommands_queue( $svc_external->svcnum,
80     user         => $self->option('user')||'root',
81     host         => $self->machine,
82     command      => eval(qq("$command")),
83   );
84 }
85
86 sub _export_replace {
87   my($self, $new, $old ) = (shift, shift, shift);
88   my $command = $self->option('usermod');
89   
90   #set variable for the command
91   no strict 'vars';
92   {
93     no strict 'refs';
94     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
95     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
96   }
97
98   my $new_cust_pkg = $new->cust_svc->cust_pkg;
99   my $old_cust_pkg = $old->cust_svc->cust_pkg;
100   $new_title = shell_quote $new->title;
101   $old_title = shell_quote $old->title;
102   my $new_cust_name = $new_cust_pkg ? $new_cust_pkg->cust_main->name : '';
103   my $old_cust_name = $old_cust_pkg ? $old_cust_pkg->cust_main->name : '';
104   my $new_pkgnum = shell_quote $new_cust_pkg->pkgnum;
105   my $new_custnum = shell_quote $new_cust_pkg->custnum;
106   my $old_pkgnum = shell_quote $old_cust_pkg->pkgnum;
107   my $old_custnum = shell_quote $old_cust_pkg->custnum;
108   #done setting variables for the command
109
110   $self->shellcommands_queue( $new->svcnum,
111     user         => $self->option('user')||'root',
112     host         => $self->machine,
113     command      => eval(qq("$command")),
114   );
115 }
116
117 #a good idea to queue anything that could fail or take any time
118 sub shellcommands_queue {
119   my( $self, $svcnum ) = (shift, shift);
120   my $queue = new FS::queue {
121     'svcnum' => $svcnum,
122     'job'    => "FS::part_export::external_shellcommands::ssh_cmd",
123   };
124   $queue->insert( @_ );
125 }
126
127 sub ssh_cmd { #subroutine, not method
128   use Net::SSH '0.08';
129   &Net::SSH::ssh_cmd( { @_ } );
130 }