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