explicitly use the necessary modules
[freeside.git] / FS / FS / part_export / forward_shellcommands.pm
1 package FS::part_export::forward_shellcommands;
2
3 use strict;
4 use vars qw(@ISA);
5 use FS::Record qw(qsearchs);
6 use FS::part_export;
7 use FS::svc_acct;
8
9 @ISA = qw(FS::part_export);
10
11 sub rebless { shift; }
12
13 sub _export_insert {
14   my($self) = shift;
15   $self->_export_command('useradd', @_);
16 }
17
18 sub _export_delete {
19   my($self) = shift;
20   $self->_export_command('userdel', @_);
21 }
22
23 sub _export_command {
24   my ( $self, $action, $svc_forward ) = (shift, shift, shift);
25   my $command = $self->option($action);
26
27   #set variable for the command
28   no strict 'vars';
29   {
30     no strict 'refs';
31     ${$_} = $svc_forward->getfield($_) foreach $svc_forward->fields;
32   }
33
34   my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $self->srcsvc } );
35   $username = $svc_acct->username;
36   $domain = $svc_acct->domain;
37   if ($self->dstsvc) {
38     $destination = $self->dstsvc_acct->email;
39   } else {
40     $destination = $self->dst;
41   }
42
43   #done setting variables for the command
44
45   $self->shellcommands_queue( $svc_forward->svcnum,
46     user         => $self->option('user')||'root',
47     host         => $self->machine,
48     command      => eval(qq("$command")),
49   );
50 }
51
52 sub _export_replace {
53   my( $self, $new, $old ) = (shift, shift, shift);
54   my $command = $self->option('usermod');
55   
56   #set variable for the command
57   no strict 'vars';
58   {
59     no strict 'refs';
60     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
61     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
62   }
63
64   my $old_svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $self->srcsvc } );
65   $old_username = $old_svc_acct->username;
66   $old_domain = $old_svc_acct->domain;
67   if ($self->dstsvc) {
68     $old_destination = $self->dstsvc_acct->email;
69   } else {
70     $old_destination = $self->dst;
71   }
72
73   my $new_svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $self->srcsvc } );
74   $new_username = $new_svc_acct->username;
75   $new_domain = $new_svc_acct->domain;
76   if ($self->dstsvc) {
77     $new_destination = $self->dstsvc_acct->email;
78   } else {
79     $new_destination = $self->dst;
80   }
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::forward_shellcommands::ssh_cmd",
97   };
98   $queue->insert( @_ );
99 }
100
101 sub ssh_cmd { #subroutine, not method
102   use Net::SSH '0.07';
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