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