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

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2003, 2004 University of Utah and the Flux Group.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# All rights reserved.
#

#
# install-boss.sh - Script to do the initial install of a boss node
#
# The main things it does not do yet:
# * Figure out where to put directories such as /usr/testbed - they must
#   already exist
# * Set up named - we probably have to do that later, when the interfaces table
#   is filled in
# * Set up a sup tree. Not sure what the right thing to do here is!
# * Doesn't do anything about SSL certificates for the web

#
# Configure variables
#
my $PREFIX     = '@prefix@';
my $SRCDIR     = '@srcdir@';
my $TOP_SRCDIR = '@top_srcdir@';
my $DBNAME     = "@TBDBNAME@";

my $OURDOMAIN  = '@OURDOMAIN@';

30 31
my $LOGFACIL   = '@TBLOGFACIL@';

32 33 34
my $USERNODE   = '@USERNODE@';
my $FSNODE     = '@FSNODE@';
my $BOSSNODE   = '@BOSSNODE@';
35 36
my $BOSSNODE_IP= '@BOSSNODE_IP@';
my $USERNODE_IP= '@USERNODE_IP@';
37 38 39 40 41 42 43

#
# Some programs we use
#
my $SH         = "/bin/sh";
my $CHMOD      = "/bin/chmod";
my $CHGRP      = "/usr/bin/chgrp";
44
my $CHOWN      = "/usr/sbin/chown";
45 46 47 48 49
my $PWD        = "/bin/pwd";
my $PW         = "/usr/sbin/pw";
my $PATCH      = "/usr/bin/patch";
my $SSH_KEYGEN = "/usr/bin/ssh-keygen";
my $PKG_INFO   = "/usr/sbin/pkg_info";
50
my $PKG_ADD    = "/usr/sbin/pkg_add";
51
my $TOUCH      = "/usr/bin/touch";
52
my $SSH        = "/usr/bin/ssh";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53
my $SCP        = "/usr/bin/scp";
54
my $CP         = "/bin/cp";
55
my $ENV        = "/usr/bin/env";
56
my $MOUNT      = "/sbin/mount";
57
my $SUDO       = "/usr/local/bin/sudo";
58 59 60 61 62 63 64 65 66

my $SUIDPERL   = "/usr/bin/suidperl";

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 $GMAKE      = "/usr/local/bin/gmake";
Robert Ricci's avatar
Robert Ricci committed
67
my $RNDC_CONFGEN = "/usr/local/sbin/rndc-confgen";
68

69 70 71 72
#
# Some files we edit/create
#
my $CRONTAB         = "/etc/crontab";
73
my $HOSTS           = "/etc/hosts";
74 75 76 77 78 79 80 81 82 83 84
my $FSTAB           = "/etc/fstab";
my $RCCONF          = "/etc/rc.conf";
my $SYSLOG_CONF     = "/etc/syslog.conf";
my $NEWSYSLOG_CONF  = "/etc/newsyslog.conf";

my $ROOT_PRIVKEY    = "/root/.ssh/identity";
my $ROOT_PUBKEY     = "$ROOT_PRIVKEY.pub";
my $ROOT_AUTHKEY    = "/root/.ssh/authorized_keys";

my $SUDOERS         = "/usr/local/etc/sudoers";
my $HTTPD_CONF      = "/usr/local/etc/apache/httpd.conf";
Robert Ricci's avatar
Robert Ricci committed
85
my $PHP_INI         = "/usr/local/etc/php.ini";
86 87 88 89 90 91
my $DHCPD_CONF	    = "/usr/local/etc/dhcpd.conf";
my $DHCPD_TEMPLATE  = "/usr/local/etc/dhcpd.conf.template";
my $DHCPD_LEASES    = "/var/db/dhcpd.leases";
my $DHCPD_MAKECONF  = "$PREFIX/sbin/dhcpd_makeconf";
my $APACHE_CERTFILE = "/usr/local/etc/apache/ssl.crt/www.${OURDOMAIN}.crt";
my $APACHE_KEYFILE  = "/usr/local/etc/apache/ssl.key/www.${OURDOMAIN}.key";
92 93 94
my $BATCHEXP        = "$PREFIX/bin/batchexp";
my $WAP             = "$PREFIX/sbin/withadminprivs";
my $NAMED_SETUP     = "$PREFIX/sbin/named_setup";
95 96 97 98

my $CRACKLIB_DICT   = "/usr/local/lib/pw_dict.pwd";

my $STL_PATCH       = "$TOP_SRCDIR/patches/g++.patch";
99
my $M2CRYPTO_PATCH  = "$TOP_SRCDIR/patches/m2crypto.patch";
100
my $PHP4_PATCH      = "$TOP_SRCDIR/patches/php4-Makefile.patch";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
101
my $INIT_PRIVKEY    = "$TOP_SRCDIR/install/identity";
102

Robert Ricci's avatar
Robert Ricci committed
103 104
my $SSH_CONFIG      = "/etc/ssh/ssh_config";

105 106
my $RNDC_KEY        = "/usr/local/etc/rndc.key";

107 108
my $LOADER_CONF	    = "/boot/loader.conf";

109 110
my $SYSCTL_CONF	    = "/etc/sysctl.conf";

111
my $EMULAB_PEM	    = "emulab.pem";
112 113 114
my $CLIENT_PEM	    = "client.pem";
my $CTRLNODE_PEM    = "ctrlnode.pem";
my $ETC_EMULAB_DIR  = "/etc/emulab";
115 116
my $APACHE_CERTPEM  = "apache_cert.pem";
my $APACHE_KEYPEM   = "apache_key.pem";
117

