source: fedkit/federate.pl @ 9bde415

compt_changesinfo-ops
Last change on this file since 9bde415 was 3df9b33, checked in by Ted Faber <faber@…>, 13 years ago

fedd-generated SEER certs and distribution (initial implementation,
untested) addresses #33

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