ops-install.in 9.58 KB
Newer Older
1
2
3
4
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2003 University of Utah and the Flux Group.
6
7
8
9
10
11
12
13
14
15
# All rights reserved.
#

#
# install-ops.sh - Script to do the initial install of an ops node
#
# The main things it does not do yet:
# * Figure out where to put directories such as /users /proj - they must
#   already exist
# * Fill out mailing list files - presumably, it's easier to just get the
16
#   User to edit them himself
17
18
19
20
21
22
23
#

#
# Configure variables
#
my $PREFIX = '@prefix@';

Robert Ricci's avatar
Robert Ricci committed
24
25
26
27
my @MAILING_LISTS = ("@TBOPSEMAIL@","@TBLOGSEMAIL@","@TBWWWEMAIL@",
    "@TBAPPROVALEMAIL@","@TBLOGSEMAIL@","@TBAUDITEMAIL@","@TBSTATEDEMAIL@",
    "@TBTESTSUITEEMAIL@");

28
29
30
31

my $OURDOMAIN = '@OURDOMAIN@';

my $USERNODE = '@USERNODE@';
Robert Ricci's avatar
Robert Ricci committed
32
my $FSNODE =   '@FSNODE@';
33
34
35
my $BOSSNODE = '@BOSSNODE@';

#
Robert Ricci's avatar
Robert Ricci committed
36
# Allow this to work if the library is left in the source directory
37
#
Robert Ricci's avatar
Robert Ricci committed
38
39
40
41
use lib '@srcdir@';
   
use English;
use libinstall;
42
43

#
Robert Ricci's avatar
Robert Ricci committed
44
# Some programs we use
45
#
Robert Ricci's avatar
Robert Ricci committed
46
47
48
49
my $CHGRP      = "/usr/bin/chgrp";
my $CHMOD      = "/bin/chmod";
my $PW         = "/usr/sbin/pw";
my $NEWALIASES = "/usr/bin/newaliases";
50
51
52
my $SH         = "/bin/sh";
my $PKG_INFO   = "/usr/sbin/pkg_info";
my $PWD        = "/bin/pwd";
53
54

#
Robert Ricci's avatar
Robert Ricci committed
55
# Some files we edit/create
56
#
Robert Ricci's avatar
Robert Ricci committed
57
my $RCCONF          = "/etc/rc.conf";
Robert Ricci's avatar
Robert Ricci committed
58
59
my $RCLOCAL         = "/etc/rc.local";
my $RCCAPTURE       = "$PREFIX/etc/rc.capture";
Robert Ricci's avatar
Robert Ricci committed
60
61
62
63
64
65
66
my $LOCAL_HOSTNAMES = "/etc/mail/local-host-names";
my $ALIASES_FILE    = "/etc/mail/aliases";
my $EXPORTS_FILE    = "/etc/exports";
my $EXPORTS_HEAD    = "$EXPORTS_FILE.head";
my $SYSLOG_CONF     = "/etc/syslog.conf";
my $NEWSYSLOG_CONF  = "/etc/newsyslog.conf";
my $SUDOERS         = "/usr/local/etc/sudoers";
67
my $SSHD_CONFIG     = "/etc/ssh/sshd_config";
68
69

#
Robert Ricci's avatar
Robert Ricci committed
70
# Some directories we care about
71
#
Robert Ricci's avatar
Robert Ricci committed
72
73
my $LIST_DIR   = "/etc/mail/lists";
my $TIPLOG_DIR = "/var/log/tiplogs";
74
my $PORTSDIR   = "/usr/ports/misc";
75
my $SRCDIR     = '@srcdir@';
76
77

#
Robert Ricci's avatar
Robert Ricci committed
78
# And some lists that we use
79
#
Robert Ricci's avatar
Robert Ricci committed
80
81
82
83
my @LOCAL_HOSTS       = ($OURDOMAIN,$BOSSNODE,$USERNODE,$FSNODE);
my @LOGFILES          = ("/var/log/logins","/var/log/tiplogs/capture.log",
    "/var/log/mountd.log");
my @MAILING_LIST_NAMES = map { /^([\w-]+)\@/ } @MAILING_LISTS;
Robert Ricci's avatar
Robert Ricci committed
84
85
86

