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