118 119 120
#
# Some directories we care about
#
121 122 123 124 125 126 127 128 129
my $LOGDIR        = "$PREFIX/log";
my $MYSQL_LOGDIR  = "$LOGDIR/mysql";
my $RCDIR         = "/usr/local/etc/rc.d";
my $USERSVAR_DIR  = "$PREFIX/usersvar";
my $OPSDIR_DIR    = "$PREFIX/opsdir";
my $PORTSDIR      = "/usr/ports/misc";
my $MIBPATH       = "/usr/local/share/snmp/mibs";
my $TFTP_DIR      = "$PREFIX/tftpboot";
my $TFTP_PROJ_DIR = "$TFTP_DIR/proj";
130
my $VARRUN        = "/var/run";
131
my $ETCSSH	  = "/etc/ssh";
132 133 134 135 136 137 138 139 140 141 142

#
# URLs
#
my $CISCO_MIB_FTP = "ftp://ftp.cisco.com/pub/mibs/v2";

#
# And some lists that we use
#
my @TESTBED_DIRS = ($PREFIX);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
143 144
my @MOUNTPOINTS = ("$PREFIX/usersvar", "$PREFIX/opsdir",
		   "/users", "/proj", "/groups", "/share");
Robert Ricci's avatar
Robert Ricci committed
145

146 147
my @LOGFILES = ("$LOGDIR/bootinfo.log", "$LOGDIR/tmcd.log",
    "$LOGDIR/capture.log", "$LOGDIR/dhcpd.log", "$LOGDIR/capserver.log",
148
    "$LOGDIR/frisbeed.log", "$LOGDIR/proxydhcpd.log",
149
    "$LOGDIR/elvind.log", "$LOGDIR/stated.log", "$LOGDIR/osselect.log",
150
    "$LOGDIR/tftpd.log", "$LOGDIR/sdcollectd.log", "$LOGDIR/genlastlog.log",
151
    "$LOGDIR/sshxmlrpc.log", "$LOGDIR/plabgetfree.log", "$LOGDIR/xmlrpcbag.log",
152
    "$LOGDIR/plabrenew.log", "$LOGDIR/sslxmlrpc.log");
153 154 155 156

my @CISCO_MIBS = ("CISCO-SMI", "CISCO-TC", "CISCO-VTP-MIB", "CISCO-PAGP-MIB",
    "CISCO-PRIVATE-VLAN-MIB", "CISCO-STACK-MIB", "CISCO-VLAN-MEMBERSHIP-MIB");

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
# Initial experiments to create. Indexed by eid.
my %EXPERIMENTS =
    ("hwdown"	     => {"pid"	       => "emulab-ops",
			 "description" => "Node reported as down"},
     "reloading"     => {"pid"	       => "emulab-ops",
			 "description" => "Nodes reloading images"},
     "reloadpending" => {"pid"	       => "emulab-ops",
			 "description" => "Nodes waiting for reload"},
     "oldreserved"   => {"pid"	       => "emulab-ops",
			 "description" => "Nodes in limbo during swap modify"},
     "nfree-leases"  => {"pid"	       => "emulab-ops",
			 "description" => "Nodes in limbo during nfree"},
     "opsnodes"	     => {"pid"	       => "emulab-ops",
			 "description" => "Nodes designated as Ops Nodes"},
    );

173 174 175 176 177 178
#
# List of names that goes into $HOSTS and which must resolve.
# 
my @OPS_NAMES = ($USERNODE, "users", "ops");
push(@OPS_NAMES, $FSNODE)
    if ($USERNODE ne $FSNODE);
Robert Ricci's avatar
Robert Ricci committed
179

180 181 182 183
#
# The meta-port (name and version) that drags in all the dependancies for
# a boss node
#
Robert Ricci's avatar
Robert Ricci committed
184
my $BOSS_PORT = "emulab-boss-1.7";
185

186 187 188 189 190 191 192 193
#
# 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/..";

194 195 196
my $ELVIND_CONF     = "/usr/local/etc/elvind.conf";
my $BOSS_ELVIND_CONF = "$TOP_OBJDIR/event/etc/elvind-boss.conf";

197 198 199 200 201 202 203
#
# Allow this to work if the library is left in the source directory
#
use lib '@srcdir@';
   
use English;
use libinstall;
204 205 206 207 208 209 210 211 212 213 214
use Getopt::Std;

#
# Handle command-line options
#
sub usage {
    print "Usage: ops-install [-p packagedir]\n";
    exit(1);
}

my $packagedir = "";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
215
my $batchmode  = 0;
216
my $password;
217 218
my %opts;

219
if (! getopts("p:bw:", \%opts)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
220 221 222 223 224 225 226
    usage();
}
if (defined($opts{p})) {
    $packagedir = $opts{p};
}
if (defined($opts{b})) {
    $batchmode = 1;
227
}
228 229 230
if (defined($opts{w})) {
    $password = $opts{w};
}
231 232 233 234

if (@ARGV) {
    usage();
}
235 236 237 238

#
# Make sure they know what they're getting into...
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
239 240 241 242 243 244 245 246 247 248
if (! $batchmode) {
    warn "***** Please run install-ops on ops, and reboot it, before running\n";
    warn "this script!\n\n";
    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 boss node. Continue? [y/N] ";
    my $response = <>;
    die "Installation aborted!\n" unless ($response =~ /^y/i);
}
249 250 251 252 253

