[6d985c0] | 1 | #! /usr/bin/perl |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | use strict; |
---|
| 5 | |
---|
| 6 | use IO::File; |
---|
| 7 | use IO::Pipe; |
---|
| 8 | use File::Copy; |
---|
| 9 | |
---|
[cc0ffd2] | 10 | use Net::hostent; |
---|
| 11 | use Socket; |
---|
| 12 | |
---|
[6d985c0] | 13 | use gateway_lib; |
---|
| 14 | |
---|
| 15 | chdir("/tmp"); |
---|
| 16 | |
---|
| 17 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
[dbc9144] | 18 | my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route"; |
---|
[6d985c0] | 19 | my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n"; |
---|
| 20 | my $shared_config_dir; |
---|
| 21 | my $local_config_dir = "/usr/local/federation/etc"; |
---|
| 22 | my %services; |
---|
| 23 | my $perl; |
---|
| 24 | |
---|
| 25 | my $gateway; |
---|
| 26 | my $smbshare = "USERS"; |
---|
| 27 | my $smbuser; |
---|
| 28 | my $smbproject; |
---|
[cc0ffd2] | 29 | my $exp; |
---|
| 30 | my $proj; |
---|
[6d985c0] | 31 | chomp (my $uname = `uname`); |
---|
| 32 | my $smbmount = "smbmount.$uname.pl"; |
---|
| 33 | |
---|
| 34 | # find perl |
---|
| 35 | for my $p ("/usr/bin/perl", "/usr/local/bin/perl") { |
---|
| 36 | if ( -x $p ) { |
---|
| 37 | $perl = $p; |
---|
| 38 | last; |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | $perl = "perl" unless $perl; |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | $tmcc_p->reader("$TMCC -b status"); |
---|
| 45 | while (<$tmcc_p>) { |
---|
| 46 | /ALLOCATED=([^\/]+)\/(\S+)/ && do { |
---|
[cc0ffd2] | 47 | ($proj, $exp) = ($1, $2); |
---|
| 48 | $shared_config_dir = "/proj/$proj/exp/$exp/tmp"; |
---|
[6d985c0] | 49 | last; |
---|
| 50 | }; |
---|
| 51 | } |
---|
| 52 | $tmcc_p->close(); |
---|
| 53 | |
---|
| 54 | mkdir($local_config_dir); |
---|
| 55 | |
---|
| 56 | foreach my $fn ("seer.conf", "client.conf", "userconf") { |
---|
| 57 | copy("$shared_config_dir/$fn", $local_config_dir ) |
---|
| 58 | if -e "$shared_config_dir/$fn"; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | my $client = new IO::File("$local_config_dir/client.conf"); |
---|
| 62 | while (<$client>) { |
---|
| 63 | chomp; |
---|
| 64 | /ControlGateway:\s+(.*)/i && do { $gateway = $1; }; |
---|
| 65 | /SMBShare:\s+(.*)/i && do { $smbshare = $1; }; |
---|
| 66 | /ProjectUser:\s+(.*)/i && do { $smbuser = $1; }; |
---|
| 67 | /ProjectName:\s+(.*)/i && do { $smbproject = $1; }; |
---|
| 68 | /Service:\s+(.*)/i && do { $services{$1}++;}; |
---|
| 69 | } |
---|
| 70 | $client->close(); |
---|
| 71 | # Create the /etc/hosts file |
---|
| 72 | my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n"; |
---|
| 73 | my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n"; |
---|
| 74 | my $config_hosts = new IO::File("$shared_config_dir/hosts") || |
---|
| 75 | die "Can't open $shared_config_dir/hosts: $!\n"; |
---|
[cc0ffd2] | 76 | my $has_control = 0; |
---|
[6d985c0] | 77 | |
---|
| 78 | while (<$hosts>) { |
---|
| 79 | /^127\.0\.0\.1/ && do { print $new_hosts $_; }; |
---|
[cc0ffd2] | 80 | /control\./ && $has_control++; |
---|
[6d985c0] | 81 | } |
---|
| 82 | $hosts->close(); |
---|
| 83 | while (<$config_hosts>) { |
---|
| 84 | print $new_hosts $_; |
---|
| 85 | } |
---|
| 86 | $config_hosts->close(); |
---|
[cc0ffd2] | 87 | |
---|
| 88 | # If seer is enabled and no local control node in play, alias the gateway to be |
---|
| 89 | # control |
---|
| 90 | if ($services{'seer'} && !$has_control ) { |
---|
| 91 | if ( my $hent = gethostbyname($gateway) ) { |
---|
| 92 | my $gwip = inet_ntoa($hent->addr_list->[0]); |
---|
| 93 | |
---|
| 94 | print $new_hosts "\n$gwip\tcontrol.$exp.$proj\n"; |
---|
| 95 | } |
---|
| 96 | } |
---|
[6d985c0] | 97 | $new_hosts->close(); |
---|
| 98 | copy("/tmp/hosts", "/etc/hosts"); |
---|
| 99 | |
---|
[dbc9144] | 100 | # Ick. at least one of our images has gated in the wrong place (sigh). If |
---|
| 101 | # so, fix it and restart routing. |
---|
| 102 | if (!-x "/usr/sbin/gated" && -x "/sbin/gated" ) { |
---|
| 103 | print "Linking to /sbin/gated and starting routing\n"; |
---|
| 104 | symlink("/sbin/gated", "/usr/sbin/gated"); |
---|
| 105 | system("$RC_ROUTE boot") if -x $RC_ROUTE; |
---|
| 106 | } |
---|
| 107 | |
---|
[55779d4] | 108 | # If there are tunnelip interfaces to bring up, bring 'em |
---|
| 109 | system("$perl -I/usr/local/federation/lib " . |
---|
| 110 | "/usr/local/federation/bin/config_from_tunnelip.pl"); |
---|
| 111 | |
---|
[6d985c0] | 112 | |
---|
| 113 | if ($services{'userconfig'}) { |
---|
| 114 | $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n"; |
---|
| 115 | my $old_accounts = new IO::File(">$local_config_dir/old_accts") || |
---|
| 116 | die "Can't open $local_config_dir/old_accts: $!\n"; |
---|
| 117 | |
---|
| 118 | $tmcc_p->reader("$TMCC -b accounts"); |
---|
| 119 | while (<$tmcc_p>) { |
---|
| 120 | print $old_accounts $_; |
---|
| 121 | } |
---|
| 122 | $tmcc_p->close(); |
---|
| 123 | $old_accounts->close(); |
---|
| 124 | print("Updating accounts"); |
---|
| 125 | system("/usr/local/federation/bin/rc.fedaccounts"); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | if ($services{'SMB'}) { |
---|
| 129 | if ($uname =~ /FreeBSD/ ) { |
---|
| 130 | system("umount -A -f -t nfs,smbfs,cifs"); |
---|
| 131 | # Restart ntp |
---|
| 132 | system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . |
---|
| 133 | "/etc/rc.d/ntpd start;"); |
---|
| 134 | |
---|
| 135 | } |
---|
| 136 | elsif ($uname =~ /Linux/ ) { |
---|
| 137 | # Pass individual filestems to Linux umount. No -A. |
---|
| 138 | my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$1\n"; |
---|
| 139 | while (<$mtab>) { |
---|
| 140 | chomp; |
---|
| 141 | my @F = split($_); |
---|
| 142 | next unless $F[2] =~ /(nfs|cifs|smbfs)/; |
---|
| 143 | system("umount -f $F[1]"); |
---|
| 144 | } |
---|
| 145 | # restart ntp |
---|
| 146 | system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". |
---|
| 147 | "/etc/rc.d/init.d/ntpd start"); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | print "Waiting for SMB server\n"; |
---|
| 151 | gateway_lib::wait_for_port($gateway, 139, 60*60) || |
---|
| 152 | die "SMB server never came up\n"; |
---|
| 153 | print "Mounting via SMB\n"; |
---|
| 154 | system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . |
---|
| 155 | "$smbuser $smbproject"); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | # startcmd |
---|
| 159 | if ($ARGV[0] && $ARGV[1]) { |
---|
| 160 | if ($uname =~ /FreeBSD/) { |
---|
| 161 | system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\""); |
---|
| 162 | } |
---|
| 163 | elsif ($uname =~ /Linux/) { |
---|
| 164 | system("su \"$ARGV[0]\" --command \"$ARGV[1]\""); |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | exit(0); |
---|