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

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2003-2008 University of Utah and the Flux Group.
6
7
8
9
# All rights reserved.
#

#
10
# install-ops - Script to do the initial install of an ops node
11
12
13
14
15
#
# 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
my @MAILING_LISTS = ("@TBOPSEMAIL@","@TBLOGSEMAIL@","@TBWWWEMAIL@",
25
    "@TBAPPROVALEMAIL@","@TBAUDITEMAIL@","@TBSTATEDEMAIL@",
26
    "@TBTESTSUITEEMAIL@", "@TBERRORSEMAIL@", "@TBAUTOMAILEMAIL@");
Robert Ricci's avatar
Robert Ricci committed
27

28

29
30
31
32
33
34
my $OURDOMAIN   = '@OURDOMAIN@';
my $USERNODE    = '@USERNODE@';
my $FSNODE      = '@FSNODE@';
my $BOSSNODE    = '@BOSSNODE@';
my $BOSSNODE_IP = '@BOSSNODE_IP@';
my $USERNODE_IP = '@USERNODE_IP@';
35
my $FSNODE_IP   = '@FSNODE_IP@';
36
my $LOGFACIL    = '@TBLOGFACIL@';
37
my $TBOPSEMAIL  = '@TBOPSEMAIL@';
38
my $ELABINELAB  = @ELABINELAB@;
39
my $WINSUPPORT  = @WINSUPPORT@;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
40
my $MAILMANSUPPORT = @MAILMANSUPPORT@;
41
my $CVSSUPPORT  = @CVSSUPPORT@;
42
my $BUGDBSUPPORT= @BUGDBSUPPORT@;
43
my $WIKISUPPORT = @WIKISUPPORT@;
44
my $QUOTA_FSLIST= '@FS_WITH_QUOTAS@';
45
46
my $LOGDIR      = "$PREFIX/log";
my $ETCDIR      = "$PREFIX/etc";
47
my $LIBDIR      = "$PREFIX/lib";
48
my $SCRATCHDIR =  '@FSDIR_SCRATCH@';
Robert Ricci's avatar
Robert Ricci committed
49
50
51
my $OUTER_BOSS = '@OUTERBOSS_NODENAME@';
if ($OUTER_BOSS eq '')
    { $OUTER_BOSS = "www.emulab.net"; }
52
53
54
55
56
57
58
59
60

#
# Fixed paths for clients
#
my $PROJROOT    = "@PROJROOT_DIR@";
my $GROUPROOT   = "@GROUPSROOT_DIR@";
my $USERROOT    = "@USERSROOT_DIR@";
my $SCRATCHROOT	= "@SCRATCHROOT_DIR@";
my $SHAREROOT	= "@SHAREROOT_DIR@";
61
my $ELVIN_COMPAT= @ELVIN_COMPAT@;
62

63
64
65
# True if we are also the FS node
my $ISFS	= ($USERNODE eq $FSNODE) ? 1 : 0;

66
67
68
69
# For /share export below.
my $CONTROL_NETWORK = "@CONTROL_NETWORK@";
my $CONTROL_NETMASK = "@CONTROL_NETMASK@";

70
71
72
# Should be configure variable
my $TBADMINGID  = 101;

73
#
Robert Ricci's avatar
Robert Ricci committed
74
# Allow this to work if the library is left in the source directory
75
#
Robert Ricci's avatar
Robert Ricci committed
76
77
78
79
use lib '@srcdir@';
   
use English;
use libinstall;
80
81
82
83
84
85
use Getopt::Std;

#
# Handle command-line options
#
sub usage {
86
87
88
    print "Usage: ops-install [-b] [-p packagedir] [-s] " .
	"[-P ops-portname] [-F fs-portname]\n";
    print "  Required: -p (for binary packages) or -s (for source makes.)\n";
89
90
91
    exit(1);
}

92
93
94
95
96
97
98
99
100
# Version of FreeBSD.
my $FBSD_VERSION = 4;
if (`uname -r` =~ /^(\d)/) {
    $FBSD_VERSION = $1;
}
else {
    die("Could not determine what version of FreeBSD you are running!\n");
}

101
#
102
103
# The meta-ports (name and version) that drag in all the dependancies for
# an ops/fs node. These are OS dependent as we upgrade.
104
#
105
106
107
my $OPS_PORT  = (($FBSD_VERSION == 4) ? "emulab-ops-1.4" : "emulab-ops-2.0");
my $FS_PORT   = (($FBSD_VERSION == 4) ? "emulab-fs-1.4" : "emulab-fs-2.0");
my $PHP4_PORT = "php4-extensions-1.0";
108

109
my $packagedir = "";
110
my $batchmode  = 0;
111
my $domakes  = 0;
112
my $password;
113
my %opts;
114
if (! getopts("P:p:bsw:F:", \%opts)) {
115
116
117
118
    usage();
}
if (defined($opts{p})) {
    $packagedir = $opts{p};
119
}
120
if (defined($opts{b})) {
121
122
    $batchmode = 1;
}
123
124
125
if (defined($opts{s})) {
    $domakes = 1;
}
126
127
128
if (defined($opts{P})) {
    $OPS_PORT = $opts{P};
}
129
130
131
132
133
134
if (defined($opts{F})) {
    $FS_PORT = $opts{F};
}
if (defined($opts{w})) {
    $password = $opts{w};
}
135
136
137
138
139
140
141
142
143
# Don't just charge into making ports from source by default.
if ($packagedir eq "" and $domakes eq 0) {
    print "At least one of -p and -s must be given.\n";
    usage();
}
if ($packagedir ne "" and $domakes eq 1) {
    print "Only one of -p and -s can be given.\n";
    usage();
}
144
145
146
if (@ARGV) {
    usage();
}
147

148
149
150
151
152
153
154
155
#
# Figure out which directory we live in, so that some stages can do thing
# relative to it.
#
my $OBJDIR = `/usr/bin/dirname $0`;
chomp $OBJDIR;
my $TOP_OBJDIR = "$OBJDIR/..";

156
#
Robert Ricci's avatar
Robert Ricci committed
157
# Some programs we use
158
#
Robert Ricci's avatar
Robert Ricci committed
159
160
my $CHGRP      = "/usr/bin/chgrp";
my $CHMOD      = "/bin/chmod";
161
my $CHOWN      = "/usr/sbin/chown";
Robert Ricci's avatar
Robert Ricci committed
162
my $PW         = "/usr/sbin/pw";
163
my $PATCH      = "/usr/bin/patch";
Robert Ricci's avatar
Robert Ricci committed
164
my $NEWALIASES = "/usr/bin/newaliases";
165
166
my $SH         = "/bin/sh";
my $PKG_INFO   = "/usr/sbin/pkg_info";
167
my $PKG_ADD    = "/usr/sbin/pkg_add";
168
my $PWD        = "/bin/pwd";
169
my $CP         = "/bin/cp";
170
my $MV         = "/bin/mv";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
171
my $GMAKE      = "/usr/local/bin/gmake";
172
my $ENV        = "/usr/bin/env";
173
my $MOUNT      = "/sbin/mount";
174
175
my $TAR	       = "/usr/bin/tar";
my $MD5	       = "/sbin/md5";
176
177

