source: fedkit/federate.pl @ 37f6592

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

always do the ntp thing

  • Property mode set to 100644
File size: 4.4 KB
Line 
1#! /usr/bin/perl
2
3
4use strict;
5
6use IO::File;
7use IO::Pipe;
8use File::Copy;
9
10use Net::hostent;
11use Socket;
12
13use gateway_lib;
14
15chdir("/tmp");
16
17my $TMCC = "/usr/local/etc/emulab/tmcc";
18my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route";
19my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n";
20my $shared_config_dir;
21my $local_config_dir = "/usr/local/federation/etc";
22my %services;
23my $perl;
24
25my $gateway;
26my $smbshare = "USERS";
27my $smbuser;
28my $smbproject;
29my $exp;
30my $proj;
31chomp (my $uname = `uname`);
32my $smbmount = "smbmount.$uname.pl";
33
34# find perl
35for my $p ("/usr/bin/perl", "/usr/local/bin/perl") {
36    if ( -x $p ) {
37        $perl = $p;
38        last;
39    }
40}
41$perl = "perl" unless $perl;
42
43
44$tmcc_p->reader("$TMCC -b status");
45while (<$tmcc_p>) {
46    /ALLOCATED=([^\/]+)\/(\S+)/ && do {
47        ($proj, $exp) = ($1, $2);
48        $shared_config_dir = "/proj/$proj/exp/$exp/tmp";
49        last;
50    };
51}
52$tmcc_p->close();
53
54mkdir($local_config_dir);
55
56foreach my $fn ("seer.conf", "client.conf", "userconf") {
57    copy("$shared_config_dir/$fn", $local_config_dir )
58        if -e "$shared_config_dir/$fn";
59}
60
61my $client = new IO::File("$local_config_dir/client.conf");
62while (<$client>) {
63    chomp;
64    /ControlGateway:\s+(.*)/i && do { $gateway = $1; };
65    /SMBShare:\s+(.*)/i && do { $smbshare = $1; };
66    /ProjectUser:\s+(.*)/i && do { $smbuser = $1; };
67    /ProjectName:\s+(.*)/i && do { $smbproject = $1; };
68    /Service:\s+(.*)/i && do { $services{$1}++;};
69}
70$client->close();
71# Create the /etc/hosts file
72my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n";
73my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n";
74my $config_hosts = new IO::File("$shared_config_dir/hosts") || 
75    die "Can't open $shared_config_dir/hosts: $!\n";
76my $has_control = 0;
77
78while (<$hosts>) {
79    /^127\.0\.0\.1/ && do { print $new_hosts $_; };
80    /control\./ && $has_control++;
81}
82$hosts->close();
83while (<$config_hosts>) {
84    print $new_hosts $_;
85}
86$config_hosts->close();
87
88# If seer is enabled and no local control node in play, alias the gateway to be
89# control
90if ($services{'seer'} && !$has_control ) {
91    if ( my $hent = gethostbyname($gateway) ) {
92        my $gwip = inet_ntoa($hent->addr_list->[0]);
93
94        print $new_hosts "\n$gwip\tcontrol.$exp.$proj\n";
95    }
96}
97$new_hosts->close();
98copy("/tmp/hosts", "/etc/hosts");
99
100# Ick.  at least one of our images has gated in the wrong place (sigh).  If
101# so, fix it and restart routing. 
102if (!-x "/usr/sbin/gated" && -x "/sbin/gated" ) {
103    print "Linking to /sbin/gated and starting routing\n";
104    symlink("/sbin/gated", "/usr/sbin/gated");
105    system("$RC_ROUTE boot") if -x $RC_ROUTE;
106}
107
108# If there are tunnelip interfaces to bring up, bring 'em
109system("$perl -I/usr/local/federation/lib " . 
110    "/usr/local/federation/bin/config_from_tunnelip.pl");
111
112
113if ($services{'userconfig'}) {
114    $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n";
115    my $old_accounts = new IO::File(">$local_config_dir/old_accts") || 
116        die "Can't open $local_config_dir/old_accts: $!\n";
117
118    $tmcc_p->reader("$TMCC -b accounts");
119    while (<$tmcc_p>) {
120        print $old_accounts $_;
121    }
122    $tmcc_p->close();
123    $old_accounts->close();
124    print("Updating accounts");
125    system("/usr/local/federation/bin/rc.fedaccounts");
126}
127
128if ($services{'SMB'}) {
129    if ($uname =~ /FreeBSD/ ) {
130        system("umount -A -f -t nfs,smbfs,cifs");
131    }
132    elsif ($uname =~ /Linux/ ) {
133        # Pass individual filestems to Linux umount.  No -A.
134        my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$1\n";
135        while (<$mtab>) {
136            chomp;
137            my @F = split($_);
138            next unless $F[2] =~ /(nfs|cifs|smbfs)/;
139            system("umount -f $F[1]");
140        }
141    }
142
143    print "Waiting for SMB server\n";
144    gateway_lib::wait_for_port($gateway, 139, 60*60) || 
145        die "SMB server never came up\n";
146    print "Mounting via SMB\n";
147    system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . 
148        "$smbuser $smbproject");
149}
150
151if ($uname =~ /FreeBSD/ ) {
152    # Restart ntp
153    system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . 
154        "/etc/rc.d/ntpd start;");
155                                               
156}
157elsif ($uname =~ /Linux/ ) {
158    # restart ntp
159    system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". 
160        "/etc/rc.d/init.d/ntpd start");
161}
162
163# startcmd
164if ($ARGV[0] && $ARGV[1]) {
165    if ($uname =~ /FreeBSD/) {
166        system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\"");
167    }
168    elsif ($uname =~ /Linux/) {
169        system("su \"$ARGV[0]\" --command \"$ARGV[1]\"");
170    }
171}
172exit(0);
Note: See TracBrowser for help on using the repository browser.