source: trunk/locker/deploy/bin/onserver.pm

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