move export info to the modules themselves
[freeside.git] / FS / FS / part_export / forward_shellcommands.pm
1 package FS::part_export::forward_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=>'root' },
12   'useradd' => { label=>'Insert command',
13                  default=>'',
14                },
15   'userdel'  => { label=>'Delete command',
16                   default=>'',
17                 },
18   'usermod'  => { label=>'Modify command',
19                   default=>'',
20                 },
21 ;
22
23 %info = (
24   'svc'     => 'svc_forward',
25   'desc'    => 'Run remote commands via SSH, for forwards',
26   'options' => \%options,
27   'notes'   => <<'END'
28 Run remote commands via SSH, for forwards.  You will need to
29 <a href="../docs/ssh.html">setup SSH for unattended operation</a>.
30 <BR><BR>Use these buttons for some useful presets:
31 <UL>
32   <LI>
33     <INPUT TYPE="button" VALUE="text vpopmail maintenance" onClick='
34       this.form.useradd.value = "[ -d /home/vpopmail/domains/$domain/$username ] && { echo \"$destination\" > /home/vpopmail/domains/$domain/$username/.qmail; chown vpopmail:vchkpw /home/vpopmail/domains/$domain/$username/.qmail; }";
35       this.form.userdel.value = "rm /home/vpopmail/domains/$domain/$username/.qmail";
36       this.form.usermod.value = "mv /home/vpopmail/domains/$old_domain/$old_username/.qmail /home/vpopmail/domains/$new_domain/$new_username; [ \"$old_destination\" != \"$new_destination\" ] && { echo \"$new_destination\" > /home/vpopmail/domains/$new_domain/$new_username/.qmail; chown vpopmail:vchkpw /home/vpopmail/domains/$new_domain/$new_username/.qmail; }";
37     '>
38 </UL>
39 The following variables are available for interpolation (prefixed with
40 <code>new_</code> or <code>old_</code> for replace operations):
41 <UL>
42   <LI><code>$username</code>
43   <LI><code>$domain</code>
44   <LI><code>$destination</code> - forward destination
45   <LI>All other fields in <a href="../docs/schema.html#svc_forward">svc_forward</a> are also available.
46 </UL>
47 END
48 );
49
50 sub rebless { shift; }
51
52 sub _export_insert {
53   my($self) = shift;
54   $self->_export_command('useradd', @_);
55 }
56
57 sub _export_delete {
58   my($self) = shift;
59   $self->_export_command('userdel', @_);
60 }
61
62 sub _export_command {
63   my ( $self, $action, $svc_forward ) = (shift, shift, shift);
64   my $command = $self->option($action);
65
66   #set variable for the command
67   no strict 'vars';
68   {
69     no strict 'refs';
70     ${$_} = $svc_forward->getfield($_) foreach $svc_forward->fields;
71   }
72
73   my $svc_acct = $svc_forward->srcsvc_acct;
74   $username = $svc_acct->username;
75   $domain = $svc_acct->domain;
76   if ($svc_forward->dstsvc_acct) {
77     $destination = $svc_forward->dstsvc_acct->email;
78   } else {
79     $destination = $svc_forward->dst;
80   }
81
82   #done setting variables for the command
83
84   $self->shellcommands_queue( $svc_forward->svcnum,
85     user         => $self->option('user')||'root',
86     host         => $self->machine,
87     command      => eval(qq("$command")),
88   );
89 }
90
91 sub _export_replace {
92   my( $self, $new, $old ) = (shift, shift, shift);
93   my $command = $self->option('usermod');
94   
95   #set variable for the command
96   no strict 'vars';
97   {
98     no strict 'refs';
99     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
100     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
101   }
102
103   my $old_svc_acct = $old->srcsvc_acct;
104   $old_username = $old_svc_acct->username;
105   $old_domain = $old_svc_acct->domain;
106   if ($old->dstsvc_acct) {
107     $old_destination = $old->dstsvc_acct->email;
108   } else {
109     $old_destination = $old->dst;
110   }
111
112   my $new_svc_acct = $new->srcsvc_acct;
113   $new_username = $new_svc_acct->username;
114   $new_domain = $new_svc_acct->domain;
115   if ($new->dstsvc) {
116     $new_destination = $new->dstsvc_acct->email;
117   } else {
118     $new_destination = $new->dst;
119   }
120
121   #done setting variables for the command
122
123   $self->shellcommands_queue( $new->svcnum,
124     user         => $self->option('user')||'root',
125     host         => $self->machine,
126     command      => eval(qq("$command")),
127   );
128 }
129
130 #a good idea to queue anything that could fail or take any time
131 sub shellcommands_queue {
132   my( $self, $svcnum ) = (shift, shift);
133   my $queue = new FS::queue {
134     'svcnum' => $svcnum,
135     'job'    => "FS::part_export::forward_shellcommands::ssh_cmd",
136   };
137   $queue->insert( @_ );
138 }
139
140 sub ssh_cmd { #subroutine, not method
141   use Net::SSH '0.08';
142   &Net::SSH::ssh_cmd( { @_ } );
143 }
144
145 #sub shellcommands_insert { #subroutine, not method
146 #}
147 #sub shellcommands_replace { #subroutine, not method
148 #}
149 #sub shellcommands_delete { #subroutine, not method
150 #}
151
152 1;
153