44d7487da5d5f92a63d5c538ad37779dc1677b6c
[freeside.git] / FS / FS / svc_www.pm
1 package FS::svc_www;
2
3 use strict;
4 use vars qw(@ISA $conf $apacheroot $apachemachine $nossh_hack );
5 #use FS::Record qw( qsearch qsearchs );
6 use FS::Record qw( qsearchs );
7 use FS::svc_Common;
8 use FS::cust_svc;
9 use FS::domain_record;
10 use FS::svc_acct;
11 use Net::SSH qw(ssh);
12
13 @ISA = qw( FS::svc_Common );
14
15 #ask FS::UID to run this stuff for us later
16 $FS::UID::callback{'FS::svc_www'} = sub { 
17   $conf = new FS::Conf;
18   $apacheroot = $conf->config('apacheroot');
19   $apachemachine = $conf->config('apachemachine');
20 };
21
22 =head1 NAME
23
24 FS::svc_www - Object methods for svc_www records
25
26 =head1 SYNOPSIS
27
28   use FS::svc_www;
29
30   $record = new FS::svc_www \%hash;
31   $record = new FS::svc_www { 'column' => 'value' };
32
33   $error = $record->insert;
34
35   $error = $new_record->replace($old_record);
36
37   $error = $record->delete;
38
39   $error = $record->check;
40
41   $error = $record->suspend;
42
43   $error = $record->unsuspend;
44
45   $error = $record->cancel;
46
47 =head1 DESCRIPTION
48
49 An FS::svc_www object represents an web virtual host.  FS::svc_www inherits
50 from FS::svc_Common.  The following fields are currently supported:
51
52 =over 4
53
54 =item svcnum - primary key
55
56 =item recnum - DNS `A' record corresponding to this web virtual host. (see L<FS::domain_record>)
57
58 =item usersvc - account (see L<FS::svc_acct>) corresponding to this web virtual host.
59
60 =back
61
62 =head1 METHODS
63
64 =over 4
65
66 =item new HASHREF
67
68 Creates a new web virtual host.  To add the record to the database, see
69 L<"insert">.
70
71 Note that this stores the hash reference, not a distinct copy of the hash it
72 points to.  You can ask the object for a copy with the I<hash> method.
73
74 =cut
75
76 sub table { 'svc_www'; }
77
78 =item insert
79
80 Adds this record to the database.  If there is an error, returns the error,
81 otherwise returns false.
82
83 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be 
84 defined.  An FS::cust_svc record will be created and inserted.
85
86 If the configuration values (see L<FS::Conf>) I<apachemachine>, and
87 I<apacheroot> exist, the command:
88
89   mkdir $apacheroot/$zone;
90   chown $username $apacheroot/$zone;
91   ln -s $apacheroot/$zone $homedir/$zone
92
93 I<$zone> is the DNS A record pointed to by I<recnum>
94 I<$username> is the username pointed to by I<usersvc>
95 I<$homedir> is that user's home directory
96
97 is executed on I<apachemachine> via ssh.  This behaviour can be surpressed by
98 setting $FS::svc_www::nossh_hack true.
99
100 =cut
101
102 sub insert {
103   my $self = shift;
104   my $error;
105
106   $error = $self->SUPER::insert;
107   return $error if $error;
108
109   my $domain_record = qsearchs('domain_record', { 'recnum' => $self->recnum } );    # or die ?
110   my $zone = $domain_record->reczone;
111     # or die ?
112   unless ( $zone =~ /\.$/ ) {
113     my $dom_svcnum = $domain_record->svcnum;
114     my $svc_domain = qsearchs('svc_domain', { 'svcnum' => $dom_svcnum } );
115       # or die ?
116     $zone .= $svc_domain->domain;
117   }
118
119   my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $self->usersvc } );
120     # or die ?
121   my $username = $svc_acct->username;
122     # or die ?
123   my $homedir = $svc_acct->dir;
124     # or die ?
125
126   if ( $apachemachine
127        && $apacheroot
128        && $zone
129        && $username
130        && $homedir
131        && ! $nossh_hack
132   ) {
133     ssh("root\@$apachemachine",
134         "mkdir $apacheroot/$zone; ".
135         "chown $username $apacheroot/$zone; ".
136         "ln -s $apacheroot/$zone $homedir/$zone"
137     );
138   }
139
140   '';
141 }
142
143 =item delete
144
145 Delete this record from the database.
146
147 =cut
148
149 sub delete {
150   my $self = shift;
151   my $error;
152
153   $error = $self->SUPER::delete;
154   return $error if $error;
155
156   '';
157 }
158
159 =item replace OLD_RECORD
160
161 Replaces the OLD_RECORD with this one in the database.  If there is an error,
162 returns the error, otherwise returns false.
163
164 =cut
165
166 sub replace {
167   my ( $new, $old ) = ( shift, shift );
168   my $error;
169
170   $error = $new->SUPER::replace($old);
171   return $error if $error;
172
173   '';
174 }
175
176 =item suspend
177
178 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
179
180 =item unsuspend
181
182 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
183
184 =item cancel
185
186 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
187
188 =item check
189
190 Checks all fields to make sure this is a valid example.  If there is
191 an error, returns the error, otherwise returns false.  Called by the insert
192 and repalce methods.
193
194 =cut
195
196 sub check {
197   my $self = shift;
198
199   my $x = $self->setfixed;
200   return $x unless ref($x);
201   my $part_svc = $x;
202
203   my $error =
204     $self->ut_numbern('svcnum')
205     || $self->ut_number('recnum')
206     || $self->ut_number('usersvc')
207   ;
208   return $error if $error;
209
210   return "Unknown recnum: ". $self->recnum
211     unless qsearchs('domain_record', { 'recnum' => $self->recnum } );
212
213   return "Unknown usersvc (svc_acct.svcnum): ". $self->usersvc
214     unless qsearchs('svc_acct', { 'svcnum' => $self->usersvc } );
215
216   ''; #no error
217 }
218
219 =back
220
221 =head1 VERSION
222
223 $Id: svc_www.pm,v 1.5 2001-08-21 02:44:47 ivan Exp $
224
225 =head1 BUGS
226
227 =head1 SEE ALSO
228
229 L<FS::svc_Common>, L<FS::Record>, L<FS::domain_record>, L<FS::cust_svc>,
230 L<FS::part_svc>, L<FS::cust_pkg>, schema.html from the base documentation.
231
232 =cut
233
234 1;
235