libdb.pm.in 63.7 KB
Newer Older
1 2
#!/usr/bin/perl -w

Leigh B. Stoller's avatar
Leigh B. Stoller committed
3 4
#
# EMULAB-COPYRIGHT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7 8
# All rights reserved.
#

9
#
10 11
# A library of useful DB stuff. Mostly things that get done a lot.
# Saves typing.
12 13 14 15 16
#
# XXX: The notion of "uid" is a tad confused. A unix uid is a number,
#      while in the DB a user uid is a string (equiv to unix login).
#      Needs to be cleaned up.
#
17

18 19 20 21
package libdb;
use Exporter;
@ISA = "Exporter";
@EXPORT =
22 23 24
    qw ( NODERELOADING_PID NODERELOADING_EID NODEDEAD_PID NODEDEAD_EID
	 NODEBOOTSTATUS_OKAY NODEBOOTSTATUS_FAILED NODEBOOTSTATUS_UNKNOWN
	 NODESTARTSTATUS_NOSTATUS PROJMEMBERTRUST_NONE PROJMEMBERTRUST_USER
Leigh B. Stoller's avatar
Leigh B. Stoller committed
25 26 27 28 29 30 31
	 PROJMEMBERTRUST_ROOT PROJMEMBERTRUST_GROUPROOT
	 PROJMEMBERTRUST_PROJROOT

	 TBTrustConvert TBMinTrust TBGrpTrust TBProjTrust

	 TB_NODEACCESS_READINFO TB_NODEACCESS_MODIFYINFO
	 TB_NODEACCESS_LOADIMAGE TB_NODEACCESS_REBOOT
32 33
	 TB_NODEACCESS_POWERCYCLE TB_NODEACCESS_MODIFYVLANS
	 TB_NODEACCESS_MIN TB_NODEACCESS_MAX
Leigh B. Stoller's avatar
Leigh B. Stoller committed
34 35 36 37

	 TB_USERINFO_READINFO TB_USERINFO_MODIFYINFO
	 TB_USERINFO_MIN TB_USERINFO_MAX

38 39
	 USERSTATUS_ACTIVE USERSTATUS_FROZEN
	 USERSTATUS_UNAPPROVED USERSTATUS_UNVERIFIED USERSTATUS_NEWUSER
40

Leigh B. Stoller's avatar
Leigh B. Stoller committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	 TB_EXPT_READINFO TB_EXPT_MODIFY TB_EXPT_DESTROY
	 TB_EXPT_MIN TB_EXPT_MAX

	 TB_PROJECT_READINFO TB_PROJECT_MAKEGROUP
	 TB_PROJECT_EDITGROUP TB_PROJECT_DELGROUP
	 TB_PROJECT_LEADGROUP TB_PROJECT_ADDUSER
	 TB_PROJECT_DELUSER TB_PROJECT_MAKEOSID
	 TB_PROJECT_DELOSID TB_PROJECT_MAKEIMAGEID TB_PROJECT_DELIMAGEID
	 TB_PROJECT_CREATEEXPT TB_PROJECT_MIN TB_PROJECT_MAX

	 TB_OSID_READINFO TB_OSID_CREATE
	 TB_OSID_DESTROY TB_OSID_MIN TB_OSID_MAX

	 TB_IMAGEID_READINFO TB_IMAGEID_MODIFYINFO
	 TB_IMAGEID_CREATE TB_IMAGEID_DESTROY
	 TB_IMAGEID_ACCESS TB_IMAGEID_MIN TB_IMAGEID_MAX
	 
	 DBLIMIT_NSFILESIZE NODERELOADPENDING_EID
59

60 61
	 EXPTSTATE_NEW EXPTSTATE_PRERUN EXPTSTATE_SWAPPED EXPTSTATE_SWAPPING
	 EXPTSTATE_ACTIVATING EXPTSTATE_ACTIVE EXPTSTATE_TESTING
62
	 EXPTSTATE_TERMINATING EXPTSTATE_TERMINATED EXPTSTATE_UPDATING
63

64 65
	 BATCHSTATE_POSTED BATCHSTATE_RUNNING BATCHSTATE_TERMINATING
	 BATCHSTATE_ACTIVATING
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66 67
	 TBBatchState TBSetBatchState

68 69 70
	 TB_NODELOGTYPE_MISC TB_NODELOGTYPES TB_DEFAULT_NODELOGTYPE 

	 TB_DEFAULT_RELOADTYPE TB_RELOADTYPE_FRISBEE TB_RELOADTYPE_NETDISK
71

72 73
	 TB_EXPTPRIORITY_LOW TB_EXPTPRIORITY_HIGH

74
	 TB_ASSIGN_TOOFEWNODES TB_OPSPID
75

76
	 TBDB_TBEVENT_NODESTATE TBDB_TBEVENT_NODEOPMODE TBDB_TBEVENT_CONTROL
Chad Barb's avatar
 
Chad Barb committed
77

78
	 TBDB_NODESTATE_ISUP TBDB_NODESTATE_REBOOTING TBDB_NODESTATE_REBOOTED
79 80 81
	 TBDB_NODESTATE_SHUTDOWN TBDB_NODESTATE_BOOTING TBDB_NODESTATE_TBSETUP
	 TBDB_NODESTATE_RELOADSETUP TBDB_NODESTATE_RELOADING 
	 TBDB_NODESTATE_RELOADDONE TBDB_NODESTATE_UNKNOWN
Chad Barb's avatar
 
Chad Barb committed
82

83 84
	 TBDB_NODEOPMODE_NORMAL TBDB_NODEOPMODE_DELAYING
	 TBDB_NODEOPMODE_UNKNOWNOS TBDB_NODEOPMODE_RELOADING
85 86 87
	 TBDB_NODEOPMODE_NORMALv1 TBDB_NODEOPMODE_MINIMAL 
	 TBDB_NODEOPMODE_RELOAD TBDB_NODEOPMODE_DELAY
	 TBDB_NODEOPMODE_BOOTWHAT
88
	 TBDB_NODEOPMODE_UNKNOWN
Chad Barb's avatar
 
Chad Barb committed
89 90 91 92 93 94 95

	 TBDB_ALLOCSTATE_FREE_CLEAN TBDB_ALLOCSTATE_FREE_DIRTY
	 TBDB_ALLOCSTATE_DOWN TBDB_ALLOCSTATE_RELOAD_TO_FREE
	 TBDB_ALLOCSTATE_RELOAD_PENDING TBDB_ALLOCSTATE_RES_RELOAD
	 TBDB_ALLOCSTATE_RES_REBOOT_DIRTY TBDB_ALLOCSTATE_RES_REBOOT_CLEAN 
	 TBDB_ALLOCSTATE_RES_READY TBDB_ALLOCSTATE_UNKNOWN

96
	 TBDB_EXPT_WORKDIR
97
	 TBSetNodeEventState TBGetNodeEventState
Chad Barb's avatar
 
Chad Barb committed
98
	 TBSetNodeAllocState TBGetNodeAllocState
99
	 TBSetNodeOpMode TBGetNodeOpMode
100 101 102 103
	 TB_OSID_MBKERNEL TB_OSID_PXEBOOT TB_OSID_FRISBEE 
	 TBBootWhat TBNodeStateTimeout
	 TBDB_TBCONTROL_RESET TBDB_TBCONTROL_RELOADDONE 
	 TBDB_TBCONTROL_TIMEOUT TBDB_NO_STATE_TIMEOUT
104

105 106
	 TBDB_LOWVPORT TBDB_MAXVPORT TBDB_PORTRANGE

Leigh B. Stoller's avatar
Leigh B. Stoller committed
107 108
	 TBAdmin TBProjAccessCheck TBNodeAccessCheck TBOSIDAccessCheck
	 TBImageIDAccessCheck TBExptAccessCheck ExpLeader MarkNodeDown
109
	 SetNodeBootStatus OSFeatureSupported IsShelved NodeidToExp
110
	 UserDBInfo DBQuery DBQueryFatal DBQueryWarn DBWarn DBFatal
111
	 DBQuoteSpecial UNIX2DBUID ExpState SetExpState ProjLeader
Leigh B. Stoller's avatar
Leigh B. Stoller committed
112
	 ExpNodes DBDateTime DefaultImageID GroupLeader TBGroupUnixInfo
113
	 TBValidNodeLogType TBValidNodeName TBSetNodeLogEntry
114
	 TBSetSchedReload MapNodeOSID TBLockExp TBUnLockExp TBSetExpSwapTime
115
	 TBUnixGroupList TBOSID TBImageID TBdbfork VnameToNodeid TBExpLocked
116
	 TBIsNodeRemote TBExptSetLogFile TBExptClearLogFile TBExptGetLogFile
117
	 TBIsNodeVirtual TBControlNetIP TBPhysNodeID
118
	 TBExptOpenLogFile TBExptCloseLogFile TBExptCreateLogFile
119
	 TBNodeUpdateAccountsByPid TBNodeUpdateAccountsByType
120
	 TBNodeUpdateAccountsByUID
121
	 TBSaveExpLogFiles TBExptWorkDir TBExptUserDir TBExptLogDir
122
	 TBExptDestroy TBIPtoNodeID TBNodeBootReset TBNodeStateWait
123 124

	 TBDB_WIDEAREA_LOCALNODE
Leigh B. Stoller's avatar
Leigh B. Stoller committed
125
	 TBWideareaNodeID TBTipServers
126
	 );
