1 | package onserver; |
---|
2 | use strict; |
---|
3 | use Exporter; |
---|
4 | use Sys::Hostname; |
---|
5 | use File::Spec::Functions; |
---|
6 | use File::Basename; |
---|
7 | use Socket; |
---|
8 | use Cwd qw(abs_path); |
---|
9 | use POSIX qw(strftime); |
---|
10 | our @ISA = qw(Exporter); |
---|
11 | our @EXPORT = qw(setup totmp 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); |
---|
12 | |
---|
13 | our $server = "scripts.mit.edu"; |
---|
14 | |
---|
15 | our ($tmp, $USER, $HOME, $sname, $deploy, $addrend, $admin_username, $requires_sql, $addrlast, $sqlhost, $sqluser, $sqlpass, $sqldb, $sqldbcurl, $admin_password, $scriptsdev, $human); |
---|
16 | |
---|
17 | $tmp = ".scripts-tmp"; |
---|
18 | sub totmp { |
---|
19 | open(FILE, ">$tmp"); |
---|
20 | print FILE $_[0]; |
---|
21 | close(FILE); |
---|
22 | } |
---|
23 | |
---|
24 | sub print_login_info { |
---|
25 | print "\nYou will be able to log in to $sname using the following:\n"; |
---|
26 | print " username: $admin_username\n"; |
---|
27 | print " password: $admin_password\n"; |
---|
28 | } |
---|
29 | |
---|
30 | sub getclienthostname { |
---|
31 | if (my $sshclient = $ENV{"SSH_CLIENT"}) { |
---|
32 | my ($clientip) = split(' ', $sshclient); |
---|
33 | my $hostname = gethostbyaddr(inet_aton($clientip), AF_INET); |
---|
34 | return $hostname || $clientip; |
---|
35 | } else { |
---|
36 | return hostname(); |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | sub press_enter { |
---|
41 | local $/ = "\n"; |
---|
42 | print "Press [enter] to continue with the install."; |
---|
43 | my $enter = <STDIN>; |
---|
44 | } |
---|
45 | |
---|
46 | sub setup { |
---|
47 | $ENV{PATH} = '/bin:/usr/bin'; |
---|
48 | $USER = $ENV{USER}; |
---|
49 | $HOME = $ENV{HOME}; |
---|
50 | |
---|
51 | ($sname, $deploy, $addrend, $admin_username, $requires_sql, $scriptsdev, $human) = @ARGV; |
---|
52 | chdir "$HOME/web_scripts/$addrend"; |
---|
53 | |
---|
54 | if($addrend =~ /^(.*)\/$/) { |
---|
55 | $addrend = $1; |
---|
56 | } |
---|
57 | ($addrlast) = ($addrend =~ /([^\/]*)$/); |
---|
58 | |
---|
59 | if($requires_sql) { |
---|
60 | print "\nCreating SQL database for $sname...\n"; |
---|
61 | |
---|
62 | my $getpwd=`/mit/scripts/sql/bin$scriptsdev/get-password`; |
---|
63 | ($sqlhost, $sqluser, $sqlpass) = split(/\s/, $getpwd); |
---|
64 | |
---|
65 | $sqldb=`/mit/scripts/sql/bin$scriptsdev/get-next-database "$addrlast"`; |
---|
66 | $sqldb=`/mit/scripts/sql/bin$scriptsdev/create-database "$sqldb"`; |
---|
67 | if($sqldb eq "") { |
---|
68 | print "\nERROR:\n"; |
---|
69 | print "Your SQL account failed to create a SQL database.\n"; |
---|
70 | print "You should log in at http://sql.mit.edu to check whether\n"; |
---|
71 | print "your SQL account is at its database limit or its storage limit.\n"; |
---|
72 | print "If you cannot determine the cause of the problem, please\n"; |
---|
73 | print "feel free to contact sql\@mit.edu for assistance.\n"; |
---|
74 | `touch .failed`; |
---|
75 | exit 1; |
---|
76 | } |
---|
77 | $sqldbcurl = $sqldb; |
---|
78 | $sqldbcurl =~ s/\+/\%2B/; |
---|
79 | } |
---|
80 | |
---|
81 | if(-e "$HOME/web_scripts/$addrend/.admin") { |
---|
82 | $admin_password=`cat $HOME/web_scripts/$addrend/.admin`; |
---|
83 | chomp($admin_password); |
---|
84 | unlink "$HOME/web_scripts/$addrend/.admin"; |
---|
85 | } |
---|
86 | |
---|
87 | print "\nConfiguring $sname...\n"; |
---|
88 | |
---|
89 | open(VERSION, ">.scripts-version") or die "Can't write scripts-version file: $!\n"; |
---|
90 | print VERSION strftime("%F %T %z\n", localtime); |
---|
91 | print VERSION $ENV{'USER'}, '@', getclienthostname(), "\n"; |
---|
92 | my $tarball = abs_path("/mit/scripts/deploy$scriptsdev/$deploy.tar.gz"); |
---|
93 | print VERSION $tarball, "\n"; |
---|
94 | $tarball =~ s|/deploydev/|/deploy/|; |
---|
95 | print VERSION dirname($tarball), "\n"; |
---|
96 | close(VERSION); |
---|
97 | if (0) { |
---|
98 | `date > .scripts-version`; |
---|
99 | `stat /mit/scripts/deploy$scriptsdev/$deploy.tar.gz >> .scripts-version`; |
---|
100 | } |
---|
101 | |
---|
102 | select STDOUT; |
---|
103 | $| = 1; # STDOUT is *hot*! |
---|
104 | } |
---|