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