#
Robert Ricci's avatar
Robert Ricci committed
178
# Some files we edit/create
179
#
Robert Ricci's avatar
Robert Ricci committed
180
my $RCCONF          = "/etc/rc.conf";
181
my $HOSTS           = "/etc/hosts";
182
my $FSTAB           = "/etc/fstab";
Robert Ricci's avatar
Robert Ricci committed
183
184
my $RCLOCAL         = "/etc/rc.local";
my $RCCAPTURE       = "$PREFIX/etc/rc.capture";
Robert Ricci's avatar
Robert Ricci committed
185
186
my $LOCAL_HOSTNAMES = "/etc/mail/local-host-names";
my $ALIASES_FILE    = "/etc/mail/aliases";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
187
my $SENDMAIL_CF     = "/etc/mail/sendmail.cf";
Robert Ricci's avatar
Robert Ricci committed
188
189
190
191
192
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";
193
my $SSHD_CONFIG     = "/etc/ssh/sshd_config";
194
my $CRONTAB         = "/etc/crontab";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
195
my $AUTHKEYS	    = "/root/.ssh/authorized_keys";
Kirk Webb's avatar
   
Kirk Webb committed
196
197
my $SMBCONF_FILE    = "/usr/local/etc/smb.conf";
my $SMBCONF_HEAD    = "$SMBCONF_FILE.head";
198
199
my $APACHE_ETCDIR   = "/usr/local/etc/apache";
my $HTTPD_CONF      = "$APACHE_ETCDIR/httpd.conf";
200
my $WWWDIR          = "/usr/local/www/data";
201
202
203
204
205
206
207
208
209

# For installing mysqld
my $MYSQL           = "/usr/local/bin/mysql";
my $MYSQLADMIN      = "/usr/local/bin/mysqladmin";
my $MYSQLSHOW       = "/usr/local/bin/mysqlshow";
my $MYSQLDUMP       = "/usr/local/bin/mysqldump";
my $MYSQLINSTALL    = "/usr/local/bin/mysql_install_db";
my $MYSQLDBDIR      = "/var/db/mysql";
my $MYSQL_LOGDIR    = "$LOGDIR/mysql";
210
211

#
Robert Ricci's avatar
Robert Ricci committed
212
# Some directories we care about
213
#
214
215
216
217
218
219
220
my $LIST_DIR      = "/etc/mail/lists";
my $TIPLOG_DIR    = "/var/log/tiplogs";
my $PORTSDIR      = "/usr/ports";
my $PORTSMISCDIR  = "$PORTSDIR/misc";
my $SRCDIR        = '@srcdir@';
my $TOP_SRCDIR    = "@top_srcdir@";
my $RCDIR         = "/usr/local/etc/rc.d";
221
my $VARRUN        = "/var/run";
222
223

#
Robert Ricci's avatar
Robert Ricci committed
224
# And some lists that we use
225
#
226
227
my @LOCAL_HOSTS        = ($OURDOMAIN,$BOSSNODE,$USERNODE,$FSNODE);
my @LOGFILES           = ("/var/log/logins","/var/log/tiplogs/capture.log",
228
229
    "/var/log/mountd.log", "$LOGDIR/pubsubd.log", "$LOGDIR/elvin_gateway.log");

230
231
my @LOCAL_MAILING_LISTS = grep(/$OURDOMAIN$/,@MAILING_LISTS);
my @MAILING_LIST_NAMES  = map { /^([\w-]+)\@/ } @LOCAL_MAILING_LISTS;
Robert Ricci's avatar
Robert Ricci committed
232

233
234
235
my @TESTBED_DIRS       = ([$PREFIX, "0775"], [$USERROOT, "0755"],
			  [$PROJROOT, "0755"], [$GROUPROOT, "0755"],
			  [$SHAREROOT, "0775"]);
236

237
238
239
240
241
242
my @MOUNTPOINTS        = ($USERROOT, $PROJROOT, $GROUPROOT, $SHAREROOT);

if ($SCRATCHDIR) {
    push(@TESTBED_DIRS, [$SCRATCHROOT, "0755"]);
    push(@MOUNTPOINTS, $SCRATCHROOT);
}
243

244
245
246
#
# A few files we have to deal with
#
247
my $M2CRYPTO_PATCH  = "$TOP_SRCDIR/patches/m2crypto.patch";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
248
my $MAILMAN_PATCH   = "$TOP_SRCDIR/patches/mailman.patch";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
249
my $IDENTPUB        = "$TOP_SRCDIR/install/identity.pub";
250

251
252
253
254
#
# List of names that goes into $HOSTS and which must resolve.
# 
my @OPS_NAMES = ($USERNODE, "users", "ops");
255
256
257
if ($ISFS) {
    push(@OPS_NAMES, "fs");
}
258

259
#
Robert Ricci's avatar
Robert Ricci committed
260
# Make sure they know what they're getting into...
261
#
262
263
264
265
if (! $batchmode) {
    print STDERR
	"WARNING: This script is ONLY intended to be run on a machine\n";
    print STDERR
266
	"that is being set up as a dedicated ops or ops+fs node. Continue? [y/N] ";
267
268
269
    my $response = <>;
    die "Installation aborted!\n" unless ($response =~ /^y/i);
}
270

Robert Ricci's avatar
Robert Ricci committed
271
272
if ($UID != 0) {
    die "This script must be run as root.\n";
273
274
275
}

#
Robert Ricci's avatar
Robert Ricci committed
276
# The phases are fairly self-explanatory
277
#
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
Phase "usersgroups", "Creating users and groups", sub {
    Phase "tbadmin", "Creating tbadmin group", sub {
	if (getgrnam("tbadmin")) {
	    PhaseSkip("tbadmin group already exists");
	}
	ExecQuietFatal("$PW groupadd tbadmin -g $TBADMINGID");
    };
    # Added next two cause the mysql package does not do this (port does).
    Phase "mysqlgroup", "Creating mysql group", sub {
	if (getgrnam("mysql")) {
	    PhaseSkip("mysql group already exists");
	}
	ExecQuietFatal("$PW groupadd mysql -g 88");
    };
    Phase "mysqluser", "Creating mysql user", sub {
	if (getpwnam("mysql")) {
	    PhaseSkip("mysql user already exists");
	}
	ExecQuietFatal("$PW useradd mysql -g 88 -g 88 -h - ".
		       "-d $MYSQLDBDIR -s /sbin/nologin -c 'MySQL Daemon'");
    };
    ExecQuietFatal("$CHOWN mysql:mysql $MYSQLDBDIR")
	if (-e $MYSQLDBDIR);
Robert Ricci's avatar
Robert Ricci committed
301
302
303
};

Phase "dirs", "Setting directory permissions", sub {
Robert Ricci's avatar
Robert Ricci committed
304
305
    foreach my $dirref (@TESTBED_DIRS) {
	my ($dir, $newmode) = @$dirref;
Robert Ricci's avatar
Robert Ricci committed
306
307
308
309
	Phase $dir, $dir, sub {
	    if (!-d $dir) {
		PhaseFail("Directory $dir does not exist");
	    }
Robert Ricci's avatar
Robert Ricci committed
310
311
312
313
	    # 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
314
315
	    # Fix up the mode (strip file type)
	    $mode = $mode & 0777;
Robert Ricci's avatar
Robert Ricci committed
316
	    if ($mode == eval $newmode && $group eq getgrnam("tbadmin")) {
Robert Ricci's avatar
Robert Ricci committed
317
318
		PhaseSkip("Already done");
	    }
Robert Ricci's avatar
Robert Ricci committed
319
320
	    ExecQuietFatal("$CHGRP tbadmin $realdir");
	    ExecQuietFatal("$CHMOD $newmode $realdir");
Robert Ricci's avatar
Robert Ricci committed
321
	};
322
    }
Robert Ricci's avatar
Robert Ricci committed
323
};
324

