[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 | |
---|
| 17 | chdir("/tmp"); |
---|
| 18 | |
---|
| 19 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
[dbc9144] | 20 | my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route"; |
---|
[6d985c0] | 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; |
---|
[d56b168] | 25 | my %aliases; |
---|
[d38ff56] | 26 | my %added; |
---|
[8209faa] | 27 | my @hide; |
---|
[6d985c0] | 28 | my $perl; |
---|
| 29 | |
---|
| 30 | my $gateway; |
---|
| 31 | my $smbshare = "USERS"; |
---|
| 32 | my $smbuser; |
---|
| 33 | my $smbproject; |
---|
[cc0ffd2] | 34 | my $exp; |
---|
| 35 | my $proj; |
---|
[f8fa72b] | 36 | my $install_smb; |
---|
[73e0a61] | 37 | my $smb_type = 'cifs'; |
---|
[6d985c0] | 38 | chomp (my $uname = `uname`); |
---|
| 39 | my $smbmount = "smbmount.$uname.pl"; |
---|
| 40 | |
---|
[f8fa72b] | 41 | GetOptions("install_samba" => \$install_smb); |
---|
| 42 | |
---|
[6d985c0] | 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 | |
---|
[f8fa72b] | 52 | if ($install_smb) { |
---|
[1d91791f] | 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'; |
---|
[6d985c0] | 57 | } |
---|
| 58 | |
---|
[f8fa72b] | 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); |
---|
[6d985c0] | 71 | |
---|
[f8fa72b] | 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 | } |
---|
[6d985c0] | 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}++;}; |
---|
[d56b168] | 86 | /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;}; |
---|
[d38ff56] | 87 | /AddedNode:\s+(.*)/i && do { $added{$1}++; }; |
---|
[8209faa] | 88 | /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));}; |
---|
[6d985c0] | 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"; |
---|
[f8fa72b] | 94 | my $config_hosts = new IO::File("$local_config_dir/hosts") || |
---|
| 95 | die "Can't open $local_config_dir/hosts: $!\n"; |
---|
[cc0ffd2] | 96 | my $has_control = 0; |
---|
[6d985c0] | 97 | |
---|
| 98 | while (<$hosts>) { |
---|
| 99 | /^127\.0\.0\.1/ && do { print $new_hosts $_; }; |
---|
[d56b168] | 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 | } |
---|
[6d985c0] | 105 | } |
---|
| 106 | $hosts->close(); |
---|
[1962a5b] | 107 | HOST: |
---|
[6d985c0] | 108 | while (<$config_hosts>) { |
---|
[8209faa] | 109 | # Trim out hosts that were hidden by their home testbeds |
---|
| 110 | for my $h (@hide) { |
---|
[1962a5b] | 111 | next HOST if /^\d+\.\d+\.\d+\.\d+\s+$h-/; |
---|
[8209faa] | 112 | } |
---|
[6d985c0] | 113 | print $new_hosts $_; |
---|
| 114 | } |
---|
[d38ff56] | 115 | print $new_hosts "\n"; |
---|
[6d985c0] | 116 | $config_hosts->close(); |
---|
[cc0ffd2] | 117 | |
---|
[d56b168] | 118 | # Add gateway aliases |
---|
[d38ff56] | 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) { |
---|
[8209faa] | 125 | # We have an IP. Make a hosts entry for the key and the key plus the |
---|
[d38ff56] | 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"; |
---|
[cc0ffd2] | 131 | } |
---|
[d38ff56] | 132 | else { print $new_hosts "# Can't get ip for $lname\n"; } |
---|
[cc0ffd2] | 133 | } |
---|
[6d985c0] | 134 | $new_hosts->close(); |
---|
| 135 | copy("/tmp/hosts", "/etc/hosts"); |
---|
| 136 | |
---|
[dbc9144] | 137 | |
---|
[dc803a7] | 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. |
---|
[55779d4] | 140 | system("$perl -I/usr/local/federation/lib " . |
---|
[dc803a7] | 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 | } |
---|
[55779d4] | 154 | |
---|
[6d985c0] | 155 | |
---|
| 156 | if ($services{'userconfig'}) { |
---|
[f8fa72b] | 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(); |
---|
[6d985c0] | 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"); |
---|
[73e0a61] | 176 | $smb_type = "smbfs"; |
---|
[6d985c0] | 177 | } |
---|
| 178 | elsif ($uname =~ /Linux/ ) { |
---|
| 179 | # Pass individual filestems to Linux umount. No -A. |
---|
[73e0a61] | 180 | my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n"; |
---|
[6d985c0] | 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 " . |
---|
[73e0a61] | 194 | "$smbuser $smbproject $smb_type"); |
---|
[6d985c0] | 195 | } |
---|
| 196 | |
---|
[37f6592] | 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 | |
---|
[6d985c0] | 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); |
---|