summaryrefslogtreecommitdiff
path: root/bin/apache.export
blob: 02dd58a2dae2d4cb7f8b75ecbebcf9bf8d9118af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/perl -w

use strict;
use Getopt::Std;
#use File::Path;
use File::Rsync;
use Net::SSH qw(ssh);
use FS::UID qw(adminsuidsetup datasrc);
use FS::Record qw(qsearch qsearchs);
use FS::part_export;
use FS::cust_svc;
use FS::svc_www;

use vars qw(%opt);
getopts("d", \%opt);

my $user = shift or die &usage;
adminsuidsetup $user;

#needs the export number in there somewhere too...?
my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/apache";
mkdir $spooldir, 0700 unless -d $spooldir;

my @exports = qsearch('part_export', { 'exporttype' => 'apache' } );

my $rsync = File::Rsync->new({
  rsh     => 'ssh',
#  dry_run => 1,
});

foreach my $export ( @exports ) {

  my $machine   = $export->machine;
  my $exportnum = $export->exportnum;
  my $file = "$spooldir/$machine.exportnum$exportnum.conf";

  warn "exporting apache configuration for $machine to $file\n"
    if $opt{d};

  open(HTTPD_CONF,">$file") or die "can't open $file: $!";

  my @svc_www = $export->svc_x;

  foreach my $svc_www ( @svc_www ) {
    use vars qw($zone $username $dir $email $config);
    $zone = $svc_www->domain_record->zone;
    $config = $svc_www->config;
    my $template = $export->option('template');
    my $cust_pkg = $svc_www->cust_svc->cust_pkg;
    if ( $cust_pkg->getfield('susp') or $cust_pkg->getfield('cancel') ) {
      $template = $export->option('template_inactive') 
                  || $export->option('template');
      # Fall back to the regular template if template_inactive doesn't exist
    }
    if ( $svc_www->svc_acct ) {
      $username = $svc_www->svc_acct->username;
      $dir = $svc_www->svc_acct->dir;
      $email = $svc_www->svc_acct->email;
    } else {
      $username = '';
      $dir      = '';
      $email    = '';
    }

    warn "  adding configuration section for $zone\n"
      if $opt{d};

    print HTTPD_CONF eval(qq("$template")). "\n\n";
  }

  my $user = $export->option('user');
  my $httpd_conf = $export->option('httpd_conf');

  warn "syncing $file to $httpd_conf on $machine\n"
    if $opt{d};

  $rsync->exec( {
    src       => $file,
    dest      => "$user\@$machine:$httpd_conf",
  } ) or die "rsync to $machine failed: ". join(" / ", $rsync->err);
 # warn $rsync->out;

  my $restart = $export->option('restart') || 'apachectl graceful';

  warn "running restart command $restart on $machine\n"
    if $opt{d};

  ssh("root\@$machine", $restart);

}

close HTTPD_CONF;

# -----

sub usage {
  die "Usage:\n  apache.export [ -d ] user\n"; 
}