[6d985c0] | 1 | #! /usr/bin/perl |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | use strict; |
---|
| 5 | |
---|
[f8fa72b] | 6 | use Getopt::Long; |
---|
| 7 | |
---|
[6d985c0] | 8 | use IO::File; |
---|
| 9 | use IO::Pipe; |
---|
| 10 | use File::Copy; |
---|
| 11 | |
---|
[cc0ffd2] | 12 | use Net::hostent; |
---|
| 13 | use Socket; |
---|
| 14 | |
---|
[6d985c0] | 15 | use gateway_lib; |
---|
| 16 | |
---|
[5ae9d94] | 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 | |
---|
[6d985c0] | 21 | chdir("/tmp"); |
---|
| 22 | |
---|
| 23 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
[dbc9144] | 24 | my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route"; |
---|
[6d985c0] | 25 | my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n"; |
---|
| 26 | my $shared_config_dir; |
---|
[3df9b33] | 27 | my $shared_seer_auth_dir; |
---|
[6d985c0] | 28 | my $local_config_dir = "/usr/local/federation/etc"; |
---|
| 29 | my %services; |
---|
[d56b168] | 30 | my %aliases; |
---|
[d38ff56] | 31 | my %added; |
---|
[8209faa] | 32 | my @hide; |
---|
[6d985c0] | 33 | my $perl; |
---|
| 34 | |
---|
| 35 | my $gateway; |
---|
| 36 | my $smbshare = "USERS"; |
---|
| 37 | my $smbuser; |
---|
| 38 | my $smbproject; |
---|
[cc0ffd2] | 39 | my $exp; |
---|
| 40 | my $proj; |
---|
[f8fa72b] | 41 | my $install_smb; |
---|
[73e0a61] | 42 | my $smb_type = 'cifs'; |
---|
[6d985c0] | 43 | chomp (my $uname = `uname`); |
---|
| 44 | my $smbmount = "smbmount.$uname.pl"; |
---|
| 45 | |
---|
[f8fa72b] | 46 | GetOptions("install_samba" => \$install_smb); |
---|
| 47 | |
---|
[6d985c0] | 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 | |
---|
[5ae9d94] | 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 | } |
---|
[6d985c0] | 72 | } |
---|
| 73 | |
---|
[f8fa72b] | 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"; |
---|
[3df9b33] | 80 | $shared_seer_auth_dir = "/proj/$proj/exp/$exp/tbdata"; |
---|
[f8fa72b] | 81 | last; |
---|
| 82 | }; |
---|
| 83 | } |
---|
| 84 | $tmcc_p->close(); |
---|
| 85 | |
---|
| 86 | mkdir($local_config_dir); |
---|
[6d985c0] | 87 | |
---|
[3df9b33] | 88 | foreach my $fn ("seer.conf", "client.conf", "userconf", "hosts", |
---|
| 89 | "ca.pem", "node.pem") { |
---|
[f8fa72b] | 90 | copy("$shared_config_dir/$fn", $local_config_dir ) |
---|
| 91 | if -e "$shared_config_dir/$fn"; |
---|
| 92 | } |
---|
[3df9b33] | 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 | } |
---|
[6d985c0] | 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}++;}; |
---|
[d56b168] | 111 | /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;}; |
---|
[d38ff56] | 112 | /AddedNode:\s+(.*)/i && do { $added{$1}++; }; |
---|
[8209faa] | 113 | /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));}; |
---|
[6d985c0] | 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"; |
---|
[f8fa72b] | 119 | my $config_hosts = new IO::File("$local_config_dir/hosts") || |
---|
| 120 | die "Can't open $local_config_dir/hosts: $!\n"; |
---|
[cc0ffd2] | 121 | my $has_control = 0; |
---|
[6d985c0] | 122 | |
---|
| 123 | while (<$hosts>) { |
---|
| 124 | /^127\.0\.0\.1/ && do { print $new_hosts $_; }; |
---|
[d56b168] | 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 | } |
---|
[6d985c0] | 130 | } |
---|
| 131 | $hosts->close(); |
---|
[1962a5b] | 132 | HOST: |
---|
[6d985c0] | 133 | while (<$config_hosts>) { |
---|
[8209faa] | 134 | # Trim out hosts that were hidden by their home testbeds |
---|
| 135 | for my $h (@hide) { |
---|
[1962a5b] | 136 | next HOST if /^\d+\.\d+\.\d+\.\d+\s+$h-/; |
---|
[8209faa] | 137 | } |
---|
[6d985c0] | 138 | print $new_hosts $_; |
---|
| 139 | } |
---|
[d38ff56] | 140 | print $new_hosts "\n"; |
---|
[6d985c0] | 141 | $config_hosts->close(); |
---|
[cc0ffd2] | 142 | |
---|
[d56b168] | 143 | # Add gateway aliases |
---|
[d38ff56] | 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) { |
---|
[8209faa] | 150 | # We have an IP. Make a hosts entry for the key and the key plus the |
---|
[d38ff56] | 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"; |
---|
[cc0ffd2] | 156 | } |
---|
[d38ff56] | 157 | else { print $new_hosts "# Can't get ip for $lname\n"; } |
---|
[cc0ffd2] | 158 | } |
---|
[6d985c0] | 159 | $new_hosts->close(); |
---|
| 160 | copy("/tmp/hosts", "/etc/hosts"); |
---|
| 161 | |
---|
[dbc9144] | 162 | |
---|
[dc803a7] | 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. |
---|
[55779d4] | 165 | system("$perl -I/usr/local/federation/lib " . |
---|
[dc803a7] | 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"; |
---|
[17c2f7b] | 172 | if ($?) { |
---|
| 173 | system("$perl /usr/local/federation/bin/quagga_routing.pl") |
---|
| 174 | if -r "/usr/local/federation/bin/quagga_routing.pl"; |
---|
| 175 | } |
---|
[dc803a7] | 176 | } |
---|
| 177 | elsif ($uname =~/FreeBSD/ ) { |
---|
| 178 | # FreeBSD needs to have ospfs installed and a router config created and |
---|
| 179 | # run. |
---|
| 180 | system("$perl /usr/local/federation/bin/ospf_routing.pl") |
---|
| 181 | if -r "/usr/local/federation/bin/ospf_routing.pl"; |
---|
| 182 | } |
---|
[55779d4] | 183 | |
---|
[6d985c0] | 184 | |
---|
| 185 | if ($services{'userconfig'}) { |
---|
[f8fa72b] | 186 | if (!-e "$local_config_dir/old_accts") { |
---|
| 187 | $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n"; |
---|
| 188 | my $old_accounts = new IO::File(">$local_config_dir/old_accts") || |
---|
| 189 | die "Can't open $local_config_dir/old_accts: $!\n"; |
---|
| 190 | |
---|
| 191 | $tmcc_p->reader("$TMCC -b accounts"); |
---|
| 192 | while (<$tmcc_p>) { |
---|
| 193 | print $old_accounts $_; |
---|
| 194 | } |
---|
| 195 | $tmcc_p->close(); |
---|
| 196 | $old_accounts->close(); |
---|
[6d985c0] | 197 | } |
---|
| 198 | print("Updating accounts"); |
---|
| 199 | system("/usr/local/federation/bin/rc.fedaccounts"); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | if ($services{'SMB'}) { |
---|
| 203 | if ($uname =~ /FreeBSD/ ) { |
---|
| 204 | system("umount -A -f -t nfs,smbfs,cifs"); |
---|
[73e0a61] | 205 | $smb_type = "smbfs"; |
---|
[6d985c0] | 206 | } |
---|
| 207 | elsif ($uname =~ /Linux/ ) { |
---|
| 208 | # Pass individual filestems to Linux umount. No -A. |
---|
[73e0a61] | 209 | my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n"; |
---|
[6d985c0] | 210 | while (<$mtab>) { |
---|
| 211 | chomp; |
---|
| 212 | my @F = split($_); |
---|
| 213 | next unless $F[2] =~ /(nfs|cifs|smbfs)/; |
---|
| 214 | system("umount -f $F[1]"); |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | print "Waiting for SMB server\n"; |
---|
| 219 | gateway_lib::wait_for_port($gateway, 139, 60*60) || |
---|
| 220 | die "SMB server never came up\n"; |
---|
| 221 | print "Mounting via SMB\n"; |
---|
| 222 | system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . |
---|
[73e0a61] | 223 | "$smbuser $smbproject $smb_type"); |
---|
[6d985c0] | 224 | } |
---|
| 225 | |
---|
[37f6592] | 226 | if ($uname =~ /FreeBSD/ ) { |
---|
| 227 | # Restart ntp |
---|
| 228 | system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . |
---|
| 229 | "/etc/rc.d/ntpd start;"); |
---|
| 230 | |
---|
| 231 | } |
---|
| 232 | elsif ($uname =~ /Linux/ ) { |
---|
| 233 | # restart ntp |
---|
| 234 | system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". |
---|
| 235 | "/etc/rc.d/init.d/ntpd start"); |
---|
| 236 | } |
---|
| 237 | |
---|
[6d985c0] | 238 | # startcmd |
---|
| 239 | if ($ARGV[0] && $ARGV[1]) { |
---|
| 240 | if ($uname =~ /FreeBSD/) { |
---|
| 241 | system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\""); |
---|
| 242 | } |
---|
| 243 | elsif ($uname =~ /Linux/) { |
---|
| 244 | system("su \"$ARGV[0]\" --command \"$ARGV[1]\""); |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | exit(0); |
---|