This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / bin / apache.export
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Std;
5 #use File::Path;
6 use File::Rsync;
7 use Net::SSH qw(ssh);
8 use FS::UID qw(adminsuidsetup datasrc);
9 use FS::Record qw(qsearch qsearchs);
10 use FS::part_export;
11 use FS::cust_svc;
12 use FS::svc_www;
13
14 use vars qw(%opt);
15 getopts("d", \%opt);
16
17 my $user = shift or die &usage;
18 adminsuidsetup $user;
19
20 #needs the export number in there somewhere too...?
21 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/apache";
22 mkdir $spooldir, 0700 unless -d $spooldir;
23
24 my @exports = qsearch('part_export', { 'exporttype' => 'apache' } );
25
26 my $rsync = File::Rsync->new({
27   rsh     => 'ssh',
28 #  dry_run => 1,
29 });
30
31 foreach my $export ( @exports ) {
32
33   my $machine   = $export->machine;
34   my $exportnum = $export->exportnum;
35   my $file = "$spooldir/$machine.exportnum$exportnum.conf";
36
37   warn "exporting apache configuration for $machine to $file\n"
38     if $opt{d};
39
40   open(HTTPD_CONF,">$file") or die "can't open $file: $!";
41
42   my $template = $export->option('template');
43
44   my @svc_www = $export->svc_x;
45
46   foreach my $svc_www ( @svc_www ) {
47     use vars qw($zone $username $dir $email $config);
48     $zone = $svc_www->domain_record->zone;
49     $config = $svc_www->config;
50     if ( $svc_www->svc_acct ) {
51       $username = $svc_www->svc_acct->username;
52       $dir = $svc_www->svc_acct->dir;
53       $email = $svc_www->svc_acct->email;
54     } else {
55       $username = '';
56       $dir      = '';
57       $email    = '';
58     }
59
60     warn "  adding configuration section for $zone\n"
61       if $opt{d};
62
63     print HTTPD_CONF eval(qq("$template")). "\n\n";
64   }
65
66   my $user = $export->option('user');
67   my $httpd_conf = $export->option('httpd_conf');
68
69   warn "syncing $file to $httpd_conf on $machine\n"
70     if $opt{d};
71
72   $rsync->exec( {
73     src       => $file,
74     dest      => "$user\@$machine:$httpd_conf",
75   } ) or die "rsync to $machine failed: ". join(" / ", $rsync->err);
76  # warn $rsync->out;
77
78   my $restart = $export->option('restart') || 'apachectl graceful';
79
80   warn "running restart command $restart on $machine\n"
81     if $opt{d};
82
83   ssh("root\@$machine", $restart);
84
85 }
86
87 close HTTPD_CONF;
88
89 # -----
90
91 sub usage {
92   die "Usage:\n  apache.export [ -d ] user\n"; 
93 }
94