870d7f1ee4e759ee852b44a260d725f4395676b9
[freeside.git] / FS / FS / part_export / www_shellcommands.pm
1 package FS::part_export::shellcommands;
2
3 use vars qw(@ISA);
4 use FS::part_export;
5
6 @ISA = qw(FS::part_export);
7
8 sub rebless { shift; }
9
10 sub _export_insert {
11   my($self) = shift;
12   $self->_export_command('useradd', @_);
13 }
14
15 sub _export_delete {
16   my($self) = shift;
17   $self->_export_command('userdel', @_);
18 }
19
20 sub _export_command {
21   my ( $self, $action, $svc_acct) = (shift, shift, shift);
22   my $command = $self->option($action);
23   my $stdin = $self->option($action."_stdin");
24   no strict 'refs';
25   ${$_} = $svc_acct->getfield($_) foreach $svc_acct->fields;
26   $self->shellcommands_queue( $svc_acct->svcnum,
27     user         => $self->options('user')||'root',
28     host         => $self->options('machine'),
29     command      => eval(qq("$command")),
30     stdin_string => eval(qq("$stdin")),
31   );
32 }
33
34 sub _export_replace {
35   my($self, $new, $old ) = (shift, shift, shift);
36   my $command = $self->option('usermod');
37   my $stdin = $self->option('usermod_stdin');
38   no strict 'refs';
39   ${"old_$_"} = $old->getfield($_) foreach $old->fields;
40   ${"new_$_"} = $new->getfield($_) foreach $new->fields;
41   $self->shellcommands_queue( $new->svcnum,
42     user         => $self->options('user')||'root',
43     host         => $self->options('machine'),
44     command      => eval(qq("$command")),
45     stdin_string => eval(qq("$stdin")),
46   );
47 }
48
49 #a good idea to queue anything that could fail or take any time
50 sub shellcommands_queue {
51   my( $self, $svcnum ) = (shift, shift);
52   my $queue = new FS::queue {
53     'svcnum' => $svcnum,
54     'job'    => "FS::part_export::shellcommands::ssh_cmd",
55   };
56   $queue->insert( @_ );
57 }
58
59 sub ssh_cmd { #subroutine, not method
60   use Net::SSH '0.06';
61   &Net::SSH::ssh_cmd( { @_ } );
62 }
63
64 #sub shellcommands_insert { #subroutine, not method
65 #}
66 #sub shellcommands_replace { #subroutine, not method
67 #}
68 #sub shellcommands_delete { #subroutine, not method
69 #}
70