[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 |
---|
[76c43e4] | 67 | system('/usr/bin/apt-get -y update'); |
---|
[5ae9d94] | 68 | system('/usr/bin/apt-get -y install samba-client'); |
---|
| 69 | system('/usr/bin/apt-get -y install smbfs'); |
---|
| 70 | # These tools expect the fstab to include cifs |
---|
| 71 | $smb_type = 'cifs'; |
---|
| 72 | } |
---|
[6d985c0] | 73 | } |
---|
| 74 | |
---|
[f8fa72b] | 75 | if (!-e "$local_config_dir/client.conf" ) { |
---|
| 76 | $tmcc_p->reader("$TMCC -b status"); |
---|
| 77 | while (<$tmcc_p>) { |
---|
| 78 | /ALLOCATED=([^\/]+)\/(\S+)/ && do { |
---|
| 79 | ($proj, $exp) = ($1, $2); |
---|
| 80 | $shared_config_dir = "/proj/$proj/exp/$exp/tmp"; |
---|
[3df9b33] | 81 | $shared_seer_auth_dir = "/proj/$proj/exp/$exp/tbdata"; |
---|
[f8fa72b] | 82 | last; |
---|
| 83 | }; |
---|
| 84 | } |
---|
| 85 | $tmcc_p->close(); |
---|
| 86 | |
---|
| 87 | mkdir($local_config_dir); |
---|
[6d985c0] | 88 | |
---|
[3df9b33] | 89 | foreach my $fn ("seer.conf", "client.conf", "userconf", "hosts", |
---|
| 90 | "ca.pem", "node.pem") { |
---|
[f8fa72b] | 91 | copy("$shared_config_dir/$fn", $local_config_dir ) |
---|
| 92 | if -e "$shared_config_dir/$fn"; |
---|
| 93 | } |
---|
[3df9b33] | 94 | |
---|
| 95 | # Copy seer authorization files into the location that standard SEER |
---|
| 96 | # invocations will look. The above loop puts them where -F invocations |
---|
| 97 | # will look. |
---|
| 98 | foreach my $fn ("ca.pem", "node.pem") { |
---|
| 99 | copy("$shared_config_dir/$fn", $shared_seer_auth_dir ) |
---|
| 100 | if -e "$shared_config_dir/$fn" && -d $shared_seer_auth_dir; |
---|
| 101 | } |
---|
[6d985c0] | 102 | } |
---|
| 103 | |
---|
| 104 | my $client = new IO::File("$local_config_dir/client.conf"); |
---|
| 105 | while (<$client>) { |
---|
| 106 | chomp; |
---|
| 107 | /ControlGateway:\s+(.*)/i && do { $gateway = $1; }; |
---|
| 108 | /SMBShare:\s+(.*)/i && do { $smbshare = $1; }; |
---|
| 109 | /ProjectUser:\s+(.*)/i && do { $smbuser = $1; }; |
---|
| 110 | /ProjectName:\s+(.*)/i && do { $smbproject = $1; }; |
---|
| 111 | /Service:\s+(.*)/i && do { $services{$1}++;}; |
---|
[d56b168] | 112 | /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;}; |
---|
[d38ff56] | 113 | /AddedNode:\s+(.*)/i && do { $added{$1}++; }; |
---|
[8209faa] | 114 | /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));}; |
---|
[6d985c0] | 115 | } |
---|
| 116 | $client->close(); |
---|
| 117 | # Create the /etc/hosts file |
---|
| 118 | my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n"; |
---|
| 119 | my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n"; |
---|
[f8fa72b] | 120 | my $config_hosts = new IO::File("$local_config_dir/hosts") || |
---|
| 121 | die "Can't open $local_config_dir/hosts: $!\n"; |
---|
[cc0ffd2] | 122 | my $has_control = 0; |
---|
[6d985c0] | 123 | |
---|
| 124 | while (<$hosts>) { |
---|
| 125 | /^127\.0\.0\.1/ && do { print $new_hosts $_; }; |
---|
[d56b168] | 126 | # If aliases conflict with existing nodes, delete the alias |
---|
| 127 | for my $n (split($_)) { |
---|
| 128 | chomp $n; |
---|
| 129 | delete $aliases{$n} if $aliases{$n}; |
---|
| 130 | } |
---|
[6d985c0] | 131 | } |
---|
| 132 | $hosts->close(); |
---|
[1962a5b] | 133 | HOST: |
---|
[6d985c0] | 134 | while (<$config_hosts>) { |
---|
[8209faa] | 135 | # Trim out hosts that were hidden by their home testbeds |
---|
| 136 | for my $h (@hide) { |
---|
[1962a5b] | 137 | next HOST if /^\d+\.\d+\.\d+\.\d+\s+$h-/; |
---|
[8209faa] | 138 | } |
---|
[6d985c0] | 139 | print $new_hosts $_; |
---|
| 140 | } |
---|
[d38ff56] | 141 | print $new_hosts "\n"; |
---|
[6d985c0] | 142 | $config_hosts->close(); |
---|
[cc0ffd2] | 143 | |
---|
[d56b168] | 144 | # Add gateway aliases |
---|
[d38ff56] | 145 | for my $k (keys %aliases) { |
---|
| 146 | # If we added a node, it's a node without a local address. Bind the name |
---|
| 147 | # to the IP in /etc/hosts. If we didn't add a node, it's the gateway node. |
---|
| 148 | (my $lname = $gateway) =~ s/^[^\.]+/$k/; |
---|
| 149 | my $ip = gateway_lib::get_ip($added{$k} ? $lname : $gateway); |
---|
| 150 | if ($ip) { |
---|
[8209faa] | 151 | # We have an IP. Make a hosts entry for the key and the key plus the |
---|
[d38ff56] | 152 | # first two subdomains (which is an emulab setup) |
---|
| 153 | my @x = split(/\./, $lname); |
---|
| 154 | if (@x > 3 ) { splice(@x, 3); } |
---|
| 155 | my $out = join(".", @x); |
---|
| 156 | print $new_hosts "$ip\t$out $k\n"; |
---|
[cc0ffd2] | 157 | } |
---|
[d38ff56] | 158 | else { print $new_hosts "# Can't get ip for $lname\n"; } |
---|
[cc0ffd2] | 159 | } |
---|
[6d985c0] | 160 | $new_hosts->close(); |
---|
| 161 | copy("/tmp/hosts", "/etc/hosts"); |
---|
| 162 | |
---|
[dbc9144] | 163 | |
---|
[dc803a7] | 164 | # If there are tunnelip interfaces to bring up, bring 'em up. Record any such |
---|
| 165 | # interfaces in /usr/local/federation/interfaces, so SEER can find them later. |
---|
[55779d4] | 166 | system("$perl -I/usr/local/federation/lib " . |
---|
[dc803a7] | 167 | "/usr/local/federation/bin/config_from_tunnelip.pl " . |
---|
| 168 | "--record=/usr/local/federation/etc/interfaces"); |
---|
| 169 | |
---|
| 170 | if ($uname =~ /Linux/ ) { |
---|
| 171 | system("$perl /usr/local/federation/bin/gated_routing.pl") |
---|
| 172 | if -r "/usr/local/federation/bin/gated_routing.pl"; |
---|
[17c2f7b] | 173 | if ($?) { |
---|
| 174 | system("$perl /usr/local/federation/bin/quagga_routing.pl") |
---|
| 175 | if -r "/usr/local/federation/bin/quagga_routing.pl"; |
---|
| 176 | } |
---|
[dc803a7] | 177 | } |
---|
| 178 | elsif ($uname =~/FreeBSD/ ) { |
---|
| 179 | # FreeBSD needs to have ospfs installed and a router config created and |
---|
| 180 | # run. |
---|
| 181 | system("$perl /usr/local/federation/bin/ospf_routing.pl") |
---|
| 182 | if -r "/usr/local/federation/bin/ospf_routing.pl"; |
---|
| 183 | } |
---|
[55779d4] | 184 | |
---|
[6d985c0] | 185 | |
---|
| 186 | if ($services{'userconfig'}) { |
---|
[f8fa72b] | 187 | if (!-e "$local_config_dir/old_accts") { |
---|
| 188 | $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n"; |
---|
| 189 | my $old_accounts = new IO::File(">$local_config_dir/old_accts") || |
---|
| 190 | die "Can't open $local_config_dir/old_accts: $!\n"; |
---|
| 191 | |
---|
| 192 | $tmcc_p->reader("$TMCC -b accounts"); |
---|
| 193 | while (<$tmcc_p>) { |
---|
| 194 | print $old_accounts $_; |
---|
| 195 | } |
---|
| 196 | $tmcc_p->close(); |
---|
| 197 | $old_accounts->close(); |
---|
[6d985c0] | 198 | } |
---|
| 199 | print("Updating accounts"); |
---|
| 200 | system("/usr/local/federation/bin/rc.fedaccounts"); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | if ($services{'SMB'}) { |
---|
| 204 | if ($uname =~ /FreeBSD/ ) { |
---|
| 205 | system("umount -A -f -t nfs,smbfs,cifs"); |
---|
[73e0a61] | 206 | $smb_type = "smbfs"; |
---|
[6d985c0] | 207 | } |
---|
| 208 | elsif ($uname =~ /Linux/ ) { |
---|
| 209 | # Pass individual filestems to Linux umount. No -A. |
---|
[73e0a61] | 210 | my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n"; |
---|
[6d985c0] | 211 | while (<$mtab>) { |
---|
| 212 | chomp; |
---|
| 213 | my @F = split($_); |
---|
| 214 | next unless $F[2] =~ /(nfs|cifs|smbfs)/; |
---|
| 215 | system("umount -f $F[1]"); |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | print "Waiting for SMB server\n"; |
---|
| 220 | gateway_lib::wait_for_port($gateway, 139, 60*60) || |
---|
| 221 | die "SMB server never came up\n"; |
---|
| 222 | print "Mounting via SMB\n"; |
---|
| 223 | system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . |
---|
[73e0a61] | 224 | "$smbuser $smbproject $smb_type"); |
---|
[6d985c0] | 225 | } |
---|
| 226 | |
---|
[37f6592] | 227 | if ($uname =~ /FreeBSD/ ) { |
---|
| 228 | # Restart ntp |
---|
| 229 | system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . |
---|
| 230 | "/etc/rc.d/ntpd start;"); |
---|
| 231 | |
---|
| 232 | } |
---|
| 233 | elsif ($uname =~ /Linux/ ) { |
---|
| 234 | # restart ntp |
---|
[558d829] | 235 | if (-d "/etc/rc.d/init.d" ) { |
---|
| 236 | system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". |
---|
| 237 | "/etc/rc.d/init.d/ntpd start"); |
---|
| 238 | elsif (-d "/etc/init.d" ) { |
---|
| 239 | system("service ntp stop; /usr/sbin/ntpdate boss; ". |
---|
| 240 | "service ntp start"); |
---|
| 241 | } |
---|
| 242 | else { |
---|
| 243 | print "Cannot find ntp directories\n"; |
---|
| 244 | } |
---|
[37f6592] | 245 | } |
---|
| 246 | |
---|
[6d985c0] | 247 | # startcmd |
---|
| 248 | if ($ARGV[0] && $ARGV[1]) { |
---|
| 249 | if ($uname =~ /FreeBSD/) { |
---|
| 250 | system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\""); |
---|
| 251 | } |
---|
| 252 | elsif ($uname =~ /Linux/) { |
---|
| 253 | system("su \"$ARGV[0]\" --command \"$ARGV[1]\""); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | exit(0); |
---|