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

Last change on this file since 467 was 467, checked in by andersk, 16 years ago
Right.
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 $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, $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
26my $ua = LWP::UserAgent->new;
27my $base_uri;
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 
80  ($sname, $deploy, $addrend, $admin_username, $requires_sql, $scriptsdev, $human) = @ARGV;
81  chdir "$HOME/web_scripts/$addrend";
82 
83  if($addrend =~ /^(.*)\/$/) {
84    $addrend = $1;
85  }
86  ($addrlast) = ($addrend =~ /([^\/]*)$/);
87 
88  $base_uri = "http://$server/~$USER/$addrend/";
89 
90  if($requires_sql) {
91    print "\nCreating SQL database for $sname...\n";
92   
93    open GETPWD, '-|', "/mit/scripts/sql/bin$scriptsdev/get-password";
94    ($sqlhost, $sqluser, $sqlpass) = split(/\s/, <GETPWD>);
95    close GETPWD;
96    open SQLDB, '-|', "/mit/scripts/sql/bin$scriptsdev/get-next-database", $addrlast;
97    $sqldb = <SQLDB>;
98    close SQLDB;
99    open SQLDB, '-|', "/mit/scripts/sql/bin$scriptsdev/create-database", $sqldb;
100    $sqldb = <SQLDB>;
101    close SQLDB;
102    if($sqldb eq "") {
103      print "\nERROR:\n";
104      print "Your SQL account failed to create a SQL database.\n";
105      print "You should log in at http://sql.mit.edu to check whether\n";
106      print "your SQL account is at its database limit or its storage limit.\n";
107      print "If you cannot determine the cause of the problem, please\n";
108      print "feel free to contact sql\@mit.edu for assistance.\n";
109      open FAILED, ">.failed";
110      close FAILED;
111      exit 1;
112    }
113    $sqldbcurl = $sqldb;
114    $sqldbcurl =~ s/\+/\%2B/;
115  }
116 
117  if(-e "$HOME/web_scripts/$addrend/.admin") { 
118    open ADMIN, "<$HOME/web_scripts/$addrend/.admin";
119    $admin_password=<ADMIN>;
120    chomp($admin_password);
121    close ADMIN;
122    unlink "$HOME/web_scripts/$addrend/.admin";
123  } 
124
125  print "\nConfiguring $sname...\n";
126 
127  open(VERSION, ">.scripts-version") or die "Can't write scripts-version file: $!\n";
128  print VERSION strftime("%F %T %z\n", localtime);
129  print VERSION $ENV{'USER'}, '@', getclienthostname(), "\n";
130  my $tarball = abs_path("/mit/scripts/deploy$scriptsdev/$deploy.tar.gz");
131  print VERSION $tarball, "\n";
132  $tarball =~ s|/deploydev/|/deploy/|;
133  print VERSION dirname($tarball), "\n";
134  close(VERSION);
135
136  select STDOUT;
137  $| = 1; # STDOUT is *hot*!
138}
139
1401;
Note: See TracBrowser for help on using the repository browser.