325
Phase "ports", "Installing ports", sub {
326
    Phase "packages", "Installing packages", sub {
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
	Phase "main", "Installing main package", sub {
	    if (!ExecQuiet("$PKG_INFO -e $OPS_PORT")) {
		PhaseSkip("Package already installed");
	    }
	    if (!$packagedir) {
		PhaseSkip("No package directory provided");
	    }
	    ExecQuietFatal("$ENV PKG_PATH=$packagedir $PKG_ADD $OPS_PORT");
	};
	if ($FBSD_VERSION > 4) {
	    Phase "php4", "Installing php4 package", sub {
		if (!ExecQuiet("$PKG_INFO -e $PHP4_PORT")) {
		    PhaseSkip("Package already installed");
		}
		if (!$packagedir) {
		    PhaseSkip("No package directory provided");
		}
		ExecQuietFatal("$ENV PKG_PATH=$packagedir ".
			       "     $PKG_ADD $PHP4_PORT");
	    };
347
	}
348
349
350
351
352
353
354
355
	# XXX Also temporary
	Phase "pubsub", "Installing pubsub package", sub {
	    if (!ExecQuiet("$PKG_INFO -x pubsub")) {
		PhaseSkip("pubsub already installed");
	    }
	    my $pname = GetPackage("pubsub", $packagedir);
	    ExecQuietFatal("$ENV PKG_PATH=$packagedir $PKG_ADD $pname");
	};
356
    };
357
358
359
360
361
362
363
364
365
366
367
368
    Phase "fs-packages", "Installing FS packages", sub {
	if (!$ISFS) {
	    PhaseSkip("Not FS Node");
	}
	if (!ExecQuiet("$PKG_INFO -e $FS_PORT")) {
	    PhaseSkip("FS ports already installed");
	}
	if (!$packagedir) {
	    PhaseSkip("No package directory provided");
	}
	ExecQuietFatal("$ENV PKG_PATH=$packagedir $PKG_ADD $FS_PORT");
    };
Leigh B. Stoller's avatar
Leigh B. Stoller committed
369
370
    if ($MAILMANSUPPORT) {
	Phase "mailman", "Installing Mailman package", sub {
371
	    if (!ExecQuiet("$PKG_INFO -x -E mailman")) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
372
373
374
375
376
		PhaseSkip("Mailman package already installed");
	    }
	    if (!$packagedir) {
		PhaseSkip("No package directory provided");
	    }
377
	    my $pname = GetPackage("mailman", $packagedir);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
378
379
380
	    ExecQuietFatal("$ENV PKG_PATH=$packagedir $PKG_ADD $pname");
	};
    }
381
382
383
384
385
    if ($CVSSUPPORT) {
	Phase "cvsd", "Installing cvsd package", sub {
	    PhaseSkip("cvsd not supported on ops")
		if ($FBSD_VERSION < 6);

386
	    if (!ExecQuiet("$PKG_INFO -x -E cvsd")) {
387
388
389
390
391
		PhaseSkip("cvsd package already installed");
	    }
	    if (!$packagedir) {
		PhaseSkip("No package directory provided");
	    }
392
	    my $pname = GetPackage("cvsd", $packagedir);
393
394
395
	    ExecQuietFatal("$ENV PKG_PATH=$packagedir $PKG_ADD $pname");
	};
    }
396
397
398
399
400
    if ($BUGDBSUPPORT) {
	Phase "flyspray", "Installing flyspray package support", sub {
	    PhaseSkip("flyspray not supported on ops")
		if ($FBSD_VERSION < 6);

401
	    if (!ExecQuiet("$PKG_INFO -x -E adodb")) {
402
403
404
405
406
		PhaseSkip("adodb package already installed");
	    }
	    if (!$packagedir) {
		PhaseSkip("No package directory provided");
	    }
407
	    my $pname = GetPackage("adodb", $packagedir);
408
409
410
	    ExecQuietFatal("$ENV PKG_PATH=$packagedir $PKG_ADD $pname");
	};
    }
411
    PhaseSkip("Package directory provided; not installing from sources")
Leigh B. Stoller's avatar
Leigh B. Stoller committed
412
413
	if ($packagedir);
    
414
    Phase "pcopy", "Copying ports into place", sub {
415
	DoneIfExists("$PORTSMISCDIR/emulab-ops");
416
417
	ExecQuietFatal("$SH $SRCDIR/ports/ports-install");
    };
418
419
    my $pwd = `$PWD`;
    chomp $pwd;
420
    Phase "pinstall", "Installing ports (may take a while)", sub {
421
	if (!ExecQuiet("$PKG_INFO -e $OPS_PORT")) {
422
423
424
425
426
427
	    PhaseSkip("Ports already installed");
	}

	#
	# This port is dead-simple, so it's safe to do it from this script
	#
428
429
	chdir "$PORTSMISCDIR/emulab-ops" or
		PhaseFail "Unable to change to $PORTSMISCDIR/emulab-ops: $!";
430
	ExecQuietFatal("make -DBATCH install");
431
    };
432
433
434
435
436
437
438
439
440
441
442
    Phase "fs-pinstall", "Installing FS ports (may take a while)", sub {
	if (!$ISFS) {
	    PhaseSkip("Not FS Node");
	}
	if (!ExecQuiet("$PKG_INFO -e $FS_PORT")) {
	    PhaseSkip("Ports already installed");
	}
	chdir "$PORTSMISCDIR/emulab-fs" or
	    PhaseFail "Unable to change to $PORTSMISCDIR/emulab-fs: $!";
	ExecQuietFatal("make -DBATCH install");
    };
Leigh B. Stoller's avatar
Leigh B. Stoller committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
    Phase "php4-pinstall", "Installing PHP4 ports (may take a while)", sub {
	if (!ExecQuiet("$PKG_INFO -e $PHP4_PORT")) {
	    PhaseSkip("Ports already installed");
	}
	chdir "$PORTSMISCDIR/emulab-php4" or
	    PhaseFail "Unable to change to $PORTSMISCDIR/emulab-php4: $!";
	ExecQuietFatal("make -DBATCH install");
    };
    if ($MAILMANSUPPORT) {
	Phase "mailman-pinstall", "Installing mailman ports", sub {
	    if (!ExecQuiet("$PKG_INFO -x -e mailman")) {
		PhaseSkip("Ports already installed");
	    }
	    chdir "$PORTSMISCDIR/emulab-mailman" or
		PhaseFail("Unable to change to ".
			  "$PORTSMISCDIR/emulab-mailman: $!");
	    ExecQuietFatal("make -DBATCH install");
	};
    }
462
    chdir $pwd;
463
464
};

465
466
# XXX Temporary.
Phase "portfixup", "Fixing up packages", sub {
467
    Phase "rsync", "Looking for rsync and installing", sub {
468
469
470
	if (!ExecQuiet("$PKG_INFO -x rsync")) {
	    PhaseSkip("rsync already installed");
	}
471
472
	my $pname = GetPackage("rsync", $packagedir);
	ExecQuietFatal("$ENV PKG_PATH=$packagedir $PKG_ADD $pname");
473
    };
474
475
    if ($WINSUPPORT) {
	Phase "samba", "Looking for Samba and installing", sub {
476
477
478
	    if (!$ISFS) {
		PhaseSkip("Not FS Node");
	    }
479
480
481
	    if (!ExecQuiet("$PKG_INFO -x samba")) {
		PhaseSkip("samba already installed");
	    }
482
483
	    my $pname = GetPackage("samba", $packagedir);
	    ExecQuietFatal("$ENV PKG_PATH=$packagedir $PKG_ADD $pname");
484
485
	};
	Phase "gcc30", "Looking for GCC 3.0 and installing", sub {
486
487
	    if ($FBSD_VERSION > 4 ||
		!ExecQuiet("$PKG_INFO -x gcc30")) {
488
		PhaseSkip("GCC 3.0 already installed");
489
	    }
490
491
	    my $pname = GetPackage("gcc30", $packagedir);
	    ExecQuietFatal("$ENV PKG_PATH=$packagedir $PKG_ADD $pname");
492
493
	};
    }
494
495
};