if ($UID != 0) {
    die "This script must be run as root.\n";
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
254
Phase "usersgroups", "Creating users and groups", sub {
255 256 257 258 259 260 261 262 263 264 265 266
    Phase "tbadmin", "Creating tbadmin group", sub {
	if (getgrnam("tbadmin")) {
	    PhaseSkip("tbadmin group already exists");
	}
	ExecQuietFatal("$PW groupadd tbadmin -g 101");
    };
    Phase "root", "Creating root group", sub {
	if (getgrnam("root")) {
	    PhaseSkip("root group already exists");
	}
	ExecQuietFatal("$PW groupadd root -g 103");
    };
Leigh B. Stoller's avatar
Leigh B. Stoller committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    # 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 /var/db/mysql -s /sbin/nologin -c 'MySQL Daemon'");
	ExecQuietFatal("$CHOWN mysql:mysql /var/db/mysql");	
    };
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
};

Phase "dirs", "Setting directory permissions", sub {
    foreach my $dir (@TESTBED_DIRS) {
	Phase $dir, $dir, sub {
	    if (!-d $dir) {
		PhaseFail("Directory $dir does not exist");
	    }
	    my ($mode,$group) = (stat($dir))[2,5];
	    # Fix up the mode (strip file type)
	    $mode = $mode & 0777;
	    if ($mode == 0775 && $group eq getgrnam("tbadmin")) {
		PhaseSkip("Already done");
	    }
	    ExecQuietFatal("$CHGRP tbadmin $dir");
	    ExecQuietFatal("$CHMOD 0775 $dir");
	};
    }
};

Robert Ricci's avatar
Robert Ricci committed
302 303 304 305 306 307 308
Phase "tftp", "Setting up directories for tftp", sub {
    Phase "tftpboot", "Creating $TFTP_DIR", sub {
	DoneIfExists($TFTP_DIR);
	mkdir $TFTP_DIR,0775 or
	    PhaseFail("Unable to create $TFTP_DIR : $!");
	ExecQuietFatal("$CHGRP tbadmin $TFTP_DIR");
    };
309 310 311 312 313 314
    Phase "tftpproj", "Creating $TFTP_PROJ_DIR", sub {
	DoneIfExists($TFTP_PROJ_DIR);
	mkdir $TFTP_PROJ_DIR,0775 or
	    PhaseFail("Unable to create $TFTP_PROJ_DIR : $!");
	ExecQuietFatal("$CHGRP tbadmin $TFTP_PROJ_DIR");
    };
Robert Ricci's avatar
Robert Ricci committed
315 316 317 318
    Phase "tftplink", "Linking /tftpboot", sub {
	DoneIfExists("/tftpboot");
	ExecQuietFatal("ln -s $TFTP_DIR /tftpboot");
    };
319 320 321
};

Phase "ports", "Installing ports", sub {
322 323 324
    if (!ExecQuiet("$PKG_INFO -e $BOSS_PORT")) {
	PhaseSkip("Ports already installed");
    }
325 326 327 328 329 330
    Phase "packages", "Installing packages", sub {
	if (!$packagedir) {
	    PhaseSkip("No package directory provided");
	}
	ExecQuietFatal("$ENV PKG_PATH=$packagedir $PKG_ADD $BOSS_PORT");
    };
331 332 333
    if ($packagedir) {
	PhaseSkip("Packages installed okay");
    }
334 335 336 337
    Phase "pcopy", "Copying ports into place", sub {
	DoneIfExists("$PORTSDIR/emulab-boss");
	ExecQuietFatal("$SH $SRCDIR/ports/ports-install");
    };
338 339 340 341 342
    # Ick. The php4 port is broken with SSL, so we have to patch it - hopefully
    # it'll get fixed someday, and we remove this
    Phase "php4patch", "Patching php4 port", sub {
	if (!ExecQuiet("$PATCH -C -f -l -R -p0 -i $PHP4_PATCH")) {
	    PhaseSkip("$PHP4_PATCH already applied");
343
	}
344
	ExecQuietFatal("$PATCH -f -l -p0 -i $PHP4_PATCH");
345
    };
346 347 348 349
    PhaseFail("Please install ports manually, since some\n of them are " .
	      "interactive. Run: \n" .
	      "cd $PORTSDIR/emulab-boss && make install\n" .
	      "then re-run this script.");
350 351 352 353 354 355 356 357 358
};

Phase "patches", "Applying patches", sub {
    Phase "g++patch", "Patching g++'s STL", sub {
	if (!ExecQuiet("$PATCH -C -f -R -p0 -i $STL_PATCH")) {
	    PhaseSkip("$STL_PATCH already applied");
	}
	ExecQuietFatal("$PATCH -f -p0 -i $STL_PATCH");
    };
359
    Phase "m2cryptopatch", "Patching m2crypto", sub {
360
	if (!ExecQuiet("$PATCH -C -f -l -R -p0 -i $M2CRYPTO_PATCH")) {
361 362
	    PhaseSkip("$M2CRYPTO_PATCH already applied");
	}
363
	ExecQuietFatal("$PATCH -f -l -p0 -i $M2CRYPTO_PATCH");
364
    };
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
};