my @TESTBED_DIRS       = ([$PREFIX, "0775"], ["/users", "0755"],
    ["/proj", "0755"], ["/groups", "0755"], ["/share", "0775"]);
87
88

#
Robert Ricci's avatar
Robert Ricci committed
89
# Make sure they know what they're getting into...
90
#
Robert Ricci's avatar
Robert Ricci committed
91
92
93
94
print STDERR "WARNING: This script is ONLY intended to be run on a machine\n";
print STDERR "that is being set up as a dedicated ops node. Continue? [y/N] ";
my $response = <>;
die "Installation aborted!\n" unless ($response =~ /^y/i);
95

Robert Ricci's avatar
Robert Ricci committed
96
97
if ($UID != 0) {
    die "This script must be run as root.\n";
98
99
100
}

#
Robert Ricci's avatar
Robert Ricci committed
101
# The phases are fairly self-explanatory
102
103
#

Robert Ricci's avatar
Robert Ricci committed
104
105
106
Phase "groups", "Creating admin group", sub {
    if (getgrnam("tbadmin")) {
	PhaseSkip("tbadmin group already exists");
107
    }
Robert Ricci's avatar
Robert Ricci committed
108
109
110
111
    ExecQuietFatal("$PW groupadd tbadmin -g 101");
};

Phase "dirs", "Setting directory permissions", sub {
Robert Ricci's avatar
Robert Ricci committed
112
113
    foreach my $dirref (@TESTBED_DIRS) {
	my ($dir, $newmode) = @$dirref;
Robert Ricci's avatar
Robert Ricci committed
114
115
116
117
	Phase $dir, $dir, sub {
	    if (!-d $dir) {
		PhaseFail("Directory $dir does not exist");
	    }
Robert Ricci's avatar
Robert Ricci committed
118
119
120
121
	    # Use the real path, to avoid symlink problems
	    my $realdir = `realpath $dir`;
	    chomp $realdir;
	    my ($mode,$group) = (stat($realdir))[2,5];
Robert Ricci's avatar
Robert Ricci committed
122
123
	    # Fix up the mode (strip file type)
	    $mode = $mode & 0777;
Robert Ricci's avatar
Robert Ricci committed
124
	    if ($mode == eval $newmode && $group eq getgrnam("tbadmin")) {
Robert Ricci's avatar
Robert Ricci committed
125
126
		PhaseSkip("Already done");
	    }
Robert Ricci's avatar
Robert Ricci committed
127
128
	    ExecQuietFatal("$CHGRP tbadmin $realdir");
	    ExecQuietFatal("$CHMOD $newmode $realdir");
Robert Ricci's avatar
Robert Ricci committed
129
	};
130
    }
Robert Ricci's avatar
Robert Ricci committed
131
};
132

133
134
135
136
137
138
Phase "ports", "Installing ports", sub {
    Phase "pcopy", "Copying ports into place", sub {
	DoneIfExists("$PORTSDIR/emulab-ops");
	ExecQuietFatal("$SH $SRCDIR/ports/ports-install");
    };
    Phase "pinstall", "Installing ports (may take a while)", sub {
Robert Ricci's avatar
Robert Ricci committed
139
	if (!ExecQuiet("$PKG_INFO -e emulab-ops-1.1")) {
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	    PhaseSkip("Ports already installed");
	}

	#
	# This port is dead-simple, so it's safe to do it from this script
	#
	my $pwd = `$PWD`;
	chomp $pwd;
	chdir "$PORTSDIR/emulab-ops" or
		PhaseFail "Unable to change to $PORTSDIR/emulab-ops: $!";
	ExecQuietFatal("make install");
	chdir $pwd;
    };
};

Robert Ricci's avatar
Robert Ricci committed
155
156
157
Phase "rc.conf", "Adding testbed content to rc.conf", sub {
    DoneIfEdited($RCCONF);
    AppendToFileFatal($RCCONF,
Robert Ricci's avatar
Robert Ricci committed
158
159
160
161
		      qq|sendmail_enable="YES"|,
		      qq|nfs_server_enable="YES"|,
		      qq|nfs_server_flags="-u -t -n 16"|,
		      qq|syslogd_flags=""|);
Robert Ricci's avatar
Robert Ricci committed
162
};
163