496
497
Phase "patches", "Applying patches", sub {
    Phase "m2cryptopatch", "Patching m2crypto", sub {
498
	my $patchfile = $M2CRYPTO_PATCH;
499

500
501
502
503
	if (ExecQuiet("$PKG_INFO -I -x m2crypto | fgrep -q -s '0.13'") == 0) {
	    $patchfile = "${patchfile}-0.13";
	}
	if (!ExecQuiet("$PATCH -C -f -l -R -p0 -i $patchfile")) {
504
	    PhaseSkip("Patch already applied");
505
506
	}
	ExecQuietFatal("$PATCH -f -l -p0 -i $patchfile");
507
    };
Leigh B. Stoller's avatar
Leigh B. Stoller committed
508
509
510
511
512
    if ($MAILMANSUPPORT) {
	Phase "mailmanpatch", "Patching mailman", sub {
	    my $patchfile = $MAILMAN_PATCH;
	
	    if (!ExecQuiet("$PATCH -C -f -l -R -p0 -i $patchfile")) {
513
		PhaseSkip("Patch already applied");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
514
515
516
517
	    }
	    ExecQuietFatal("$PATCH -f -l -p0 -i $patchfile");
	};
    }
518
519
}; 

Robert Ricci's avatar
Robert Ricci committed
520
521
522
Phase "rc.conf", "Adding testbed content to rc.conf", sub {
    DoneIfEdited($RCCONF);
    AppendToFileFatal($RCCONF,
Robert Ricci's avatar
Robert Ricci committed
523
		      qq|sendmail_enable="YES"|,
524
525
		      qq|rpcbind_enable="YES"|,
		      qq|mountd_enable="YES"|,
Robert Ricci's avatar
Robert Ricci committed
526
527
		      qq|nfs_server_enable="YES"|,
		      qq|nfs_server_flags="-u -t -n 16"|,
528
		      qq|mountd_flags="-r -p 900"|,
529
		      (($ISFS && $WINSUPPORT) ? qq|smbd_enable="YES"| : ()),
530
		      qq|apache_enable="YES"|,
Robert Ricci's avatar
Robert Ricci committed
531
		      qq|syslogd_flags=""|);
Robert Ricci's avatar
Robert Ricci committed
532
};
533

534
Phase "hosts", "Adding boss/ops/fs IP addresses to $HOSTS", sub {
535
    DoneIfEdited($HOSTS);
536
537
538
539
540
541
    my $hstr = "${BOSSNODE_IP}\t${BOSSNODE} boss" .
	"\n${USERNODE_IP}\t@OPS_NAMES";
    if (!$ISFS) {
	$hstr .= "\n${FSNODE_IP}\t${FSNODE} fs";
    }
    AppendToFileFatal($HOSTS, $hstr);
542
543
};

544
545
546
547
548
549
550
Phase "resolve", "Checking to make sure names for boss/ops/fs resolve", sub {
    my @hnames = (@OPS_NAMES, $BOSSNODE, "boss");

    if (!$ISFS) {
	push @hnames, $FSNODE, "fs";
    }
    foreach my $name (@hnames) {
551
552
	Phase $name, $name, sub {
	    if (gethostbyname($name)) {
553
		PhaseSucceed("$name resolves");
554
555
556
557
558
559
560
	    } else {
		PhaseFail("$name does not resolve");
	    }
	};
    }
};

Robert Ricci's avatar
Robert Ricci committed
561
562
563
564
565
566
567
568
569
570
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: $!");
571
572
	    ExecQuietFatal("$CHGRP mailnull $LIST_DIR");
	    ExecQuietFatal("$CHMOD 750 $LIST_DIR");
Robert Ricci's avatar
Robert Ricci committed
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
	};
	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 {
588
	    PhaseSkip("No new aliases") unless @MAILING_LIST_NAMES;
Robert Ricci's avatar
Robert Ricci committed
589
590
591
592
593
594
595
596
597
598
599
600
	    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
601
602
	# filesystems.  Note: we cannot do /share on the same exports line
	# as the other filesystems because of the RO mount below (trust me).
Robert Ricci's avatar
Robert Ricci committed
603
	#
604
	my @dirs = ('/var', $PREFIX);
605
	if ($ISFS) {
606
	    @dirs = (grep(!/^$SHAREROOT$/, @MOUNTPOINTS), @dirs);
607
	}
Robert Ricci's avatar
Robert Ricci committed
608
609
610
611
612
613
614
615
616
617
618
619
620
621
	@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,
622
623
		join(" ",@{$filesystems{$key}}) .
		    "\t$BOSSNODE -maproot=root";
Robert Ricci's avatar
Robert Ricci committed
624
625
	}

626
627
628
629
630
631
632
	if ($ISFS) {
	    #
	    # /share is special. We want to export to boss read-write,
	    # but to the control network read-only.
	    #
	    my ($a,$b,$c,$d) =
		($CONTROL_NETWORK =~ /^(\d*)\.(\d*)\.(\d*)\.(\d*)/);
633
	    my $realdir = `realpath $SHAREROOT`;
634
635
636
637
638
	    chomp($realdir);
	    push(@exports_lines,
		 "$realdir\t$BOSSNODE -maproot=root");
	    push(@exports_lines,
		 "$realdir\t-network ${a}.${b}.${c} -mask $CONTROL_NETMASK ".
639
		 "-maproot=root -ro -alldirs");
640
	}
641

642
643
644
645
646
	#
	# Add localhost mount to proj/cvsrepos.
	#
	if ($CVSSUPPORT) {
	    my $pdir;
647
	    my $dfout = `df $PROJROOT | fgrep '/dev'`;
648
	    if ($?) {
649
		PhaseFail("'df $PROJROOT' failed!");
650
651
652
653
654
	    }
	    if ($dfout =~ /\s+([\/\w]*)$/) {
		$pdir = $1;
	    }
	    else {
655
		PhaseFail("Could not determine where $PROJROOT is mounted!");
656
657
658
659
	    }
	    push(@exports_lines, "$pdir\tlocalhost -alldirs");
	}

Robert Ricci's avatar
Robert Ricci committed
660
661
662
663
664
665
666
	#
	# Put them in exports.head, and copy that to /etc/exports
	#
	CreateFileFatal($EXPORTS_HEAD, @exports_lines);
	ExecQuietFatal("cp $EXPORTS_HEAD $EXPORTS_FILE");
    };

667
    # XXX Newhup
Robert Ricci's avatar
Robert Ricci committed
668
669
670
671
672
673
    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");
    };
};
674