Phase "cracklib", "Installing cracklib", sub {
    DoneIfExists("$CRACKLIB_DICT");
    my $pwd = `$PWD`;
    chomp $pwd;
    chdir "$TOP_SRCDIR/tbsetup/checkpass/cracklib,2.7" or
	PhaseFail "Unable to change to " .
	    "$TOP_SRCDIR/tbsetup/checkpass/cracklib,2.7: $!";
    ExecQuietFatal("make install clean");
    chdir $pwd;
};

Phase "apache", "Installing apache config file", sub {
    DoneIfEdited("$HTTPD_CONF");
    # ICK!!! If we installed apache AFTER we unpacked the source tarball,
    # make will not properly install the new apache config file! So, we use
    # this shameful hack to force it to do so!
    ExecQuietFatal("$TOUCH -t 01010000 $HTTPD_CONF");
    ExecQuietFatal("$GMAKE -C $TOP_OBJDIR/apache install");
};

Phase "rc.d", "Setting up rc.d scripts", sub {
    Phase "my-client", "Moving $RCDIR/mysql-client.sh", sub {
	DoneIfDoesntExist("$RCDIR/mysql-client.sh");
	ExecQuietFatal("mv $RCDIR/mysql-client.sh $RCDIR/1.mysql-client.sh");
    };
    Phase "my-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: $!");
	}
    };
398
    Phase "snmpd", "Removing snmpd startup script",  sub {
399 400 401 402 403
	DoneIfDoesntExist("$RCDIR/snmpd.sh");
	if (!unlink "$RCDIR/snmpd.sh") {
	    PhaseFail("Unable to remove $RCDIR/snmpd.sh: $!");
	}
    };
404 405 406 407 408 409 410
    Phase "rsyncd", "Removing rsyncd startup script",  sub {
	DoneIfDoesntExist("$RCDIR/rsyncd.sh");
	if (!unlink "$RCDIR/rsyncd.sh") {
	    PhaseFail("Unable to remove $RCDIR/rsyncd.sh: $!");
	}
    };
    Phase "dhcpd", "Removing isc-dhcpd startup script",  sub {
411 412 413 414 415
	DoneIfDoesntExist("$RCDIR/isc-dhcpd.sh");
	if (!unlink "$RCDIR/isc-dhcpd.sh") {
	    PhaseFail("Unable to remove $RCDIR/isc-dhcpd.sh: $!");
	}
    };
416
    Phase "rc.testbed", "Installing testbed RC scripts", sub {
417 418 419 420
        Phase "elvind.sh", "Removing port version of elvind.sh", sub {
	    DoneIfDoesntExist("$RCDIR/elvind.sh");
            ExecQuietFatal("/bin/rm -f $RCDIR/elvind.sh");
        };
421 422 423 424 425
	DoneIfExists("$RCDIR/3.testbed.sh");
	ExecQuietFatal("$GMAKE -C $TOP_OBJDIR/rc.d install");
    };
};