127

128
# Must come after package declaration!
129
use lib '@prefix@/lib';
130
use English;
131
use File::Basename;
132
use POSIX qw(strftime);
133 134
require Mysql;

135 136 137
# Configure variables
my $TB		= "@prefix@";
my $DBNAME	= "@TBDBNAME@";
138
my $TBOPS       = "@TBOPSEMAIL@";
139 140
my $EVENTSYS    = "@EVENTSYS@";
my $BOSSNODE    = "@BOSSNODE@";
141
my $TBOPSPID	= "emulab-ops";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
142
my $SCRIPTNAME = "Unknown";
143

144 145 146 147 148
if ($EVENTSYS) {
    require event;
    import event;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
149 150 151
# Untainted scriptname for email below.
if ($PROGRAM_NAME =~ /^([-\w\.\/]+)$/) {
    $SCRIPTNAME = basename($1);
152 153
}
else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
154
    $SCRIPTNAME = "Tainted";
155 156
}

157
#
158 159
# Set up for querying the database. Note that fork causes a reconnect
# to the DB in the child. 
160 161
#
my $DB;
162 163 164
$DBQUERY_MAXTRIES = 1;
$DBCONN_MAXTRIES  = 5;
@EXPORT_OK        = qw($DBQUERY_MAXTRIES $DBCONN_MAXTRIES);
165 166 167

sub TBDBConnect()
{
168
    my $maxtries = $DBCONN_MAXTRIES;
169 170 171 172 173 174 175 176 177

    #
    # Construct a 'username' from the name of this script and the user who
    # ran it. This is for accounting purposes.
    #
    my $name = getpwuid($UID);
    if (!$name) {
	$name = "uid$UID";
    }
178
    my $dbuser = "$SCRIPTNAME:$name";
179

180
    while ($maxtries) {
181
	$DB = Mysql->connect("localhost", $DBNAME, $dbuser, "none");
182 183 184
	if (defined($DB)) {
	    last;
	}
185 186 187 188 189 190
	$maxtries--;
	sleep(1);
    }
    if (!defined($DB)) {
	die("Cannot connect to DB after several attempts!\n");
    }
191 192
    $DB->{'dbh'}->{'PrintError'} = 0;
    $Mysql::QUIET = 1;
193 194
}
TBDBConnect();
195

196 197 198
sub TBdbfork()
{
    undef($DB);
199
    TBDBConnect();
200 201
}

202 203 204 205 206
#
# Record last DB error string.
#
my $DBErrorString = "";

207 208 209 210 211
#
# Needs to be config'ed.
#
sub TBDB_EXPT_WORKDIR()		{ "/usr/testbed/expwork"; }
    
212 213 214 215 216
#
# Define exported "constants". Basically, these are just perl subroutines
# that look like constants cause you do not need to call a perl subroutine
# with parens. That is, FOO and FOO() are the same thing.
#
217
sub NODERELOADING_PID()		{ "emulab-ops"; }
218
sub NODERELOADING_EID()		{ "reloading"; }
219
sub NODERELOADPENDING_EID()	{ "reloadpending"; }
220 221 222 223 224 225 226 227
sub NODEDEAD_PID()		{ "emulab-ops"; }
sub NODEDEAD_EID()		{ "hwdown"; }

sub NODEBOOTSTATUS_OKAY()	{ "okay" ; }
sub NODEBOOTSTATUS_FAILED()	{ "failed"; }
sub NODEBOOTSTATUS_UNKNOWN()	{ "unknown"; }
sub NODESTARTSTATUS_NOSTATUS()	{ "none"; }