Robert Ricci's avatar
Robert Ricci committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
Phase "sendmail","Configuring sendmail", sub {
    Phase "localhosts", "Setting up $LOCAL_HOSTNAMES", sub {
	DoneIfExists($LOCAL_HOSTNAMES);
	CreateFileFatal($LOCAL_HOSTNAMES,@LOCAL_HOSTS);
    };
    Phase "maillists", "Setting up mailing lists", sub {
	Phase "listdir", "Creating $LIST_DIR", sub { 
	    DoneIfExists($LIST_DIR);
	    mkdir($LIST_DIR,0755) or
		PhaseFail("Unable to create $LIST_DIR: $!");
	};
	Phase "listfiles", "Creating mailing list files", sub {
	    foreach my $list (@MAILING_LIST_NAMES) {
		Phase $list, $list, sub {
		    DoneIfExists("$LIST_DIR/$list");
		    CreateFileFatal("$LIST_DIR/$list");
		};
	    }
	};
	Phase "aliases", "Adding lists to $ALIASES_FILE", sub {
	    DoneIfEdited($ALIASES_FILE);
	    AppendToFileFatal($ALIASES_FILE,
		map("$_:\t:include:$LIST_DIR/$_",@MAILING_LIST_NAMES));
	};
	Phase "newaliases", "Running newaliases", sub {
	    PhaseSkip("No new aliases") unless @MAILING_LISTS;
	    PhaseSkip("No new aliases") if PhaseWasSkipped("aliases");
	    ExecQuietFatal($NEWALIASES);
	};
    };
};


Phase "exports", "Setting up exports", sub {
    Phase "ex.head", "Creating $EXPORTS_HEAD", sub {
	DoneIfExists($EXPORTS_HEAD);

	#
	# Figure out which of these directories are on the same
	# filesystems
	#
Robert Ricci's avatar
Robert Ricci committed
205
	my @dirs = ('/users','/groups','/proj','/share','/var');
Robert Ricci's avatar
Robert Ricci committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
	@dirs = map {`realpath $_`} @dirs;
	chomp @dirs;
	my %filesystems;
	foreach my $dir (@dirs) {
	    my ($dev,@junk) = stat $dir;
	    push @{$filesystems{$dev}}, $dir;
	}

	#
	# Use that knowledge to create lines for /etc/exports.head
	#
	my @exports_lines;
	foreach my $key (keys %filesystems) {
	    push @exports_lines,
Robert Ricci's avatar
Robert Ricci committed
220
		join(" ",@{$filesystems{$key}}) . "\t$BOSSNODE -maproot=root";
Robert Ricci's avatar
Robert Ricci committed
221
222
223
224
225
226
227
228
229
	}

	#
	# Put them in exports.head, and copy that to /etc/exports
	#
	CreateFileFatal($EXPORTS_HEAD, @exports_lines);
	ExecQuietFatal("cp $EXPORTS_HEAD $EXPORTS_FILE");
    };

230
    # XXX Newhup
Robert Ricci's avatar
Robert Ricci committed
231
232
233
234
235
236
    Phase "mountd", "HUPing mountd", sub {
	PhaseSkip("No new exports file") if PhaseWasSkipped("ex.head");
	PhaseSkip("mountd not running") unless `ps -auxw | grep mountd | grep -v grep`;
	ExecQuietFatal("killall -HUP mountd");
    };
};
237
238
239
240

