default commands now keep web content in user homedirs and link to /var/www
[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.',
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="ISPMan CLI" onClick='
41       this.form.user.value = "root";
42       this.form.useradd.value = "/usr/local/ispman/bin/ispman.addvhost -d $domain $bare_zone";
43       this.form.userdel.value = "/usr/local/ispman/bin/ispman.deletevhost -d $domain $bare_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>$bare_zone</code> - just the zone of this virtual host, without the domain portion
52   <LI><code>$domain</code> - base domain
53   <LI><code>$username</code>
54   <LI><code>$homedir</code>
55   <LI>All other fields in <a href="../docs/schema.html#svc_www">svc_www</a>
56     are also available.
57 </UL>
58 END
59 );
60
61
62 sub rebless { shift; }
63
64 sub _export_insert {
65   my($self) = shift;
66   $self->_export_command('useradd', @_);
67 }
68
69 sub _export_delete {
70   my($self) = shift;
71   $self->_export_command('userdel', @_);
72 }
73
74 sub _export_command {
75   my ( $self, $action, $svc_www) = (shift, shift, shift);
76   my $command = $self->option($action);
77
78   #set variable for the command
79   no strict 'vars';
80   {
81     no strict 'refs';
82     ${$_} = $svc_www->getfield($_) foreach $svc_www->fields;
83   }
84   my $domain_record = $svc_www->domain_record; # or die ?
85   my $zone = $domain_record->zone; # or die ?
86   my $domain = $domain_record->svc_domain->domain;
87   ( my $bare_zone = $zone ) =~ s/\.$domain$//;
88   my $svc_acct = $svc_www->svc_acct; # or die ?
89   my $username = $svc_acct->username;
90   my $homedir = $svc_acct->dir; # or die ?
91
92   #done setting variables for the command
93
94   $self->shellcommands_queue( $svc_www->svcnum,
95     user         => $self->option('user')||'root',
96     host         => $self->machine,
97     command      => eval(qq("$command")),
98   );
99 }
100
101 sub _export_replace {
102   my($self, $new, $old ) = (shift, shift, shift);
103   my $command = $self->option('usermod');
104   
105   #set variable for the command
106   no strict 'vars';
107   {
108     no strict 'refs';
109     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
110     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
111   }
112   my $old_domain_record = $old->domain_record; # or die ?
113   my $old_zone = $old_domain_record->zone; # or die ?
114   my $old_domain = $old_domain_record->svc_domain->domain;
115   ( my $old_bare_zone = $old_zone ) =~ s/\.$old_domain$//;
116   my $old_svc_acct = $old->svc_acct; # or die ?
117   my $old_username = $old_svc_acct->username;
118   my $old_homedir = $old_svc_acct->dir; # or die ?
119
120   my $new_domain_record = $new->domain_record; # or die ?
121   my $new_zone = $new_domain_record->zone; # or die ?
122   my $new_domain = $new_domain_record->svc_domain->domain;
123   ( my $new_bare_zone = $new_zone ) =~ s/\.$new_domain$//;
124   my $new_svc_acct = $new->svc_acct; # or die ?
125   my $new_username = $new_svc_acct->username;
126   my $new_homedir = $new_svc_acct->dir; # or die ?
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::www_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