This commit was generated by cvs2svn to compensate for changes in r4407,
[freeside.git] / FS / FS / part_export / cpanel.pm
1 package FS::part_export::cpanel;
2
3 use vars qw(@ISA %info);
4 use Tie::IxHash;
5 use FS::part_export;
6
7 @ISA = qw(FS::part_export);
8
9 tie my %options, 'Tie::IxHash',
10   'user'       => { label=>'Remote access username' },
11   'accesshash' => { label=>'Remote access key', type=>'textarea' },
12   'debug'      => { label=>'Enable debugging', type=>'checkbox' },
13 ;
14
15 %info = (
16   'svc'      => 'svc_acct',
17   'desc'     => 'Real-time export to Cpanel control panel.',
18   'options'  => \%options,
19   'nodomain' => 'Y',
20   'notes'    => 'Real time export to a the <a href="http://www.cpanel.net/">Cpanel</a> control panel software.  Service definition names are exported as Cpanel packages.  Requires installation of the Cpanel::Accounting perl module distributed with Cpanel.',
21 );
22
23 sub rebless { shift; }
24
25 sub _export_insert { 
26   my($self, $svc_acct) = (shift, shift);
27   $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum, 'insert',
28     $svc_acct->domain,
29     $svc_acct->username,
30     $svc_acct->_password,
31     $svc_acct->cust_svc->part_svc->svc,
32   );
33   ref($err_or_queue) ? '' : $err_or_queue;
34 }
35
36 sub _export_replace {
37   my( $self, $new, $old ) = (shift, shift, shift);
38   return "can't change username with cpanel"
39     if $old->username ne $new->username;
40   return "can't change password with cpanel"
41     if $old->_passsword ne $new->_password;
42   return "can't change domain with cpanel"
43     if $old->domain ne $new->domain;
44
45   '';
46
47   ##return '' unless $old->_password ne $new->_password;
48   #$err_or_queue = $self->cpanel_queue( $new->svcnum,
49   #  'replace', $new->username, $new->_password );
50   #ref($err_or_queue) ? '' : $err_or_queue;
51 }
52
53 sub _export_delete {
54   my( $self, $svc_acct ) = (shift, shift);
55   $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum,
56     'delete', $svc_acct->username
57   );
58   ref($err_or_queue) ? '' : $err_or_queue;
59 }
60
61 sub _export_suspend {
62   my( $self, $svc_acct ) = (shift, shift);
63   $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum,
64     'suspend', $svc_acct->username );
65   ref($err_or_queue) ? '' : $err_or_queue;
66 }
67
68 sub _export_unsuspend {
69   my( $self, $svc_acct ) = (shift, shift);
70   $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum,
71     'unsuspend', $svc_acct->username );
72   ref($err_or_queue) ? '' : $err_or_queue;
73 }
74
75
76 sub cpanel_queue {
77   my( $self, $svcnum, $method ) = (shift, shift, shift);
78   my $queue = new FS::queue {
79     'svcnum' => $svcnum,
80     'job'    => "FS::part_export::cpanel::cpanel_$method",
81   };
82   $queue->insert(
83     $self->machine,
84     $self->option('user'),
85     $self->option('accesshash'),
86     $self->option('debug'),
87     @_ 
88   ) or $queue;
89 }
90
91
92 sub cpanel_insert { #subroutine, not method
93   my( $machine, $user, $accesshash, $debug ) = splice(@_,0,4);
94   my $whm = cpanel_connect($machine, $user, $accesshash, $debug);
95   warn "  cpanel->createacct ". join(', ', @_). "\n"
96     if $debug;
97   my $response = $whm->createacct(@_);
98   die $whm->{'error'} if $whm->{'error'};
99   warn "  cpanel response: $response\n"
100     if $debug;
101 }
102
103 #sub cpanel_replace { #subroutine, not method
104 #}
105
106 sub cpanel_delete { #subroutine, not method
107   my( $machine, $user, $accesshash, $debug ) = splice(@_,0,4);
108   my $whm = cpanel_connect($machine, $user, $accesshash, $debug);
109   warn "  cpanel->killacct ". join(', ', @_). "\n"
110     if $debug;
111   my $response = $whm->killacct(shift);
112   die $whm->{'error'} if $whm->{'error'};
113   warn "  cpanel response: $response\n"
114     if $debug;
115 }
116
117 sub cpanel_suspend { #subroutine, not method
118   my( $machine, $user, $accesshash, $debug ) = splice(@_,0,4);
119   my $whm = cpanel_connect($machine, $user, $accesshash, $debug);
120   warn "  cpanel->suspend ". join(', ', @_). "\n"
121     if $debug;
122   my $response = $whm->suspend(shift);
123   die $whm->{'error'} if $whm->{'error'};
124   warn "  cpanel response: $response\n"
125     if $debug;
126 }
127
128 sub cpanel_unsuspend { #subroutine, not method
129   my( $machine, $user, $accesshash, $debug ) = splice(@_,0,4);
130   my $whm = cpanel_connect($machine, $user, $accesshash, $debug);
131   warn "  cpanel->unsuspend ". join(', ', @_). "\n"
132     if $debug;
133   my $response = $whm->unsuspend(shift);
134   die $whm->{'error'} if $whm->{'error'};
135   warn "  cpanel response: $response\n"
136     if $debug;
137 }
138
139 sub cpanel_connect {
140   my( $host, $user, $accesshash, $debug ) = @_;
141
142   eval "use Cpanel::Accounting;";
143   die $@ if $@;
144
145   warn "creating new Cpanel::Accounting connection to $user@$host\n"
146     if $debug;
147
148   my $whm = new Cpanel::Accounting;
149   $whm->{'host'}       = $host;
150   $whm->{'user'}       = $user;
151   $whm->{'accesshash'} = $accesshash;
152   $whm->{'usessl'}     = 1;
153
154   $whm;
155 }