1 | #! /usr/bin/perl |
---|
2 | |
---|
3 | |
---|
4 | use strict; |
---|
5 | |
---|
6 | use Getopt::Long; |
---|
7 | |
---|
8 | use IO::File; |
---|
9 | use IO::Pipe; |
---|
10 | use File::Copy; |
---|
11 | |
---|
12 | use Net::hostent; |
---|
13 | use Socket; |
---|
14 | |
---|
15 | use gateway_lib; |
---|
16 | |
---|
17 | # rc.fedaccounts is going to send a TERM to any processes running as an old |
---|
18 | # user, so insulate this (and children) process from propagated signals. |
---|
19 | $SIG{'TERM'} = 'IGNORE'; |
---|
20 | |
---|
21 | chdir("/tmp"); |
---|
22 | |
---|
23 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
24 | my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route"; |
---|
25 | my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n"; |
---|
26 | my $shared_config_dir; |
---|
27 | my $shared_seer_auth_dir; |
---|
28 | my $local_config_dir = "/usr/local/federation/etc"; |
---|
29 | my %services; |
---|
30 | my %aliases; |
---|
31 | my %added; |
---|
32 | my @hide; |
---|
33 | my $perl; |
---|
34 | |
---|
35 | my $gateway; |
---|
36 | my $smbshare = "USERS"; |
---|
37 | my $smbuser; |
---|
38 | my $smbproject; |
---|
39 | my $exp; |
---|
40 | my $proj; |
---|
41 | my $install_smb; |
---|
42 | my $smb_type = 'cifs'; |
---|
43 | chomp (my $uname = `uname`); |
---|
44 | my $smbmount = "smbmount.$uname.pl"; |
---|
45 | |
---|
46 | GetOptions("install_samba" => \$install_smb); |
---|
47 | |
---|
48 | # find perl |
---|
49 | for my $p ("/usr/bin/perl", "/usr/local/bin/perl") { |
---|
50 | if ( -x $p ) { |
---|
51 | $perl = $p; |
---|
52 | last; |
---|
53 | } |
---|
54 | } |
---|
55 | $perl = "perl" unless $perl; |
---|
56 | |
---|
57 | if (!-x '/sbin/mount.cifs' ) { |
---|
58 | if ( -x '/usr/bin/yum' ) { |
---|
59 | # Install samba |
---|
60 | system('/usr/bin/yum -y install samba-client'); |
---|
61 | system('/usr/bin/yum -y install cifs-utils'); |
---|
62 | # These tools expect the fstab to include cifs |
---|
63 | $smb_type = 'cifs'; |
---|
64 | } |
---|
65 | elsif (-x '/usr/bin/apt-get') { |
---|
66 | # Install samba |
---|
67 | system('/usr/bin/apt-get -y install samba-client'); |
---|
68 | system('/usr/bin/apt-get -y install smbfs'); |
---|
69 | # These tools expect the fstab to include cifs |
---|
70 | $smb_type = 'cifs'; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | if (!-e "$local_config_dir/client.conf" ) { |
---|
75 | $tmcc_p->reader("$TMCC -b status"); |
---|
76 | while (<$tmcc_p>) { |
---|
77 | /ALLOCATED=([^\/]+)\/(\S+)/ && do { |
---|
78 | ($proj, $exp) = ($1, $2); |
---|
79 | $shared_config_dir = "/proj/$proj/exp/$exp/tmp"; |
---|
80 | $shared_seer_auth_dir = "/proj/$proj/exp/$exp/tbdata"; |
---|
81 | last; |
---|
82 | }; |
---|
83 | } |
---|
84 | $tmcc_p->close(); |
---|
85 | |
---|
86 | mkdir($local_config_dir); |
---|
87 | |
---|
88 | foreach my $fn ("seer.conf", "client.conf", "userconf", "hosts", |
---|
89 | "ca.pem", "node.pem") { |
---|
90 | copy("$shared_config_dir/$fn", $local_config_dir ) |
---|
91 | if -e "$shared_config_dir/$fn"; |
---|
92 | } |
---|
93 | |
---|
94 | # Copy seer authorization files into the location that standard SEER |
---|
95 | # invocations will look. The above loop puts them where -F invocations |
---|
96 | # will look. |
---|
97 | foreach my $fn ("ca.pem", "node.pem") { |
---|
98 | copy("$shared_config_dir/$fn", $shared_seer_auth_dir ) |
---|
99 | if -e "$shared_config_dir/$fn" && -d $shared_seer_auth_dir; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | my $client = new IO::File("$local_config_dir/client.conf"); |
---|
104 | while (<$client>) { |
---|
105 | chomp; |
---|
106 | /ControlGateway:\s+(.*)/i && do { $gateway = $1; }; |
---|
107 | /SMBShare:\s+(.*)/i && do { $smbshare = $1; }; |
---|
108 | /ProjectUser:\s+(.*)/i && do { $smbuser = $1; }; |
---|
109 | /ProjectName:\s+(.*)/i && do { $smbproject = $1; }; |
---|
110 | /Service:\s+(.*)/i && do { $services{$1}++;}; |
---|
111 | /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;}; |
---|
112 | /AddedNode:\s+(.*)/i && do { $added{$1}++; }; |
---|
113 | /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));}; |
---|
114 | } |
---|
115 | $client->close(); |
---|
116 | # Create the /etc/hosts file |
---|
117 | my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n"; |
---|
118 | my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n"; |
---|
119 | my $config_hosts = new IO::File("$local_config_dir/hosts") || |
---|
120 | die "Can't open $local_config_dir/hosts: $!\n"; |
---|
121 | my $has_control = 0; |
---|
122 | |
---|
123 | while (<$hosts>) { |
---|
124 | /^127\.0\.0\.1/ && do { print $new_hosts $_; }; |
---|
125 | # If aliases conflict with existing nodes, delete the alias |
---|
126 | for my $n (split($_)) { |
---|
127 | chomp $n; |
---|
128 | delete $aliases{$n} if $aliases{$n}; |
---|
129 | } |
---|
130 | } |
---|
131 | $hosts->close(); |
---|
132 | HOST: |
---|
133 | while (<$config_hosts>) { |
---|
134 | # Trim out hosts that were hidden by their home testbeds |
---|
135 | for my $h (@hide) { |
---|
136 | next HOST if /^\d+\.\d+\.\d+\.\d+\s+$h-/; |
---|
137 | } |
---|
138 | print $new_hosts $_; |
---|
139 | } |
---|
140 | print $new_hosts "\n"; |
---|
141 | $config_hosts->close(); |
---|
142 | |
---|
143 | # Add gateway aliases |
---|
144 | for my $k (keys %aliases) { |
---|
145 | # If we added a node, it's a node without a local address. Bind the name |
---|
146 | # to the IP in /etc/hosts. If we didn't add a node, it's the gateway node. |
---|
147 | (my $lname = $gateway) =~ s/^[^\.]+/$k/; |
---|
148 | my $ip = gateway_lib::get_ip($added{$k} ? $lname : $gateway); |
---|
149 | if ($ip) { |
---|
150 | # We have an IP. Make a hosts entry for the key and the key plus the |
---|
151 | # first two subdomains (which is an emulab setup) |
---|
152 | my @x = split(/\./, $lname); |
---|
153 | if (@x > 3 ) { splice(@x, 3); } |
---|
154 | my $out = join(".", @x); |
---|
155 | print $new_hosts "$ip\t$out $k\n"; |
---|
156 | } |
---|
157 | else { print $new_hosts "# Can't get ip for $lname\n"; } |
---|
158 | } |
---|
159 | $new_hosts->close(); |
---|
160 | copy("/tmp/hosts", "/etc/hosts"); |
---|
161 | |
---|
162 | |
---|
163 | # If there are tunnelip interfaces to bring up, bring 'em up. Record any such |
---|
164 | # interfaces in /usr/local/federation/interfaces, so SEER can find them later. |
---|
165 | system("$perl -I/usr/local/federation/lib " . |
---|
166 | "/usr/local/federation/bin/config_from_tunnelip.pl " . |
---|
167 | "--record=/usr/local/federation/etc/interfaces"); |
---|
168 | |
---|
169 | if ($uname =~ /Linux/ ) { |
---|
170 | system("$perl /usr/local/federation/bin/gated_routing.pl") |
---|
171 | if -r "/usr/local/federation/bin/gated_routing.pl"; |
---|
172 | } |
---|
173 | elsif ($uname =~/FreeBSD/ ) { |
---|
174 | # FreeBSD needs to have ospfs installed and a router config created and |
---|
175 | # run. |
---|
176 | system("$perl /usr/local/federation/bin/ospf_routing.pl") |
---|
177 | if -r "/usr/local/federation/bin/ospf_routing.pl"; |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | if ($services{'userconfig'}) { |
---|
182 | if (!-e "$local_config_dir/old_accts") { |
---|
183 | $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n"; |
---|
184 | my $old_accounts = new IO::File(">$local_config_dir/old_accts") || |
---|
185 | die "Can't open $local_config_dir/old_accts: $!\n"; |
---|
186 | |
---|
187 | $tmcc_p->reader("$TMCC -b accounts"); |
---|
188 | while (<$tmcc_p>) { |
---|
189 | print $old_accounts $_; |
---|
190 | } |
---|
191 | $tmcc_p->close(); |
---|
192 | $old_accounts->close(); |
---|
193 | } |
---|
194 | print("Updating accounts"); |
---|
195 | system("/usr/local/federation/bin/rc.fedaccounts"); |
---|
196 | } |
---|
197 | |
---|
198 | if ($services{'SMB'}) { |
---|
199 | if ($uname =~ /FreeBSD/ ) { |
---|
200 | system("umount -A -f -t nfs,smbfs,cifs"); |
---|
201 | $smb_type = "smbfs"; |
---|
202 | } |
---|
203 | elsif ($uname =~ /Linux/ ) { |
---|
204 | # Pass individual filestems to Linux umount. No -A. |
---|
205 | my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n"; |
---|
206 | while (<$mtab>) { |
---|
207 | chomp; |
---|
208 | my @F = split($_); |
---|
209 | next unless $F[2] =~ /(nfs|cifs|smbfs)/; |
---|
210 | system("umount -f $F[1]"); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | print "Waiting for SMB server\n"; |
---|
215 | gateway_lib::wait_for_port($gateway, 139, 60*60) || |
---|
216 | die "SMB server never came up\n"; |
---|
217 | print "Mounting via SMB\n"; |
---|
218 | system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . |
---|
219 | "$smbuser $smbproject $smb_type"); |
---|
220 | } |
---|
221 | |
---|
222 | if ($uname =~ /FreeBSD/ ) { |
---|
223 | # Restart ntp |
---|
224 | system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . |
---|
225 | "/etc/rc.d/ntpd start;"); |
---|
226 | |
---|
227 | } |
---|
228 | elsif ($uname =~ /Linux/ ) { |
---|
229 | # restart ntp |
---|
230 | system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". |
---|
231 | "/etc/rc.d/init.d/ntpd start"); |
---|
232 | } |
---|
233 | |
---|
234 | # startcmd |
---|
235 | if ($ARGV[0] && $ARGV[1]) { |
---|
236 | if ($uname =~ /FreeBSD/) { |
---|
237 | system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\""); |
---|
238 | } |
---|
239 | elsif ($uname =~ /Linux/) { |
---|
240 | system("su \"$ARGV[0]\" --command \"$ARGV[1]\""); |
---|
241 | } |
---|
242 | } |
---|
243 | exit(0); |
---|