228 229 230 231 232 233 234 235 236
sub EXPTSTATE_NEW()		{ "new"; }
sub EXPTSTATE_PRERUN()		{ "prerunning"; }
sub EXPTSTATE_SWAPPED()		{ "swapped"; }
sub EXPTSTATE_SWAPPING()	{ "swapping"; }
sub EXPTSTATE_ACTIVATING()	{ "activating"; }
sub EXPTSTATE_ACTIVE()		{ "active"; }
sub EXPTSTATE_TESTING()		{ "testing"; }
sub EXPTSTATE_TERMINATING()	{ "terminating"; }
sub EXPTSTATE_TERMINATED()	{ "ended"; }
237
sub EXPTSTATE_UPDATING()	{ "updating"; }
238

Leigh B. Stoller's avatar
Leigh B. Stoller committed
239 240 241 242 243
sub BATCHSTATE_POSTED()		{ "posted"; }
sub BATCHSTATE_ACTIVATING()	{ "activating"; }
sub BATCHSTATE_RUNNING()	{ "active"; }
sub BATCHSTATE_TERMINATING()	{ "terminating"; }

244 245
sub USERSTATUS_ACTIVE()		{ "active"; }
sub USERSTATUS_FROZEN()		{ "frozen"; }
246 247 248
sub USERSTATUS_UNAPPROVED()	{ "unapproved"; }
sub USERSTATUS_UNVERIFIED()	{ "unverified"; }
sub USERSTATUS_NEWUSER()	{ "newuser"; }
249

