source: fedkit/federate.pl @ f8fa72b

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

More ProtoGENI accomodation

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