1 | #! /usr/bin/perl |
---|
2 | |
---|
3 | |
---|
4 | use strict; |
---|
5 | |
---|
6 | use IO::File; |
---|
7 | use IO::Pipe; |
---|
8 | use File::Copy; |
---|
9 | |
---|
10 | use Net::hostent; |
---|
11 | use Socket; |
---|
12 | |
---|
13 | use gateway_lib; |
---|
14 | |
---|
15 | chdir("/tmp"); |
---|
16 | |
---|
17 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
18 | my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route"; |
---|
19 | my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n"; |
---|
20 | my $shared_config_dir; |
---|
21 | my $local_config_dir = "/usr/local/federation/etc"; |
---|
22 | my %services; |
---|
23 | my $perl; |
---|
24 | |
---|
25 | my $gateway; |
---|
26 | my $smbshare = "USERS"; |
---|
27 | my $smbuser; |
---|
28 | my $smbproject; |
---|
29 | my $exp; |
---|
30 | my $proj; |
---|
31 | chomp (my $uname = `uname`); |
---|
32 | my $smbmount = "smbmount.$uname.pl"; |
---|
33 | |
---|
34 | # find perl |
---|
35 | for my $p ("/usr/bin/perl", "/usr/local/bin/perl") { |
---|
36 | if ( -x $p ) { |
---|
37 | $perl = $p; |
---|
38 | last; |
---|
39 | } |
---|
40 | } |
---|
41 | $perl = "perl" unless $perl; |
---|
42 | |
---|
43 | |
---|
44 | $tmcc_p->reader("$TMCC -b status"); |
---|
45 | while (<$tmcc_p>) { |
---|
46 | /ALLOCATED=([^\/]+)\/(\S+)/ && do { |
---|
47 | ($proj, $exp) = ($1, $2); |
---|
48 | $shared_config_dir = "/proj/$proj/exp/$exp/tmp"; |
---|
49 | last; |
---|
50 | }; |
---|
51 | } |
---|
52 | $tmcc_p->close(); |
---|
53 | |
---|
54 | mkdir($local_config_dir); |
---|
55 | |
---|
56 | foreach my $fn ("seer.conf", "client.conf", "userconf") { |
---|
57 | copy("$shared_config_dir/$fn", $local_config_dir ) |
---|
58 | if -e "$shared_config_dir/$fn"; |
---|
59 | } |
---|
60 | |
---|
61 | my $client = new IO::File("$local_config_dir/client.conf"); |
---|
62 | while (<$client>) { |
---|
63 | chomp; |
---|
64 | /ControlGateway:\s+(.*)/i && do { $gateway = $1; }; |
---|
65 | /SMBShare:\s+(.*)/i && do { $smbshare = $1; }; |
---|
66 | /ProjectUser:\s+(.*)/i && do { $smbuser = $1; }; |
---|
67 | /ProjectName:\s+(.*)/i && do { $smbproject = $1; }; |
---|
68 | /Service:\s+(.*)/i && do { $services{$1}++;}; |
---|
69 | } |
---|
70 | $client->close(); |
---|
71 | # Create the /etc/hosts file |
---|
72 | my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n"; |
---|
73 | my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n"; |
---|
74 | my $config_hosts = new IO::File("$shared_config_dir/hosts") || |
---|
75 | die "Can't open $shared_config_dir/hosts: $!\n"; |
---|
76 | my $has_control = 0; |
---|
77 | |
---|
78 | while (<$hosts>) { |
---|
79 | /^127\.0\.0\.1/ && do { print $new_hosts $_; }; |
---|
80 | /control\./ && $has_control++; |
---|
81 | } |
---|
82 | $hosts->close(); |
---|
83 | while (<$config_hosts>) { |
---|
84 | print $new_hosts $_; |
---|
85 | } |
---|
86 | $config_hosts->close(); |
---|
87 | |
---|
88 | # If seer is enabled and no local control node in play, alias the gateway to be |
---|
89 | # control |
---|
90 | if ($services{'seer'} && !$has_control ) { |
---|
91 | if ( my $hent = gethostbyname($gateway) ) { |
---|
92 | my $gwip = inet_ntoa($hent->addr_list->[0]); |
---|
93 | |
---|
94 | print $new_hosts "\n$gwip\tcontrol.$exp.$proj\n"; |
---|
95 | } |
---|
96 | } |
---|
97 | $new_hosts->close(); |
---|
98 | copy("/tmp/hosts", "/etc/hosts"); |
---|
99 | |
---|
100 | # Ick. at least one of our images has gated in the wrong place (sigh). If |
---|
101 | # so, fix it and restart routing. |
---|
102 | if (!-x "/usr/sbin/gated" && -x "/sbin/gated" ) { |
---|
103 | print "Linking to /sbin/gated and starting routing\n"; |
---|
104 | symlink("/sbin/gated", "/usr/sbin/gated"); |
---|
105 | system("$RC_ROUTE boot") if -x $RC_ROUTE; |
---|
106 | } |
---|
107 | |
---|
108 | # If there are tunnelip interfaces to bring up, bring 'em |
---|
109 | system("$perl -I/usr/local/federation/lib " . |
---|
110 | "/usr/local/federation/bin/config_from_tunnelip.pl"); |
---|
111 | |
---|
112 | |
---|
113 | if ($services{'userconfig'}) { |
---|
114 | $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n"; |
---|
115 | my $old_accounts = new IO::File(">$local_config_dir/old_accts") || |
---|
116 | die "Can't open $local_config_dir/old_accts: $!\n"; |
---|
117 | |
---|
118 | $tmcc_p->reader("$TMCC -b accounts"); |
---|
119 | while (<$tmcc_p>) { |
---|
120 | print $old_accounts $_; |
---|
121 | } |
---|
122 | $tmcc_p->close(); |
---|
123 | $old_accounts->close(); |
---|
124 | print("Updating accounts"); |
---|
125 | system("/usr/local/federation/bin/rc.fedaccounts"); |
---|
126 | } |
---|
127 | |
---|
128 | if ($services{'SMB'}) { |
---|
129 | if ($uname =~ /FreeBSD/ ) { |
---|
130 | system("umount -A -f -t nfs,smbfs,cifs"); |
---|
131 | } |
---|
132 | elsif ($uname =~ /Linux/ ) { |
---|
133 | # Pass individual filestems to Linux umount. No -A. |
---|
134 | my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$1\n"; |
---|
135 | while (<$mtab>) { |
---|
136 | chomp; |
---|
137 | my @F = split($_); |
---|
138 | next unless $F[2] =~ /(nfs|cifs|smbfs)/; |
---|
139 | system("umount -f $F[1]"); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | print "Waiting for SMB server\n"; |
---|
144 | gateway_lib::wait_for_port($gateway, 139, 60*60) || |
---|
145 | die "SMB server never came up\n"; |
---|
146 | print "Mounting via SMB\n"; |
---|
147 | system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . |
---|
148 | "$smbuser $smbproject"); |
---|
149 | } |
---|
150 | |
---|
151 | if ($uname =~ /FreeBSD/ ) { |
---|
152 | # Restart ntp |
---|
153 | system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . |
---|
154 | "/etc/rc.d/ntpd start;"); |
---|
155 | |
---|
156 | } |
---|
157 | elsif ($uname =~ /Linux/ ) { |
---|
158 | # restart ntp |
---|
159 | system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". |
---|
160 | "/etc/rc.d/init.d/ntpd start"); |
---|
161 | } |
---|
162 | |
---|
163 | # startcmd |
---|
164 | if ($ARGV[0] && $ARGV[1]) { |
---|
165 | if ($uname =~ /FreeBSD/) { |
---|
166 | system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\""); |
---|
167 | } |
---|
168 | elsif ($uname =~ /Linux/) { |
---|
169 | system("su \"$ARGV[0]\" --command \"$ARGV[1]\""); |
---|
170 | } |
---|
171 | } |
---|
172 | exit(0); |
---|