426 427 428
Phase "syslog", "Setting up syslog", sub {
    Phase "sysconf", "Editing $SYSLOG_CONF", sub {
	DoneIfEdited($SYSLOG_CONF);
429 430

	#
431 432
	# Modify the /dev/console and /var/log/messages lines to exclude
	# testbed stuff
433 434 435 436 437 438 439 440 441 442
	#
	open(SC,"+<$SYSLOG_CONF") or
	    PhaseFail("Unable to open $SYSLOG_CONF : $!");
	my @sc = <SC>;
	if (scalar(grep(/$LOGFACIL/, @sc)) != 0) {
	    PhaseFail("Testbed chosen facility $LOGFACIL already in use in /etc/syslog.conf!");
	}
	seek(SC,0,0);
	truncate(SC,0);
	foreach my $line (@sc) {
443 444 445 446 447 448 449
	    my $cpat = q(\s+/dev/console);
	    my $mpat = q(\s+/var/log/messages);
	    if ($line =~ /^[^#].*$cpat/) {
		$line =~ s/($cpat)/\;$LOGFACIL.none$1/;
	    }
	    elsif ($line =~ /^[^#].*$mpat/) {
		$line =~ s/($mpat)/\;$LOGFACIL.none$1/;
450 451 452 453 454
	    }
	    print SC $line;
	}
	close(SC);

455 456 457 458 459
	AppendToFileFatal($SYSLOG_CONF,
	    "!bootinfo",  "*.*\t\t\t\t\t\t$LOGDIR/bootinfo.log",
	    "!tmcd",      "*.*\t\t\t\t\t\t$LOGDIR/tmcd.log",
	    "!capture",   "*.*\t\t\t\t\t\t$LOGDIR/capture.log",
	    "!dhcpd",     "*.*\t\t\t\t\t\t$LOGDIR/dhcpd.log",
460 461
	    "!proxydhcpd","*.*\t\t\t\t\t\t$LOGDIR/proxydhcpd.log",
	    "!tftpd",     "*.*\t\t\t\t\t\t$LOGDIR/tftpd.log",
462 463 464 465
	    "!capserver", "*.*\t\t\t\t\t\t$LOGDIR/capserver.log",
	    "!frisbeed",  "*.*\t\t\t\t\t\t$LOGDIR/frisbeed.log",
	    "!elvind",    "*.*\t\t\t\t\t\t$LOGDIR/elvind.log",
	    "!stated",    "*.*\t\t\t\t\t\t$LOGDIR/stated.log",
466 467
	    "!osselect",  "*.*\t\t\t\t\t\t$LOGDIR/osselect.log",
	    "!genlastlog","*.*\t\t\t\t\t\t$LOGDIR/genlastlog.log",
468
	    "!sdcollectd","*.*\t\t\t\t\t\t$LOGDIR/sdcollectd.log",
469
	    "!plabgetfree","*.*\t\t\t\t\t\t$LOGDIR/plabgetfree.log",
470
	    "!plabrenew", "*.*\t\t\t\t\t\t$LOGDIR/plabrenew.log",
471
	    "!xmlrpcbag", "*.*\t\t\t\t\t\t$LOGDIR/xmlrpcbag.log",
472 473
	    "!sshxmlrpc", "*.*\t\t\t\t\t\t$LOGDIR/sshxmlrpc.log",
	    "!sslxmlrpc", "*.*\t\t\t\t\t\t$LOGDIR/sslxmlrpc.log");
474 475 476 477 478 479
    };

    Phase "logdir", "Creating log directory", sub {
	DoneIfExists($LOGDIR);
	mkdir $LOGDIR, 0775 or PhaseFail("Unable to create $LOGDIR : $!");
	ExecQuietFatal("$CHGRP tbadmin $LOGDIR");
480
	ExecQuietFatal("$CHMOD 775 $LOGDIR");
481 482 483 484 485 486
    };

    Phase "logdir", "Creating mysql log directory", sub {
	DoneIfExists($MYSQL_LOGDIR);
	mkdir $MYSQL_LOGDIR, 0775 or
	    PhaseFail("Unable to create $MYSQL_LOGDIR : $!");
487 488
	ExecQuietFatal("$CHOWN mysql:mysql $MYSQL_LOGDIR");
	ExecQuietFatal("$CHMOD 775 $MYSQL_LOGDIR");
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    };

    Phase "logfiles", "Creating log files", sub {
	foreach my $logfile (@LOGFILES) {
	    Phase $logfile, $logfile, sub {
		DoneIfExists($logfile);
		CreateFileFatal($logfile);
		ExecQuietFatal("$CHGRP tbadmin $logfile");
		ExecQuietFatal("$CHMOD 640 $logfile");
	    };
	}
    };

    Phase "newsyslog", "Setting up $NEWSYSLOG_CONF", sub {
	DoneIfEdited($NEWSYSLOG_CONF);
	AppendToFileFatal($NEWSYSLOG_CONF,
	    "$LOGDIR/tmcd.log               640  9     1000 *     Z",
	    "$LOGDIR/stated.log             640  9     300  *     Z",
	    "$LOGDIR/osselect.log           640  9     300  *     Z",
	    "$LOGDIR/power.log              640  7     300  *     Z",
	    "$LOGDIR/frisbeed.log           640  7     300  *     Z",
	    "$LOGDIR/tftpd.log              640  7     200  *     Z",
	    "$LOGDIR/dhcpd.log              640  7     200  *     Z",
	    "$LOGDIR/bootinfo.log           640  7     200  *     Z",
	    "$LOGDIR/capserver.log          640  5     200  *     Z",
	    "$LOGDIR/elvind.log             640  5     1000 *     Z",
	    "$LOGDIR/suexec.log             640  3     200  *     Z",
516
	    "$LOGDIR/genlastlog.log         640  3     200  *     Z",
517
	    "$LOGDIR/genlastlog             640  3     200  *     Z " .
518 519
	        "/var/run/lastlog_daemon.pid",
	    "$LOGDIR/plabmetrics.log        640  7     1000 *     Z",
520
	    "$LOGDIR/plablinkdata.log       640  7     1000 *     Z",
521
	    "$LOGDIR/xmlrpcbag.log          640  7     300  *     Z",
522 523
	    "$LOGDIR/sshxmlrpc.log          640  7     300  *     Z",
	    "$LOGDIR/sslxmlrpc.log          640  7     300  *     Z");
524 525 526
    };
};

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
Phase "database", "Setting up database", sub {
    Phase "mysql", "Starting mysql", sub {
	if (!ExecQuiet("$MYSQLADMIN ping")) {
	    PhaseSkip("mysqld already running");
	}
	ExecQuietFatal("$RCDIR/2.mysql-server.sh start");
	# Give mysqld some time to start, then make sure it did
	sleep 5;
	ExecQuietFatal("$MYSQLADMIN ping");
    };
    Phase "$DBNAME", "Creating $DBNAME", sub {
	if (!ExecQuiet("$MYSQLSHOW $DBNAME")) {
	    PhaseSkip("tbdb already exists");
	}
	ExecQuietFatal("$MYSQLADMIN create $DBNAME");
    };
    Phase "tables", "Creating tables in $DBNAME", sub {
	if (!ExecQuiet("$MYSQLDUMP -d $DBNAME users")) {
	    PhaseSkip("Tables have already been created");
	}
	ExecQuietFatal("$MYSQL $DBNAME < $TOP_SRCDIR/sql/database-create.sql");
    };
    Phase "dbdata", "Filling tables with initial data", sub {
	my ($exitval, @rows) = ExecQuiet("echo 'select * from " .
	    "exported_tables' | $MYSQL -s $DBNAME");
	if ($exitval) {
	    PhaseFail("Error running query");
	}
	if (scalar @rows) {
	    PhaseSkip("Already done");
	}
	ExecQuietFatal("$MYSQL $DBNAME < $TOP_SRCDIR/sql/database-fill.sql");
    };
560 561 562 563 564 565 566 567 568 569 570 571
    Phase "sdbdata", "Filling tables with supplemental data", sub {
	my ($exitval, @rows) = ExecQuiet("echo 'select * from " .
	    "os_info' | $MYSQL -s $DBNAME");
	if ($exitval) {
	    PhaseFail("Error running query");
	}
	if (scalar @rows) {
	    PhaseSkip("Already done");
	}
	ExecQuietFatal("$MYSQL $DBNAME < " .
	    "$TOP_SRCDIR/sql/database-fill-supplemental.sql");
    };
Robert Ricci's avatar
Robert Ricci committed
572 573 574 575 576 577 578 579 580 581 582
    Phase "sitevars", "Setting sitevars to default values", sub {
	my ($exitval, @rows) = ExecQuiet("echo 'select * from " .
	    "sitevariables' | $MYSQL -s $DBNAME");
	if ($exitval) {
	    PhaseFail("Error running query");
	}
	if (scalar @rows) {
	    PhaseSkip("Already done");
	}
	ExecQuietFatal("$MYSQL $DBNAME < $TOP_SRCDIR/sql/sitevars-create.sql");
    };
583 584 585 586 587
};

Phase "rc.conf", "Adding testbed content to $RCCONF", sub {
    DoneIfEdited($RCCONF);
    AppendToFileFatal($RCCONF,
Robert Ricci's avatar
Robert Ricci committed
588 589 590 591 592
	qq|nfs_server_enable="YES"|,
	qq|nfs_server_flags="-u -t -n 16"|,
	qq|nfs_client_enable="YES"|,
	qq|inetd_enable="YES"|,
	qq|inetd_flags="-wW -R 0"|,
593
	qq|named_enable="NO"|,
Robert Ricci's avatar
Robert Ricci committed
594 595
	qq|xntpd_enable="YES"|,
	qq|syslogd_flags=""|,
596 597
	qq|tftpd_flags="-lvvvv -C 40 -s /tftpboot"|,
    	qq|apache_enable="YES"|);
598 599 600 601 602 603 604
};

Phase "suidperl", "Setting the suid bit on $SUIDPERL", sub {
    PhaseSkip("Already done") if (-u $SUIDPERL);
    ExecQuietFatal("$CHMOD u+s $SUIDPERL");
};

605 606 607 608 609 610 611
Phase "hosts", "Adding boss/ops IP addresses to $HOSTS", sub {
    DoneIfEdited($HOSTS);
    AppendToFileFatal($HOSTS,
		      "${BOSSNODE_IP}\t${BOSSNODE} boss",
		      "${USERNODE_IP}\t@OPS_NAMES");
};

Robert Ricci's avatar
Robert Ricci committed
612
Phase "resolve", "Checking to make sure names for ops resolve", sub {
613
    foreach my $name (@OPS_NAMES, ${BOSSNODE}, "boss") {
Robert Ricci's avatar
Robert Ricci committed
614 615 616 617 618 619 620 621 622 623 624
	Phase $name, $name, sub {
	    if (gethostbyname($name)) {
		PhaseSkip("$name resolves");
	    } else {
		PhaseFail("$name does not resolve - please see setup.txt\n" .
		    "for further instructions!");
	    }
	};
    }
};

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
Phase "NFSmounts", "Setting up NFS mounts", sub {
    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);
	AppendToFileFatal($FSTAB,
		"$FSNODE:/users\t\t/users\tnfs\trw,nodev,nosuid\t0\t0",
		"$FSNODE:/proj\t\t/proj\tnfs\trw,nodev,nosuid\t0\t0",
		"$FSNODE:/groups\t\t/groups\tnfs\trw,nodev,nosuid\t0\t0",
		"$FSNODE:/share\t\t/share\tnfs\trw,nodev,nosuid\t0\t0",
		"$USERNODE:/usr/testbed\t\t$OPSDIR_DIR\tnfs\trw,soft,".
			  "-b,nodev,nosuid\t0\t0",
		"$USERNODE:/var\t\t$USERSVAR_DIR\tnfs\tro,soft,".
			  "-b,nodev,nosuid\t0\t0");
    };
    Phase "mounts", "Mounting NFS filesystems", sub {
	foreach my $dir (@MOUNTPOINTS) {
	    Phase $dir, $dir, sub {
		DoneIfMounted($dir);
		ExecQuietFatal("$MOUNT -o '-R 1' $dir");
	    };
	}
    };
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
};

