source: fedkit/federate.pl @ a65a65a

axis_examplecompt_changesinfo-opsversion-3.01version-3.02
Last change on this file since a65a65a was 1962a5b, checked in by Ted Faber <faber@…>, 14 years ago

Next in the right loop.

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