b3bfdfeff929f9dda1a9b738ff88c8c2183e978c
[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);
5 use FS::part_export;
6
7 @ISA = qw(FS::part_export);
8
9 sub rebless { shift; }
10
11 sub _export_insert {
12   my($self) = shift;
13   $self->_export_command('useradd', @_);
14 }
15
16 sub _export_delete {
17   my($self) = shift;
18   $self->_export_command('userdel', @_);
19 }
20
21 sub _export_command {
22   my ( $self, $action, $svc_www) = (shift, shift, shift);
23   my $command = $self->option($action);
24
25   #set variable for the command
26   no strict 'vars';
27   {
28     no strict 'refs';
29     ${$_} = $svc_www->getfield($_) foreach $svc_www->fields;
30   }
31   my $domain_record = $svc_www->domain_record; # or die ?
32   my $zone = $domain_record->reczone; # or die ?
33   if ( $zone =~ /\.$/ ) {
34     $zone =~ s/\.$//;
35   } else {
36     my $svc_domain = $domain_record->svc_domain; # or die ?
37     $zone .= '.'. $svc_domain->domain;
38     $zone =~ s/^\@\.//;
39   }
40
41   my $svc_acct = $svc_www->svc_acct; # or die ?
42   my $username = $svc_acct->username;
43   my $homedir = $svc_acct->dir; # or die ?
44
45   #done setting variables for the command
46
47   $self->shellcommands_queue( $svc_www->svcnum,
48     user         => $self->option('user')||'root',
49     host         => $self->machine,
50     command      => eval(qq("$command")),
51   );
52 }
53
54 sub _export_replace {
55   my($self, $new, $old ) = (shift, shift, shift);
56   my $command = $self->option('usermod');
57   
58   #set variable for the command
59   no strict 'vars';
60   {
61     no strict 'refs';
62     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
63     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
64   }
65   my $old_domain_record = $old->domain_record; # or die ?
66   my $old_zone = $old_domain_record->reczone; # or die ?
67   unless ( $old_zone =~ /\.$/ ) {
68     my $old_svc_domain = $old_domain_record->svc_domain; # or die ?
69     $old_zone .= '.'. $old_svc_domain->domain;
70   }
71
72   my $old_svc_acct = $old->svc_acct; # or die ?
73   my $old_username = $old_svc_acct->username;
74   my $old_homedir = $old_svc_acct->dir; # or die ?
75
76   my $new_domain_record = $new->domain_record; # or die ?
77   my $new_zone = $new_domain_record->reczone; # or die ?
78   unless ( $new_zone =~ /\.$/ ) {
79     my $new_svc_domain = $new_domain_record->svc_domain; # or die ?
80     $new_zone .= '.'. $new_svc_domain->domain;
81   }
82
83   my $new_svc_acct = $new->svc_acct; # or die ?
84   my $new_username = $new_svc_acct->username;
85   my $new_homedir = $new_svc_acct->dir; # or die ?
86
87   #done setting variables for the command
88
89   $self->shellcommands_queue( $new->svcnum,
90     user         => $self->option('user')||'root',
91     host         => $self->machine,
92     command      => eval(qq("$command")),
93   );
94 }
95
96 #a good idea to queue anything that could fail or take any time
97 sub shellcommands_queue {
98   my( $self, $svcnum ) = (shift, shift);
99   my $queue = new FS::queue {
100     'svcnum' => $svcnum,
101     'job'    => "FS::part_export::www_shellcommands::ssh_cmd",
102   };
103   $queue->insert( @_ );
104 }
105
106 sub ssh_cmd { #subroutine, not method
107   use Net::SSH '0.07';
108   &Net::SSH::ssh_cmd( { @_ } );
109 }
110
111 #sub shellcommands_insert { #subroutine, not method
112 #}
113 #sub shellcommands_replace { #subroutine, not method
114 #}
115 #sub shellcommands_delete { #subroutine, not method
116 #}
117