1 | #! /usr/bin/perl |
---|
2 | |
---|
3 | |
---|
4 | use strict; |
---|
5 | |
---|
6 | use IO::File; |
---|
7 | use IO::Pipe; |
---|
8 | use File::Copy; |
---|
9 | |
---|
10 | use gateway_lib; |
---|
11 | |
---|
12 | chdir("/tmp"); |
---|
13 | |
---|
14 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
15 | my $tmcc_p = new IO::Pipe() || die "Can't open pipe: $!\n"; |
---|
16 | my $shared_config_dir; |
---|
17 | my $local_config_dir = "/usr/local/federation/etc"; |
---|
18 | my %services; |
---|
19 | my $perl; |
---|
20 | |
---|
21 | my $gateway; |
---|
22 | my $smbshare = "USERS"; |
---|
23 | my $smbuser; |
---|
24 | my $smbproject; |
---|
25 | chomp (my $uname = `uname`); |
---|
26 | my $smbmount = "smbmount.$uname.pl"; |
---|
27 | |
---|
28 | # find perl |
---|
29 | for my $p ("/usr/bin/perl", "/usr/local/bin/perl") { |
---|
30 | if ( -x $p ) { |
---|
31 | $perl = $p; |
---|
32 | last; |
---|
33 | } |
---|
34 | } |
---|
35 | $perl = "perl" unless $perl; |
---|
36 | |
---|
37 | |
---|
38 | $tmcc_p->reader("$TMCC -b status"); |
---|
39 | while (<$tmcc_p>) { |
---|
40 | /ALLOCATED=([^\/]+)\/(\S+)/ && do { |
---|
41 | $shared_config_dir = "/proj/$1/exp/$2/tmp"; |
---|
42 | last; |
---|
43 | }; |
---|
44 | } |
---|
45 | $tmcc_p->close(); |
---|
46 | |
---|
47 | mkdir($local_config_dir); |
---|
48 | |
---|
49 | foreach my $fn ("seer.conf", "client.conf", "userconf") { |
---|
50 | copy("$shared_config_dir/$fn", $local_config_dir ) |
---|
51 | if -e "$shared_config_dir/$fn"; |
---|
52 | } |
---|
53 | |
---|
54 | my $client = new IO::File("$local_config_dir/client.conf"); |
---|
55 | while (<$client>) { |
---|
56 | chomp; |
---|
57 | /ControlGateway:\s+(.*)/i && do { $gateway = $1; }; |
---|
58 | /SMBShare:\s+(.*)/i && do { $smbshare = $1; }; |
---|
59 | /ProjectUser:\s+(.*)/i && do { $smbuser = $1; }; |
---|
60 | /ProjectName:\s+(.*)/i && do { $smbproject = $1; }; |
---|
61 | /Service:\s+(.*)/i && do { $services{$1}++;}; |
---|
62 | } |
---|
63 | $client->close(); |
---|
64 | # Create the /etc/hosts file |
---|
65 | my $hosts = new IO::File("/etc/hosts") || die "Can't open /etc/hosts:$!\n"; |
---|
66 | my $new_hosts = new IO::File(">/tmp/hosts") || die "Can't open /tmp/hosts:$!\n"; |
---|
67 | my $config_hosts = new IO::File("$shared_config_dir/hosts") || |
---|
68 | die "Can't open $shared_config_dir/hosts: $!\n"; |
---|
69 | |
---|
70 | while (<$hosts>) { |
---|
71 | /^127\.0\.0\.1/ && do { print $new_hosts $_; }; |
---|
72 | } |
---|
73 | $hosts->close(); |
---|
74 | while (<$config_hosts>) { |
---|
75 | print $new_hosts $_; |
---|
76 | } |
---|
77 | $config_hosts->close(); |
---|
78 | $new_hosts->close(); |
---|
79 | copy("/tmp/hosts", "/etc/hosts"); |
---|
80 | |
---|
81 | # If there are tunnelip interfaces to bring up, bring 'em |
---|
82 | system("$perl -I/usr/local/federation/lib " . |
---|
83 | "/usr/local/federation/bin/config_from_tunnelip.pl"); |
---|
84 | |
---|
85 | |
---|
86 | if ($services{'userconfig'}) { |
---|
87 | $tmcc_p = new IO::Pipe() || die "Can't open pipe for accounts:$!\n"; |
---|
88 | my $old_accounts = new IO::File(">$local_config_dir/old_accts") || |
---|
89 | die "Can't open $local_config_dir/old_accts: $!\n"; |
---|
90 | |
---|
91 | $tmcc_p->reader("$TMCC -b accounts"); |
---|
92 | while (<$tmcc_p>) { |
---|
93 | print $old_accounts $_; |
---|
94 | } |
---|
95 | $tmcc_p->close(); |
---|
96 | $old_accounts->close(); |
---|
97 | print("Updating accounts"); |
---|
98 | system("/usr/local/federation/bin/rc.fedaccounts"); |
---|
99 | } |
---|
100 | |
---|
101 | if ($services{'SMB'}) { |
---|
102 | if ($uname =~ /FreeBSD/ ) { |
---|
103 | system("umount -A -f -t nfs,smbfs,cifs"); |
---|
104 | # Restart ntp |
---|
105 | system("/etc/rc.d/ntpd stop; /usr/sbin/ntpdate boss; " . |
---|
106 | "/etc/rc.d/ntpd start;"); |
---|
107 | |
---|
108 | } |
---|
109 | elsif ($uname =~ /Linux/ ) { |
---|
110 | # Pass individual filestems to Linux umount. No -A. |
---|
111 | my $mtab = new IO::File("/etc/mtab") || die "Can't open /etc/mtab:$1\n"; |
---|
112 | while (<$mtab>) { |
---|
113 | chomp; |
---|
114 | my @F = split($_); |
---|
115 | next unless $F[2] =~ /(nfs|cifs|smbfs)/; |
---|
116 | system("umount -f $F[1]"); |
---|
117 | } |
---|
118 | # restart ntp |
---|
119 | system("/etc/rc.d/init.d/ntpd stop; /usr/sbin/ntpdate boss; ". |
---|
120 | "/etc/rc.d/init.d/ntpd start"); |
---|
121 | } |
---|
122 | |
---|
123 | print "Waiting for SMB server\n"; |
---|
124 | gateway_lib::wait_for_port($gateway, 139, 60*60) || |
---|
125 | die "SMB server never came up\n"; |
---|
126 | print "Mounting via SMB\n"; |
---|
127 | system("$perl /usr/local/federation/bin/$smbmount $smbshare $gateway " . |
---|
128 | "$smbuser $smbproject"); |
---|
129 | } |
---|
130 | |
---|
131 | # startcmd |
---|
132 | if ($ARGV[0] && $ARGV[1]) { |
---|
133 | if ($uname =~ /FreeBSD/) { |
---|
134 | system("su -l \"$ARGV[0]\" -c \"$ARGV[1]\""); |
---|
135 | } |
---|
136 | elsif ($uname =~ /Linux/) { |
---|
137 | system("su \"$ARGV[0]\" --command \"$ARGV[1]\""); |
---|
138 | } |
---|
139 | } |
---|
140 | exit(0); |
---|