targetsys 13.2 KB
Newer Older
1
#
2 3
# This is the generic part of target system setup. There should be
# targetsys dependent setup file as well.
4 5 6 7 8 9 10 11 12 13 14 15 16 17
#

use strict;
use libinstall;
use installvars;
use libtestbed;
use emutil;
use libEmulab;
use emdb;
use User;
use NodeType;
use OSImage;
use Node;
use EmulabFeatures;
18
use EmulabConstants;
19 20 21

my $TARGETSYS_DIR= lc($TARGETSYS_TARGET);
my $CONFIGVARS   = "$PREFIX/configvars.txt";
22
my $TARGETSYSVARS= "$PREFIX/etc/targetsys/variables.txt";
23
my $ELABPASSWORD = "$PREFIX/etc/elabman.pswd";
24
my $SWITCHPSWD   = "$PREFIX/etc/switch.pswd";
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
my $PORTSRC      = "http://www.emulab.net/downloads/FreeBSD-".
    "${FBSD_MAJOR}.${FBSD_MINOR}" . "-ports.tar.gz";
my $ZZZ		 = "/usr/local/etc/rc.d/zzz-inelab.sh";
my $DEFAULTOSID  = "UBUNTU18-64-STD";
my @EXPORTEDOSIDS= ("UBUNTU18-64-STD");
my $PGOSSITEVAR  = "protogeni/default_osname";
my $CHPASS	 = "/usr/bin/chpass";
my $SSHTB	 = "$PREFIX/bin/sshtb";
my $DELETENODE	 = "$PREFIX/sbin/deletenode";
my $ELABMANDIR   = "/users/elabman";