675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
Phase "NFSmounts", "Setting up NFS mounts", sub {
    if ($ISFS) {
	PhaseSkip("FSes are local");
    }

    Phase "mountpoints", "Creating mountpoints", sub {
	foreach my $dir (@MOUNTPOINTS) {
	    Phase $dir, $dir, sub {
		DoneIfExists($dir);
		mkdir $dir, 0777 or
		    PhaseFail("Unable to create $dir : $!");
	    };
	}
    };
    Phase "fstab", "Adding NFS mounts to $FSTAB", sub {
	DoneIfEdited($FSTAB);
691
692
693
694
695
696
697
698
699
700
	my @lines = ("$FSNODE:$USERROOT\t\t$USERROOT\tnfs\trw,nodev,nosuid\t0\t0",
		     "$FSNODE:$PROJROOT\t\t$PROJROOT\tnfs\trw,nodev,nosuid\t0\t0",
		     "$FSNODE:$GROUPROOT\t\t$GROUPROOT\tnfs\trw,nodev,nosuid\t0\t0",
		     "$FSNODE:$SHAREROOT\t\t$SHAREROOT\tnfs\trw,nodev,nosuid\t0\t0");
	if ($SCRATCHDIR) {
	    push(@lines,
		 "$FSNODE:$SCRATCHROOT\t\t$SCRATCHROOT\tnfs\trw,nodev,nosuid\t0\t0");
	}

	AppendToFileFatal($FSTAB, @lines);
701
702
703
704
705
706
707
708
709
710
711
    };
    Phase "mounts", "Mounting NFS filesystems", sub {
	foreach my $dir (@MOUNTPOINTS) {
	    Phase $dir, $dir, sub {
		DoneIfMounted($dir);
		ExecQuietFatal("$MOUNT -o '-R 1' $dir");
	    };
	}
    };
};

712
713
714
#
# Set up syslog
#
Robert Ricci's avatar
Robert Ricci committed
715
716
717
718
719
720
721
722
723
724
725
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>;
726
727
728
	if (scalar(grep(/$LOGFACIL/, @sc)) != 0) {
	    PhaseFail("Testbed chosen facility $LOGFACIL already in use in /etc/syslog.conf!");
	}
Robert Ricci's avatar
Robert Ricci committed
729
730
	if (scalar(grep(/^cron/, @sc)) != 1) {
	    PhaseFail("Unable to find marker in /etc/syslog.conf!");
731
732
	}

Robert Ricci's avatar
Robert Ricci committed
733
734
735
736
737
	#
	# Clobber and re-write
	#
	seek(SC,0,0);
	truncate(SC,0);
738

Robert Ricci's avatar
Robert Ricci committed
739
	foreach my $line (@sc) {
740
741
742
743
744
745
746
	    #
	    # Modify the /var/log/messages line to exclude testbed stuff
	    #
	    my $pat = q(\s+/var/log/messages);
	    if ($line =~ /^[^#].*$pat/) {
		$line =~ s/($pat)/\;$LOGFACIL.none$1/;
	    }
747
748
749
750
751
752
753
754
755
756
757
758

	    #
	    # XXX don't send anything to logged in root users.
	    # Per-user linktest proxies run on ops as root in a "full"
	    # ssh ("-t -t") which appears as a login shell.  Thus the
	    # linktest output given to the user might include syslog
	    # messages.
	    #
	    if ($line =~ /root$/) {
		$line =~ s/^/#/;
	    }

Robert Ricci's avatar
Robert Ricci committed
759
	    print SC $line;
760
761
762
763

	    #
	    # Find the cron line, after which we place our auth.info line
	    #
Robert Ricci's avatar
Robert Ricci committed
764
765
766
767
768
769
	    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";
	    }
	}
770

Robert Ricci's avatar
Robert Ricci committed
771
772
773
774
775
776
777
778
	#
	# 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";
779
	print SC "!pubsubd\n";
780
	print SC "*.*\t\t\t\t\t\t${LOGDIR}/pubsubd.log\n";
781
	print SC "!elvin_gateway\n";
782
	print SC "*.*\t\t\t\t\t\t${LOGDIR}/elvin_gateway.log\n";
Robert Ricci's avatar
Robert Ricci committed
783
784
785
786
787
788
789
790
791
	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 : $!");
    };
    
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
    Phase "logdir", "Creating log directory", sub {
	DoneIfExists($LOGDIR);
	mkdir $LOGDIR, 0775 or PhaseFail("Unable to create $LOGDIR : $!");
	ExecQuietFatal("$CHGRP tbadmin $LOGDIR");
	ExecQuietFatal("$CHMOD 775 $LOGDIR");
    };

    Phase "mysql-logdir", "Creating mysql log directory", sub {
	DoneIfExists($MYSQL_LOGDIR);
	mkdir $MYSQL_LOGDIR, 0775 or
	    PhaseFail("Unable to create $MYSQL_LOGDIR : $!");
	ExecQuietFatal("$CHOWN mysql:mysql $MYSQL_LOGDIR");
	ExecQuietFatal("$CHMOD 775 $MYSQL_LOGDIR");
    };

Robert Ricci's avatar
Robert Ricci committed
807
808
809
810
811
    Phase "logfiles", "Creating log files", sub {
	foreach my $logfile (@LOGFILES) {
	    Phase $logfile, $logfile, sub {
		DoneIfExists($logfile);
		CreateFileFatal($logfile);
812
		ExecQuietFatal("$CHGRP tbadmin $logfile");
Robert Ricci's avatar
Robert Ricci committed
813
814
815
816
817
818
819
820
821
822
		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",
823
824
	    "$LOGDIR/pubsubd.log\t\t\t640  5     1000 *     Z",
	    "$LOGDIR/elvin_gateway.log\t\t\t640  5     1000 *     Z",
Robert Ricci's avatar
Robert Ricci committed
825
826
827
828
	    "/var/log/tiplogs/capture.log\t\t644  7     *    168   Z");
    };
};

829
830
Phase "cron", "Adding cron jobs", sub {
    Phase "crontab", "Editing $CRONTAB", sub {
831
832
833
	if ($QUOTA_FSLIST eq "") {
	    PhaseSkip("No filesystem quotas");
	}
834
	DoneIfEdited($CRONTAB);
835
836
837
838
839
840
841
842

	my @cronlist = ("0 \t6\t*\t*\t*\troot\t$PREFIX/sbin/quotamail");
	if ($WIKISUPPORT) {
	    push(@cronlist,
		 "*/15 \t*\t*\t*\t*\troot\t(cd /usr/local/www/data/twiki/bin;".
		 " ./mailnotify -q >> /var/tmp/mailnotify.log 2>&1)");
	}
	AppendToFileFatal($CRONTAB, @cronlist);
843
844
845
846
847
848
849
    };
    Phase "cronhup", "HUPing cron", sub {
	if (PhaseWasSkipped("crontab")) { PhaseSkip("No new crontab"); }
	HUPDaemon("cron");
    };
};

850
Phase "sudoers", "Editing $SUDOERS to allow wheel group", sub {
Robert Ricci's avatar
Robert Ricci committed
851
    DoneIfEdited($SUDOERS);
Russ Fish's avatar
Russ Fish committed
852
853
854
    AppendToFileFatal($SUDOERS,
		      "%wheel    ALL=(ALL) NOPASSWD: ALL",
		      "Defaults  logfile=/var/log/sudo.log");
Robert Ricci's avatar
Robert Ricci committed
855
};
856

857
858
859
860
861
862
863
864
865
866
867
868
869
Phase "samba", "Setting up Samba", sub {
    if (!$ISFS) {
	PhaseSkip("Not FS node");
    }
    if (!$WINSUPPORT) {
	PhaseSkip("Windows support not enabled");
    }
    Phase "smb.conf", "Installing smb.conf[.head]", sub {
	DoneIfEdited($SMBCONF_HEAD);
	ExecQuietFatal("$CP -pf $TOP_OBJDIR/install/smb.conf.head $SMBCONF_HEAD");
	AppendToFileFatal($SMBCONF_HEAD,
			  "# This file created by Emulab Control");
	ExecQuietFatal("$CP -pf $SMBCONF_HEAD $SMBCONF_FILE");
870
    };
871
872
873
874
875
876
877
878
879
880
881
882
    Phase "samba.sh", "Installing samba.sh", sub {
	DoneIfExists("$RCDIR/samba.sh");
	DoneIfDoesntExist("$RCDIR/samba.sh.sample");
	ExecQuietFatal("$MV -f $RCDIR/samba.sh.sample $RCDIR/samba.sh");
    };
    if ($ELABINELAB) {	
	Phase "starting", "Starting Samba", sub {
	    DoneIfExists("$VARRUN/smbd.pid");
	    ExecQuietFatal("$RCDIR/samba.sh start");
	};
    }
};
883

