914589529e952796cf24e9fcdb28bb40abfc2dfc
[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 (qmail, ISPMan).',
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   <LI>
38     <INPUT TYPE="button" VALUE="ISPMan CLI" onClick='
39       this.form.useradd.value = "/usr/local/ispman/bin/ispman.addDomain -d $domain changeme";
40       this.form.userdel.value = "/usr/local/ispman/bin/ispman.deleteDomain -d $domain";
41       this.form.usermod.value = "";
42     '>
43 </UL>
44 The following variables are available for interpolation (prefixed with <code>new_</code> or <code>old_</code> for replace operations):
45 <UL>
46   <LI><code>$domain</code>
47   <LI><code>$qdomain</code> - domain with periods replaced by colons
48   <LI><code>$uid</code> - of catchall account
49   <LI><code>$gid</code> - of catchall account
50   <LI><code>$dir</code> - home directory of catchall account
51   <LI>All other fields in
52     <a href="../docs/schema.html#svc_domain">svc_domain</a> are also available.
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_command {
70   my ( $self, $action, $svc_domain) = (shift, shift, shift);
71   my $command = $self->option($action);
72
73   #set variable for the command
74   no strict 'vars';
75   {
76     no strict 'refs';
77     ${$_} = $svc_domain->getfield($_) foreach $svc_domain->fields;
78   }
79   ( $qdomain = $domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
80
81   if ( $svc_domain->catchall ) {
82     no strict 'refs';
83     my $svc_acct = $svc_domain->catchall_svc_acct;
84     ${$_} = $svc_acct->getfield($_) foreach qw(uid gid dir);
85   } else {
86     no strict 'refs';
87     ${$_} = '' foreach qw(uid gid dir);
88   }
89
90   #done setting variables for the command
91
92   $self->shellcommands_queue( $svc_domain->svcnum,
93     user         => $self->option('user')||'root',
94     host         => $self->machine,
95     command      => eval(qq("$command")),
96   );
97 }
98
99 sub _export_replace {
100   my($self, $new, $old ) = (shift, shift, shift);
101   my $command = $self->option('usermod');
102   
103   #set variable for the command
104   no strict 'vars';
105   {
106     no strict 'refs';
107     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
108     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
109   }
110   ( $old_qdomain = $old_domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
111   ( $new_qdomain = $new_domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
112
113   if ( $old->catchall ) {
114     no strict 'refs';
115     my $svc_acct = $old->catchall_svc_acct;
116     ${"old_$_"} = $svc_acct->getfield($_) foreach qw(uid gid dir);
117   } else {
118     ${"old_$_"} = '' foreach qw(uid gid dir);
119   }
120   if ( $new->catchall ) {
121     no strict 'refs';
122     my $svc_acct = $new->catchall_svc_acct;
123     ${"new_$_"} = $svc_acct->getfield($_) foreach qw(uid gid dir);
124   } else {
125     ${"new_$_"} = '' foreach qw(uid gid dir);
126   }
127
128   #done setting variables for the command
129
130   $self->shellcommands_queue( $new->svcnum,
131     user         => $self->option('user')||'root',
132     host         => $self->machine,
133     command      => eval(qq("$command")),
134   );
135 }
136
137 #a good idea to queue anything that could fail or take any time
138 sub shellcommands_queue {
139   my( $self, $svcnum ) = (shift, shift);
140   my $queue = new FS::queue {
141     'svcnum' => $svcnum,
142     'job'    => "FS::part_export::domain_shellcommands::ssh_cmd",
143   };
144   $queue->insert( @_ );
145 }
146
147 sub ssh_cmd { #subroutine, not method
148   use Net::SSH '0.08';
149   &Net::SSH::ssh_cmd( { @_ } );
150 }
151
152 #sub shellcommands_insert { #subroutine, not method
153 #}
154 #sub shellcommands_replace { #subroutine, not method
155 #}
156 #sub shellcommands_delete { #subroutine, not method
157 #}
158
159 1;
160