Phase "mibs", "Fetching Cisco MIBs", sub {
    foreach my $mib (@CISCO_MIBS) {
	my $localfile = "$MIBPATH/$mib.txt";
	my $mibURL = "$CISCO_MIB_FTP/$mib.my";
	Phase "$mib", "Fetching $mib", sub {
	    DoneIfExists($localfile);
	    FetchFileFatal($mibURL,$localfile);
	};
    }
};

Phase "cron", "Adding cron jobs", sub {
    Phase "crontab", "Editing $CRONTAB", sub {
	DoneIfEdited($CRONTAB);
	AppendToFileFatal($CRONTAB,
672
	    "0  \t6\t*\t*\t*\troot\t$PREFIX/sbin/audit",
673 674 675
	    "45 \t1\t*\t*\t*\troot\t$PREFIX/sbin/backup",
	    "*/5\t*\t*\t*\t*\troot\t$PREFIX/sbin/node_status",
	    "*/5\t*\t*\t*\t*\troot\t$PREFIX/sbin/idlemail");
676 677 678 679 680 681 682 683
    };
};

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

Robert Ricci's avatar
Robert Ricci committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697
Phase "php.ini", "Creating php.ini file", sub {
    DoneIfExists($PHP_INI);
    CreateFileFatal($PHP_INI,
	"[PHP]","",
	";",
	"; So that quotes are not escaped. Needed for netbuild application.",
	";", 
	"magic_quotes_gpc        =       Off","",
	";",
	"; Our scripts depend on this!",
	";",
	"register_globals        =       On");
};

