d8ae0c376620884675befaca68ef91a122514e28
[freeside.git] / FS / FS / part_export / domain_shellcommands.pm
1 package FS::part_export::domain_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_domain',
25   'desc'    => 'Run remote commands via SSH, for domains.',
26   'options' => \%options,
27   'notes'   => <<'END'
28 Run remote commands via SSH, for domains.  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="qmail catchall .qmail-domain-default maintenance" onClick='
34       this.form.useradd.value = "[ \"$uid\" -a \"$gid\" -a \"$dir\" -a \"$qdomain\" ] && [ -e $dir/.qmail-$qdomain-default ] || { touch $dir/.qmail-$qdomain-default; chown $uid:$gid $dir/.qmail-$qdomain-default; }";
35       this.form.userdel.value = "";
36       this.form.usermod.value = "";
37     '>
38 </UL>
39 The following variables are available for interpolation (prefixed with <code>new_</code> or <code>old_</code> for replace operations):
40 <UL>
41   <LI><code>$domain</code>
42   <LI><code>$qdomain</code> - domain with periods replaced by colons
43   <LI><code>$uid</code> - of catchall account
44   <LI><code>$gid</code> - of catchall account
45   <LI><code>$dir</code> - home directory of catchall account
46   <LI>All other fields in
47     <a href="../docs/schema.html#svc_domain">svc_domain</a> are also available.
48 </UL>
49 END
50 );
51
52 sub rebless { shift; }
53
54 sub _export_insert {
55   my($self) = shift;
56   $self->_export_command('useradd', @_);
57 }
58
59 sub _export_delete {
60   my($self) = shift;
61   $self->_export_command('userdel', @_);
62 }
63
64 sub _export_command {
65   my ( $self, $action, $svc_domain) = (shift, shift, shift);
66   my $command = $self->option($action);
67
68   #set variable for the command
69   no strict 'vars';
70   {
71     no strict 'refs';
72     ${$_} = $svc_domain->getfield($_) foreach $svc_domain->fields;
73   }
74   ( $qdomain = $domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
75
76   if ( $svc_domain->catchall ) {
77     no strict 'refs';
78     my $svc_acct = $svc_domain->catchall_svc_acct;
79     ${$_} = $svc_acct->getfield($_) foreach qw(uid gid dir);
80   } else {
81     no strict 'refs';
82     ${$_} = '' foreach qw(uid gid dir);
83   }
84
85   #done setting variables for the command
86
87   $self->shellcommands_queue( $svc_domain->svcnum,
88     user         => $self->option('user')||'root',
89     host         => $self->machine,
90     command      => eval(qq("$command")),
91   );
92 }
93
94 sub _export_replace {
95   my($self, $new, $old ) = (shift, shift, shift);
96   my $command = $self->option('usermod');
97   
98   #set variable for the command
99   no strict 'vars';
100   {
101     no strict 'refs';
102     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
103     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
104   }
105   ( $old_qdomain = $old_domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
106   ( $new_qdomain = $new_domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
107
108   if ( $old->catchall ) {
109     no strict 'refs';
110     my $svc_acct = $old->catchall_svc_acct;
111     ${"old_$_"} = $svc_acct->getfield($_) foreach qw(uid gid dir);
112   } else {
113     ${"old_$_"} = '' foreach qw(uid gid dir);
114   }
115   if ( $new->catchall ) {
116     no strict 'refs';
117     my $svc_acct = $new->catchall_svc_acct;
118     ${"new_$_"} = $svc_acct->getfield($_) foreach qw(uid gid dir);
119   } else {
120     ${"new_$_"} = '' foreach qw(uid gid dir);
121   }
122
123   #done setting variables for the command
124
125   $self->shellcommands_queue( $new->svcnum,
126     user         => $self->option('user')||'root',
127     host         => $self->machine,
128     command      => eval(qq("$command")),
129   );
130 }
131
132 #a good idea to queue anything that could fail or take any time
133 sub shellcommands_queue {
134   my( $self, $svcnum ) = (shift, shift);
135   my $queue = new FS::queue {
136     'svcnum' => $svcnum,
137     'job'    => "FS::part_export::domain_shellcommands::ssh_cmd",
138   };
139   $queue->insert( @_ );
140 }
141
142 sub ssh_cmd { #subroutine, not method
143   use Net::SSH '0.08';
144   &Net::SSH::ssh_cmd( { @_ } );
145 }
146
147 #sub shellcommands_insert { #subroutine, not method
148 #}
149 #sub shellcommands_replace { #subroutine, not method
150 #}
151 #sub shellcommands_delete { #subroutine, not method
152 #}
153
154 1;
155