source: fedkit/federate.pl @ 73e0a61

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

PG works (w/o routing)

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