This commit was generated by cvs2svn to compensate for changes in r2523,
[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::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_forward ) = (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_forward->getfield($_) foreach $svc_forward->fields;
30   }
31
32   my $svc_acct = $svc_forward->srcsvc_acct;
33   $username = $svc_acct->username;
34   $domain = $svc_acct->domain;
35   if ($svc_forward->dstsvc_acct) {
36     $destination = $svc_forward->dstsvc_acct->email;
37   } else {
38     $destination = $svc_forward->dst;
39   }
40
41   #done setting variables for the command
42
43   $self->shellcommands_queue( $svc_forward->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   no strict 'vars';
56   {
57     no strict 'refs';
58     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
59     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
60   }
61
62   my $old_svc_acct = $old->srcsvc_acct;
63   $old_username = $old_svc_acct->username;
64   $old_domain = $old_svc_acct->domain;
65   if ($old->dstsvc_acct) {
66     $old_destination = $old->dstsvc_acct->email;
67   } else {
68     $old_destination = $old->dst;
69   }
70
71   my $new_svc_acct = $new->srcsvc_acct;
72   $new_username = $new_svc_acct->username;
73   $new_domain = $new_svc_acct->domain;
74   if ($new->dstsvc) {
75     $new_destination = $new->dstsvc_acct->email;
76   } else {
77     $new_destination = $new->dst;
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::forward_shellcommands::ssh_cmd",
95   };
96   $queue->insert( @_ );
97 }
98
99 sub ssh_cmd { #subroutine, not method
100   use Net::SSH '0.07';
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