d295eece0fcc9a6a9790dc05f057e9ab460444f7
[freeside.git] / FS / FS / part_export / domain_shellcommands.pm
1 package FS::part_export::domain_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_domain) = (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_domain->getfield($_) foreach $svc_domain->fields;
30   }
31   ( $qdomain = $domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
32
33   if ( $svc_domain->catchall ) {
34     no strict 'refs';
35     my $svc_acct = $svc_domain->catchall_svc_acct;
36     ${$_} = $svc_acct->getfield($_) foreach qw(uid gid dir);
37   } else {
38     no strict 'refs';
39     ${$_} = '' foreach qw(uid gid dir);
40   }
41
42   #done setting variables for the command
43
44   $self->shellcommands_queue( $svc_domain->svcnum,
45     user         => $self->option('user')||'root',
46     host         => $self->machine,
47     command      => eval(qq("$command")),
48   );
49 }
50
51 sub _export_replace {
52   my($self, $new, $old ) = (shift, shift, shift);
53   my $command = $self->option('usermod');
54   
55   #set variable for the command
56   no strict 'vars';
57   {
58     no strict 'refs';
59     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
60     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
61   }
62   ( $old_qdomain = $old_domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
63   ( $new_qdomain = $new_domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
64
65   if ( $old->catchall ) {
66     no strict 'refs';
67     my $svc_acct = $old->catchall_svc_acct;
68     ${"old_$_"} = $svc_acct->getfield($_) foreach qw(uid gid dir);
69   } else {
70     ${"old_$_"} = '' foreach qw(uid gid dir);
71   }
72   if ( $new->catchall ) {
73     no strict 'refs';
74     my $svc_acct = $new->catchall_svc_acct;
75     ${"new_$_"} = $svc_acct->getfield($_) foreach qw(uid gid dir);
76   } else {
77     ${"new_$_"} = '' foreach qw(uid gid dir);
78   }
79
80   #done setting variables for the command
81
82   $self->shellcommands_queue( $new->svcnum,
83     user         => $self->option('user')||'root',
84     host         => $self->machine,
85     command      => eval(qq("$command")),
86   );
87 }
88
89 #a good idea to queue anything that could fail or take any time
90 sub shellcommands_queue {
91   my( $self, $svcnum ) = (shift, shift);
92   my $queue = new FS::queue {
93     'svcnum' => $svcnum,
94     'job'    => "FS::part_export::domain_shellcommands::ssh_cmd",
95   };
96   $queue->insert( @_ );
97 }
98
99 sub ssh_cmd { #subroutine, not method
100   use Net::SSH '0.08';
101   &Net::SSH::ssh_cmd( { @_ } );
102 }
103
104 #sub shellcommands_insert { #subroutine, not method
105 #}
106 #sub shellcommands_replace { #subroutine, not method
107 #}
108 #sub shellcommands_delete { #subroutine, not method
109 #}
110