source: fedkit/federate.pl @ b06744b

Last change on this file since b06744b was 9a52a80, checked in by Ted Faber <faber@…>, 12 years ago

Static routing

  • Property mode set to 100644
File size: 7.5 KB
RevLine 
[6d985c0]1#! /usr/bin/perl
2
3
4use strict;
5
[f8fa72b]6use Getopt::Long;
7
[6d985c0]8use IO::File;
9use IO::Pipe;
10use File::Copy;
11
[cc0ffd2]12use Net::hostent;
13use Socket;
14
[6d985c0]15use gateway_lib;
16
[5ae9d94]17# rc.fedaccounts is going to send a TERM to any processes running as an old
18# user, so insulate this (and children) process from propagated signals.
19$SIG{'TERM'} = 'IGNORE';
20
[6d985c0]21chdir("/tmp");
22
23my $TMCC = "/usr/local/etc/emulab/tmcc";
[dbc9144]24my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route";
[6d985c0]25my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n";
26my $shared_config_dir;
[3df9b33]27my $shared_seer_auth_dir;
[6d985c0]28my $local_config_dir = "/usr/local/federation/etc";
29my %services;
[d56b168]30my %aliases;
[d38ff56]31my %added;
[8209faa]32my @hide;
[6d985c0]33my $perl;
34
35my $gateway;
36my $smbshare = "USERS";
37my $smbuser;
38my $smbproject;
[cc0ffd2]39my $exp;
40my $proj;
[f8fa72b]41my $install_smb;
[73e0a61]42my $smb_type = 'cifs';
[6d985c0]43chomp (my $uname = `uname`);
44my $smbmount = "smbmount.$uname.pl";
45
[f8fa72b]46GetOptions("install_samba" => \$install_smb);
47
[6d985c0]48# find perl
49for my $p ("/usr/bin/perl", "/usr/local/bin/perl") {
50    if ( -x $p ) {
51        $perl = $p;
52        last;
53    }
54}
55$perl = "perl" unless $perl;
56
[5ae9d94]57if (!-x '/sbin/mount.cifs' ) {
58    if ( -x '/usr/bin/yum' ) {
59        # Install samba
60        system('/usr/bin/yum -y install samba-client');
61        system('/usr/bin/yum -y install cifs-utils');
62        # These tools expect the fstab to include cifs
63        $smb_type = 'cifs';
64    }
65    elsif (-x '/usr/bin/apt-get') {
66        # Install samba
[76c43e4]67        system('/usr/bin/apt-get -y update');
[5ae9d94]68        system('/usr/bin/apt-get -y install samba-client');
69        system('/usr/bin/apt-get -y install smbfs');
70        # These tools expect the fstab to include cifs
71        $smb_type = 'cifs';
72    }
[6d985c0]73}
74
[f8fa72b]75if (!-e "$local_config_dir/client.conf" ) {
76    $tmcc_p->reader("$TMCC -b status");
77    while (<$tmcc_p>) {
78        /ALLOCATED=([^\/]+)\/(\S+)/ && do {
79            ($proj, $exp) = ($1, $2);
80            $shared_config_dir = "/proj/$proj/exp/$exp/tmp";
[3df9b33]81            $shared_seer_auth_dir = "/proj/$proj/exp/$exp/tbdata";
[f8fa72b]82            last;
83        };
84    }
85    $tmcc_p->close();
86
87    mkdir($local_config_dir);
[6d985c0]88
[3df9b33]89    foreach my $fn ("seer.conf", "client.conf", "userconf", "hosts",
[9a52a80]90            "ca.pem", "node.pem", "route.tgz") {
[f8fa72b]91        copy("$shared_config_dir/$fn", $local_config_dir )
92            if -e "$shared_config_dir/$fn";
93    }
[9a52a80]94    # If there are static routes, unpack them
95    system("tar -C $local_config_dir -xzf $local_config_dir/route.tgz")
96        if -e "$local_config_dir/route.tgz";
[3df9b33]97
98    # Copy seer authorization files into the location that standard SEER
99    # invocations will look.  The above loop puts them where -F invocations
100    # will look.
101    foreach my $fn ("ca.pem", "node.pem") {
102        copy("$shared_config_dir/$fn", $shared_seer_auth_dir )
103            if -e "$shared_config_dir/$fn" && -d $shared_seer_auth_dir;
104    }
[6d985c0]105}
106
107my $client = new IO::File("$local_config_dir/client.conf");
108while (<$client>) {
109    chomp;
110    /ControlGateway:\s+(.*)/i && do { $gateway = $1; };
111    /SMBShare:\s+(.*)/i && do { $smbshare = $1; };
112    /ProjectUser:\s+(.*)/i && do { $smbuser = $1; };
113    /ProjectName:\s+(.*)/i && do { $smbproject = $1; };
114    /Service:\s+(.*)/i && do { $services{$1}++;};
[d56b168]115    /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;};
[d38ff56]116    /AddedNode:\s+(.*)/i && do { $added{$1}++; };
[8209faa]117    /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));};
[6d985c0]118}
119$client->close();
120# Create the /etc/hosts file
121my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n";
122my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n";
[f8fa72b]123my $config_hosts = new IO::File("$local_config_dir/hosts") || 
124    die "Can't open $local_config_dir/hosts: $!\n";
[cc0ffd2]125my $has_control = 0;
[6d985c0]126
127while (<$hosts>) {
128    /^127\.0\.0\.1/ && do { print $new_hosts $_; };
[d56b168]129    # If aliases conflict with existing nodes, delete the alias
130    for my $n (split($_)) {
131        chomp $n;
132        delete $aliases{$n} if $aliases{$n};
133    }
[6d985c0]134}
135$hosts->close();
[1962a5b]136HOST:
[6d985c0]137while (<$config_hosts>) {
[8209faa]138    # Trim out hosts that were hidden by their home testbeds
139    for my $h (@hide) {
[1962a5b]140        next HOST if /^\d+\.\d+\.\d+\.\d+\s+$h-/;
[8209faa]141    }
[6d985c0]142    print $new_hosts $_;
143}
[d38ff56]144print $new_hosts "\n";
[6d985c0]145$config_hosts->close();
[cc0ffd2]146
[d56b168]147# Add gateway aliases
[d38ff56]148for my $k (keys %aliases) {
149    # If we added a node, it's a node without a local address.  Bind the name
150    # to the IP in /etc/hosts.  If we didn't add a node, it's the gateway node.
151    (my $lname = $gateway) =~ s/^[^\.]+/$k/;
152    my $ip = gateway_lib::get_ip($added{$k} ? $lname : $gateway);
153    if ($ip) { 
[8209faa]154        # We have an IP.  Make a hosts entry for the key and the key plus the
[d38ff56]155        # first two subdomains (which is an emulab setup)
156        my @x = split(/\./, $lname);
157        if (@x > 3 ) { splice(@x, 3); }
158        my $out = join(".", @x);
159        print $new_hosts "$ip\t$out $k\n";
[cc0ffd2]160    }
[d38ff56]161    else { print $new_hosts "# Can't get ip for $lname\n"; }
[cc0ffd2]162}
[6d985c0]163$new_hosts->close();
164copy("/tmp/hosts", "/etc/hosts");
165
[dbc9144]166
[dc803a7]167# If there are tunnelip interfaces to bring up, bring 'em up.  Record any such
168# interfaces in /usr/local/federation/interfaces, so SEER can find them later.
[55779d4]169system("$perl -I/usr/local/federation/lib " . 
[dc803a7]170    "/usr/local/federation/bin/config_from_tunnelip.pl " . 
171    "--record=/usr/local/federation/etc/interfaces");
172
173if ($uname =~ /Linux/ ) {
[9a52a80]174    system("$perl /usr/local/federation/bin/static_routing.pl")
175        if -r "/usr/local/federation/bin/static_routing.pl";
176    if ($?) {
177        system("$perl /usr/local/federation/bin/gated_routing.pl")
178            if -r "/usr/local/federation/bin/gated_routing.pl";
179        }
[17c2f7b]180    if ($?) {
181        system("$perl /usr/local/federation/bin/quagga_routing.pl")
182            if -r "/usr/local/federation/bin/quagga_routing.pl";
183    }
[dc803a7]184}
185elsif ($uname =~/FreeBSD/ ) {
[9a52a80]186    system("$perl /usr/local/federation/bin/static_routing.pl")
187        if -r "/usr/local/federation/bin/static_routing.pl";
188    if ($?) {
189        # FreeBSD needs to have ospfs installed and a router config created and
190        # run.
191        system("$perl /usr/local/federation/bin/ospf_routing.pl")
192            if -r "/usr/local/federation/bin/ospf_routing.pl";
193    }
[dc803a7]194}
[55779d4]195
[6d985c0]196
197if ($services{'userconfig'}) {
[f8fa72b]198    if (!-e "$local_config_dir/old_accts") {
199        $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n";
200        my $old_accounts = new IO::File(">$local_config_dir/old_accts") || 
201            die "Can't open $local_config_dir/old_accts: $!\n";
202
203        $tmcc_p->reader("$TMCC -b accounts");
204        while (<$tmcc_p>) {
205            print $old_accounts $_;
206        }
207        $tmcc_p->close();
208        $old_accounts->close();
[6d985c0]209    }
210    print("Updating accounts");
211    system("/usr/local/federation/bin/rc.fedaccounts");
212}
213
214if ($services{'SMB'}) {
215    if ($uname =~ /FreeBSD/ ) {
216        system("umount -A -f -t nfs,smbfs,cifs");
[73e0a61]217        $smb_type = "smbfs";
[6d985c0]218    }
219    elsif ($uname =~ /Linux/ ) {
220        # Pass individual filestems to Linux umount.  No -A.
[73e0a61]221        my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n";
[6d985c0]222        while (<$mtab>) {
223            chomp;
224            my @F = split($_);
225            next unless $F[2] =~ /(nfs|cifs|smbfs)/;
226            system("umount -f $F[1]");
227        }
228    }
229
230    print "Waiting for SMB server\n";
231    gateway_lib::wait_for_port($gateway, 139, 60*60) || 
232        die "SMB server never came up\n";
233    print "Mounting via SMB\n";
234    system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . 
[73e0a61]235        "$smbuser $smbproject $smb_type");
[6d985c0]236}
237
[37f6592]238if ($uname =~ /FreeBSD/ ) {
239    # Restart ntp
240    system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . 
241        "/etc/rc.d/ntpd start;");
242                                               
243}
244elsif ($uname =~ /Linux/ ) {
245    # restart ntp
[558d829]246    if (-d "/etc/rc.d/init.d" ) {
247        system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". 
248            "/etc/rc.d/init.d/ntpd start");
[dcac546]249    } elsif (-d "/etc/init.d" ) {
[558d829]250        system("service ntp stop; /usr/sbin/ntpdate boss; ". 
251            "service ntp start");
252    }
253    else {
254        print "Cannot find ntp directories\n";
255    }
[37f6592]256}
257
[6d985c0]258# startcmd
259if ($ARGV[0] && $ARGV[1]) {
260    if ($uname =~ /FreeBSD/) {
261        system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\"");
262    }
263    elsif ($uname =~ /Linux/) {
264        system("su \"$ARGV[0]\" --command \"$ARGV[1]\"");
265    }
266}
267exit(0);
Note: See TracBrowser for help on using the repository browser.