e90120ec9694125faf7abfb115cf33327fb8f544
[freeside.git] / FS / FS / part_export / www_shellcommands.pm
1 package FS::part_export::www_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=>'mkdir /var/www/$zone; chown $username /var/www/$zone; ln -s /var/www/$zone $homedir/$zone',
14                },
15   'userdel'  => { label=>'Delete command',
16                   default=>'[ -n "$zone" ] && rm -rf /var/www/$zone; rm $homedir/$zone',
17                 },
18   'usermod'  => { label=>'Modify command',
19                   default=>'[ -n "$old_zone" ] && rm $old_homedir/$old_zone; [ "$old_zone" != "$new_zone" -a -n "$new_zone" ] && mv /var/www/$old_zone /var/www/$new_zone; [ "$old_username" != "$new_username" ] && chown -R $new_username /var/www/$new_zone; ln -s /var/www/$new_zone $new_homedir/$new_zone',
20                 },
21 ;
22
23 %info = (
24   'svc'     => 'svc_www',
25   'desc'    => 'Run remote commands via SSH, for virtual web sites.',
26   'options' => \%options,
27   'notes'   => <<'END'
28 Run remote commands via SSH, for virtual web sites.  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="Maintain directories" onClick='
34       this.form.user.value = "root";
35       this.form.useradd.value = "mkdir /var/www/$zone; chown $username /var/www/$zone; ln -s /var/www/$zone $homedir/$zone";
36       this.form.userdel.value = "[ -n &quot;$zone&quot; ] && rm -rf /var/www/$zone; rm $homedir/$zone";
37       this.form.usermod.value = "[ -n &quot;$old_zone&quot; ] && rm $old_homedir/$old_zone; [ &quot;$old_zone&quot; != &quot;$new_zone&quot; -a -n &quot;$new_zone&quot; ] && mv /var/www/$old_zone /var/www/$new_zone; [ &quot;$old_username&quot; != &quot;$new_username&quot; ] && chown -R $new_username /var/www/$new_zone; ln -s /var/www/$new_zone $new_homedir/$new_zone";
38     '>
39   <LI>
40     <INPUT TYPE="button" VALUE="ISPMan CLI" onClick='
41       this.form.user.value = "root";
42       this.form.useradd.value = "/usr/local/ispman/ispman.addvhost -d $domain $zone";
43       this.form.userdel.value = "/usr/local/ispman/idpman.deletevhost -d $domain $zone";
44       this.form.usermod.value = "";
45     '>
46 </UL>
47 The following variables are available for interpolation (prefixed with
48 <code>new_</code> or <code>old_</code> for replace operations):
49 <UL>
50   <LI><code>$zone</code> - fully-qualified zone of this virtual host
51   <LI><code>$domain</code> - base domain
52   <LI><code>$username</code>
53   <LI><code>$homedir</code>
54   <LI>All other fields in <a href="../docs/schema.html#svc_www">svc_www</a>
55     are also available.
56 </UL>
57 END
58 );
59
60
61 sub rebless { shift; }
62
63 sub _export_insert {
64   my($self) = shift;
65   $self->_export_command('useradd', @_);
66 }
67
68 sub _export_delete {
69   my($self) = shift;
70   $self->_export_command('userdel', @_);
71 }
72
73 sub _export_command {
74   my ( $self, $action, $svc_www) = (shift, shift, shift);
75   my $command = $self->option($action);
76
77   #set variable for the command
78   no strict 'vars';
79   {
80     no strict 'refs';
81     ${$_} = $svc_www->getfield($_) foreach $svc_www->fields;
82   }
83   my $domain_record = $svc_www->domain_record; # or die ?
84   my $zone = $domain_record->zone; # or die ?
85   my $domain = $domain_record->svc_domain->domain;
86   my $svc_acct = $svc_www->svc_acct; # or die ?
87   my $username = $svc_acct->username;
88   my $homedir = $svc_acct->dir; # or die ?
89
90   #done setting variables for the command
91
92   $self->shellcommands_queue( $svc_www->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   my $old_domain_record = $old->domain_record; # or die ?
111   my $old_zone = $old_domain_record->reczone; # or die ?
112   my $old_domain = $old_domain_record->svc_domain->domain;
113   $old_zone .= ".$old_domain" unless $old_zone =~ /\.$/;
114
115   my $old_svc_acct = $old->svc_acct; # or die ?
116   my $old_username = $old_svc_acct->username;
117   my $old_homedir = $old_svc_acct->dir; # or die ?
118
119   my $new_domain_record = $new->domain_record; # or die ?
120   my $new_zone = $new_domain_record->reczone; # or die ?
121   my $new_domain = $new_domain_record->svc_domain->domain;
122   unless ( $new_zone =~ /\.$/ ) {
123     my $new_svc_domain = $new_domain_record->svc_domain; # or die ?
124     $new_zone .= '.'. $new_svc_domain->domain;
125   }
126
127   my $new_svc_acct = $new->svc_acct; # or die ?
128   my $new_username = $new_svc_acct->username;
129   my $new_homedir = $new_svc_acct->dir; # or die ?
130
131   #done setting variables for the command
132
133   $self->shellcommands_queue( $new->svcnum,
134     user         => $self->option('user')||'root',
135     host         => $self->machine,
136     command      => eval(qq("$command")),
137   );
138 }
139
140 #a good idea to queue anything that could fail or take any time
141 sub shellcommands_queue {
142   my( $self, $svcnum ) = (shift, shift);
143   my $queue = new FS::queue {
144     'svcnum' => $svcnum,
145     'job'    => "FS::part_export::www_shellcommands::ssh_cmd",
146   };
147   $queue->insert( @_ );
148 }
149
150 sub ssh_cmd { #subroutine, not method
151   use Net::SSH '0.08';
152   &Net::SSH::ssh_cmd( { @_ } );
153 }
154
155 #sub shellcommands_insert { #subroutine, not method
156 #}
157 #sub shellcommands_replace { #subroutine, not method
158 #}
159 #sub shellcommands_delete { #subroutine, not method
160 #}
161