250 251 252 253 254 255
#
# We want valid project membership to be non-zero for easy membership
# testing. Specific trust levels are encoded thusly.
# 
sub PROJMEMBERTRUST_NONE()	{ 0; }
sub PROJMEMBERTRUST_USER()	{ 1; }
256
sub PROJMEMBERTRUST_ROOT()	{ 2; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270
sub PROJMEMBERTRUST_LOCALROOT()	{ 2; }
sub PROJMEMBERTRUST_GROUPROOT()	{ 3; }
sub PROJMEMBERTRUST_PROJROOT()	{ 4; }
sub PROJMEMBERTRUST_ADMIN()	{ 5; }

#
# Access types. Duplicated in the web interface. Make changes there too!
# 
# Things you can do to a node.
sub TB_NODEACCESS_READINFO()	{ 1; }
sub TB_NODEACCESS_MODIFYINFO()	{ 2; }
sub TB_NODEACCESS_LOADIMAGE()	{ 3; }
sub TB_NODEACCESS_REBOOT()	{ 4; }
sub TB_NODEACCESS_POWERCYCLE()	{ 5; }
271
sub TB_NODEACCESS_MODIFYVLANS()	{ 6; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
272
sub TB_NODEACCESS_MIN()		{ TB_NODEACCESS_READINFO; }
273
sub TB_NODEACCESS_MAX()		{ TB_NODEACCESS_MODIFYVLANS; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

# User Info (modinfo web page, etc).
sub TB_USERINFO_READINFO()	{ 1; }
sub TB_USERINFO_MODIFYINFO()	{ 2; }
sub TB_USERINFO_MIN()		{ TB_USERINFO_READINFO; }
sub TB_USERINFO_MAX()		{ TB_USERINFO_MODIFYINFO; }

# Experiments (also batch experiments).
sub TB_EXPT_READINFO()		{ 1; }
sub TB_EXPT_MODIFY()		{ 2; }
sub TB_EXPT_DESTROY()		{ 3; }
sub TB_EXPT_MIN()		{ TB_EXPT_READINFO; }
sub TB_EXPT_MAX()		{ TB_EXPT_DESTROY; }

# Projects.
sub TB_PROJECT_READINFO()	{ 1; }
sub TB_PROJECT_MAKEGROUP()	{ 2; }
sub TB_PROJECT_EDITGROUP()	{ 3; }
sub TB_PROJECT_DELGROUP()	{ 4; }
sub TB_PROJECT_LEADGROUP()	{ 5; }
sub TB_PROJECT_ADDUSER()	{ 6; }
sub TB_PROJECT_DELUSER()	{ 7; }
296 297 298 299 300
sub TB_PROJECT_MAKEOSID()	{ 8; }
sub TB_PROJECT_DELOSID()	{ 9; }
sub TB_PROJECT_MAKEIMAGEID()	{ 10; }
sub TB_PROJECT_DELIMAGEID()	{ 11; }
sub TB_PROJECT_CREATEEXPT()	{ 12; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
301 302 303 304 305 306 307 308 309 310
sub TB_PROJECT_MIN()		{ TB_PROJECT_READINFO; }
sub TB_PROJECT_MAX()		{ TB_PROJECT_CREATEEXPT; }

# OSIDs 
sub TB_OSID_READINFO()		{ 1; }
sub TB_OSID_CREATE()		{ 2; }
sub TB_OSID_DESTROY()		{ 3; }
sub TB_OSID_MIN()		{ TB_OSID_READINFO; }
sub TB_OSID_MAX()		{ TB_OSID_DESTROY; }

311 312 313 314 315
# Magic OSID constants
sub TB_OSID_MBKERNEL()          { "_KERNEL_"; } # multiboot kernel OSID
sub TB_OSID_PXEBOOT()           { "PXEBOOT"; } # osid for def pxe_boot_path
sub TB_OSID_FRISBEE()           { "PXEFRISBEE"; } 

Leigh B. Stoller's avatar
Leigh B. Stoller committed
316
# ImageIDs
317 318 319 320 321
#
# Clarification:
# READINFO is read-only access to the image and its contents
# (This is what people get for shared images)
# ACCESS means complete power over the image and its [meta]data
Leigh B. Stoller's avatar
Leigh B. Stoller committed
322 323 324 325 326 327 328
sub TB_IMAGEID_READINFO()	{ 1; }
sub TB_IMAGEID_MODIFYINFO()	{ 2; }
sub TB_IMAGEID_CREATE()		{ 3; }
sub TB_IMAGEID_DESTROY()	{ 4; }
sub TB_IMAGEID_ACCESS()		{ 5; }
sub TB_IMAGEID_MIN()		{ TB_IMAGEID_READINFO; }
sub TB_IMAGEID_MAX()		{ TB_IMAGEID_ACCESS; }
329

330
# Node Log Types
331 332 333 334 335 336 337
sub TB_NODELOGTYPE_MISC		{ "misc"; }
sub TB_NODELOGTYPES()		{ ( TB_NODELOGTYPE_MISC ) ; }
sub TB_DEFAULT_NODELOGTYPE()	{ TB_NODELOGTYPE_MISC; }

# Reload Types.
sub TB_RELOADTYPE_NETDISK()	{ "netdisk"; }
sub TB_RELOADTYPE_FRISBEE()	{ "frisbee"; }
338
sub TB_DEFAULT_RELOADTYPE()	{ TB_RELOADTYPE_FRISBEE; }
339

340 341 342 343 344 345 346
# Experiment priorities.
sub TB_EXPTPRIORITY_LOW()	{ 0; }
sub TB_EXPTPRIORITY_HIGH()	{ 20; }

# Assign exit status for too few nodes.
sub TB_ASSIGN_TOOFEWNODES()	{ 2; }

347 348 349
# System PID.
sub TB_OPSPID()			{ $TBOPSPID; }

350
#
351 352 353 354
# Events we may want to send
#
sub TBDB_TBEVENT_NODESTATE	{ "TBNODESTATE"; }
sub TBDB_TBEVENT_NODEOPMODE	{ "TBNODEOPMODE"; }
355
sub TBDB_TBEVENT_CONTROL	{ "TBCONTROL"; }
356 357 358 359

#
# For nodes, we use this set of events.
#
360 361 362 363 364 365 366 367 368
sub TBDB_NODESTATE_ISUP()	{ "ISUP"; }
sub TBDB_NODESTATE_REBOOTED()	{ "REBOOTED"; }
sub TBDB_NODESTATE_REBOOTING()	{ "REBOOTING"; }
sub TBDB_NODESTATE_SHUTDOWN()	{ "SHUTDOWN"; }
sub TBDB_NODESTATE_BOOTING()	{ "BOOTING"; }
sub TBDB_NODESTATE_TBSETUP()	{ "TBSETUP"; }
sub TBDB_NODESTATE_RELOADSETUP() { "RELOADSETUP"; }
sub TBDB_NODESTATE_RELOADING()	{ "RELOADING"; }
sub TBDB_NODESTATE_RELOADDONE()	{ "RELOADDONE"; }
369 370
sub TBDB_NODESTATE_UNKNOWN()	{ "UNKNOWN"; };

371 372 373 374 375 376 377 378 379
sub TBDB_NODEOPMODE_NORMAL	{ "NORMAL"; }
sub TBDB_NODEOPMODE_DELAYING	{ "DELAYING"; }
sub TBDB_NODEOPMODE_UNKNOWNOS	{ "UNKNOWNOS"; }
sub TBDB_NODEOPMODE_RELOADING	{ "RELOADING"; }
sub TBDB_NODEOPMODE_NORMALv1	{ "NORMALv1"; } 
sub TBDB_NODEOPMODE_MINIMAL	{ "MINIMAL"; } 
sub TBDB_NODEOPMODE_RELOAD	{ "RELOAD"; } 
sub TBDB_NODEOPMODE_DELAY	{ "DELAY"; } 
sub TBDB_NODEOPMODE_BOOTWHAT	{ "_BOOTWHAT_"; } # A redirection opmode
380 381
sub TBDB_NODEOPMODE_UNKNOWN	{ "UNKNOWN"; }

Chad Barb's avatar
 
Chad Barb committed
382 383 384 385 386 387 388 389 390 391 392
sub TBDB_ALLOCSTATE_FREE_CLEAN()       { "FREE_CLEAN"; }
sub TBDB_ALLOCSTATE_FREE_DIRTY()       { "FREE_DIRTY"; }
sub TBDB_ALLOCSTATE_DOWN()             { "DOWN"; }
sub TBDB_ALLOCSTATE_RELOAD_TO_FREE()   { "RELOAD_TO_FREE"; }
sub TBDB_ALLOCSTATE_RELOAD_PENDING()   { "RELOAD_PENDING"; }
sub TBDB_ALLOCSTATE_RES_RELOAD()       { "RES_RELOAD"; }
sub TBDB_ALLOCSTATE_RES_DIRTY_REBOOT() { "RES_DIRTY_REBOOT"; }
sub TBDB_ALLOCSTATE_RES_CLEAN_REBOOT() { "RES_CLEAN_REBOOT"; }
sub TBDB_ALLOCSTATE_RES_READY()        { "RES_READY"; }
sub TBDB_ALLOCSTATE_UNKNOWN()          { "UNKNOWN"; };

393 394 395 396 397 398 399
sub TBDB_TBCONTROL_RESET	{ "RESET"; }
sub TBDB_TBCONTROL_RELOADDONE	{ "RELOADDONE"; }
sub TBDB_TBCONTROL_TIMEOUT	{ "TIMEOUT"; }

# Constant we use for the timeout field when there is no timeout for a state
sub TBDB_NO_STATE_TIMEOUT	{ 0; }

400 401 402 403 404 405
#
# Node name we use in the widearea_* tables to represent a generic local node.
# All local nodes are considered to have the same network characteristcs.
#
sub TBDB_WIDEAREA_LOCALNODE     { "boss"; }

406 407 408 409 410
#
# We should list all of the DB limits.
#
sub DBLIMIT_NSFILESIZE()	{ (1024 * 16); }

411 412 413 414 415 416 417 418 419 420 421
#
# Virtual nodes must operate within a restricted port range. The range
# is effective across all virtual nodes in the experiment. When an
# experiment is swapped in, allocate a subrange from this and setup
# all the vnodes to allocate from that range. We tell the user this
# range so this they can set up their programs to operate in that range.
#
sub TBDB_LOWVPORT()		{ 30000; }
sub TBDB_MAXVPORT()		{ 60000; }
sub TBDB_PORTRANGE()		{ 256;   }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
#
# Auth stuff.
#

#
# Convert a trust string to the above numeric values.
#
sub TBTrustConvert($)
{
    my($trust_string) = @_;
    my $trust_value = 0;

    #
    # Convert string to value. Perhaps the DB should have done it this way?
    # 
    if ($trust_string eq "none") {
	$trust_value = PROJMEMBERTRUST_NONE;
    }
    elsif ($trust_string eq "user") {
	$trust_value = PROJMEMBERTRUST_USER;
    }
    elsif ($trust_string eq "local_root") {
	$trust_value = PROJMEMBERTRUST_LOCALROOT;
    }
    elsif ($trust_string eq "group_root") {
	$trust_value = PROJMEMBERTRUST_GROUPROOT;
    }
    elsif ($trust_string eq "project_root") {
	$trust_value = PROJMEMBERTRUST_PROJROOT;
    }
    elsif ($trust_string eq "admin") {
	$trust_value = PROJMEMBERTRUST_ADMIN;
    }
    else {
	    die("*** Invalid trust value $trust_string!");
    }

    return $trust_value;
}

#
# Return true if the given trust string is >= to the minimum required.
# The trust value can be either numeric or a string; if a string its
# first converted to the numeric equiv.
#
sub TBMinTrust($$)
{
    my ($trust_value, $minimum) = @_;

    if ($minimum < PROJMEMBERTRUST_NONE ||
	$minimum > PROJMEMBERTRUST_ADMIN) {
	    die("*** Invalid minimum trust $minimum!");
    }

    #
    # Sleazy? How do you do a typeof in perl?
    #
    if (length($trust_value) != 1) {
	$trust_value = TBTrustConvert($trust_value);
    }
    
    return $trust_value >= $minimum;
}

#
# Determine the trust level for a uid/pid/gid. That is, each uid will have
# a different trust level depending on the project/group in question.
# Return that trust level as one of the numeric values above. 
# 
# usage: TBGrpTrust($dbuid, $pid, $gid)
#        returns numeric trust value if a group member.
#        returns PROJMEMBERTRUST_NONE if not a group member.
# 
sub TBGrpTrust($$$)
{
    my ($uid, $pid, $gid) = @_;

    #
    # No group, then use the default group.
    #
    if (! $gid) {
	$gid = $pid;
    }

    my $query_result =
	DBQueryFatal("select trust from group_membership ".
		     "where uid='$uid' and pid='$pid' and gid='$gid'");

    #
    # No membership is the same as no trust. True? Maybe an error instead?
    #
    if ($query_result->numrows == 0) {
	return PROJMEMBERTRUST_NONE;
    }

    my @row = $query_result->fetchrow_array();
    $trust_string = $row[0];

    return TBTrustConvert($trust_string);
}

#
# Determine the project trust level for a uid/pid. This is the trust level
# for the default group in the project.
#
# usage: TBProjTrust($dbuid, $pid)
#        returns numeric trust value if a project member.
#        returns PROJMEMBERTRUST_NONE if not a project member.
# 
sub TBProjTrust($$)
{
    my ($uid, $pid) = @_;
    
    return TBGrpTrust($uid, $pid, $pid);
}

538
#
539 540
# Test admin status. Optional argument is the UID or Name to test. If not
# provided, then test the current UID.
541
#
542 543 544
# XXX Argument is *either* a numeric UID, or a string name.
#
# usage: TBAdmin([int or char* uid]);
545 546 547 548 549 550
#        returns 1 if an admin type.
#        returns 0 if a mere user.
# 
sub TBAdmin(;$)
{
    my($uid) = @_;
551
    my($name);
552

553 554 555 556
    #
    # No one is considered an admin unless they have the magic environment
    # variable set (so that you have to be a bit more explict about wanting
    # admin privs.) Use the withadminprivs script to get this variable set.
557 558
    # Also check with HTTP_ at the front of the name, since this is required
    # to get it through suexec from the web scripts.
559
    #
560
    if (!($ENV{WITH_TB_ADMIN_PRIVS} || $ENV{HTTP_WITH_TB_ADMIN_PRIVS})) {
561 562 563
	return 0;
    }

564 565 566 567
    if (!defined($uid)) {
	$uid = $UID;
    }

568 569 570 571 572 573 574 575 576 577
    #
    # Test if numeric. Map to name if it is.
    # 
    if ($uid =~ /^[0-9]+$/) {
	($name) = getpwuid($uid)
	    or die "$uid not in passwd file\n";
    }
    else {
	$name = $uid;
    }
578 579

    my $query_result =
580
	DBQueryFatal("select admin from users where uid='$name'");
581 582 583 584 585 586 587 588 589

    my @row = $query_result->fetchrow_array();
    if ($row[0] == 1) {
	return 1;
    }
    return 0;
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
590 591
# Project permission checks. The group id (gid) can be undef, in which case
# the pid is used (ie: a default group check is made).
592
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
593 594 595 596 597
# Usage: TBProjAccessCheck($uid, $pid, $gid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
# 
sub TBProjAccessCheck($$$$)
598
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
599 600
    my ($uid, $pid, $gid, $access_type) = @_;
    my $mintrust;
601

Leigh B. Stoller's avatar
Leigh B. Stoller committed
602 603 604
    if ($access_type < TB_PROJECT_MIN ||
	$access_type > TB_PROJECT_MAX) {
	die("*** Invalid access type: $access_type!");
605 606
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
607 608 609 610 611
    #
    # Admins do whatever they want!
    # 
    if (TBAdmin($uid)) {
	return 1;
612
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
613 614 615 616 617 618 619
    $uid = MapNumericUID($uid);

    #
    # No group, then use the default group.
    #
    if (! defined($gid)) {
	$gid = $pid;
620 621
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
622 623 624 625 626 627
    if ($access_type == TB_PROJECT_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
    }
    elsif ($access_type == TB_PROJECT_CREATEEXPT) {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }
628 629 630
    elsif ($access_type == TB_PROJECT_DELUSER) {
	$mintrust = PROJMEMBERTRUST_PROJROOT;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
    else {
	die("*** Unexpected access type: $access_type!");
    }

    return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust);
}

#
# Experiment permission checks.
#
# Usage: TBExptAccessCheck($uid, $pid, $eid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
# 
sub TBExptAccessCheck($$$$)
{
    my ($uid, $pid, $eid, $access_type) = @_;
    my $mintrust;

    if ($access_type < TB_EXPT_MIN ||
	$access_type > TB_EXPT_MAX) {
	die("*** Invalid access type: $access_type!");
653 654 655
    }

    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
656 657 658
    # Admins do whatever they want!
    # 
    if (TBAdmin($uid)) {
659 660
	return 1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
661
    $uid = MapNumericUID($uid);
662

Leigh B. Stoller's avatar
Leigh B. Stoller committed
663
    my $query_result =
664
	DBQueryFatal("SELECT gid,expt_head_uid FROM experiments WHERE ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
665 666 667 668 669 670
		     "eid='$eid' and pid='$pid'");
    
    if ($query_result->numrows == 0) {
	return 0;
    }
    my @row = $query_result->fetchrow_array();
671 672
    my $gid     = $row[0];
    my $creator = $row[1];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
673

674 675 676 677 678 679 680 681 682 683 684
    #
    # An experiment may be destroyed by the experiment creator or the
    # project/group leader.
    # 
    if ($access_type == TB_EXPT_DESTROY) {
	if ($uid eq $creator) {
	    return 1;
	}
	$mintrust = PROJMEMBERTRUST_GROUPROOT;
    }
    elsif ($access_type == TB_EXPT_READINFO) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
	$mintrust = PROJMEMBERTRUST_USER;
    }
    else {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }

    return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust);
}

#
# Determine if uid can access a node or list of nodes.
#
# Usage: TBNodeAccessCheck($uid, $access_type, $node_id, ...)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
# 
sub TBNodeAccessCheck($$@)
{
    my ($uid, $access_type) = (shift, shift);
    my @nodelist = @_;
    my $mintrust;

    if ($access_type < TB_NODEACCESS_MIN ||
	$access_type > TB_NODEACCESS_MAX) {
	die("*** Invalid access type: $access_type!");
    }
711 712

    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    # Admins do whatever they want!
    # 
    if (TBAdmin($uid)) {
	return 1;
    }
    $uid = MapNumericUID($uid);
 
    if ($access_type == TB_NODEACCESS_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
    }
    else {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }

    foreach my $node (@nodelist) {
728
	my $query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
729 730 731 732 733 734
	    DBQueryFatal("select trust from reserved as n ".
			 "left join experiments as e on ".
			 "     e.pid=n.pid and e.eid=n.eid ".
			 "left join group_membership as g on ".
			 "     g.pid=e.pid and g.gid=e.gid ".
			 "where g.uid='$uid' and n.node_id='$node'");
735

736
	if ($query_result->numrows == 0) {
737 738
	    return 0;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
739 740 741 742 743
	my @row = $query_result->fetchrow_array();

	if (! TBMinTrust($row[0], $mintrust)) {
	    return 0;
	}
744 745 746 747 748
    }
    return 1;
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
749
# Access checks for an OSID. Tests for tbadmin.
750
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
751 752 753
# Usage: TBOSIDAccessCheck($uid, $osid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
754
# 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
755
sub TBOSIDAccessCheck($$$)
756
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
757 758
    my ($uid, $osid, $access_type) = @_;
    my $mintrust;
759

Leigh B. Stoller's avatar
Leigh B. Stoller committed
760 761
    if ($access_type < TB_OSID_MIN || $access_type > TB_OSID_MAX) {
	die("*** Invalid access type $access_type!");
762 763
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
764 765 766 767 768 769 770
    #
    # Admins do whatever they want!
    # 
    if (TBAdmin($uid)) {
	return 1;
    }
    $uid = MapNumericUID($uid);
771

Leigh B. Stoller's avatar
Leigh B. Stoller committed
772 773 774 775
    #
    # No GIDs yet.
    #
    my $query_result =
776
	DBQueryFatal("SELECT pid,shared FROM os_info WHERE osid='$osid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
777
    
778
    if ($query_result->numrows == 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
779
	return 0;
780
    }
781
    my @row = $query_result->fetchrow_array();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
782
    my $pid = $row[0];
783
    my $shared = $row[1];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
784 785 786 787

    #
    # Global OSIDs can be read by anyone.
    # 
788
    if ($shared) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
	if ($access_type == TB_OSID_READINFO) {
	    return 1;
	}
	return 0;
    }
    
    #
    # Otherwise must have proper trust in the project.
    # 
    if ($access_type == TB_OSID_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
    }
    else {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }

    return TBMinTrust(TBProjTrust($uid, $pid), $mintrust);
}

#
# Access checks for an ImageID
#
# Usage: TBImageIDAccessCheck($uid, $imageid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
# 
sub TBImageIDAccessCheck($$$)
{
    my ($uid, $imageid, $access_type) = @_;
    my $mintrust;

    if ($access_type < TB_IMAGEID_MIN || $access_type > TB_IMAGEID_MAX) {
	die("*** Invalid access type $access_type!");
822
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
823 824

    #
825
    # Admins and root do whatever they want!
Leigh B. Stoller's avatar
Leigh B. Stoller committed
826
    # 
827
    if (TBAdmin($uid) || !$UID || $UID eq "root" || $uid eq "root") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
828
	return 1;
829
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
830 831 832 833 834 835
    $uid = MapNumericUID($uid);

    #
    # No GIDs yet.
    #
    my $query_result =
836
	DBQueryFatal("SELECT pid,shared FROM images WHERE imageid='$imageid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
837 838 839
    
    if ($query_result->numrows == 0) {
	return 0;
840
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
841 842
    my @row = $query_result->fetchrow_array();
    my $pid = $row[0];
843
    my $shared = $row[1];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
844

845
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
846 847
    # Global ImageIDs can be read by anyone.
    # 
848
    if ($shared) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
849 850 851 852 853 854
	if ($access_type == TB_IMAGEID_READINFO) {
	    return 1;
	}
	return 0;
    }

855
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
856 857 858 859 860 861 862 863 864 865
    # Otherwise must have proper trust in the project.
    # 
    if ($access_type == TB_IMAGEID_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
    }
    else {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }

    return TBMinTrust(TBProjTrust($uid, $pid), $mintrust);
866 867
}

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
#
# Return Project leader. First argument pid.
#
# usage: ProjLeader(char *pid)
#        returns char *leader if a valid pid.
#        returns 0 if an invalid pid.
# 
sub ProjLeader($)
{
    my($pid) = @_;

    my $query_result =
	DBQueryFatal("select head_uid from projects where pid='$pid'");

    if ($query_result->numrows == 0) {
	return 0;
    }

    my @row = $query_result->fetchrow_array();
    return $row[0];
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
#
# Return Group leader. 
#
# usage: GroupLeader(char *pid, char *gid)
#        returns char *leader if a valid pid.
#        returns 0 if an invalid pid.
# 
sub GroupLeader($$)
{
    my($pid, $gid) = @_;

    my $query_result =
	DBQueryFatal("select leader from groups where ".
		     "pid='$pid' and gid='$gid'");

    if ($query_result->numrows == 0) {
	return 0;
    }

    my @row = $query_result->fetchrow_array();
    return $row[0];
}

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
#
# Return Experiment leader. First argument pid. Second argument is eid.
#
# usage: ExpLeader(char *pid, char *eid)
#        returns char *leader if a valid pid/eid.
#        returns 0 if an invalid pid/eid.
# 
sub ExpLeader($$)
{
    my($pid, $eid) = @_;

    my $query_result =
	DBQueryFatal("select expt_head_uid from experiments ".
		     "where eid='$eid' and pid='$pid'");

    if ($query_result->numrows == 0) {
	return 0;
    }

    my @row = $query_result->fetchrow_array();
    return $row[0];
}

936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
#
# Return Experiment state.
#
# usage: ExpState(char *pid, char *eid)
#        returns state if a valid pid/eid.
#        returns 0 if an invalid pid/eid or if an error.
# 
sub ExpState($$)
{
    my($pid, $eid) = @_;

    my $query_result =
	DBQueryWarn("select state from experiments ".
		    "where eid='$eid' and pid='$pid'");

    if (! $query_result ||
	$query_result->numrows == 0) {
	return 0;
    }

    my @row = $query_result->fetchrow_array();
    return $row[0];
}

#
# Set Experiment state.
#
# usage: SetExpState(char *pid, char *eid, char *state)
#        returns 1 if okay.
#        returns 0 if an invalid pid/eid or if an error.
# 
sub SetExpState($$$)
{
    my($pid, $eid, $state) = @_;

    my $query_result =
	DBQueryWarn("update experiments set state='$state' ".
		    "where eid='$eid' and pid='$pid'");

975
    if (! $query_result) {
976 977 978 979 980
	return 0;
    }
    return 1;
}

981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
#
# Set the swap in/out time for an experiment.
#
# usage: TBSetExpSwapTime(char *pid, char *eid)
#        returns 1 if okay.
#        returns 0 if an invalid pid/eid or if an error.
# 
sub TBSetExpSwapTime($$)
{
    my($pid, $eid) = @_;

    my $query_result =
	DBQueryWarn("update experiments set expt_swapped=now() ".
		    "where eid='$eid' and pid='$pid'");

    if (! $query_result ||
	$query_result->numrows == 0) {
	return 0;
    }
    return 1;
}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
#
# Lock Experiment.
#
# usage: TBLockExp(char *pid, char *eid)
#        returns 1 if okay.
#        returns 0 if an invalid pid/eid or if an error.
# 
sub TBLockExp($$)
{
    my($pid, $eid) = @_;

    my $query_result =
	DBQueryWarn("update experiments set expt_locked=now() ".
		    "where eid='$eid' and pid='$pid'");

    if (! $query_result ||
	$query_result->numrows == 0) {
	return 0;
    }
    return 1;
}

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
#
# Test if Experiment is locked
#
# usage: TBExpLocked(char *pid, char *eid)
#        returns 1 if locked.
#        returns 0 if an invalid pid/eid or if an error.
# 
sub TBExpLocked($$)
{
    my($pid, $eid) = @_;

    my $query_result =
	DBQueryWarn("select expt_locked from experiments ".
		    "where eid='$eid' and pid='$pid'");

    if (! $query_result ||
	$query_result->numrows == 0) {
	return 0;
    }
    my @row = $query_result->fetchrow_array();
    if (! defined($row[0])) {
	return 0;
    }
    return 1;
}

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
#
# UnLock Experiment.
#
# usage: TBUnLockExp(char *pid, char *eid)
#        returns 1 if okay.
#        returns 0 if an invalid pid/eid or if an error.
# 
sub TBUnLockExp($$)
{
    my($pid, $eid) = @_;

    my $query_result =
	DBQueryWarn("update experiments set expt_locked=NULL ".
		    "where eid='$eid' and pid='$pid'");

    if (! $query_result ||
1067 1068 1069 1070 1071 1072
	$query_result->numrows == 0) {
	return 0;
    }
    return 1;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
#
# Return BatchMode state.
#
# usage: TBBatchState(char *pid, char *eid)
#        returns state if a valid pid/eid.
#        returns 0 if an invalid pid/eid or if an error.
# 
sub TBBatchState($$)
{
    my($pid, $eid) = @_;

    my $query_result =
	DBQueryWarn("select batchstate from experiments ".
		    "where eid='$eid' and pid='$pid' and batchmode=1");

    if (! $query_result ||
	$query_result->numrows == 0) {
	return 0;
    }

    my @row = $query_result->fetchrow_array();
    return $row[0];
}

#
# Set BatctMode state.
#
# usage: SetBatchState(char *pid, char *eid, char *state)
#        returns 1 if okay.
#        returns 0 if an invalid pid/eid or if an error.
# 
sub TBSetBatchState($$$)
{
    my($pid, $eid, $state) = @_;

    my $query_result =
	DBQueryWarn("update experiments set batchstate='$state',batchmode=1 ".
		    "where eid='$eid' and pid='$pid'");

    if (! $query_result ||
	$query_result->numrows == 0) {
	return 0;
    }
    return 1;
}

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
#
# Return a list of all the nodes in an experiment.
#
# usage: ExpNodes(char *pid, char *eid)
#        returns the list if a valid pid/eid.
#        returns 0 if an invalid pid/eid or if an error.
# 
sub ExpNodes($$)
{
    my($pid, $eid) = @_;
    my(@row);
    my(@nodes);

    my $query_result =
	DBQueryWarn("select node_id from reserved where ".
		    "pid='$pid' and eid='$eid'");

    if (! $query_result or
	$query_result->numrows == 0) {
	return ();
    }
    while (@row = $query_result->fetchrow_array()) {
1141 1142 1143 1144 1145 1146
	$node = $row[0];

	#
	# Taint check. I do not understand this sillyness, but if I
	# taint check these node names, I avoid warnings throughout.
	# 
1147
	if ($node =~ /^([-\w]+)$/) {
1148 1149 1150 1151 1152 1153 1154
	    $node = $1;
	    
	    push(@nodes, $node);
	}
	else {
	    print "*** $0: WARNING: Bad node name: $node.\n";
	}
1155 1156 1157 1158
    }
    return @nodes;
}

1159
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1160 1161 1162
# Mark a node as down. We schedule a next reservation for it so that it
# remains in the users experiment through the termination so that there
# are no permission errors (say, from snmpit).
1163 1164 1165 1166 1167 1168
#
# usage: MarkNodeDown(char *nodeid)
#
sub MarkNodeDown($)
{
    my($node) = $_[0];
1169 1170
    my($pid, $eid);

1171 1172
    $pid = NODEDEAD_PID;
    $eid = NODEDEAD_EID;
1173 1174

    my $query_result =
1175
	DBQueryFatal("replace into next_reserve " .
1176 1177
		     "(node_id, pid, eid) " .
		     "values ('$node', '$pid', '$eid')");    
1178 1179 1180 1181 1182 1183

    if ($query_result->num_rows < 1) {
	DBWarn("WARNING: Could not mark $node down");
    }
}

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
#
# Set the boot status for a node.
#
# usage: SetNodeBootStatus(char *status)
#
sub SetNodeBootStatus($$)
{
    my($node, $bstat) = @_;

    DBQueryFatal("update nodes set bootstatus='$bstat' ".
		 "where node_id='$node'");
}

1197 1198 1199 1200 1201 1202
#
# Check if a particular feature is supported by an OSID. 
#
# usage: OSFeatureSupported(char *osid, char *feature)
#        returns 1 if supported, 0 if not.
#
1203
sub OSFeatureSupported($$) {
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
    my($osid, $feature) = @_;

    my $query_result =
	DBQueryFatal("select osfeatures from os_info where osid='$osid'");

    # Invalid OSID?
    if ($query_result->numrows < 1) {
	return 0;
    }
    
    foreach my $osfeature (split(',', $query_result->fetchrow_array())) {
	if ($feature eq $osfeature) {
	    return 1;
	}
    }
    return 0;
}

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
#
# Find out what osid a node will boot next time it comes up, 
# Usually (but not always) the currently running OS as well.
# May also return TB_OSID_MBKERNEL
#
sub TBBootWhat($;$) {
    # WARNING!!!
    #
    # DO NOT change this function without making corresponding changes to
    # the C version of this code in the bootinfo_mysql.c . They MUST
    # ALWAYS find exactly the same resulting OSID given the same inputs.
    # [The only exception is that here we must take into account 
    # *pxe_boot_path since that can specify an OS as well.]
    #
    # Boot priorities go like this:
    # NEXT_PXE_BOOT_PATH
    # PXE_BOOT_PATH
    # NEXT_BOOT_PATH        multiboot kernel
    # DEF_BOOT_PATH         multiboot kernel
    # NEXT_BOOT_OSID        osid from os_info
    # DEF_BOOT_OSID         osid from os_info
    # if all the above are null, error
    my $node = shift || return 0; # error if no node
    my $d = shift || 0; # debug flag
    my $cmd = "select path from os_info where osid='".TB_OSID_PXEBOOT."'";
    my $q = DBQueryFatal($cmd);
    my @r = $q->fetchrow_array();
    my $defpxepath = $r[0];
    $cmd = "select next_pxe_boot_path, pxe_boot_path, 
next_boot_path, def_boot_path, next_boot_osid, def_boot_osid 
from nodes where node_id = '$node'";
    $q = DBQueryFatal($cmd);
    if ($q->numrows() < 1) {
	warn("TBBootWhat: Ignoring invalid node '$node' (non-existent)\n");
	return 0;
    }
    @r = $q->fetchrow_array();
    $nextpxepath=$r[0];
    $pxepath=$r[1];
    $nextpath=$r[2];
    $defpath=$r[3];
    $nextosid=$r[4];
    $defosid=$r[5];
    print "TBBootWhat: $node => (".($nextpxepath?"nextpxe=$nextpxepath, ":"").
	( $pxepath ne $defpxepath ? "pxe=$pxepath, ":"") .
	    "np=$nextpath, dp=$defpath, no=$nextosid, do=$defosid)\n" if $d>1;
    # With PXE paths, look up the osid for the path. If none, use 
    # TB_OSID_MBKERNEL. If it is the default pxe path, just go on like normal.
    if (defined($nextpxepath) && $nextpxepath) {
	if ($nextpxepath ne $defpxepath) {
	    print "TBBootWhat: Using next_pxe_boot_path $nextpxepath\n" if $d;
	    my $cmd = "select osid from os_info where path='$nextpxepath'";
	    my $q = DBQueryFatal($cmd);
	    if ($q->numrows() > 0) {
		my @r = $q->fetchrow_array();
		my $osid = $r[0];
		return $osid;
	    } else {
		return TB_OSID_MBKERNEL;
	    }
	} # else go on as normal
    } else {
	if ($pxepath ne $defpxepath) {
	    print "TBBootWhat: Using pxe_boot_path $pxepath\n" if $d;
	    my $cmd = "select osid from os_info where path='$pxepath'";
	    my $q = DBQueryFatal($cmd);
	    if ($q->numrows() > 0) {
		my @r = $q->fetchrow_array();
		my $osid = $r[0];
		return $osid;
	    } else {
		return TB_OSID_MBKERNEL;
	    }
	} # else go on as normal
    }	
    if ((defined($nextpath) && $nextpath) || 
	(defined($defpath) && $defpath)) {
	# No OSID. Return a magic one.
	print "TBBootWhat: Using {def,next}_boot_path ${nextpath}$defpath\n" 
	    if $d;
	return TB_OSID_MBKERNEL;
    }
    if (defined($nextosid) && $nextosid) { 
	print "TBBootWhat: Using next_boot_osid $nextosid\n" if $d;
	return $nextosid; 
    }
    if (defined($defosid) && $defosid) { 
	print "TBBootWhat: Using def_boot_osid $defosid\n" if $d;
	return $defosid; 
    }
    warn("***Warning: node '$node': All boot info was null!\n");
    return TB_OSID_MBKERNEL;
}

1316 1317 1318 1319 1320 1321
#
# Ah, what a hack! I'm tired of seeing regexs for sharks scattered around
# the code. Anyway, this checks to see if a node is a shelf, and fills
# in the shelf/node, return 1 if it is. The shelf/node arguments are
# optional, if all you want to do is see if its a shelf type thing.
#
1322
# usage: IsShelved(char *nodeid, [\$shelf], [\$node])
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
#        returns 1 if the node is a shelf type thing. Optionally fills in info.
#        returns 0 if the node is just a normal kind of node.
#
sub IsShelved ($;$$) {
    my($nodeid, $shelf, $node) = @_;

    if ($nodeid =~ /sh(\d+)-(\d+)/) {
	if (defined($shelf)) {
	    $$shelf = $1;
	}
	if (defined($node)) {
	    $$node = $2;
	}
	return 1;
    }
    return 0;
}

1341
#
1342
# Map nodeid to its pid/eid/vname. vname is optional.
1343
#
1344
# usage: NodeidToExp(char *nodeid, \$pid, \$eid, \$vname)
1345 1346 1347
#        returns 1 if the node is reserved.
#        returns 0 if the node is not reserved.
#
1348 1349
sub NodeidToExp ($$$;$) {
    my($nodeid, $pid, $eid, $vname) = @_;
1350 1351

    my $query_result =
1352 1353
	DBQueryWarn("select pid,eid,vname from reserved ".
		    "where node_id='$nodeid'");
1354

1355 1356
    if (! $query_result ||
	! $query_result->num_rows) {
1357 1358 1359 1360 1361 1362
	return 0;
    }
    
    my @row = $query_result->fetchrow_array();
    $$pid = $row[0];
    $$eid = $row[1];
1363 1364 1365 1366 1367 1368 1369 1370
    if (defined($vname)) {
	if (defined($row[2])) {
	    $$vname = $row[2];
	}
	else {
	    $$vname = undef;
	}
    }
1371 1372 1373
    return 1;
}

1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
#
# Map a pid/eid/vname to its real nodename
#
# usage: VnameToNodeid(char *pid, char * eid, char *vname, \$nodeid)
#        returns 1 if the specified pid/eid/vname exists
#        returns 0 if it does not
#
sub VnameToNodeid ($$$$) {
    my($pid, $eid, $vname, $nodeid) = @_;

    my $query_result =
	DBQueryWarn("select node_id from reserved ".
		    "where pid='$pid' and eid='$eid' and vname='$vname'");

    if (! $query_result ||
	! $query_result->num_rows) {
	return 0;
    }
    
    my @row = $query_result->fetchrow_array();
    $$nodeid = $row[0];
    return 1;
}


Leigh B. Stoller's avatar
Leigh B. Stoller committed
1399 1400