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