698
Phase "ssh", "Setting up root ssh from boss to ops", sub {
Robert Ricci's avatar
Robert Ricci committed
699 700 701 702
    Phase "keygen", "Creating root private key", sub {
	DoneIfExists($ROOT_PRIVKEY);
	ExecQuietFatal("$SSH_KEYGEN -t rsa1 -P '' -f $ROOT_PRIVKEY");
    };
703 704 705 706 707 708 709 710 711 712 713
    #
    # Stick it into the DB.
    # WARNING: This sitevar (node/ssh_pubkey) is referenced in tmcd.c
    #
    Phase "sitevar", "Inserting pubkey into DB", sub {
	my $pubkey = `cat $ROOT_PUBKEY`;
	chomp $pubkey;
	ExecQuietFatal("echo \"update sitevariables set value='$pubkey' ".
		       "       where name='node/ssh_pubkey'\" | ".
		       "$MYSQL $DBNAME");
    };
Robert Ricci's avatar
Robert Ricci committed
714 715 716 717 718 719 720
    Phase "ssh", "Editing ssh config file", sub {
	DoneIfEdited($SSH_CONFIG);
	AppendToFileFatal($SSH_CONFIG,
	    "Host *",
	    "   StrictHostKeyChecking no",
	    "   Protocol 1,2");
    };
721
    Phase "keycopy", "Copy root ssh keys to ops", sub {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
722
	if (! ExecQuiet("$SSH -o 'BatchMode=yes' root\@${USERNODE} pwd")) {
723 724
	    PhaseSkip("Key already copied");
	} else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
725 726 727
	    ExecQuietFatal("$SCP -i $INIT_PRIVKEY ".
			   "$ROOT_PUBKEY ${USERNODE}:$ROOT_AUTHKEY");

728 729 730 731
	    # Copy hosts keys to ops.
	    ExecQuietFatal("cat ${ETCSSH}/*.pub | $SSH ${USERNODE} ".
			   "  '(cat > ${ETCSSH}/ssh_known_hosts)'");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
732 733 734 735 736 737
	    if (ExecQuiet("$SSH -o 'BatchMode=yes' root\@${USERNODE} pwd")) {
		PhaseFail("You'll need to manually copy boss's public SSH key\n".
			  "over to ops print so boss can get into ops without\n".
			  "a password. Run the following print as root:\n" .
			  "scp $ROOT_PUBKEY ${USERNODE}:$ROOT_AUTHKEY");
	    }
738
	}
739
    };
740 741
};

742 743 744 745 746
Phase "rndc", "Setting up rndc for control of nameserver", sub {
    DoneIfExists($RNDC_KEY);
    ExecQuietFatal("$RNDC_CONFGEN -a -r /dev/urandom");
};

747 748 749 750 751 752 753
Phase "loader.conf", "Setting up $LOADER_CONF", sub {
    DoneIfEdited($LOADER_CONF);
    AppendToFileFatal($LOADER_CONF,
	"kern.hz=1000"
    );
};

754 755 756 757 758 759 760 761
Phase "sysctl.conf", "Setting up $SYSCTL_CONF", sub {
    DoneIfEdited($SYSCTL_CONF);
    AppendToFileFatal($SYSCTL_CONF,
	"net.local.dgram.maxdgram=65536",
	"net.local.dgram.recvspace=65536"
    );
};

762 763 764 765 766 767 768 769 770
Phase "sslcerts", "Setting up SSL certificates", sub {
    Phase "sslgen", "Generating SSL certificates", sub {
	DoneIfExists("$TOP_OBJDIR/ssl/$EMULAB_PEM");
	ExecQuietFatal("$GMAKE -C $TOP_OBJDIR/ssl remote-site");
    };
    Phase "sslinstall", "Installing SSL certificates", sub {
	DoneIfExists("$PREFIX/etc/$EMULAB_PEM");
	ExecQuietFatal("$GMAKE -C $TOP_OBJDIR/ssl remote-site-boss-install");
    };
771
    Phase "sslopscopy", "Copying SSL certificates to ops", sub {
772 773
 	ExecQuietFatal("$SSH -o 'BatchMode=yes' root\@${USERNODE} ".
		       "     mkdir -p ${ETC_EMULAB_DIR} ");
774 775 776 777 778 779 780 781
 	ExecQuietFatal("$SCP $PREFIX/etc/$EMULAB_PEM ".
 		       "     ${USERNODE}:${ETC_EMULAB_DIR}");
 	ExecQuietFatal("$SCP $TOP_OBJDIR/ssl/$CTRLNODE_PEM".
		       "     ${USERNODE}:${ETC_EMULAB_DIR}/${CLIENT_PEM}");
 	ExecQuietFatal("$SSH -o 'BatchMode=yes' root\@${USERNODE} ".
		       "     chmod 640 ${ETC_EMULAB_DIR}/${CLIENT_PEM}; ".
		       "     chmod 640 ${ETC_EMULAB_DIR}/${EMULAB_PEM}  ");
    };
782
    Phase "apache", "Setting up Apache", sub {
783 784 785 786 787 788 789 790 791 792
	Phase "cert", "Installing Apache SSL certificate", sub {
	    DoneIfExists("$APACHE_CERTFILE");
	    ExecQuietFatal("$CP $TOP_OBJDIR/ssl/$APACHE_CERTPEM ".
			   "    $APACHE_CERTFILE");
	};
	Phase "key", "Installing Apache SSL key", sub {
	    DoneIfExists("$APACHE_KEYFILE");
	    ExecQuietFatal("$CP $TOP_OBJDIR/ssl/$APACHE_KEYPEM ".
			   "    $APACHE_KEYFILE");
	};
793
	Phase "rc.d", "Installing Apache startup file", sub {
794 795 796
	    DoneIfExists("$RCDIR/apache.sh");
	    ExecQuietFatal("mv $RCDIR/apache.sh.sample $RCDIR/apache.sh");
	};
797 798 799 800
	Phase "starting", "Starting Apache server", sub {
	    DoneIfExists("$VARRUN/httpd.pid");
	    ExecQuietFatal("$RCDIR/apache.sh start");
	};
801
    };
802 803
};

