source: fedkit/federate.pl @ cc0ffd2

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

Missed a newline

  • Property mode set to 100644
File size: 4.3 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        # Restart ntp
132        system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . 
133            "/etc/rc.d/ntpd start;");
134                                                   
135    }
136    elsif ($uname =~ /Linux/ ) {
137        # Pass individual filestems to Linux umount.  No -A.
138        my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$1\n";
139        while (<$mtab>) {
140            chomp;
141            my @F = split($_);
142            next unless $F[2] =~ /(nfs|cifs|smbfs)/;
143            system("umount -f $F[1]");
144        }
145        # restart ntp
146        system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". 
147            "/etc/rc.d/init.d/ntpd start");
148    }
149
150    print "Waiting for SMB server\n";
151    gateway_lib::wait_for_port($gateway, 139, 60*60) || 
152        die "SMB server never came up\n";
153    print "Mounting via SMB\n";
154    system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . 
155        "$smbuser $smbproject");
156}
157
158# startcmd
159if ($ARGV[0] && $ARGV[1]) {
160    if ($uname =~ /FreeBSD/) {
161        system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\"");
162    }
163    elsif ($uname =~ /Linux/) {
164        system("su \"$ARGV[0]\" --command \"$ARGV[1]\"");
165    }
166}
167exit(0);
Note: See TracBrowser for help on using the repository browser.