source: fedkit/federate.pl @ 558d829

compt_changes
Last change on this file since 558d829 was 558d829, checked in by Ted Faber <faber@…>, 12 years ago

Better ntp start for ubuntu

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