source: locker/deploy/bin/onserver.pm @ 472

Last change on this file since 472 was 472, checked in by andersk, 15 years ago
Uncurl advancedbook, advancedpoll, e107, joomla, phpbb.
File size: 4.0 KB
Line 
1package onserver;
2use strict;
3use Exporter;
4use Sys::Hostname;
5use File::Spec::Functions;
6use File::Basename;
7use Socket;
8use Cwd qw(abs_path);
9use POSIX qw(strftime);
10use LWP::UserAgent;
11use URI;
12our @ISA = qw(Exporter);
13our @EXPORT = qw(setup totmp fetch_uri print_login_info press_enter $server $tmp $USER $HOME $sname $deploy $addrend $base_uri $ua $admin_username $requires_sql $addrlast $sqlhost $sqluser $sqlpass $sqldb $sqldbcurl $admin_password $scriptsdev $human);
14
15our $server = "scripts.mit.edu";
16
17our ($tmp, $USER, $HOME, $sname, $deploy, $addrend, $base_uri, $ua, $admin_username, $requires_sql, $addrlast, $sqlhost, $sqluser, $sqlpass, $sqldb, $sqldbcurl, $admin_password, $scriptsdev, $human);
18
19$tmp = ".scripts-tmp";
20sub totmp {
21  open(FILE, ">$tmp");
22  print FILE $_[0];
23  close(FILE);
24}
25
26$ua = LWP::UserAgent->new;
27
28sub fetch_uri {
29    my ($uri, $get, $post) = @_;
30    my $u = URI->new($uri);
31    my $req;
32    if (defined $post) {
33        $u->query_form($post);
34        my $content = $u->query;
35        $u->query_form($get);
36        $req = HTTP::Request->new(POST => $u->abs($base_uri));
37        $req->content_type('application/x-www-form-urlencoded');
38        $req->content($content);
39    } else {
40        $u->query_form($get) if (defined $get);
41        $req = HTTP::Request->new(GET => $u->abs($base_uri));
42    }
43    my $res = $ua->request($req);
44    if ($res->is_success) {
45        return $res->content;
46    } else {
47        print STDERR "Error fetching configuration page: ", $res->status_line, "\n";
48        return undef;
49    }
50}
51
52sub print_login_info {
53  print "\nYou will be able to log in to $sname using the following:\n";
54  print "  username: $admin_username\n";
55  print "  password: $admin_password\n";
56}
57
58sub getclienthostname {
59    if (my $sshclient = $ENV{"SSH_CLIENT"}) {
60        my ($clientip) = split(' ', $sshclient);
61        my $hostname = gethostbyaddr(inet_aton($clientip), AF_INET);
62        return $hostname || $clientip;
63    } else {
64        return hostname();
65    }
66}
67
68sub press_enter {
69  local $/ = "\n";
70  print "Press [enter] to continue with the install.";
71  my $enter = <STDIN>; 
72}
73
74sub setup {
75  $ENV{PATH} = '/bin:/usr/bin';
76  $USER = $ENV{USER};
77  $HOME = $ENV{HOME};
78 
79  ($sname, $deploy, $addrend, $admin_username, $requires_sql, $scriptsdev, $human) = @ARGV;
80  chdir "$HOME/web_scripts/$addrend";
81 
82  if($addrend =~ /^(.*)\/$/) {
83    $addrend = $1;
84  }
85  ($addrlast) = ($addrend =~ /([^\/]*)$/);
86 
87  $base_uri = "http://$server/~$USER/$addrend/";
88 
89  if($requires_sql) {
90    print "\nCreating SQL database for $sname...\n";
91   
92    open GETPWD, '-|', "/mit/scripts/sql/bin$scriptsdev/get-password";
93    ($sqlhost, $sqluser, $sqlpass) = split(/\s/, <GETPWD>);
94    close GETPWD;
95    open SQLDB, '-|', "/mit/scripts/sql/bin$scriptsdev/get-next-database", $addrlast;
96    $sqldb = <SQLDB>;
97    close SQLDB;
98    open SQLDB, '-|', "/mit/scripts/sql/bin$scriptsdev/create-database", $sqldb;
99    $sqldb = <SQLDB>;
100    close SQLDB;
101    if($sqldb eq "") {
102      print "\nERROR:\n";
103      print "Your SQL account failed to create a SQL database.\n";
104      print "You should log in at http://sql.mit.edu to check whether\n";
105      print "your SQL account is at its database limit or its storage limit.\n";
106      print "If you cannot determine the cause of the problem, please\n";
107      print "feel free to contact sql\@mit.edu for assistance.\n";
108      open FAILED, ">.failed";
109      close FAILED;
110      exit 1;
111    }
112    $sqldbcurl = $sqldb;
113    $sqldbcurl =~ s/\+/\%2B/;
114  }
115 
116  if(-e "$HOME/web_scripts/$addrend/.admin") { 
117    open ADMIN, "<$HOME/web_scripts/$addrend/.admin";
118    $admin_password=<ADMIN>;
119    chomp($admin_password);
120    close ADMIN;
121    unlink "$HOME/web_scripts/$addrend/.admin";
122  } 
123
124  print "\nConfiguring $sname...\n";
125 
126  open(VERSION, ">.scripts-version") or die "Can't write scripts-version file: $!\n";
127  print VERSION strftime("%F %T %z\n", localtime);
128  print VERSION $ENV{'USER'}, '@', getclienthostname(), "\n";
129  my $tarball = abs_path("/mit/scripts/deploy$scriptsdev/$deploy.tar.gz");
130  print VERSION $tarball, "\n";
131  $tarball =~ s|/deploydev/|/deploy/|;
132  print VERSION dirname($tarball), "\n";
133  close(VERSION);
134
135  select STDOUT;
136  $| = 1; # STDOUT is *hot*!
137}
138
1391;
Note: See TracBrowser for help on using the repository browser.