move export info to the modules themselves
[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>The following variables are available for interpolation (prefixed with
31 <code>new_</code> or <code>old_</code> for replace operations):
32 <UL>
33   <LI><code>$zone</code>
34   <LI><code>$username</code>
35   <LI><code>$homedir</code>
36   <LI>All other fields in <a href="../docs/schema.html#svc_www">svc_www</a>
37     are also available.
38 </UL>
39 END
40 );
41
42
43 sub rebless { shift; }
44
45 sub _export_insert {
46   my($self) = shift;
47   $self->_export_command('useradd', @_);
48 }
49
50 sub _export_delete {
51   my($self) = shift;
52   $self->_export_command('userdel', @_);
53 }
54
55 sub _export_command {
56   my ( $self, $action, $svc_www) = (shift, shift, shift);
57   my $command = $self->option($action);
58
59   #set variable for the command
60   no strict 'vars';
61   {
62     no strict 'refs';
63     ${$_} = $svc_www->getfield($_) foreach $svc_www->fields;
64   }
65   my $domain_record = $svc_www->domain_record; # or die ?
66   my $zone = $domain_record->zone; # or die ?
67   my $svc_acct = $svc_www->svc_acct; # or die ?
68   my $username = $svc_acct->username;
69   my $homedir = $svc_acct->dir; # or die ?
70
71   #done setting variables for the command
72
73   $self->shellcommands_queue( $svc_www->svcnum,
74     user         => $self->option('user')||'root',
75     host         => $self->machine,
76     command      => eval(qq("$command")),
77   );
78 }
79
80 sub _export_replace {
81   my($self, $new, $old ) = (shift, shift, shift);
82   my $command = $self->option('usermod');
83   
84   #set variable for the command
85   no strict 'vars';
86   {
87     no strict 'refs';
88     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
89     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
90   }
91   my $old_domain_record = $old->domain_record; # or die ?
92   my $old_zone = $old_domain_record->reczone; # or die ?
93   unless ( $old_zone =~ /\.$/ ) {
94     my $old_svc_domain = $old_domain_record->svc_domain; # or die ?
95     $old_zone .= '.'. $old_svc_domain->domain;
96   }
97
98   my $old_svc_acct = $old->svc_acct; # or die ?
99   my $old_username = $old_svc_acct->username;
100   my $old_homedir = $old_svc_acct->dir; # or die ?
101
102   my $new_domain_record = $new->domain_record; # or die ?
103   my $new_zone = $new_domain_record->reczone; # or die ?
104   unless ( $new_zone =~ /\.$/ ) {
105     my $new_svc_domain = $new_domain_record->svc_domain; # or die ?
106     $new_zone .= '.'. $new_svc_domain->domain;
107   }
108
109   my $new_svc_acct = $new->svc_acct; # or die ?
110   my $new_username = $new_svc_acct->username;
111   my $new_homedir = $new_svc_acct->dir; # or die ?
112
113   #done setting variables for the command
114
115   $self->shellcommands_queue( $new->svcnum,
116     user         => $self->option('user')||'root',
117     host         => $self->machine,
118     command      => eval(qq("$command")),
119   );
120 }
121
122 #a good idea to queue anything that could fail or take any time
123 sub shellcommands_queue {
124   my( $self, $svcnum ) = (shift, shift);
125   my $queue = new FS::queue {
126     'svcnum' => $svcnum,
127     'job'    => "FS::part_export::www_shellcommands::ssh_cmd",
128   };
129   $queue->insert( @_ );
130 }
131
132 sub ssh_cmd { #subroutine, not method
133   use Net::SSH '0.08';
134   &Net::SSH::ssh_cmd( { @_ } );
135 }
136
137 #sub shellcommands_insert { #subroutine, not method
138 #}
139 #sub shellcommands_replace { #subroutine, not method
140 #}
141 #sub shellcommands_delete { #subroutine, not method
142 #}
143