Robert Ricci's avatar
Robert Ricci committed
884
Phase "ssh", "Allowing root ssh", sub {
Robert Ricci's avatar
Robert Ricci committed
885
886
    Phase "sshdconfig", "Permitting root login through ssh", sub {
	DoneIfEdited($SSHD_CONFIG);
887
888
889
	AppendToFileFatal($SSHD_CONFIG,
			  "PermitRootLogin yes",
			  "Protocol 2,1");
Robert Ricci's avatar
Robert Ricci committed
890
891
892
893
894
895
    };
    Phase "dotssh", "Making root's .ssh directory", sub {
	DoneIfExists("/root/.ssh");
	mkdir("/root/.ssh",0700) or
	    PhaseFail("Unable to create /root/.ssh: $!");
    };
896
897
898
899
900
901
902
903
904
905
906
907
    Phase "authkeys", "Adding stub identity to root authorized_keys", sub {
	DoneIfEdited($AUTHKEYS);
	my $ident = `cat $IDENTPUB`;
	PhaseFail("Could not read $IDENTPUB")
	    if ($?);
	chomp($ident);
	if (! -e $AUTHKEYS) {
	    CreateFileFatal($AUTHKEYS);
	}
	AppendToFileFatal($AUTHKEYS,
			  "from=\"${BOSSNODE}\" $ident");
    };
Robert Ricci's avatar
Robert Ricci committed
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
};

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");
    };
933
934
};

935
Phase "rc.d", "Setting up rc.d scripts", sub {
936
937
938
939
940
941
    Phase "rsyncd", "Removing rsyncd startup script",  sub {
	DoneIfDoesntExist("$RCDIR/rsyncd.sh");
	if (!unlink "$RCDIR/rsyncd.sh") {
	    PhaseFail("Unable to remove $RCDIR/rsyncd.sh: $!");
	}
    };
942
943
944
945
946
947
    Phase "mysql-server", "Removing $RCDIR/mysql-server.sh", sub {
	DoneIfDoesntExist("$RCDIR/mysql-server.sh");
	if (!unlink "$RCDIR/mysql-server.sh") {
	    PhaseFail("Unable to remove $RCDIR/mysql-server.sh: $!");
	}
    };
948
949
950
951
952
953
    Phase "my-server2", "Removing $RCDIR/mysql-server", sub {
	DoneIfDoesntExist("$RCDIR/mysql-server");
	if (!unlink "$RCDIR/mysql-server") {
	    PhaseFail("Unable to remove $RCDIR/mysql-server: $!");
	}
    };
954
955
    if ($ELVIN_COMPAT) {
	Phase "elvin-gateway", "Installing elvin gateway startup file", sub {
956
957
	    PhaseSkip("elvind not installed, elvin_gateway not enabled")
		if (! -x "/usr/local/libexec/elvind");
958
959
960
961
	    DoneIfExists("$RCDIR/3.elvin_gateway.sh");
	    PhaseFail("Pubsub system or elvin_gateway is not installed")
	};
    }
962
    
963
    Phase "rc.testbed", "Installing testbed RC scripts", sub {
964
965
966
967
        Phase "elvind.sh", "Removing port version of elvind.sh", sub {
	    DoneIfDoesntExist("$RCDIR/elvind.sh");
            ExecQuietFatal("/bin/rm -f $RCDIR/elvind.sh");
        };
968
	ExecQuietFatal("$GMAKE -C $TOP_OBJDIR/rc.d control-install");
969
970
971
    };
};

972
973
974
975
976
977
978
Phase "apache", "Installing apache config file", sub {
    PhaseSkip("apache not supported on ops")
	if ($FBSD_VERSION == 4);

    DoneIfEdited("$HTTPD_CONF");
    ExecQuietFatal("$GMAKE -C $TOP_OBJDIR/apache control-install");
};
979
980
981
982
983
984
985
986
987
988
Phase "wikidocs", "Installing wikidocs redirect", sub {
    DoneIfExists("$WWWDIR/wikidocs/.htaccess");

    if (! -e "$WWWDIR/wikidocs") {
	mkdir("$WWWDIR/wikidocs", 0755) or
	    PhaseFail("Unable to make $WWWDIR/wikidocs: $!");
    }
    CreateFileFatal("$WWWDIR/wikidocs/.htaccess",
		    "Redirect /wikidocs http://users.emulab.net/trac/emulab");
};
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048

