[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 | |
---|
[dc803a7] | 126 | # If there are tunnelip interfaces to bring up, bring 'em up. Record any such |
---|
| 127 | # interfaces in /usr/local/federation/interfaces, so SEER can find them later. |
---|
[55779d4] | 128 | system("$perl -I/usr/local/federation/lib " . |
---|
[dc803a7] | 129 | "/usr/local/federation/bin/config_from_tunnelip.pl " . |
---|
| 130 | "--record=/usr/local/federation/etc/interfaces"); |
---|
| 131 | |
---|
| 132 | if ($uname =~ /Linux/ ) { |
---|
| 133 | system("$perl /usr/local/federation/bin/gated_routing.pl") |
---|
| 134 | if -r "/usr/local/federation/bin/gated_routing.pl"; |
---|
| 135 | } |
---|
| 136 | elsif ($uname =~/FreeBSD/ ) { |
---|
| 137 | # FreeBSD needs to have ospfs installed and a router config created and |
---|
| 138 | # run. |
---|
| 139 | system("$perl /usr/local/federation/bin/ospf_routing.pl") |
---|
| 140 | if -r "/usr/local/federation/bin/ospf_routing.pl"; |
---|
| 141 | } |
---|
[55779d4] | 142 | |
---|
[6d985c0] | 143 | |
---|
| 144 | if ($services{'userconfig'}) { |
---|
[f8fa72b] | 145 | if (!-e "$local_config_dir/old_accts") { |
---|
| 146 | $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n"; |
---|
| 147 | my $old_accounts = new IO::File(">$local_config_dir/old_accts") || |
---|
| 148 | die "Can't open $local_config_dir/old_accts: $!\n"; |
---|
| 149 | |
---|
| 150 | $tmcc_p->reader("$TMCC -b accounts"); |
---|
| 151 | while (<$tmcc_p>) { |
---|
| 152 | print $old_accounts $_; |
---|
| 153 | } |
---|
| 154 | $tmcc_p->close(); |
---|
| 155 | $old_accounts->close(); |
---|
[6d985c0] | 156 | } |
---|
| 157 | print("Updating accounts"); |
---|
| 158 | system("/usr/local/federation/bin/rc.fedaccounts"); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | if ($services{'SMB'}) { |
---|
| 162 | if ($uname =~ /FreeBSD/ ) { |
---|
| 163 | system("umount -A -f -t nfs,smbfs,cifs"); |
---|
[73e0a61] | 164 | $smb_type = "smbfs"; |
---|
[6d985c0] | 165 | } |
---|
| 166 | elsif ($uname =~ /Linux/ ) { |
---|
| 167 | # Pass individual filestems to Linux umount. No -A. |
---|
[73e0a61] | 168 | my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n"; |
---|
[6d985c0] | 169 | while (<$mtab>) { |
---|
| 170 | chomp; |
---|
| 171 | my @F = split($_); |
---|
| 172 | next unless $F[2] =~ /(nfs|cifs|smbfs)/; |
---|
| 173 | system("umount -f $F[1]"); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | print "Waiting for SMB server\n"; |
---|
| 178 | gateway_lib::wait_for_port($gateway, 139, 60*60) || |
---|
| 179 | die "SMB server never came up\n"; |
---|
| 180 | print "Mounting via SMB\n"; |
---|
| 181 | system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . |
---|
[73e0a61] | 182 | "$smbuser $smbproject $smb_type"); |
---|
[6d985c0] | 183 | } |
---|
| 184 | |
---|
[37f6592] | 185 | if ($uname =~ /FreeBSD/ ) { |
---|
| 186 | # Restart ntp |
---|
| 187 | system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . |
---|
| 188 | "/etc/rc.d/ntpd start;"); |
---|
| 189 | |
---|
| 190 | } |
---|
| 191 | elsif ($uname =~ /Linux/ ) { |
---|
| 192 | # restart ntp |
---|
| 193 | system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". |
---|
| 194 | "/etc/rc.d/init.d/ntpd start"); |
---|
| 195 | } |
---|
| 196 | |
---|
[6d985c0] | 197 | # startcmd |
---|
| 198 | if ($ARGV[0] && $ARGV[1]) { |
---|
| 199 | if ($uname =~ /FreeBSD/) { |
---|
| 200 | system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\""); |
---|
| 201 | } |
---|
| 202 | elsif ($uname =~ /Linux/) { |
---|
| 203 | system("su \"$ARGV[0]\" --command \"$ARGV[1]\""); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | exit(0); |
---|