[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; |
---|
[d56b168] | 25 | my %aliases; |
---|
| 26 | my @added; |
---|
[6d985c0] | 27 | my $perl; |
---|
| 28 | |
---|
| 29 | my $gateway; |
---|
| 30 | my $smbshare = "USERS"; |
---|
| 31 | my $smbuser; |
---|
| 32 | my $smbproject; |
---|
[cc0ffd2] | 33 | my $exp; |
---|
| 34 | my $proj; |
---|
[f8fa72b] | 35 | my $install_smb; |
---|
[73e0a61] | 36 | my $smb_type = 'cifs'; |
---|
[6d985c0] | 37 | chomp (my $uname = `uname`); |
---|
| 38 | my $smbmount = "smbmount.$uname.pl"; |
---|
| 39 | |
---|
[f8fa72b] | 40 | GetOptions("install_samba" => \$install_smb); |
---|
| 41 | |
---|
[6d985c0] | 42 | # find perl |
---|
| 43 | for my $p ("/usr/bin/perl", "/usr/local/bin/perl") { |
---|
| 44 | if ( -x $p ) { |
---|
| 45 | $perl = $p; |
---|
| 46 | last; |
---|
| 47 | } |
---|
| 48 | } |
---|
| 49 | $perl = "perl" unless $perl; |
---|
| 50 | |
---|
[f8fa72b] | 51 | if ($install_smb) { |
---|
| 52 | # fix yum.conf |
---|
| 53 | copy("/etc/yum.conf", "/etc/yum.conf.bak"); |
---|
| 54 | my $from = new IO::File("/etc/yum.conf.bak"); |
---|
| 55 | my $to = new IO::File(">/etc/yum.conf"); |
---|
| 56 | while (<$from>) { |
---|
| 57 | s/download.fedoralegacy.org/fedoralegacy.lsu.edu/g; |
---|
| 58 | print $to $_; |
---|
| 59 | } |
---|
| 60 | $from->close(); |
---|
| 61 | $to->close(); |
---|
[9b3627e] | 62 | # Now, samba. Because of the python dance on PG, we need to call python2.2 |
---|
| 63 | # explicitly |
---|
| 64 | system('/usr/bin/python2.2 /usr/bin/yum -y install samba-client'); |
---|
[73e0a61] | 65 | # These tools expect the fstab to include smbfs instead |
---|
| 66 | $smb_type = 'smbfs'; |
---|
[6d985c0] | 67 | } |
---|
| 68 | |
---|
[f8fa72b] | 69 | if (!-e "$local_config_dir/client.conf" ) { |
---|
| 70 | $tmcc_p->reader("$TMCC -b status"); |
---|
| 71 | while (<$tmcc_p>) { |
---|
| 72 | /ALLOCATED=([^\/]+)\/(\S+)/ && do { |
---|
| 73 | ($proj, $exp) = ($1, $2); |
---|
| 74 | $shared_config_dir = "/proj/$proj/exp/$exp/tmp"; |
---|
| 75 | last; |
---|
| 76 | }; |
---|
| 77 | } |
---|
| 78 | $tmcc_p->close(); |
---|
| 79 | |
---|
| 80 | mkdir($local_config_dir); |
---|
[6d985c0] | 81 | |
---|
[f8fa72b] | 82 | foreach my $fn ("seer.conf", "client.conf", "userconf", "hosts") { |
---|
| 83 | copy("$shared_config_dir/$fn", $local_config_dir ) |
---|
| 84 | if -e "$shared_config_dir/$fn"; |
---|
| 85 | } |
---|
[6d985c0] | 86 | } |
---|
| 87 | |
---|
| 88 | my $client = new IO::File("$local_config_dir/client.conf"); |
---|
| 89 | while (<$client>) { |
---|
| 90 | chomp; |
---|
| 91 | /ControlGateway:\s+(.*)/i && do { $gateway = $1; }; |
---|
| 92 | /SMBShare:\s+(.*)/i && do { $smbshare = $1; }; |
---|
| 93 | /ProjectUser:\s+(.*)/i && do { $smbuser = $1; }; |
---|
| 94 | /ProjectName:\s+(.*)/i && do { $smbproject = $1; }; |
---|
| 95 | /Service:\s+(.*)/i && do { $services{$1}++;}; |
---|
[d56b168] | 96 | /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;}; |
---|
[301e941] | 97 | /AddedNode:\s+(.*)/i && do { push(@added, $1); }; |
---|
[d56b168] | 98 | } |
---|
| 99 | |
---|
| 100 | # If nodes have been added with this name, do not alias the name to the portal |
---|
| 101 | for my $a (@added) { |
---|
| 102 | chomp $a; |
---|
| 103 | delete $aliases{$a} if $aliases{$a}; |
---|
[6d985c0] | 104 | } |
---|
| 105 | $client->close(); |
---|
| 106 | # Create the /etc/hosts file |
---|
| 107 | my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n"; |
---|
| 108 | my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n"; |
---|
[f8fa72b] | 109 | my $config_hosts = new IO::File("$local_config_dir/hosts") || |
---|
| 110 | die "Can't open $local_config_dir/hosts: $!\n"; |
---|
[cc0ffd2] | 111 | my $has_control = 0; |
---|
[6d985c0] | 112 | |
---|
| 113 | while (<$hosts>) { |
---|
| 114 | /^127\.0\.0\.1/ && do { print $new_hosts $_; }; |
---|
[d56b168] | 115 | # If aliases conflict with existing nodes, delete the alias |
---|
| 116 | for my $n (split($_)) { |
---|
| 117 | chomp $n; |
---|
| 118 | delete $aliases{$n} if $aliases{$n}; |
---|
| 119 | } |
---|
[6d985c0] | 120 | } |
---|
| 121 | $hosts->close(); |
---|
| 122 | while (<$config_hosts>) { |
---|
| 123 | print $new_hosts $_; |
---|
| 124 | } |
---|
| 125 | $config_hosts->close(); |
---|
[cc0ffd2] | 126 | |
---|
[d56b168] | 127 | # Add gateway aliases |
---|
[301e941] | 128 | if (%aliases) { |
---|
[cc0ffd2] | 129 | if ( my $hent = gethostbyname($gateway) ) { |
---|
| 130 | my $gwip = inet_ntoa($hent->addr_list->[0]); |
---|
| 131 | |
---|
[d56b168] | 132 | for my $k (keys %aliases) { |
---|
[301e941] | 133 | if ($exp && $proj) { |
---|
| 134 | print $new_hosts "\n$gwip\t$k.$exp.$proj $k\n"; |
---|
| 135 | } |
---|
| 136 | else { |
---|
| 137 | print $new_hosts "\n$gwip\t$k\n"; |
---|
| 138 | } |
---|
[d56b168] | 139 | } |
---|
[cc0ffd2] | 140 | } |
---|
| 141 | } |
---|
[6d985c0] | 142 | $new_hosts->close(); |
---|
| 143 | copy("/tmp/hosts", "/etc/hosts"); |
---|
| 144 | |
---|
[dbc9144] | 145 | |
---|
[dc803a7] | 146 | # If there are tunnelip interfaces to bring up, bring 'em up. Record any such |
---|
| 147 | # interfaces in /usr/local/federation/interfaces, so SEER can find them later. |
---|
[55779d4] | 148 | system("$perl -I/usr/local/federation/lib " . |
---|
[dc803a7] | 149 | "/usr/local/federation/bin/config_from_tunnelip.pl " . |
---|
| 150 | "--record=/usr/local/federation/etc/interfaces"); |
---|
| 151 | |
---|
| 152 | if ($uname =~ /Linux/ ) { |
---|
| 153 | system("$perl /usr/local/federation/bin/gated_routing.pl") |
---|
| 154 | if -r "/usr/local/federation/bin/gated_routing.pl"; |
---|
| 155 | } |
---|
| 156 | elsif ($uname =~/FreeBSD/ ) { |
---|
| 157 | # FreeBSD needs to have ospfs installed and a router config created and |
---|
| 158 | # run. |
---|
| 159 | system("$perl /usr/local/federation/bin/ospf_routing.pl") |
---|
| 160 | if -r "/usr/local/federation/bin/ospf_routing.pl"; |
---|
| 161 | } |
---|
[55779d4] | 162 | |
---|
[6d985c0] | 163 | |
---|
| 164 | if ($services{'userconfig'}) { |
---|
[f8fa72b] | 165 | if (!-e "$local_config_dir/old_accts") { |
---|
| 166 | $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n"; |
---|
| 167 | my $old_accounts = new IO::File(">$local_config_dir/old_accts") || |
---|
| 168 | die "Can't open $local_config_dir/old_accts: $!\n"; |
---|
| 169 | |
---|
| 170 | $tmcc_p->reader("$TMCC -b accounts"); |
---|
| 171 | while (<$tmcc_p>) { |
---|
| 172 | print $old_accounts $_; |
---|
| 173 | } |
---|
| 174 | $tmcc_p->close(); |
---|
| 175 | $old_accounts->close(); |
---|
[6d985c0] | 176 | } |
---|
| 177 | print("Updating accounts"); |
---|
| 178 | system("/usr/local/federation/bin/rc.fedaccounts"); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | if ($services{'SMB'}) { |
---|
| 182 | if ($uname =~ /FreeBSD/ ) { |
---|
| 183 | system("umount -A -f -t nfs,smbfs,cifs"); |
---|
[73e0a61] | 184 | $smb_type = "smbfs"; |
---|
[6d985c0] | 185 | } |
---|
| 186 | elsif ($uname =~ /Linux/ ) { |
---|
| 187 | # Pass individual filestems to Linux umount. No -A. |
---|
[73e0a61] | 188 | my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n"; |
---|
[6d985c0] | 189 | while (<$mtab>) { |
---|
| 190 | chomp; |
---|
| 191 | my @F = split($_); |
---|
| 192 | next unless $F[2] =~ /(nfs|cifs|smbfs)/; |
---|
| 193 | system("umount -f $F[1]"); |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | print "Waiting for SMB server\n"; |
---|
| 198 | gateway_lib::wait_for_port($gateway, 139, 60*60) || |
---|
| 199 | die "SMB server never came up\n"; |
---|
| 200 | print "Mounting via SMB\n"; |
---|
| 201 | system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . |
---|
[73e0a61] | 202 | "$smbuser $smbproject $smb_type"); |
---|
[6d985c0] | 203 | } |
---|
| 204 | |
---|
[37f6592] | 205 | if ($uname =~ /FreeBSD/ ) { |
---|
| 206 | # Restart ntp |
---|
| 207 | system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . |
---|
| 208 | "/etc/rc.d/ntpd start;"); |
---|
| 209 | |
---|
| 210 | } |
---|
| 211 | elsif ($uname =~ /Linux/ ) { |
---|
| 212 | # restart ntp |
---|
| 213 | system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". |
---|
| 214 | "/etc/rc.d/init.d/ntpd start"); |
---|
| 215 | } |
---|
| 216 | |
---|
[6d985c0] | 217 | # startcmd |
---|
| 218 | if ($ARGV[0] && $ARGV[1]) { |
---|
| 219 | if ($uname =~ /FreeBSD/) { |
---|
| 220 | system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\""); |
---|
| 221 | } |
---|
| 222 | elsif ($uname =~ /Linux/) { |
---|
| 223 | system("su \"$ARGV[0]\" --command \"$ARGV[1]\""); |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | exit(0); |
---|