source: fedkit/federate.pl @ c2f92c5

axis_examplecompt_changesinfo-opsversion-3.01version-3.02
Last change on this file since c2f92c5 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
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    # 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();
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');
66    # These tools expect the fstab to include smbfs instead
67    $smb_type = 'smbfs';
68}
69
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);
82
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    }
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}++;};
97    /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;};
98    /AddedNode:\s+(.*)/i && do { $added{$1}++; };
99    /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));};
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";
105my $config_hosts = new IO::File("$local_config_dir/hosts") || 
106    die "Can't open $local_config_dir/hosts: $!\n";
107my $has_control = 0;
108
109while (<$hosts>) {
110    /^127\.0\.0\.1/ && do { print $new_hosts $_; };
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    }
116}
117$hosts->close();
118HOST:
119while (<$config_hosts>) {
120    # Trim out hosts that were hidden by their home testbeds
121    for my $h (@hide) {
122        next HOST if /^\d+\.\d+\.\d+\.\d+\s+$h-/;
123    }
124    print $new_hosts $_;
125}
126print $new_hosts "\n";
127$config_hosts->close();
128
129# Add gateway aliases
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) { 
136        # We have an IP.  Make a hosts entry for the key and the key plus the
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";
142    }
143    else { print $new_hosts "# Can't get ip for $lname\n"; }
144}
145$new_hosts->close();
146copy("/tmp/hosts", "/etc/hosts");
147
148
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.
151system("$perl -I/usr/local/federation/lib " . 
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}
165
166
167if ($services{'userconfig'}) {
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();
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");
187        $smb_type = "smbfs";
188    }
189    elsif ($uname =~ /Linux/ ) {
190        # Pass individual filestems to Linux umount.  No -A.
191        my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n";
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 " . 
205        "$smbuser $smbproject $smb_type");
206}
207
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
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.