source: fedkit/federate.pl @ 0de1b94

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

hiding IP addresses. Committed to transfer. Untested.

  • Property mode set to 100644
File size: 6.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 %aliases;
26my %added;
27my @hide;
28my $perl;
29
30my $gateway;
31my $smbshare = "USERS";
32my $smbuser;
33my $smbproject;
34my $exp;
35my $proj;
36my $install_smb;
37my $smb_type = 'cifs';
38chomp (my $uname = `uname`);
39my $smbmount = "smbmount.$uname.pl";
40
41GetOptions("install_samba" => \$install_smb);
42
43# find perl
44for my $p ("/usr/bin/perl", "/usr/local/bin/perl") {
45    if ( -x $p ) {
46        $perl = $p;
47        last;
48    }
49}
50$perl = "perl" unless $perl;
51
52if ($install_smb) {
53    # fix yum.conf
54    copy("/etc/yum.conf", "/etc/yum.conf.bak");
55    my $from = new IO::File("/etc/yum.conf.bak");
56    my $to = new IO::File(">/etc/yum.conf");
57    while (<$from>) {
58        s/download.fedoralegacy.org/fedoralegacy.lsu.edu/g;
59        print $to $_;
60    }
61    $from->close();
62    $to->close();
63    # Now, samba.  Because of the python dance on PG, we need to call python2.2
64    # explicitly
65    system('/usr/bin/python2.2 /usr/bin/yum -y install samba-client');
66    # These tools expect the fstab to include smbfs instead
67    $smb_type = 'smbfs';
68}
69
70if (!-e "$local_config_dir/client.conf" ) {
71    $tmcc_p->reader("$TMCC -b status");
72    while (<$tmcc_p>) {
73        /ALLOCATED=([^\/]+)\/(\S+)/ && do {
74            ($proj, $exp) = ($1, $2);
75            $shared_config_dir = "/proj/$proj/exp/$exp/tmp";
76            last;
77        };
78    }
79    $tmcc_p->close();
80
81    mkdir($local_config_dir);
82
83    foreach my $fn ("seer.conf", "client.conf", "userconf", "hosts") {
84        copy("$shared_config_dir/$fn", $local_config_dir )
85            if -e "$shared_config_dir/$fn";
86    }
87}
88
89my $client = new IO::File("$local_config_dir/client.conf");
90while (<$client>) {
91    chomp;
92    /ControlGateway:\s+(.*)/i && do { $gateway = $1; };
93    /SMBShare:\s+(.*)/i && do { $smbshare = $1; };
94    /ProjectUser:\s+(.*)/i && do { $smbuser = $1; };
95    /ProjectName:\s+(.*)/i && do { $smbproject = $1; };
96    /Service:\s+(.*)/i && do { $services{$1}++;};
97    /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;};
98    /AddedNode:\s+(.*)/i && do { $added{$1}++; };
99    /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));};
100}
101$client->close();
102# Create the /etc/hosts file
103my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n";
104my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n";
105my $config_hosts = new IO::File("$local_config_dir/hosts") || 
106    die "Can't open $local_config_dir/hosts: $!\n";
107my $has_control = 0;
108
109while (<$hosts>) {
110    /^127\.0\.0\.1/ && do { print $new_hosts $_; };
111    # If aliases conflict with existing nodes, delete the alias
112    for my $n (split($_)) {
113        chomp $n;
114        delete $aliases{$n} if $aliases{$n};
115    }
116}
117$hosts->close();
118while (<$config_hosts>) {
119    # Trim out hosts that were hidden by their home testbeds
120    for my $h (@hide) {
121        next if /^\d+\.\d+\.\d+\.\d+\s+$h-/;
122    }
123    print $new_hosts $_;
124}
125print $new_hosts "\n";
126$config_hosts->close();
127
128# Add gateway aliases
129for my $k (keys %aliases) {
130    # If we added a node, it's a node without a local address.  Bind the name
131    # to the IP in /etc/hosts.  If we didn't add a node, it's the gateway node.
132    (my $lname = $gateway) =~ s/^[^\.]+/$k/;
133    my $ip = gateway_lib::get_ip($added{$k} ? $lname : $gateway);
134    if ($ip) { 
135        # We have an IP.  Make a hosts entry for the key and the key plus the
136        # first two subdomains (which is an emulab setup)
137        my @x = split(/\./, $lname);
138        if (@x > 3 ) { splice(@x, 3); }
139        my $out = join(".", @x);
140        print $new_hosts "$ip\t$out $k\n";
141    }
142    else { print $new_hosts "# Can't get ip for $lname\n"; }
143}
144$new_hosts->close();
145copy("/tmp/hosts", "/etc/hosts");
146
147
148# If there are tunnelip interfaces to bring up, bring 'em up.  Record any such
149# interfaces in /usr/local/federation/interfaces, so SEER can find them later.
150system("$perl -I/usr/local/federation/lib " . 
151    "/usr/local/federation/bin/config_from_tunnelip.pl " . 
152    "--record=/usr/local/federation/etc/interfaces");
153
154if ($uname =~ /Linux/ ) {
155    system("$perl /usr/local/federation/bin/gated_routing.pl")
156        if -r "/usr/local/federation/bin/gated_routing.pl";
157}
158elsif ($uname =~/FreeBSD/ ) {
159    # FreeBSD needs to have ospfs installed and a router config created and
160    # run.
161    system("$perl /usr/local/federation/bin/ospf_routing.pl")
162        if -r "/usr/local/federation/bin/ospf_routing.pl";
163}
164
165
166if ($services{'userconfig'}) {
167    if (!-e "$local_config_dir/old_accts") {
168        $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n";
169        my $old_accounts = new IO::File(">$local_config_dir/old_accts") || 
170            die "Can't open $local_config_dir/old_accts: $!\n";
171
172        $tmcc_p->reader("$TMCC -b accounts");
173        while (<$tmcc_p>) {
174            print $old_accounts $_;
175        }
176        $tmcc_p->close();
177        $old_accounts->close();
178    }
179    print("Updating accounts");
180    system("/usr/local/federation/bin/rc.fedaccounts");
181}
182
183if ($services{'SMB'}) {
184    if ($uname =~ /FreeBSD/ ) {
185        system("umount -A -f -t nfs,smbfs,cifs");
186        $smb_type = "smbfs";
187    }
188    elsif ($uname =~ /Linux/ ) {
189        # Pass individual filestems to Linux umount.  No -A.
190        my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n";
191        while (<$mtab>) {
192            chomp;
193            my @F = split($_);
194            next unless $F[2] =~ /(nfs|cifs|smbfs)/;
195            system("umount -f $F[1]");
196        }
197    }
198
199    print "Waiting for SMB server\n";
200    gateway_lib::wait_for_port($gateway, 139, 60*60) || 
201        die "SMB server never came up\n";
202    print "Mounting via SMB\n";
203    system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . 
204        "$smbuser $smbproject $smb_type");
205}
206
207if ($uname =~ /FreeBSD/ ) {
208    # Restart ntp
209    system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . 
210        "/etc/rc.d/ntpd start;");
211                                               
212}
213elsif ($uname =~ /Linux/ ) {
214    # restart ntp
215    system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". 
216        "/etc/rc.d/init.d/ntpd start");
217}
218
219# startcmd
220if ($ARGV[0] && $ARGV[1]) {
221    if ($uname =~ /FreeBSD/) {
222        system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\"");
223    }
224    elsif ($uname =~ /Linux/) {
225        system("su \"$ARGV[0]\" --command \"$ARGV[1]\"");
226    }
227}
228exit(0);
Note: See TracBrowser for help on using the repository browser.