add frontpage extensions to www_shellcommands export
[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 ;
22
23 %info = (
24   'svc'     => 'svc_www',
25   'desc'    => 'Run remote commands via SSH, for virtual web sites (directory maintenance, FrontPage, ISPMan)',
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 $homedir/$zone; chown $username $homedir/$zone; ln -s $homedir/$zone /var/www/$zone";
36       this.form.userdel.value = "[ -n \"$zone\" ] && rm -rf /var/www/$zone; rm -rf $homedir/$zone";
37       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";
38     '>
39   <LI>
40     <INPUT TYPE="button" VALUE="FrontPage extensions" onClick='
41       this.form.user.value = "root";
42       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";
43       this.form.userdel.value = "/usr/local/frontpage/version5.0/bin/owsadm.exe -o uninstall -p 80 -m $zone -s /etc/apache/httpd.conf";
44       this.form.usermod.value = "";
45     '>
46   <LI>
47     <INPUT TYPE="button" VALUE="ISPMan CLI" onClick='
48       this.form.user.value = "root";
49       this.form.useradd.value = "/usr/local/ispman/bin/ispman.addvhost -d $domain $bare_zone";
50       this.form.userdel.value = "/usr/local/ispman/bin/ispman.deletevhost -d $domain $bare_zone";
51       this.form.usermod.value = "";
52     '></UL>
53 The following variables are available for interpolation (prefixed with
54 <code>new_</code> or <code>old_</code> for replace operations):
55 <UL>
56   <LI><code>$zone</code> - fully-qualified zone of this virtual host
57   <LI><code>$bare_zone</code> - just the zone of this virtual host, without the domain portion
58   <LI><code>$domain</code> - base domain
59   <LI><code>$username</code>
60   <LI><code>$homedir</code>
61   <LI>All other fields in <a href="../docs/schema.html#svc_www">svc_www</a>
62     are also available.
63 </UL>
64 END
65 );
66
67
68 sub rebless { shift; }
69
70 sub _export_insert {
71   my($self) = shift;
72   $self->_export_command('useradd', @_);
73 }
74
75 sub _export_delete {
76   my($self) = shift;
77   $self->_export_command('userdel', @_);
78 }
79
80 sub _export_command {
81   my ( $self, $action, $svc_www) = (shift, shift, shift);
82   my $command = $self->option($action);
83
84   #set variable for the command
85   no strict 'vars';
86   {
87     no strict 'refs';
88     ${$_} = $svc_www->getfield($_) foreach $svc_www->fields;
89   }
90   my $domain_record = $svc_www->domain_record; # or die ?
91   my $zone = $domain_record->zone; # or die ?
92   my $domain = $domain_record->svc_domain->domain;
93   ( my $bare_zone = $zone ) =~ s/\.$domain$//;
94   my $svc_acct = $svc_www->svc_acct; # or die ?
95   my $username = $svc_acct->username;
96   my $_password = $svc_acct->_password;
97   my $homedir = $svc_acct->dir; # or die ?
98
99   #done setting variables for the command
100
101   $self->shellcommands_queue( $svc_www->svcnum,
102     user         => $self->option('user')||'root',
103     host         => $self->machine,
104     command      => eval(qq("$command")),
105   );
106 }
107
108 sub _export_replace {
109   my($self, $new, $old ) = (shift, shift, shift);
110   my $command = $self->option('usermod');
111   
112   #set variable for the command
113   no strict 'vars';
114   {
115     no strict 'refs';
116     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
117     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
118   }
119   my $old_domain_record = $old->domain_record; # or die ?
120   my $old_zone = $old_domain_record->zone; # or die ?
121   my $old_domain = $old_domain_record->svc_domain->domain;
122   ( my $old_bare_zone = $old_zone ) =~ s/\.$old_domain$//;
123   my $old_svc_acct = $old->svc_acct; # or die ?
124   my $old_username = $old_svc_acct->username;
125   my $old_homedir = $old_svc_acct->dir; # or die ?
126
127   my $new_domain_record = $new->domain_record; # or die ?
128   my $new_zone = $new_domain_record->zone; # or die ?
129   my $new_domain = $new_domain_record->svc_domain->domain;
130   ( my $new_bare_zone = $new_zone ) =~ s/\.$new_domain$//;
131   my $new_svc_acct = $new->svc_acct; # or die ?
132   my $new_username = $new_svc_acct->username;
133   #my $new__password = $new_svc_acct->_password;
134   my $new_homedir = $new_svc_acct->dir; # or die ?
135
136   #done setting variables for the command
137
138   $self->shellcommands_queue( $new->svcnum,
139     user         => $self->option('user')||'root',
140     host         => $self->machine,
141     command      => eval(qq("$command")),
142   );
143 }
144
145 #a good idea to queue anything that could fail or take any time
146 sub shellcommands_queue {
147   my( $self, $svcnum ) = (shift, shift);
148   my $queue = new FS::queue {
149     'svcnum' => $svcnum,
150     'job'    => "FS::part_export::www_shellcommands::ssh_cmd",
151   };
152   $queue->insert( @_ );
153 }
154
155 sub ssh_cmd { #subroutine, not method
156   use Net::SSH '0.08';
157   &Net::SSH::ssh_cmd( { @_ } );
158 }
159
160 #sub shellcommands_insert { #subroutine, not method
161 #}
162 #sub shellcommands_replace { #subroutine, not method
163 #}
164 #sub shellcommands_delete { #subroutine, not method
165 #}
166