#
# Set up syslog
#
Robert Ricci's avatar
Robert Ricci committed
241
242
243
244
245
246
247
248
249
250
251
252
253
Phase "syslog", "Setting up syslog", sub {
    Phase "sysconf", "Editing $SYSLOG_CONF", sub {
	DoneIfEdited($SYSLOG_CONF);
	
	#
	# Can't just append to this file, unfortunately. Have to put some of
	# the lines in the middle of the file
	#
	open(SC,"+<$SYSLOG_CONF") or
	    PhaseFail("Unable to open $SYSLOG_CONF : $!");
	my @sc = <SC>;
	if (scalar(grep(/^cron/, @sc)) != 1) {
	    PhaseFail("Unable to find marker in /etc/syslog.conf!");
254
255
	}

Robert Ricci's avatar
Robert Ricci committed
256
257
258
259
260
	#
	# Clobber and re-write
	#
	seek(SC,0,0);
	truncate(SC,0);
261

Robert Ricci's avatar
Robert Ricci committed
262
263
264
265
266
267
268
269
270
271
272
	#
	# Find the cron line, after which we place our auth.info line
	#
	foreach my $line (@sc) {
	    print SC $line;
	    if ($line =~ /^cron/) {
		print SC "# " . MAGIC_TESTBED_START . "\n";
		print SC "auth.info\t\t\t\t\t/var/log/logins\n";
		print SC "# " . MAGIC_TESTBED_END . "\n";
	    }
	}
273

Robert Ricci's avatar
Robert Ricci committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
	#
	# Put a few more lines at the end
	#
	print SC "# " . MAGIC_TESTBED_START . "\n";
	print SC "!capture\n";
	print SC "*.*\t\t\t\t\t\t/var/log/tiplogs/capture.log\n";
	print SC "!mountd\n";
	print SC "*.*\t\t\t\t\t\t/var/log/mountd.log\n";
	print SC "# " . MAGIC_TESTBED_END . "\n";
	close SC;
    };

    Phase "tiplog", "Creating $TIPLOG_DIR", sub {
	DoneIfExists($TIPLOG_DIR);
	mkdir($TIPLOG_DIR,0755) or PhaseFail("Unable to make $TIPLOG_DIR : $!");
    };
    
    Phase "logfiles", "Creating log files", sub {
	foreach my $logfile (@LOGFILES) {
	    Phase $logfile, $logfile, sub {
		DoneIfExists($logfile);
		CreateFileFatal($logfile);
296
		ExecQuietFatal("$CHGRP tbadmin $logfile");
Robert Ricci's avatar
Robert Ricci committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
		ExecQuietFatal("$CHMOD 640 $logfile");
	    };
	}
    };

    Phase "newsyslog", "Setting up $NEWSYSLOG_CONF", sub {
	DoneIfEdited($NEWSYSLOG_CONF);
	AppendToFileFatal($NEWSYSLOG_CONF,
	    "/var/log/logins\t\t\t\t640  7     200 *      Z",
	    "/var/log/mountd.log\t\t\t640  5     200 *      Z",
	    "/var/log/tiplogs/capture.log\t\t644  7     *    168   Z");
    };
};

Phase "sudoers", "Editing $SUDOERS", sub {
    DoneIfEdited($SUDOERS);
    AppendToFileFatal($SUDOERS,"%wheel    ALL=(ALL) NOPASSWD: ALL");
};
315

Robert Ricci's avatar
Robert Ricci committed
316
Phase "ssh", "Allowing root ssh", sub {
317
    DoneIfEdited($SSHD_CONFIG);
Robert Ricci's avatar
Robert Ricci committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
    AppendToFileFatal($SSHD_CONFIG,"PermitRootLogin yes");
};

Phase "capture", "Setting up capture", sub {
    Phase "rc.local", "Creating $RCLOCAL", sub {
	DoneIfExists($RCLOCAL);
	CreateFileFatal($RCLOCAL,
	    "if [ -f /etc/defaults/rc.conf ]; then",
	    "\t. /etc/defaults/rc.conf",
	    "fi",
	    "",
	    "if [ -x $RCCAPTURE ]; then",
	    "\techo -n \" capture\"",
	    "\t$RCCAPTURE",
	    "fi");
    };
    Phase "etc", "Creating $PREFIX/etc", sub {
	DoneIfExists("$PREFIX/etc");
	mkdir("$PREFIX/etc",0755) or
	    PhaseFail("Unable to create $PREFIX/etc: $!");
    };
    Phase "rc.capture", "Creating empty $RCCAPTURE", sub {
	DoneIfExists($RCCAPTURE);
	CreateFileFatal($RCCAPTURE,"#!/bin/sh");
	ExecQuietFatal("$CHMOD a+rx $RCCAPTURE");
    };
344
345
};

346
347
348
print "----------------------------------------------------------------------\n";
print "Installation completed succesfully!\n";
print "Please reboot this machine before proceeding with boss setup\n";
Robert Ricci's avatar
Robert Ricci committed
349
if (!PhaseWasSkipped("maillists")) {
350
    print "Local mailing lists have been created, with no members, in\n";
Robert Ricci's avatar
Robert Ricci committed
351
352
    print "$LIST_DIR . Please add members to the following lists:\n";
    print map "$_\n", @MAILING_LISTS;
353
}