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