source: fedkit/federate.pl @ d38ff56

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

More flexible and useful gateway aliasing.

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