[6d985c0] | 1 | #! /usr/bin/perl |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | use strict; |
---|
| 5 | |
---|
| 6 | use IO::File; |
---|
| 7 | use IO::Pipe; |
---|
| 8 | use File::Copy; |
---|
| 9 | |
---|
| 10 | use gateway_lib; |
---|
| 11 | |
---|
| 12 | chdir("/tmp"); |
---|
| 13 | |
---|
| 14 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
| 15 | my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n"; |
---|
| 16 | my $shared_config_dir; |
---|
| 17 | my $local_config_dir = "/usr/local/federation/etc"; |
---|
| 18 | my %services; |
---|
| 19 | my $perl; |
---|
| 20 | |
---|
| 21 | my $gateway; |
---|
| 22 | my $smbshare = "USERS"; |
---|
| 23 | my $smbuser; |
---|
| 24 | my $smbproject; |
---|
| 25 | chomp (my $uname = `uname`); |
---|
| 26 | my $smbmount = "smbmount.$uname.pl"; |
---|
| 27 | |
---|
| 28 | # find perl |
---|
| 29 | for my $p ("/usr/bin/perl", "/usr/local/bin/perl") { |
---|
| 30 | if ( -x $p ) { |
---|
| 31 | $perl = $p; |
---|
| 32 | last; |
---|
| 33 | } |
---|
| 34 | } |
---|
| 35 | $perl = "perl" unless $perl; |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | $tmcc_p->reader("$TMCC -b status"); |
---|
| 39 | while (<$tmcc_p>) { |
---|
| 40 | /ALLOCATED=([^\/]+)\/(\S+)/ && do { |
---|
| 41 | $shared_config_dir = "/proj/$1/exp/$2/tmp"; |
---|
| 42 | last; |
---|
| 43 | }; |
---|
| 44 | } |
---|
| 45 | $tmcc_p->close(); |
---|
| 46 | |
---|
| 47 | mkdir($local_config_dir); |
---|
| 48 | |
---|
| 49 | foreach my $fn ("seer.conf", "client.conf", "userconf") { |
---|
| 50 | copy("$shared_config_dir/$fn", $local_config_dir ) |
---|
| 51 | if -e "$shared_config_dir/$fn"; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | my $client = new IO::File("$local_config_dir/client.conf"); |
---|
| 55 | while (<$client>) { |
---|
| 56 | chomp; |
---|
| 57 | /ControlGateway:\s+(.*)/i && do { $gateway = $1; }; |
---|
| 58 | /SMBShare:\s+(.*)/i && do { $smbshare = $1; }; |
---|
| 59 | /ProjectUser:\s+(.*)/i && do { $smbuser = $1; }; |
---|
| 60 | /ProjectName:\s+(.*)/i && do { $smbproject = $1; }; |
---|
| 61 | /Service:\s+(.*)/i && do { $services{$1}++;}; |
---|
| 62 | } |
---|
| 63 | $client->close(); |
---|
| 64 | # Create the /etc/hosts file |
---|
| 65 | my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n"; |
---|
| 66 | my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n"; |
---|
| 67 | my $config_hosts = new IO::File("$shared_config_dir/hosts") || |
---|
| 68 | die "Can't open $shared_config_dir/hosts: $!\n"; |
---|
| 69 | |
---|
| 70 | while (<$hosts>) { |
---|
| 71 | /^127\.0\.0\.1/ && do { print $new_hosts $_; }; |
---|
| 72 | } |
---|
| 73 | $hosts->close(); |
---|
| 74 | while (<$config_hosts>) { |
---|
| 75 | print $new_hosts $_; |
---|
| 76 | } |
---|
| 77 | $config_hosts->close(); |
---|
| 78 | $new_hosts->close(); |
---|
| 79 | copy("/tmp/hosts", "/etc/hosts"); |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | if ($services{'userconfig'}) { |
---|
| 83 | $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n"; |
---|
| 84 | my $old_accounts = new IO::File(">$local_config_dir/old_accts") || |
---|
| 85 | die "Can't open $local_config_dir/old_accts: $!\n"; |
---|
| 86 | |
---|
| 87 | $tmcc_p->reader("$TMCC -b accounts"); |
---|
| 88 | while (<$tmcc_p>) { |
---|
| 89 | print $old_accounts $_; |
---|
| 90 | } |
---|
| 91 | $tmcc_p->close(); |
---|
| 92 | $old_accounts->close(); |
---|
| 93 | print("Updating accounts"); |
---|
| 94 | system("/usr/local/federation/bin/rc.fedaccounts"); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | if ($services{'SMB'}) { |
---|
| 98 | if ($uname =~ /FreeBSD/ ) { |
---|
| 99 | system("umount -A -f -t nfs,smbfs,cifs"); |
---|
| 100 | # Restart ntp |
---|
| 101 | system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . |
---|
| 102 | "/etc/rc.d/ntpd start;"); |
---|
| 103 | |
---|
| 104 | } |
---|
| 105 | elsif ($uname =~ /Linux/ ) { |
---|
| 106 | # Pass individual filestems to Linux umount. No -A. |
---|
| 107 | my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$1\n"; |
---|
| 108 | while (<$mtab>) { |
---|
| 109 | chomp; |
---|
| 110 | my @F = split($_); |
---|
| 111 | next unless $F[2] =~ /(nfs|cifs|smbfs)/; |
---|
| 112 | system("umount -f $F[1]"); |
---|
| 113 | } |
---|
| 114 | # restart ntp |
---|
| 115 | system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". |
---|
| 116 | "/etc/rc.d/init.d/ntpd start"); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | print "Waiting for SMB server\n"; |
---|
| 120 | gateway_lib::wait_for_port($gateway, 139, 60*60) || |
---|
| 121 | die "SMB server never came up\n"; |
---|
| 122 | print "Mounting via SMB\n"; |
---|
| 123 | system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . |
---|
| 124 | "$smbuser $smbproject"); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | # startcmd |
---|
| 128 | if ($ARGV[0] && $ARGV[1]) { |
---|
| 129 | if ($uname =~ /FreeBSD/) { |
---|
| 130 | system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\""); |
---|
| 131 | } |
---|
| 132 | elsif ($uname =~ /Linux/) { |
---|
| 133 | system("su \"$ARGV[0]\" --command \"$ARGV[1]\""); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | exit(0); |
---|