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