source: fedkit/federate.pl @ ccfc03d

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

Static routing

  • Property mode set to 100644
File size: 7.5 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
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
21chdir("/tmp");
22
23my $TMCC = "/usr/local/etc/emulab/tmcc";
24my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route";
25my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n";
26my $shared_config_dir;
27my $shared_seer_auth_dir;
28my $local_config_dir = "/usr/local/federation/etc";
29my %services;
30my %aliases;
31my %added;
32my @hide;
33my $perl;
34
35my $gateway;
36my $smbshare = "USERS";
37my $smbuser;
38my $smbproject;
39my $exp;
40my $proj;
41my $install_smb;
42my $smb_type = 'cifs';
43chomp (my $uname = `uname`);
44my $smbmount = "smbmount.$uname.pl";
45
46GetOptions("install_samba" => \$install_smb);
47
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
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
67        system('/usr/bin/apt-get -y update');
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    }
73}
74
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";
81            $shared_seer_auth_dir = "/proj/$proj/exp/$exp/tbdata";
82            last;
83        };
84    }
85    $tmcc_p->close();
86
87    mkdir($local_config_dir);
88
89    foreach my $fn ("seer.conf", "client.conf", "userconf", "hosts",
90            "ca.pem", "node.pem", "route.tgz") {
91        copy("$shared_config_dir/$fn", $local_config_dir )
92            if -e "$shared_config_dir/$fn";
93    }
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";
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    }
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}++;};
115    /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;};
116    /AddedNode:\s+(.*)/i && do { $added{$1}++; };
117    /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));};
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";
123my $config_hosts = new IO::File("$local_config_dir/hosts") || 
124    die "Can't open $local_config_dir/hosts: $!\n";
125my $has_control = 0;
126
127while (<$hosts>) {
128    /^127\.0\.0\.1/ && do { print $new_hosts $_; };
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    }
134}
135$hosts->close();
136HOST:
137while (<$config_hosts>) {
138    # Trim out hosts that were hidden by their home testbeds
139    for my $h (@hide) {
140        next HOST if /^\d+\.\d+\.\d+\.\d+\s+$h-/;
141    }
142    print $new_hosts $_;
143}
144print $new_hosts "\n";
145$config_hosts->close();
146
147# Add gateway aliases
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) { 
154        # We have an IP.  Make a hosts entry for the key and the key plus the
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";
160    }
161    else { print $new_hosts "# Can't get ip for $lname\n"; }
162}
163$new_hosts->close();
164copy("/tmp/hosts", "/etc/hosts");
165
166
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.
169system("$perl -I/usr/local/federation/lib " . 
170    "/usr/local/federation/bin/config_from_tunnelip.pl " . 
171    "--record=/usr/local/federation/etc/interfaces");
172
173if ($uname =~ /Linux/ ) {
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        }
180    if ($?) {
181        system("$perl /usr/local/federation/bin/quagga_routing.pl")
182            if -r "/usr/local/federation/bin/quagga_routing.pl";
183    }
184}
185elsif ($uname =~/FreeBSD/ ) {
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    }
194}
195
196
197if ($services{'userconfig'}) {
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();
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");
217        $smb_type = "smbfs";
218    }
219    elsif ($uname =~ /Linux/ ) {
220        # Pass individual filestems to Linux umount.  No -A.
221        my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n";
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 " . 
235        "$smbuser $smbproject $smb_type");
236}
237
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
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");
249    } elsif (-d "/etc/init.d" ) {
250        system("service ntp stop; /usr/sbin/ntpdate boss; ". 
251            "service ntp start");
252    }
253    else {
254        print "Cannot find ntp directories\n";
255    }
256}
257
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.