source: fedkit/federate.pl @ cf9598a

compt_changesinfo-ops
Last change on this file since cf9598a 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
Line 
1#! /usr/bin/perl
2
3
4use strict;
5
6use Getopt::Long;
7
8use IO::File;
9use IO::Pipe;
10use File::Copy;
11
12use Net::hostent;
13use Socket;
14
15use gateway_lib;
16
17chdir("/tmp");
18
19my $TMCC = "/usr/local/etc/emulab/tmcc";
20my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route";
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;
25my %aliases;
26my %added;
27my @hide;
28my $perl;
29
30my $gateway;
31my $smbshare = "USERS";
32my $smbuser;
33my $smbproject;
34my $exp;
35my $proj;
36my $install_smb;
37my $smb_type = 'cifs';
38chomp (my $uname = `uname`);
39my $smbmount = "smbmount.$uname.pl";
40
41GetOptions("install_samba" => \$install_smb);
42
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
52if ($install_smb) {
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';
57}
58
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);
71
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    }
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}++;};
86    /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;};
87    /AddedNode:\s+(.*)/i && do { $added{$1}++; };
88    /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));};
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";
94my $config_hosts = new IO::File("$local_config_dir/hosts") || 
95    die "Can't open $local_config_dir/hosts: $!\n";
96my $has_control = 0;
97
98while (<$hosts>) {
99    /^127\.0\.0\.1/ && do { print $new_hosts $_; };
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    }
105}
106$hosts->close();
107HOST:
108while (<$config_hosts>) {
109    # Trim out hosts that were hidden by their home testbeds
110    for my $h (@hide) {
111        next HOST if /^\d+\.\d+\.\d+\.\d+\s+$h-/;
112    }
113    print $new_hosts $_;
114}
115print $new_hosts "\n";
116$config_hosts->close();
117
118# Add gateway aliases
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) { 
125        # We have an IP.  Make a hosts entry for the key and the key plus the
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";
131    }
132    else { print $new_hosts "# Can't get ip for $lname\n"; }
133}
134$new_hosts->close();
135copy("/tmp/hosts", "/etc/hosts");
136
137
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.
140system("$perl -I/usr/local/federation/lib " . 
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}
154
155
156if ($services{'userconfig'}) {
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();
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");
176        $smb_type = "smbfs";
177    }
178    elsif ($uname =~ /Linux/ ) {
179        # Pass individual filestems to Linux umount.  No -A.
180        my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n";
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 " . 
194        "$smbuser $smbproject $smb_type");
195}
196
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
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.