Phase "database", "Setting up database", sub {
    PhaseSkip("mysqld not supported on ops")
	if ($FBSD_VERSION == 4);
    
    # Get a password for the the DB.
    Phase "password", "Asking for mysqld root password", sub {
	DoneIfExists("$ETCDIR/mysqld.pwd");

	if (!defined($password)) {
	    print "\n\nPick a password for mysqld (warning, will be echoed): ";
	    $password = <>;
	    chomp($password)
		if (defined($password));
	}
	PhaseFail("Invalid password supplied")
	    if (!defined($password) || $password eq "");
	
	CreateFileFatal("$ETCDIR/mysqld.pwd", "$password");
	ExecQuietFatal("$CHMOD 750 $ETCDIR/mysqld.pwd");
	ExecQuietFatal("$CHGRP tbadmin $ETCDIR/mysqld.pwd");
    };
    $password = `cat $ETCDIR/mysqld.pwd`;
    chomp($password);

    Phase "initialize", "Initializing mysql", sub {
	PhaseSkip("mysqld already initialzed")
	    if (-d "$MYSQLDBDIR/mysql");

	ExecQuietFatal("$MYSQLINSTALL --ldata=${MYSQLDBDIR}");
	ExecQuietFatal("$CHOWN -R mysql:mysql $MYSQLDBDIR");
    };
    
    Phase "start", "Starting mysqld", sub {
	my ($exitval) = ExecQuiet("$MYSQLADMIN -u mysql ping");

	PhaseSkip("mysqld already running")
	    if ($exitval == 0);

	ExecQuietFatal("$RCDIR/1.mysql-server.sh start");
	# Give mysqld some time to start, then make sure it did
	sleep 5;
	ExecQuietFatal("$MYSQLADMIN -u mysql ping");
    };

    # Once the password is inserted and privs flushed, will need a password
    # from this point forward!
    Phase "privs", "Initializing mysqld priv system", sub {
	my ($exitval, @rows) =
	    ExecQuiet("echo 'select * from user limit 1' | ".
		      "$MYSQL -s -u root mysql");
	if ($exitval) {
	    PhaseSkip("Privs already initialized");
	}
	($exitval, @rows) =
	    ExecQuietFatal("echo 'update user set ".
			   " Password=PASSWORD(\"$password\") ".
			   "where user=\"root\"; flush privileges' | ".
			   "$MYSQL -s -u root mysql");
    };
1049
1050
1051
1052
1053
1054
1055
1056
1057

    Phase "dbpatch", "Patching up mysql DB", sub {
	if (!ExecQuiet("$MYSQLDUMP -u root -p$password mysql emulab_dbs")) {
	    PhaseSkip("DB already patched");
	}

	ExecQuietFatal("$MYSQL -u root -p$password mysql ".
		       "  < $TOP_SRCDIR/sql/opsdb.sql");
    };
1058
1059
};

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
if ($MAILMANSUPPORT) {
    my $MAILMANDIR   = "/usr/local/mailman";
    my $MAILMANCFG   = "$MAILMANDIR/Mailman/mm_cfg.py";
    my $MAILMANLISTS = "$MAILMANDIR/lists";
    my $MAILMANPWD   = "$ETCDIR/mailman.pwd";
    my $MAILMANALIAS = "/etc/mail/aliases.mailman";
    
    Phase "Mailman", "Setting up Mailman", sub {
	PhaseSkip("mailman not supported on ops")
	    if ($FBSD_VERSION == 4);

	# Patch for broken install
	ExecQuietFatal("$CHMOD 2770 $MAILMANDIR/archives/private");

	# Get a password for the mailman installation.
	Phase "password", "Generating mailman password", sub {
	    DoneIfExists($MAILMANPWD);
	    
	    my $string = substr(GenSecretKey(), 0, 10);

	    PhaseFail("Invalid password supplied")
		if (!defined($string) || $string eq "");
	
	    CreateFileFatal("$MAILMANPWD", "$string");
	    ExecQuietFatal("$CHMOD 750 $MAILMANPWD");
	    ExecQuietFatal("$CHGRP tbadmin $MAILMANPWD");
	};
	my $secret = `cat $MAILMANPWD`;
	chomp($secret);

	Phase "config", "Editing local config file", sub {
	    DoneIfEdited("$MAILMANCFG");

	    AppendToFileFatal($MAILMANCFG,
			      "DEFAULT_EMAIL_HOST = '${OURDOMAIN}'",
			      "DEFAULT_URL_HOST = '${USERNODE}'",
			      "add_virtualhost(DEFAULT_URL_HOST, ".
			                       "DEFAULT_EMAIL_HOST)");
	};
	Phase "mailman", "Creating mailman list", sub {
	    PhaseSkip("List already created")
		if (-d "$MAILMANLISTS/mailman");

	    ExecQuietFatal("cd $MAILMANDIR; ".
			   "bin/newlist -q mailman $TBOPSEMAIL $secret");
	};
	Phase "subscribe", "Subscribing testbed-ops", sub {
	    PhaseSkip("Already subscribed")
		if (ExecQuiet("$MAILMANDIR/bin/list_members mailman | ".
			      "fgrep -q -s $TBOPSEMAIL") == 0);

	    ExecQuietFatal("cd $MAILMANDIR; ".
			   "echo $TBOPSEMAIL | ".
			   "   bin/add_members -r - -w n -a n mailman");
	};
	# Can do this as many times as we like. No way to know.
	ExecQuietFatal("cd $MAILMANDIR; ".
		       "bin/config_list -i data/sitelist.cfg mailman");
	# Ditto
	ExecQuietFatal("cd $MAILMANDIR; ".
		       "bin/mmsitepass $secret; ".
		       "bin/mmsitepass -c $secret");

	Phase "config", "Editing $SENDMAIL_CF", sub {
	    PhaseSkip("Already edited")
		if (ExecQuiet("fgrep AliasFile $SENDMAIL_CF | ".
			      "fgrep -q -s mailman") == 0);

1128
	    # Insert the mailman aliases file first, to avoid a forwarding loop.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1129
	    ExecQuietFatal("sed -i .orig -e ".
1130
			   "  '/AliasFile=/s;=;=$MAILMANALIAS,;' ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
			   "$SENDMAIL_CF");
	};
	Phase "aliases", "Creating $MAILMANALIAS", sub {
	    DoneIfExists($MAILMANALIAS);
	    CreateFileFatal($MAILMANALIAS);
	};
	Phase "newaliases", "Running newaliases", sub {
	    PhaseSkip("No new aliases") if PhaseWasSkipped("aliases");
	    ExecQuietFatal($NEWALIASES);
	};
    };
}
1143
1144
1145
1146
1147
1148
1149

if ($CVSSUPPORT) {
    my $CVSDDIR   = "/usr/local/etc/cvsd";
    my $CVSDCONF  = "$CVSDDIR/cvsd.conf";
    my $CVSDHEAD  = "$LIBDIR/cvsd.conf.head";
    my $CVSDJAIL  = "/var/cvsjail";
    my $BUILDROOT = "/usr/local/sbin/cvsd-buildroot";
1150
    my $REPOSDIR  = "$PROJROOT/cvsrepos";
1151
1152
1153
1154
1155
1156
1157
    
    Phase "cvsd", "Installing cvsd", sub {
	PhaseSkip("cvsd not supported on ops")
	    if ($FBSD_VERSION < 6);

	Phase "files", "Installing cvsd files", sub {
	    DoneIfExists($CVSDHEAD);
1158
1159
	    ExecQuietFatal("$GMAKE -C $TOP_OBJDIR/collab/cvstools ".
			   "  control-install");
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
	};

	Phase "cvsd.conf", "Installing cvsd.conf", sub {
	    DoneIfExists($CVSDCONF);
	    ExecQuietFatal("$CP -f $CVSDHEAD $CVSDCONF");
	};

	Phase "jail", "Creating cvsd jail hierarchy", sub {
	    DoneIfExists($CVSDJAIL);
	    ExecQuietFatal("$BUILDROOT $CVSDJAIL");
	};
	
	Phase "dirs", "Creating extra directories", sub {
	    if (! -e "$CVSDJAIL/dummy") {
		mkdir("$CVSDJAIL/dummy", 0755) or
		    PhaseFail("Unable to make $CVSDJAIL/dummy : $!");
	    }
	    if (! -e "$CVSDJAIL/cvsrepos") {
		mkdir("$CVSDJAIL/cvsrepos", 0755) or
		    PhaseFail("Unable to make $CVSDJAIL/cvsrepos : $!");
	    }
	    ExecQuietFatal("$CHGRP nobody $CVSDJAIL/dummy $CVSDJAIL/cvsrepos");
	    
	    if (! -e "$REPOSDIR") {
		mkdir("$REPOSDIR", 0775) or
		    PhaseFail("Unable to make $REPOSDIR : $!");
	    }
	};

	Phase "cvsd.sh", "Installing cvsd.sh", sub {
	    DoneIfExists("$RCDIR/cvsd.sh");
	    ExecQuietFatal("mv $RCDIR/cvsd.sh.sample $RCDIR/cvsd.sh");
	    ExecQuietFatal("$CHMOD a+x $RCDIR/cvsd.sh");
	};
    };
}

