point to wiki docs
[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 $homedir/$zone; chown $username $homedir/$zone; ln -s $homedir/$zone /var/www/$zone',
14                },
15   'userdel'  => { label=>'Delete command',
16                   default=>'[ -n "$zone" ] && rm -rf /var/www/$zone; rm -rf $homedir/$zone',
17                 },
18   'usermod'  => { label=>'Modify command',
19                   default=>'[ -n "$old_zone" ] && rm /var/www/$old_zone; [ "$old_zone" != "$new_zone" -a -n "$new_zone" ] && ( mv $old_homedir/$old_zone $new_homedir/$new_zone; ln -sf $new_homedir/$new_zone /var/www/$new_zone ); [ "$old_username" != "$new_username" ] && chown -R $new_username $new_homedir/$new_zone; ln -sf $new_homedir/$new_zone /var/www/$new_zone',
20                 },
21   'suspend'  => { label=>'Suspension command',
22                   default=>'[ -n "$zone" ] && chmod 0 /var/www/$zone',
23                 },
24   'unsuspend'=> { label=>'Unsuspension command',
25                   default=>'[ -n "$zone" ] && chmod 755 /var/www/$zone',
26                 },
27 ;
28
29 %info = (
30   'svc'     => 'svc_www',
31   'desc'    => 'Run remote commands via SSH, for virtual web sites (directory maintenance, FrontPage, ISPMan)',
32   'options' => \%options,
33   'notes'   => <<'END'
34 Run remote commands via SSH, for virtual web sites.  You will need to
35 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Administration:SSH_Keys">setup SSH for unattended operation</a>.
36 <BR><BR>Use these buttons for some useful presets:
37 <UL>
38   <LI>
39     <INPUT TYPE="button" VALUE="Maintain directories" onClick='
40       this.form.user.value = "root";
41       this.form.useradd.value = "mkdir $homedir/$zone; chown $username $homedir/$zone; ln -s $homedir/$zone /var/www/$zone";
42       this.form.userdel.value = "[ -n \"$zone\" ] && rm -rf /var/www/$zone; rm -rf $homedir/$zone";
43       this.form.usermod.value = "[ -n \"$old_zone\" ] && rm /var/www/$old_zone; [ \"$old_zone\" != \"$new_zone\" -a -n \"$new_zone\" ] && ( mv $old_homedir/$old_zone $new_homedir/$new_zone; ln -sf $new_homedir/$new_zone /var/www/$new_zone ); [ \"$old_username\" != \"$new_username\" ] && chown -R $new_username $new_homedir/$new_zone; ln -sf $new_homedir/$new_zone /var/www/$new_zone";
44       this.form.suspend.value = "[ -n \"$zone\" ] && chmod 0 /var/www/$zone";
45       this.form.unsuspend.value = "[ -n \"$zone\" ] && chmod 755 /var/www/$zone";
46     '>
47   <LI>
48     <INPUT TYPE="button" VALUE="FrontPage extensions" onClick='
49       this.form.user.value = "root";
50       this.form.useradd.value = "/usr/local/frontpage/version5.0/bin/owsadm.exe -o install -p 80 -m $zone -xu $username -xg www-data -s /etc/apache/httpd.conf -u $username -pw $_password";
51       this.form.userdel.value = "/usr/local/frontpage/version5.0/bin/owsadm.exe -o uninstall -p 80 -m $zone -s /etc/apache/httpd.conf";
52       this.form.usermod.value = "";
53       this.form.suspend.value = "";
54       this.form.unsuspend.value = "";
55     '>
56   <LI>
57     <INPUT TYPE="button" VALUE="ISPMan CLI" onClick='
58       this.form.user.value = "root";
59       this.form.useradd.value = "/usr/local/ispman/bin/ispman.addvhost -d $domain $bare_zone";
60       this.form.userdel.value = "/usr/local/ispman/bin/ispman.deletevhost -d $domain $bare_zone";
61       this.form.usermod.value = "";
62       this.form.suspend.value = "";
63       this.form.unsuspend.value = "";
64     '></UL>
65 The following variables are available for interpolation (prefixed with
66 <code>new_</code> or <code>old_</code> for replace operations):
67 <UL>
68   <LI><code>$zone</code> - fully-qualified zone of this virtual host
69   <LI><code>$bare_zone</code> - just the zone of this virtual host, without the domain portion
70   <LI><code>$domain</code> - base domain
71   <LI><code>$username</code>
72   <LI><code>$_password</code>
73   <LI><code>$homedir</code>
74   <LI>All other fields in <a href="../docs/schema.html#svc_www">svc_www</a>
75     are also available.
76 </UL>
77 END
78 );
79
80
81 sub rebless { shift; }
82
83 sub _export_insert {
84   my($self) = shift;
85   $self->_export_command('useradd', @_);
86 }
87
88 sub _export_delete {
89   my($self) = shift;
90   $self->_export_command('userdel', @_);
91 }
92
93 sub _export_suspend {
94   my($self) = shift;
95   $self->_export_command('suspend', @_);
96 }
97
98 sub _export_unsuspend {
99   my($self) = shift;
100   $self->_export_command('unsuspend', @_);
101 }
102
103 sub _export_command {
104   my ( $self, $action, $svc_www) = (shift, shift, shift);
105   my $command = $self->option($action);
106   return '' if $command =~ /^\s*$/;
107
108   #set variable for the command
109   no strict 'vars';
110   {
111     no strict 'refs';
112     ${$_} = $svc_www->getfield($_) foreach $svc_www->fields;
113   }
114   my $domain_record = $svc_www->domain_record; # or die ?
115   my $zone = $domain_record->zone; # or die ?
116   my $domain = $domain_record->svc_domain->domain;
117   ( my $bare_zone = $zone ) =~ s/\.$domain$//;
118   my $svc_acct = $svc_www->svc_acct; # or die ?
119   my $username = $svc_acct->username;
120   my $_password = $svc_acct->_password;
121   my $homedir = $svc_acct->dir; # or die ?
122
123   #done setting variables for the command
124
125   $self->shellcommands_queue( $svc_www->svcnum,
126     user         => $self->option('user')||'root',
127     host         => $self->machine,
128     command      => eval(qq("$command")),
129   );
130 }
131
132 sub _export_replace {
133   my($self, $new, $old ) = (shift, shift, shift);
134   my $command = $self->option('usermod');
135   
136   #set variable for the command
137   no strict 'vars';
138   {
139     no strict 'refs';
140     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
141     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
142   }
143   my $old_domain_record = $old->domain_record; # or die ?
144   my $old_zone = $old_domain_record->zone; # or die ?
145   my $old_domain = $old_domain_record->svc_domain->domain;
146   ( my $old_bare_zone = $old_zone ) =~ s/\.$old_domain$//;
147   my $old_svc_acct = $old->svc_acct; # or die ?
148   my $old_username = $old_svc_acct->username;
149   my $old_homedir = $old_svc_acct->dir; # or die ?
150
151   my $new_domain_record = $new->domain_record; # or die ?
152   my $new_zone = $new_domain_record->zone; # or die ?
153   my $new_domain = $new_domain_record->svc_domain->domain;
154   ( my $new_bare_zone = $new_zone ) =~ s/\.$new_domain$//;
155   my $new_svc_acct = $new->svc_acct; # or die ?
156   my $new_username = $new_svc_acct->username;
157   #my $new__password = $new_svc_acct->_password;
158   my $new_homedir = $new_svc_acct->dir; # or die ?
159
160   #done setting variables for the command
161
162   $self->shellcommands_queue( $new->svcnum,
163     user         => $self->option('user')||'root',
164     host         => $self->machine,
165     command      => eval(qq("$command")),
166   );
167 }
168
169 #a good idea to queue anything that could fail or take any time
170 sub shellcommands_queue {
171   my( $self, $svcnum ) = (shift, shift);
172   my $queue = new FS::queue {
173     'svcnum' => $svcnum,
174     'job'    => "FS::part_export::www_shellcommands::ssh_cmd",
175   };
176   $queue->insert( @_ );
177 }
178
179 sub ssh_cmd { #subroutine, not method
180   use Net::SSH '0.08';
181   &Net::SSH::ssh_cmd( { @_ } );
182 }
183
184 #sub shellcommands_insert { #subroutine, not method
185 #}
186 #sub shellcommands_replace { #subroutine, not method
187 #}
188 #sub shellcommands_delete { #subroutine, not method
189 #}
190