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

Last change on this file since 466 was 466, checked in by andersk, 17 years ago
Prepare to get rid of curl.
File size: 3.9 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    my $getpwd=system("/mit/scripts/sql/bin$scriptsdev/get-password");
94    ($sqlhost, $sqluser, $sqlpass) = split(/\s/, $getpwd);
95   
96    $sqldb=system("/mit/scripts/sql/bin$scriptsdev/get-next-database", $addrlast);
97    $sqldb=system("/mit/scripts/sql/bin$scriptsdev/create-database", $sqldb);
98    if($sqldb eq "") {
99      print "\nERROR:\n";
100      print "Your SQL account failed to create a SQL database.\n";
101      print "You should log in at http://sql.mit.edu to check whether\n";
102      print "your SQL account is at its database limit or its storage limit.\n";
103      print "If you cannot determine the cause of the problem, please\n";
104      print "feel free to contact sql\@mit.edu for assistance.\n";
105      open FAILED, ">.failed";
106      close FAILED;
107      exit 1;
108    }
109    $sqldbcurl = $sqldb;
110    $sqldbcurl =~ s/\+/\%2B/;
111  }
112 
113  if(-e "$HOME/web_scripts/$addrend/.admin") { 
114    open ADMIN, "<$HOME/web_scripts/$addrend/.admin";
115    $admin_password=<ADMIN>;
116    chomp($admin_password);
117    close ADMIN;
118    unlink "$HOME/web_scripts/$addrend/.admin";
119  } 
120
121  print "\nConfiguring $sname...\n";
122 
123  open(VERSION, ">.scripts-version") or die "Can't write scripts-version file: $!\n";
124  print VERSION strftime("%F %T %z\n", localtime);
125  print VERSION $ENV{'USER'}, '@', getclienthostname(), "\n";
126  my $tarball = abs_path("/mit/scripts/deploy$scriptsdev/$deploy.tar.gz");
127  print VERSION $tarball, "\n";
128  $tarball =~ s|/deploydev/|/deploy/|;
129  print VERSION dirname($tarball), "\n";
130  close(VERSION);
131
132  select STDOUT;
133  $| = 1; # STDOUT is *hot*!
134}
135
1361;
Note: See TracBrowser for help on using the repository browser.