1197
1198
1199
if ($BUGDBSUPPORT) {
    my $FLYSPRAYPWD   = "$ETCDIR/flyspray.pwd";
    my $FLYSPRAYDIR   = "/usr/local/www/data/flyspray";
Robert Ricci's avatar
Robert Ricci committed
1200
    my $FLYSPRAYURL   = "http://$OUTER_BOSS/downloads/flyspray-0.9.9.tgz";
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
    my $FLYSPRAYCONF  = "$ETCDIR/flyspray.conf.php";
    my $PHPCONFIG     = "/usr/local/etc/php.ini";
    my $localtarfile  = "/tmp/flyspray.tgz";
    my $ADODBPATH     = "/usr/local/share/adodb/adodb.inc.php";

    Phase "flyspray", "Installing flyspray", sub {
	PhaseSkip("flyspray not supported on ops")
	    if ($FBSD_VERSION < 6);

	# Get a password for the installation.
	Phase "password", "Generating flyspray password", sub {
	    DoneIfExists($FLYSPRAYPWD);
	    
	    my $string = substr(GenSecretKey(), 0, 10);

	    PhaseFail("Invalid password supplied")
		if (!defined($string) || $string eq "");
	
	    CreateFileFatal("$FLYSPRAYPWD", "$string");
	    ExecQuietFatal("$CHMOD 750 $FLYSPRAYPWD");
	    ExecQuietFatal("$CHGRP tbadmin $FLYSPRAYPWD");
	};
	my $secret = `cat $FLYSPRAYPWD`;
	chomp($secret);

	Phase "php", "Patching up PHP config file", sub {
	    DoneIfExists($PHPCONFIG);
	    CreateFileFatal($PHPCONFIG,
			    "[PHP]",
			    ";",
			    "; For ADODB, which is needed by flyspray.",
			    ";",
			    "include_path=\"/usr/local/share/adodb\"");
	};

	Phase "dbcreate", "Creating flyspray DB", sub {
	    if (! -d "$MYSQLDBDIR/flyspray") {
		ExecQuietFatal("$MYSQLADMIN -u root -p${password} ".
			       "            create flyspray");
	    }
	    ExecQuietFatal("echo 'grant all on flyspray.* to ".
			   "      flyspray\@localhost ".
			   "      identified by \"$secret\"' | ".
			   "$MYSQL -s -u root -p${password} mysql");
	};

	Phase "fetching", "Fetching flyspray distribution", sub {
	    DoneIfExists($localtarfile);
	    FetchFileFatal($FLYSPRAYURL, $localtarfile);
	};

	Phase "unpacking", "Unpacking flyspray distribution", sub {
	    if (! -e "$FLYSPRAYDIR") {
		mkdir("$FLYSPRAYDIR", 0770) or
		    PhaseFail("Unable to make $FLYSPRAYDIR : $!");
	    }
	    ExecQuietFatal("$CHOWN nobody $FLYSPRAYDIR");
	    PhaseSkip("Flyspray already unpacked")
		if (-e "$FLYSPRAYDIR/.htaccess");
	    ExecQuietFatal("$TAR zxf $localtarfile -C $FLYSPRAYDIR"); 
	};

	Phase "dbinit", "Initializing flyspray DB", sub {
	    if (!ExecQuiet("$MYSQLDUMP -d -u flyspray -p${secret} ".
			   "flyspray flyspray_groups")) {
		PhaseSkip("DB already initialized");
	    }
	    
	    ExecQuietFatal("$MYSQL -u flyspray -p${secret} flyspray ".
			   "  < $FLYSPRAYDIR/sql/flyspray-0.9.8.mysql");
	    ExecQuietFatal("$MYSQL -u flyspray -p${secret} flyspray ".
			   "  < $FLYSPRAYDIR/sql/flyspray-0.9.9-devel.mysql");
	};

	Phase "dbpatch", "Patching up flyspray DB", sub {
	    if (!ExecQuiet("$MYSQLDUMP -d -u flyspray -p${secret} ".
			   "flyspray emulab_project_mapping")) {
		PhaseSkip("DB already patched");
	    }

	    ExecQuietFatal("$MYSQL -u flyspray -p${secret} flyspray ".
			   "  < $TOP_SRCDIR/bugdb/sqlextras");
	};
	
	#
	# Fix up the password for the flyspray "superuser"
	#
	my $md5_secret = `$MD5 -q -s $secret`;
	PhaseFail("Could not take MD5 of flyspray password!")
	    if ($?);
	chomp($md5_secret);

	ExecQuietFatal("echo 'update flyspray_users set ".
		       "        user_pass=\"${md5_secret}\", ".
		       "        jabber_id=\"\", ".
		       "        email_address=\"${TBOPSEMAIL}\"' | ".
		       "$MYSQL -u flyspray -p${secret} flyspray");
	
	Phase "configfile", "Creating flyspray config file", sub {
	    DoneIfExists($FLYSPRAYCONF);
	    CreateFileFatal($FLYSPRAYCONF,
			    ";",
			    "; Flyspray configuration",
			    ";",
			    "[general]",
			    "basedir   = \"${FLYSPRAYDIR}\"",
			    "baseurl   = \"https://${USERNODE}/flyspray/\"",
			    "adodbpath = \"$ADODBPATH\"",
			    "cookiesalt= 130918332",
			    "output_buffering = \"on\"",
			    "passwdcrypt = \"md5\"",
			    "address_rewriting = \"0\"",
			    "reminder_daemon = \"0\"",
			    "[database]",
			    "dbtype    = \"mysql\"",
			    "dbhost    = \"localhost\"",
			    "dbname    = \"flyspray\"",
			    "dbprefix  = \"flyspray_\"",
			    "dbuser    = \"flyspray\"",
			    "dbpass    = \"$secret\""
			   );
	    ExecQuietFatal("$CHOWN nobody:tbadmin $FLYSPRAYCONF");
	    ExecQuietFatal("$CHMOD 750 $FLYSPRAYCONF");
	};
    };
}

1328
1329
1330
if ($WIKISUPPORT) {
    my $WIKIDIR       = "/usr/local/www/data/twiki";
    my $WIKICONF      = "LocalSite.cfg";
Robert Ricci's avatar
Robert Ricci committed
1331
    my $WIKIURL       = "http://$OUTER_BOSS/downloads/twiki-4.1.tgz";
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
    my $localtarfile  = "/tmp/twiki.tgz";
    my $CGISESSDIR    = "/var/db/cgisess";
    my $WIKI_LOGDIR   = "$LOGDIR/twiki";
    
    Phase "TWiki", "Installing TWiki", sub {
	PhaseSkip("TWiki not supported on ops")
	    if ($FBSD_VERSION < 6);

	Phase "fetching", "Fetching TWiki distribution", sub {
	    DoneIfExists($localtarfile);
	    FetchFileFatal($WIKIURL, $localtarfile);
	};

	if (! -e "$CGISESSDIR") {
	    mkdir("$CGISESSDIR", 0770) or
		PhaseFail("Unable to make $CGISESSDIR : $!");
	}
	ExecQuietFatal("$CHOWN nobody:wheel $CGISESSDIR");

	Phase "unpacking", "Unpacking TWiki distribution", sub {
	    if (! -e "$WIKIDIR") {
		mkdir("$WIKIDIR", 0770) or
		    PhaseFail("Unable to make $WIKIDIR : $!");
	    }
	    ExecQuietFatal("$CHMOD 770 $WIKIDIR");

1358
	    PhaseSkip("TWiki already unpacked")
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
		if (-e "$WIKIDIR/data");
	    ExecQuietFatal("$TAR zxf $localtarfile -C $WIKIDIR"); 
	};
	ExecQuietFatal("$CHOWN -R nobody:tbadmin $WIKIDIR");

	Phase "logdir", "Creating TWiki log directory", sub {
	    DoneIfExists($WIKI_LOGDIR);
	    mkdir $WIKI_LOGDIR, 0775 or
		PhaseFail("Unable to create $WIKI_LOGDIR : $!");
	    
	    ExecQuietFatal("$CHOWN nobody:tbadmin $WIKI_LOGDIR");
	    ExecQuietFatal("$CHMOD 770 $WIKI_LOGDIR");
	};

	Phase "config", "Configuring TWiki", sub {
	    # Must overwrite the distribution version.
	    ExecQuietFatal("$CP -f $TOP_OBJDIR/wiki/$WIKICONF ".
			   "       $WIKIDIR/lib/$WIKICONF");
	};
    };
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1381
1382
1383
exit(0)
    if ($ELABINELAB);

1384
1385
1386
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
1387
if (!PhaseWasSkipped("maillists")) {
1388
    print "Local mailing lists have been created, with no members, in\n";
Robert Ricci's avatar
Robert Ricci committed
1389
    print "$LIST_DIR . Please add members to the following lists:\n";
1390
    print map "$_\n", @LOCAL_MAILING_LISTS;
1391
}