source: fedkit/federate.pl @ 9f4b3c6

compt_changes
Last change on this file since 9f4b3c6 was 17c2f7b, checked in by Ted Faber <faber@…>, 13 years ago

Ubuntu wrouting using quagga

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