804 805
Phase "event", "Setting up event system", sub {
    Phase "elvinconf", "Installing elvind config file", sub {
806
	DoneIfIdentical($ELVIND_CONF,$BOSS_ELVIND_CONF);
807 808
	ExecQuietFatal("$CP $BOSS_ELVIND_CONF $ELVIND_CONF");
    };
809 810 811 812 813 814 815 816 817 818
    # For elabinelab, be sure to kill off running event system. Harmless.
    Phase "stopping", "Stopping event system", sub {
	if (ExecQuiet("killall -s elvind")) {
	    PhaseSkip("elvind not running");
	}
	ExecQuietFatal("$RCDIR/2.elvind.sh stop");
    };
    Phase "starting", "Starting event system", sub {
	ExecQuietFatal("$RCDIR/2.elvind.sh start");
    };
819 820
};

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
#
# Build and install software. Note that I am not looking to see if its
# been done; I suppose we could touch a file to indicate that build has
# been completed, but maybe that is not such a good idea.
#
Phase "Software", "Building and Installing Software", sub {
    Phase "building", "Building (please be patient)", sub {
	ExecQuietFatal("cd $TOP_OBJDIR; $GMAKE");
    };
    Phase "installing", "Installing (please be patient)", sub {
	ExecQuietFatal("cd $TOP_OBJDIR; $GMAKE boss-install");
    };
    Phase "postinstall", "Post Installing Testbed Software", sub {
	ExecQuietFatal("cd $TOP_OBJDIR; $GMAKE post-install");
    };
};

838 839 840 841
#
# The next few items must be after the software install since they use
# testbed libraries and such.
#
842 843
Phase "dhcpd", "Setting up initial dhcpd configuration", sub {
    Phase "template", "Installing $DHCPD_TEMPLATE", sub {
844
	ExecQuietFatal("cd $TOP_OBJDIR/dhcpd; $GMAKE install");
845 846
    };
    Phase "config", "Creating $DHCPD_CONF from template", sub {
847
	ExecQuietFatal("$DHCPD_MAKECONF -i");
848 849 850 851 852 853 854 855
    };
    # How silly is this?
    Phase "leases", "Creating stub leases file", sub {
	DoneIfExists("$DHCPD_LEASES");
	ExecQuietFatal("touch $DHCPD_LEASES");
    };
};

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
Phase "named", "Setting up initial named configuration", sub {
    Phase "building", "Building named files and templates", sub {
	ExecQuietFatal("cd $TOP_OBJDIR/named; $GMAKE");
    };
    Phase "installing", "Installing named files and templates", sub {
	ExecQuietFatal("cd $TOP_OBJDIR/named; $GMAKE install-real");
    };
    Phase "generating", "Generating named zone files", sub {
	ExecQuietFatal("$NAMED_SETUP -norestart");
    };
    Phase "stopping", "Stopping named", sub {
	if (ExecQuiet("killall -s named")) {
	    PhaseSkip("named not running");
	}
	ExecQuietFatal("$RCDIR/1.named.sh stop");
    };
    Phase "starting", "Starting named", sub {
	ExecQuietFatal("$RCDIR/1.named.sh start");
    };
};

Phase "firstuser", "Setting up initial user (elabman)", sub {
    PhaseSkip("elabman already created")
	if (-d "/users/elabman");
    ExecQuietFatal("perl $TOP_OBJDIR/utils/firstuser ".
		   ($batchmode ? " -b" : "") .
		   (defined($password) ? " -p $password" : ""));
};

Phase "experiments", "Setting up system experiments", sub {
    foreach my $eid (keys(%EXPERIMENTS)) {
	my $pid  = $EXPERIMENTS{$eid}->{"pid"};
	my $desc = $EXPERIMENTS{$eid}->{"description"};
	
	Phase "$pid/$eid", "$pid/$eid", sub {
	    PhaseSkip("Experiment Created")
		if (-d "/proj/$pid/exp/$eid");
	    ExecQuietFatal("$SUDO -u elabman $WAP $BATCHEXP ".
894
			   "  -q -i -w -f -n -S 'System Experiment' ".
895 896 897 898 899 900 901
			   "  -L 'System Experiment' ".
			   "  -E '$desc - DO NOT DELETE' ".
			   "  -p $pid -e $eid");
	};
    }
};

902 903 904 905 906
print "----------------------------------------------------------------------\n";
print "Installation completed succesfully!\n";
print "Please reboot this machine before proceeding with boss setup\n";

exit 0;