sub Install($$$)
{
    my ($server, $isupdate, $impotent) = @_;
    my %configvars = ();

    # Replace if this script does an update for ip/domain.
    return 0
	if ($isupdate);

    SET_TESTBED_VERSION($TARGETSYS_TARGET);

    Phase "targetsys", "Doing additional targetsys tasks", sub {
	PhaseSkip("Not a target system")
	    if (!$CONFIG_TARGETSYS);

	Phase "config", "Reading in Emulab config variables", sub {
	    PhaseFail("No config file")
		if (! -e $CONFIGVARS);
	    
	    open(CN, $CONFIGVARS)
		 or PhaseFail("Could not open $CONFIGVARS: $!");
	    while (<CN>) {
		if ($_ =~ /^([-\w]*)\s*=\s*(.*)$/) {
		    my $key = $1;
		    my $val = $2;
		    if ($val =~ /^'(.*)'$/) {
			$val = $1;
		    }
		    $configvars{$key} = "$val";
		}
	    }
	    close(CN);
	};

70
	Phase "targetconfig", "Reading in targetsys config variables", sub {
71
	    PhaseFail("No config file")
72
		if (! -e $TARGETSYSVARS);
73
	    
74 75
	    open(CN, $TARGETSYSVARS)
		 or PhaseFail("Could not open $TARGETSYSVARS: $!");
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	    while (<CN>) {
		if ($_ =~ /^([-\w]*)\s*=\s*(.*)$/) {
		    my $key = $1;
		    my $val = $2;
		    if ($val =~ /^'(.*)'$/) {
			$val = $1;
		    }
		    $configvars{$key} = "$val";
		}
	    }
	    close(CN);
	};

	#
	# Have not figured this out yet; it runs during boss setup but
        # the line in fstab gets lost. Run again and it works fine. 
	#
	Phase "swapon", "Reruning rc.freebsd", sub {
	    PhaseSkip("already enabled") unless `grep -q swap $FSTAB`;
	    ExecQuietFatal("/usr/local/etc/emulab/rc/rc.freebsd");
	};

	Phase "rclocal", "Removing $ZZZ", sub {
	    PhaseSkip("already deleted")
		if (! -e $ZZZ);
	    DeleteFileFatal($ZZZ);
	};

	#
	# Update geniuser password.
	#
	if (exists($configvars{'PROTOGENI_PASSWORD'}) &&
	    $configvars{'PROTOGENI_PASSWORD'} ne "") {
	    my $geniuser = User->Lookup("geniuser");
	    PhaseFail("No geniuser account")
		if (!defined($geniuser));
	    my $passhash = PassWordHash($configvars{'PROTOGENI_PASSWORD'});
	    $geniuser->SetPassword($passhash);
	}

	#
	# This is the password for the switches.
	#
119 120 121 122 123 124
	if (exists($configvars{'SWITCH_PASSWORD'}) &&
	    $configvars{'SWITCH_PASSWORD'} ne "" &&
	    ! -e $SWITCHPSWD) {
	    CreateFileFatal($SWITCHPSWD,
			    $configvars{'SWITCH_PASSWORD'});
	    ExecQuietFatal("$CHMOD 400 $SWITCHPSWD");
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	}

	#
	# This is the elabman password.
	#
	if (exists($configvars{'PASSWORD'}) && $configvars{'PASSWORD'} ne "") {
	    my $PASSWORD = $configvars{'PASSWORD'};
	    my $passhash = PassWordHash($PASSWORD);
	    
	    Phase "elabman_pswd", "Storing the elabman password", sub {
		PhaseSkip("exists")
		    if (-e $ELABPASSWORD);
		
		CreateFileFatal($ELABPASSWORD, $PASSWORD);
		ExecQuietFatal("$CHMOD 400 $ELABPASSWORD");
	    };

	    #
	    # Make sure root/toor have the same password on boss/ops.
	    #
	    Phase "root_pswd", "Setting root password on boss", sub {
		ExecQuietFatal("$CHPASS -p '$passhash' root");
		ExecQuietFatal("$CHPASS -p '$passhash' toor");
	    };

	    # shell escape.
	    $passhash =~ s/\$/\\\$/g;
	    $passhash =~ s/\*/\\\*/g;

	    Phase "root_pswdops", "Setting root password on ops", sub {
		ExecQuietFatal("$SSHTB -host $USERNODE ".
			       "  $CHPASS -p '$passhash' root");
		ExecQuietFatal("$SSHTB -host $USERNODE ".
			       "  $CHPASS -p '$passhash' toor");
	    };
	}

	#
163
	# Need to put root public key so tftp can get it.
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 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 344 345 346 347 348 349 350 351 352 353 354 355 356
	#
	my $SSHPUBKEY = "/root/.ssh/id_rsa.pub";
	my $COPYPUBKEY= "$TFTP_DIR/id_rsa.pub";
	
	Phase "sshpubkey", "Copying $SSHPUBKEY to $TFTP_DIR", sub {
	    DoneIfExists($COPYPUBKEY);
	    ExecQuietFatal("/bin/cp -p $SSHPUBKEY $COPYPUBKEY");
	};

	#
	# This is the elabman SSL cert password.
	#
	my $SSLCERT_PSWDFILE = "/users/elabman/.ssl/password";
	
	if (exists($configvars{'ELABMAN_SSLCERT_PASSWORD'}) &&
	    $configvars{'ELABMAN_SSLCERT_PASSWORD'} ne "") {
	    if (! -e $SSLCERT_PSWDFILE) {
		CreateFileFatal($SSLCERT_PSWDFILE,
				$configvars{'ELABMAN_SSLCERT_PASSWORD'});
		ExecQuietFatal("$CHMOD 400 $SSLCERT_PSWDFILE");
		ExecQuietFatal("$CHOWN elabman:emulab-ops $SSLCERT_PSWDFILE");
	    }
	    #
	    # Create encrypted SSL cert for elabman, for testing PG later.
	    #
	    ExecQuietFatal("$SUDO -u $PROTOUSER $PREFIX/sbin/mkusercert ".
	         " -p " . $configvars{'ELABMAN_SSLCERT_PASSWORD'} . " elabman");
	}

	#
	# Trun on arp lockdown.
	#
	Phase "arplockdown", "Turning on arplockdown", sub {
	    ExecQuietFatal("$PREFIX/sbin/setsitevar ".
			   "general/arplockdown static");
	};

	#
	# Mike says: With the default server/client settings, the
	# client is still dropping packets and causing re-requests of
	# blocks. So for now, use the sitevars to scale back the frisbeed
	# send rate to 50000000 (50Mb/sec) for both system and user
	# images. The run time for single client is actually slightly
	# improved at this lower rate due to many fewer re-requests.
	#
	Phase "frisbeerate", "Setting frisbeed max rate", sub {
	    ExecQuietFatal("$PREFIX/sbin/setsitevar ".
			   "images/frisbee/maxrate_std 50000000");
	    ExecQuietFatal("$PREFIX/sbin/setsitevar ".
			   "images/frisbee/maxrate_usr 50000000");
	};

	#
	# A reasonable default image.
	#
	Phase "sitevar1", "Setting sitevar $PGOSSITEVAR", sub {
	    my $osimage = OSImage->LookupByName($DEFAULTOSID);
	    PhaseFail("Could not look up $DEFAULTOSID osid")
		if (!defined($osimage));
	    
	    ExecQuietFatal("$PREFIX/sbin/setsitevar ".
			   "'$PGOSSITEVAR' '$DEFAULTOSID'");
	};

	#
	# Latitude/Longitude
	#
	if (exists($configvars{'TARGETSYS_COUNTRY'}) &&
	    $configvars{'TARGETSYS_COUNTRY'} ne "") {
	    Phase "sitevar2", "Setting country sitevar", sub {
		SetSiteVar("general/default_country",
			   $configvars{'TARTGETSYS_COUNTRY'})
		    or PhaseFail("failed");
	    };
	}
	if (exists($configvars{'TARGETSYS_LATITUDE'}) &&
	    $configvars{'TARGETSYS_LATITUDE'} ne "") {
	    Phase "sitevar3", "Setting latitude sitevar", sub {
		SetSiteVar("general/default_latitude",
			   $configvars{'TARGETSYS_LATITUDE'})
		    or PhaseFail("failed");
	    };
	}
	if (exists($configvars{'TARGETSYS_LONGITUDE'}) &&
	    $configvars{'TARGETSYS_LONGITUDE'} ne "") {
	    Phase "sitevar4", "Setting longitude sitevar", sub {
		SetSiteVar("general/default_longitude",
			   $configvars{'TARGETSYS_LONGITUDE'})
		    or PhaseFail("failed");
	    };
	}
	
	
	#
	# Shutdown the testbed for the rest of this.
	#
	Phase "shutdown", "Shutting down the testbed", sub {
	    PhaseSkip("not running")
		if (!CheckDaemonRunning("stated"));
	    ExecQuietFatal("$TESTBED_CONTROL shutdown");
	};

	#
	# Also grab the port source while close to the server.
	#
	Phase "portsrc", "Getting port source tarball", sub {
	    DoneIfExists("$PREFIX/ports.tar.gz");
	    FetchFileFatal($PORTSRC, "$PREFIX/ports.tar.gz");
	};
	Phase "ports", "Unpacking port source tarball", sub {
	    DoneIfExists("$PREFIX/ports/Mk");
	    ExecQuietFatal("cd $PREFIX; $TAR zxf $PREFIX/ports.tar.gz");
	};
	Phase "portlink", "Linking to unpacked ports tree", sub {
	    ExecQuietFatal("/bin/rm -rf /usr/ports")
		if (-e "/usr/ports" && -d "/usr/ports");
	    ExecQuietFatal("cd /usr; ln -sf testbed/ports .");
	};

	# Need to add a symlink. 
	
	#
	# Need to remove the ELABINELAB flag.
	#
	my $OLDDEFSFILE = "$main::TOP_SRCDIR/defs-elabinelab";
	my $NEWDEFSFILE = "$main::TOP_SRCDIR/defs-targetsys";
	
	Phase "elabinelab", "Removing ELABINELAB stuff from defs file", sub {
	    DoneIfExists($NEWDEFSFILE);
	    # And some other stuff.
	    ExecQuietFatal("echo 'EC2META_ENABLE=1' | ".
			   " cat $OLDDEFSFILE - | sed ".
			   "   -e 's/^ELABINELAB/#ELABINELAB/' ".
			   "   -e 's/^WWWHOST=boss/WWWHOST=www/' ".
			   "   -e 's/^EXTERNAL_NTPS/#EXTERNAL_NTPS/' ".
			   "   > $NEWDEFSFILE");
	};

	#
	# Build and install software without ELABINELAB defined.
	#
	Phase "Software", "Rebuilding and Reinstalling Software", sub {
	    DoneIfExists("$PREFIX/marker");
	    
	    Phase "cleaning", "Cleaning (please be patient)", sub {
		ExecQuietFatal("cd $TOP_OBJDIR; $GMAKE clean");
	    };
	    Phase "config", "Configuring (please be patient)", sub {
		ExecQuietFatal("cd $TOP_OBJDIR; ".
			       "   $main::TOP_SRCDIR/configure ".
			       "      --with-TBDEFS=$NEWDEFSFILE ".
			       "      --disable-windows");
	    };
	    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 "rc.testbed", "Installing testbed RC scripts", sub {
		ExecQuietFatal("$GMAKE -C $TOP_OBJDIR/rc.d install");
	    };
	    Phase "ntpd", "Installing testbed ntp config", sub {
		ExecQuietFatal("$GMAKE -C $TOP_OBJDIR/ntpd install");
	    };
	    CreateFile("$PREFIX/marker", "targetsys install marker");
	};

	#
	# Add this feature so we get the right libvtop.
	#
	my $LIBVTOP_FEATURE     = "LibVtopTest";
	my $LIBVTOP_DESRIPTION  = "Test version of libvtop";
    
	my $feature = EmulabFeatures->Lookup($LIBVTOP_FEATURE);
	Phase "feature", "Adding libvtop_test feature", sub {
	    PhaseSkip("already exists")
		if (defined($feature));
	    $feature = EmulabFeatures->Create($LIBVTOP_FEATURE,
					      $LIBVTOP_DESRIPTION);
	    PhaseFail("Failed to create feature")
		if (!defined($feature));
	};
	Phase "feature", "Enabling libvtop_test feature", sub {
	    $feature->SetGlobalEnable(1) == 0
		or PhaseFail("Could not enable feature");
	    $feature->SetGlobalDisable(0) == 0
		or PhaseFail("Could not clear disable flag on feature");
	};

	#
	# Update the DB with the switch community string.
	#
357 358
	if (exists($configvars{'SWITCH_COMMUNITY'})) {
	    my $safe_password = DBQuoteSpecial($configvars{'SWITCH_COMMUNITY'});
359 360 361 362 363 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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	    DBQueryWarn("update switch_stack_types set ".
			"snmp_community=$safe_password")
		or PhaseFail("Could not set snmp community strings");
	}

	#
	# Delete existing ops node; we recreate it later. 
	#
	Phase "opsnode", "Deleting ops node", sub {
	    PhaseSkip("already deleted")
		if (!defined(Node->Lookup("ops")));
	    ExecQuietFatal("$DELETENODE -f -s ops");
	};

	#
	# Shutdown DHCP and fix the dynamic range that rc.mkelab changed.
	# This is okay since the subnet is different then Emulab.
	#
	Phase "dhcpd", "Fixing dhcpd configuration", sub {
	    ExecQuietFatal("/bin/rm -f $DHCPD_TEMPLATE");
	    ExecQuietFatal("cd $TOP_OBJDIR/dhcpd; $GMAKE install");
	    ExecQuietFatal("$DHCPD_MAKECONF -i");
	};

	foreach my $osid (@EXPORTEDOSIDS) {
	    Phase "$osid", "Enabling protogeni export for $osid", sub {
		my $osimage = OSImage->LookupByName($osid);
		PhaseFail("No such image $osid")
		    if (!defined($osimage));
		$osimage->Update({"protogeni_export" => 1}) == 0
		    or PhaseFail("DB query failed");
	    };
	}

	#
	# 
	#
	Phase "buildenv", "Creating build environment", sub {
	    Phase "mkdir", "Creating directory in elabman", sub {
		DoneIfExists("$ELABMANDIR/emulab-devel");
		mkdir "$ELABMANDIR/emulab-devel",0775 or
		    PhaseFail("Unable to create $ELABMANDIR/emulab-devel: $!");

	    };
	    Phase "cpdefs", "Coping defs file to elabman", sub {
		DoneIfExists("$ELABMANDIR/emulab-devel/defs-targetsys");
		ExecQuietFatal("/bin/cp -f $PREFIX/src/testbed/defs-targetsys ".
			       "        $ELABMANDIR/emulab-devel");
	    };
	    Phase "mvsrc", "Moving src directory to elabman", sub {
		DoneIfExists("$ELABMANDIR/emulab-devel/emulab-devel");
		ExecQuietFatal("/bin/mv -f $PREFIX/src/testbed ".
			       "        $ELABMANDIR/emulab-devel/emulab-devel");
	    };
	    Phase "mvobj", "Moving obj directory to elabman", sub {
		DoneIfExists("$ELABMANDIR/emulab-devel/obj");
		ExecQuietFatal("/bin/mv -f $PREFIX/obj/testbed ".
			       "        $ELABMANDIR/emulab-devel/obj");
	    };
	    Phase "srcsymlink", "Leaving src symlink behind", sub {
		DoneIfExists("$PREFIX/src/testbed");
		ExecQuietFatal("cd $PREFIX/src; ln -s ".
			       "  $ELABMANDIR/emulab-devel/emulab-devel ".
			       "  testbed");
	    };
	    Phase "objsymlink", "Leaving obj symlink behind", sub {
		DoneIfExists("$PREFIX/obj/testbed");
		ExecQuietFatal("cd $PREFIX/obj; ln -s ".
			       "  $ELABMANDIR/emulab-devel/obj obj");
	    };
	    Phase "chown", "Changing owner and group", sub {
		ExecQuietFatal("$CHOWN -R elabman:emulab-ops ".
			       "   $ELABMANDIR/emulab-devel");
		ExecQuietFatal("$CHOWN -R -h elabman:emulab-ops ".
			       "   $ELABMANDIR/emulab-devel");
	    };
	};

437
	PhaseSucceed("Targetsys Setup Done");
438 439 440 441 442 443 444
    };
    return 0;
}

# Local Variables:
# mode:perl
# End: