source: fedkit/federate.pl @ 5ae9d94

compt_changes
Last change on this file since 5ae9d94 was 5ae9d94, checked in by Ted Faber <faber@…>, 12 years ago

Get remote mounting working on linux

  • Property mode set to 100644
File size: 6.7 KB
RevLine 
[6d985c0]1#! /usr/bin/perl
2
3
4use strict;
5
[f8fa72b]6use Getopt::Long;
7
[6d985c0]8use IO::File;
9use IO::Pipe;
10use File::Copy;
11
[cc0ffd2]12use Net::hostent;
13use Socket;
14
[6d985c0]15use gateway_lib;
16
[5ae9d94]17# rc.fedaccounts is going to send a TERM to any processes running as an old
18# user, so insulate this (and children) process from propagated signals.
19$SIG{'TERM'} = 'IGNORE';
20
[6d985c0]21chdir("/tmp");
22
23my $TMCC = "/usr/local/etc/emulab/tmcc";
[dbc9144]24my $RC_ROUTE = "/usr/local/etc/emulab/rc/rc.route";
[6d985c0]25my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n";
26my $shared_config_dir;
[3df9b33]27my $shared_seer_auth_dir;
[6d985c0]28my $local_config_dir = "/usr/local/federation/etc";
29my %services;
[d56b168]30my %aliases;
[d38ff56]31my %added;
[8209faa]32my @hide;
[6d985c0]33my $perl;
34
35my $gateway;
36my $smbshare = "USERS";
37my $smbuser;
38my $smbproject;
[cc0ffd2]39my $exp;
40my $proj;
[f8fa72b]41my $install_smb;
[73e0a61]42my $smb_type = 'cifs';
[6d985c0]43chomp (my $uname = `uname`);
44my $smbmount = "smbmount.$uname.pl";
45
[f8fa72b]46GetOptions("install_samba" => \$install_smb);
47
[6d985c0]48# find perl
49for my $p ("/usr/bin/perl", "/usr/local/bin/perl") {
50    if ( -x $p ) {
51        $perl = $p;
52        last;
53    }
54}
55$perl = "perl" unless $perl;
56
[5ae9d94]57if (!-x '/sbin/mount.cifs' ) {
58    if ( -x '/usr/bin/yum' ) {
59        # Install samba
60        system('/usr/bin/yum -y install samba-client');
61        system('/usr/bin/yum -y install cifs-utils');
62        # These tools expect the fstab to include cifs
63        $smb_type = 'cifs';
64    }
65    elsif (-x '/usr/bin/apt-get') {
66        # Install samba
67        system('/usr/bin/apt-get -y install samba-client');
68        system('/usr/bin/apt-get -y install smbfs');
69        # These tools expect the fstab to include cifs
70        $smb_type = 'cifs';
71    }
[6d985c0]72}
73
[f8fa72b]74if (!-e "$local_config_dir/client.conf" ) {
75    $tmcc_p->reader("$TMCC -b status");
76    while (<$tmcc_p>) {
77        /ALLOCATED=([^\/]+)\/(\S+)/ && do {
78            ($proj, $exp) = ($1, $2);
79            $shared_config_dir = "/proj/$proj/exp/$exp/tmp";
[3df9b33]80            $shared_seer_auth_dir = "/proj/$proj/exp/$exp/tbdata";
[f8fa72b]81            last;
82        };
83    }
84    $tmcc_p->close();
85
86    mkdir($local_config_dir);
[6d985c0]87
[3df9b33]88    foreach my $fn ("seer.conf", "client.conf", "userconf", "hosts",
89            "ca.pem", "node.pem") {
[f8fa72b]90        copy("$shared_config_dir/$fn", $local_config_dir )
91            if -e "$shared_config_dir/$fn";
92    }
[3df9b33]93
94    # Copy seer authorization files into the location that standard SEER
95    # invocations will look.  The above loop puts them where -F invocations
96    # will look.
97    foreach my $fn ("ca.pem", "node.pem") {
98        copy("$shared_config_dir/$fn", $shared_seer_auth_dir )
99            if -e "$shared_config_dir/$fn" && -d $shared_seer_auth_dir;
100    }
[6d985c0]101}
102
103my $client = new IO::File("$local_config_dir/client.conf");
104while (<$client>) {
105    chomp;
106    /ControlGateway:\s+(.*)/i && do { $gateway = $1; };
107    /SMBShare:\s+(.*)/i && do { $smbshare = $1; };
108    /ProjectUser:\s+(.*)/i && do { $smbuser = $1; };
109    /ProjectName:\s+(.*)/i && do { $smbproject = $1; };
110    /Service:\s+(.*)/i && do { $services{$1}++;};
[d56b168]111    /PortalAlias:\s+(.*)/i && do { $aliases{$1}++;};
[d38ff56]112    /AddedNode:\s+(.*)/i && do { $added{$1}++; };
[8209faa]113    /Hide:\s+(.*)/i && do { push(@hide, split(",", $1));};
[6d985c0]114}
115$client->close();
116# Create the /etc/hosts file
117my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n";
118my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n";
[f8fa72b]119my $config_hosts = new IO::File("$local_config_dir/hosts") || 
120    die "Can't open $local_config_dir/hosts: $!\n";
[cc0ffd2]121my $has_control = 0;
[6d985c0]122
123while (<$hosts>) {
124    /^127\.0\.0\.1/ && do { print $new_hosts $_; };
[d56b168]125    # If aliases conflict with existing nodes, delete the alias
126    for my $n (split($_)) {
127        chomp $n;
128        delete $aliases{$n} if $aliases{$n};
129    }
[6d985c0]130}
131$hosts->close();
[1962a5b]132HOST:
[6d985c0]133while (<$config_hosts>) {
[8209faa]134    # Trim out hosts that were hidden by their home testbeds
135    for my $h (@hide) {
[1962a5b]136        next HOST if /^\d+\.\d+\.\d+\.\d+\s+$h-/;
[8209faa]137    }
[6d985c0]138    print $new_hosts $_;
139}
[d38ff56]140print $new_hosts "\n";
[6d985c0]141$config_hosts->close();
[cc0ffd2]142
[d56b168]143# Add gateway aliases
[d38ff56]144for my $k (keys %aliases) {
145    # If we added a node, it's a node without a local address.  Bind the name
146    # to the IP in /etc/hosts.  If we didn't add a node, it's the gateway node.
147    (my $lname = $gateway) =~ s/^[^\.]+/$k/;
148    my $ip = gateway_lib::get_ip($added{$k} ? $lname : $gateway);
149    if ($ip) { 
[8209faa]150        # We have an IP.  Make a hosts entry for the key and the key plus the
[d38ff56]151        # first two subdomains (which is an emulab setup)
152        my @x = split(/\./, $lname);
153        if (@x > 3 ) { splice(@x, 3); }
154        my $out = join(".", @x);
155        print $new_hosts "$ip\t$out $k\n";
[cc0ffd2]156    }
[d38ff56]157    else { print $new_hosts "# Can't get ip for $lname\n"; }
[cc0ffd2]158}
[6d985c0]159$new_hosts->close();
160copy("/tmp/hosts", "/etc/hosts");
161
[dbc9144]162
[dc803a7]163# If there are tunnelip interfaces to bring up, bring 'em up.  Record any such
164# interfaces in /usr/local/federation/interfaces, so SEER can find them later.
[55779d4]165system("$perl -I/usr/local/federation/lib " . 
[dc803a7]166    "/usr/local/federation/bin/config_from_tunnelip.pl " . 
167    "--record=/usr/local/federation/etc/interfaces");
168
169if ($uname =~ /Linux/ ) {
170    system("$perl /usr/local/federation/bin/gated_routing.pl")
171        if -r "/usr/local/federation/bin/gated_routing.pl";
172}
173elsif ($uname =~/FreeBSD/ ) {
174    # FreeBSD needs to have ospfs installed and a router config created and
175    # run.
176    system("$perl /usr/local/federation/bin/ospf_routing.pl")
177        if -r "/usr/local/federation/bin/ospf_routing.pl";
178}
[55779d4]179
[6d985c0]180
181if ($services{'userconfig'}) {
[f8fa72b]182    if (!-e "$local_config_dir/old_accts") {
183        $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n";
184        my $old_accounts = new IO::File(">$local_config_dir/old_accts") || 
185            die "Can't open $local_config_dir/old_accts: $!\n";
186
187        $tmcc_p->reader("$TMCC -b accounts");
188        while (<$tmcc_p>) {
189            print $old_accounts $_;
190        }
191        $tmcc_p->close();
192        $old_accounts->close();
[6d985c0]193    }
194    print("Updating accounts");
195    system("/usr/local/federation/bin/rc.fedaccounts");
196}
197
198if ($services{'SMB'}) {
199    if ($uname =~ /FreeBSD/ ) {
200        system("umount -A -f -t nfs,smbfs,cifs");
[73e0a61]201        $smb_type = "smbfs";
[6d985c0]202    }
203    elsif ($uname =~ /Linux/ ) {
204        # Pass individual filestems to Linux umount.  No -A.
[73e0a61]205        my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$!\n";
[6d985c0]206        while (<$mtab>) {
207            chomp;
208            my @F = split($_);
209            next unless $F[2] =~ /(nfs|cifs|smbfs)/;
210            system("umount -f $F[1]");
211        }
212    }
213
214    print "Waiting for SMB server\n";
215    gateway_lib::wait_for_port($gateway, 139, 60*60) || 
216        die "SMB server never came up\n";
217    print "Mounting via SMB\n";
218    system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . 
[73e0a61]219        "$smbuser $smbproject $smb_type");
[6d985c0]220}
221
[37f6592]222if ($uname =~ /FreeBSD/ ) {
223    # Restart ntp
224    system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . 
225        "/etc/rc.d/ntpd start;");
226                                               
227}
228elsif ($uname =~ /Linux/ ) {
229    # restart ntp
230    system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". 
231        "/etc/rc.d/init.d/ntpd start");
232}
233
[6d985c0]234# startcmd
235if ($ARGV[0] && $ARGV[1]) {
236    if ($uname =~ /FreeBSD/) {
237        system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\"");
238    }
239    elsif ($uname =~ /Linux/) {
240        system("su \"$ARGV[0]\" --command \"$ARGV[1]\"");
241    }
242}
243exit(0);
Note: See TracBrowser for help on using the repository browser.