source: fedkit/federate.pl @ 9b3627e

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

SEER support

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