source: fedkit/federate.pl @ 88dbe63

axis_examplecompt_changesinfo-opsversion-3.01version-3.02
Last change on this file since 88dbe63 was 1d91791f, checked in by Ted Faber <faber@…>, 14 years ago

Remove support for ancient linux distros, which effectively adds support for
modern ones.

  • Property mode set to 100644
File size: 5.8 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
17chdir("/tmp");
18
19my $TMCC = "/usr/local/etc/emulab/tmcc";
[dbc9144]20my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route";
[6d985c0]21my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n";
22my $shared_config_dir;
23my $local_config_dir = "/usr/local/federation/etc";
24my %services;
[d56b168]25my %aliases;
[d38ff56]26my %added;
[8209faa]27my @hide;
[6d985c0]28my $perl;
29
30my $gateway;
31my $smbshare = "USERS";
32my $smbuser;
33my $smbproject;
[cc0ffd2]34my $exp;
35my $proj;
[f8fa72b]36my $install_smb;
[73e0a61]37my $smb_type = 'cifs';
[6d985c0]38chomp (my $uname = `uname`);
39my $smbmount = "smbmount.$uname.pl";
40
[f8fa72b]41GetOptions("install_samba" => \$install_smb);
42
[6d985c0]43# find perl
44for my $p ("/usr/bin/perl", "/usr/local/bin/perl") {
45    if ( -x $p ) {
46        $perl = $p;
47        last;
48    }
49}
50$perl = "perl" unless $perl;
51
[f8fa72b]52if ($install_smb) {
[1d91791f]53    # Install samba
54    system('/usr/bin/yum -y install samba-client');
55    # These tools expect the fstab to include cifs
56    $smb_type = 'cifs';
[6d985c0]57}
58
[f8fa72b]59if (!-e "$local_config_dir/client.conf" ) {
60    $tmcc_p->reader("$TMCC -b status");
61    while (<$tmcc_p>) {
62        /ALLOCATED=([^\/]+)\/(\S+)/ && do {
63            ($proj, $exp) = ($1, $2);
64            $shared_config_dir = "/proj/$proj/exp/$exp/tmp";
65            last;
66        };
67    }
68    $tmcc_p->close();
69
70    mkdir($local_config_dir);
[6d985c0]71
[f8fa72b]72    foreach my $fn ("seer.conf", "client.conf", "userconf", "hosts") {
73        copy("$shared_config_dir/$fn", $local_config_dir )
74            if -e "$shared_config_dir/$fn";
75    }
[6d985c0]76}
77
78my $client = new IO::File("$local_config_dir/client.conf");
79while (<$client>) {
80    chomp;
81    /ControlGateway:\s+(.*)/i && do { $gateway = $1; };
82    /SMBShare:\s+(.*)/i && do { $smbshare = $1; };
83    /ProjectUser:\s+(.*)/i && do { $smbuser = $1; };
84    /ProjectName:\s+(.*)/i && do { $smbproject = $1; };
85    /Service:\s+(.*)/i && do { $services{$1}++;};
[d56b168]86    /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;};
[d38ff56]87    /AddedNode:\s+(.*)/i && do { $added{$1}++; };
[8209faa]88    /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));};
[6d985c0]89}
90$client->close();
91# Create the /etc/hosts file
92my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n";
93my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n";
[f8fa72b]94my $config_hosts = new IO::File("$local_config_dir/hosts") || 
95    die "Can't open $local_config_dir/hosts: $!\n";
[cc0ffd2]96my $has_control = 0;
[6d985c0]97
98while (<$hosts>) {
99    /^127\.0\.0\.1/ && do { print $new_hosts $_; };
[d56b168]100    # If aliases conflict with existing nodes, delete the alias
101    for my $n (split($_)) {
102        chomp $n;
103        delete $aliases{$n} if $aliases{$n};
104    }
[6d985c0]105}
106$hosts->close();
[1962a5b]107HOST:
[6d985c0]108while (<$config_hosts>) {
[8209faa]109    # Trim out hosts that were hidden by their home testbeds
110    for my $h (@hide) {
[1962a5b]111        next HOST if /^\d+\.\d+\.\d+\.\d+\s+$h-/;
[8209faa]112    }
[6d985c0]113    print $new_hosts $_;
114}
[d38ff56]115print $new_hosts "\n";
[6d985c0]116$config_hosts->close();
[cc0ffd2]117
[d56b168]118# Add gateway aliases
[d38ff56]119for my $k (keys %aliases) {
120    # If we added a node, it's a node without a local address.  Bind the name
121    # to the IP in /etc/hosts.  If we didn't add a node, it's the gateway node.
122    (my $lname = $gateway) =~ s/^[^\.]+/$k/;
123    my $ip = gateway_lib::get_ip($added{$k} ? $lname : $gateway);
124    if ($ip) { 
[8209faa]125        # We have an IP.  Make a hosts entry for the key and the key plus the
[d38ff56]126        # first two subdomains (which is an emulab setup)
127        my @x = split(/\./, $lname);
128        if (@x > 3 ) { splice(@x, 3); }
129        my $out = join(".", @x);
130        print $new_hosts "$ip\t$out $k\n";
[cc0ffd2]131    }
[d38ff56]132    else { print $new_hosts "# Can't get ip for $lname\n"; }
[cc0ffd2]133}
[6d985c0]134$new_hosts->close();
135copy("/tmp/hosts", "/etc/hosts");
136
[dbc9144]137
[dc803a7]138# If there are tunnelip interfaces to bring up, bring 'em up.  Record any such
139# interfaces in /usr/local/federation/interfaces, so SEER can find them later.
[55779d4]140system("$perl -I/usr/local/federation/lib " . 
[dc803a7]141    "/usr/local/federation/bin/config_from_tunnelip.pl " . 
142    "--record=/usr/local/federation/etc/interfaces");
143
144if ($uname =~ /Linux/ ) {
145    system("$perl /usr/local/federation/bin/gated_routing.pl")
146        if -r "/usr/local/federation/bin/gated_routing.pl";
147}
148elsif ($uname =~/FreeBSD/ ) {
149    # FreeBSD needs to have ospfs installed and a router config created and
150    # run.
151    system("$perl /usr/local/federation/bin/ospf_routing.pl")
152        if -r "/usr/local/federation/bin/ospf_routing.pl";
153}
[55779d4]154
[6d985c0]155
156if ($services{'userconfig'}) {
[f8fa72b]157    if (!-e "$local_config_dir/old_accts") {
158        $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n";
159        my $old_accounts = new IO::File(">$local_config_dir/old_accts") || 
160            die "Can't open $local_config_dir/old_accts: $!\n";
161
162        $tmcc_p->reader("$TMCC -b accounts");
163        while (<$tmcc_p>) {
164            print $old_accounts $_;
165        }
166        $tmcc_p->close();
167        $old_accounts->close();
[6d985c0]168    }
169    print("Updating accounts");
170    system("/usr/local/federation/bin/rc.fedaccounts");
171}
172
173if ($services{'SMB'}) {
174    if ($uname =~ /FreeBSD/ ) {
175        system("umount -A -f -t nfs,smbfs,cifs");
[73e0a61]176        $smb_type = "smbfs";
[6d985c0]177    }
178    elsif ($uname =~ /Linux/ ) {
179        # Pass individual filestems to Linux umount.  No -A.
[73e0a61]180        my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n";
[6d985c0]181        while (<$mtab>) {
182            chomp;
183            my @F = split($_);
184            next unless $F[2] =~ /(nfs|cifs|smbfs)/;
185            system("umount -f $F[1]");
186        }
187    }
188
189    print "Waiting for SMB server\n";
190    gateway_lib::wait_for_port($gateway, 139, 60*60) || 
191        die "SMB server never came up\n";
192    print "Mounting via SMB\n";
193    system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . 
[73e0a61]194        "$smbuser $smbproject $smb_type");
[6d985c0]195}
196
[37f6592]197if ($uname =~ /FreeBSD/ ) {
198    # Restart ntp
199    system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . 
200        "/etc/rc.d/ntpd start;");
201                                               
202}
203elsif ($uname =~ /Linux/ ) {
204    # restart ntp
205    system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". 
206        "/etc/rc.d/init.d/ntpd start");
207}
208
[6d985c0]209# startcmd
210if ($ARGV[0] && $ARGV[1]) {
211    if ($uname =~ /FreeBSD/) {
212        system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\"");
213    }
214    elsif ($uname =~ /Linux/) {
215        system("su \"$ARGV[0]\" --command \"$ARGV[1]\"");
216    }
217}
218exit(0);
Note: See TracBrowser for help on using the repository browser.