1 package FS::part_export::external_shellcommands;
4 use vars qw(@ISA %info);
6 use String::ShellQuote;
9 @ISA = qw(FS::part_export);
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', },
21 'svc' => 'svc_external',
22 'desc' => 'Run remote commands via SSH, for external Service',
23 'options' => \%options,
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>.
28 The following variables are available for interpolation (prefixed with new_ or
29 old_ for replace operations):
32 <LI><code>$title</code>
33 <LI><code>$pkgnum</code>
34 <LI><code>$custnum</code>
39 sub rebless { shift; }
43 $self->_export_command('useradd', @_);
48 $self->_export_command('userdel', @_);
53 $self->_export_command('suspend', @_);
56 sub _export_unsuspend {
58 $self->_export_command('unsuspend', @_);
62 my ( $self, $action, $svc_external) = (shift, shift, shift);
63 my $command = $self->option($action);
64 return '' if $command =~ /^\s*$/;
66 #set variable for the command
70 ${$_} = $svc_external->getfield($_) foreach $svc_external->fields;
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
79 $self->shellcommands_queue( $svc_external->svcnum,
80 user => $self->option('user')||'root',
81 host => $self->machine,
82 command => eval(qq("$command")),
87 my($self, $new, $old ) = (shift, shift, shift);
88 my $command = $self->option('usermod');
90 #set variable for the command
94 ${"old_$_"} = $old->getfield($_) foreach $old->fields;
95 ${"new_$_"} = $new->getfield($_) foreach $new->fields;
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
110 $self->shellcommands_queue( $new->svcnum,
111 user => $self->option('user')||'root',
112 host => $self->machine,
113 command => eval(qq("$command")),
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 {
122 'job' => "FS::part_export::external_shellcommands::ssh_cmd",
124 $queue->insert( @_ );
127 sub ssh_cmd { #subroutine, not method
129 &Net::SSH::ssh_cmd( { @_ } );