newimageid.php3 26.7 KB
Newer Older
1
<?php
Leigh Stoller's avatar
Leigh Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2004 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
5 6
# All rights reserved.
#
7 8
include("defs.php3");
include("showstuff.php3");
9
include("osiddefs.php3");
10 11 12 13

#
# Standard Testbed Header
#
14
PAGEHEADER("Create a new Image Descriptor (long form)");
15 16

#
17
# Only known and logged in users!
18 19 20 21 22 23
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

#
24
# See what projects the uid can do this in.
25
#
26 27 28 29 30
$projlist = TBProjList($uid, $TB_PROJECT_MAKEIMAGEID);

if (! count($projlist)) {
    USERERROR("You do not appear to be a member of any Projects in which ".
	      "you have permission to create new Image descriptors.", 1);
31
}
32 33

#
34 35 36
# Need a list of node types. We join this over the nodes table so that
# we get a list of just the nodes that currently in the testbed, not
# just in the node_types table.
37 38
#
$types_result =
39 40 41
    DBQueryFatal("select distinct n.type from nodes as n ".
		 "left join node_types as nt on n.type=nt.type ".
		 "where nt.imageable=1");
42 43 44 45 46 47 48 49 50 51

#
# Spit the form out using the array of data. 
# 
function SPITFORM($formfields, $errors)
{
    global $uid, $projlist, $isadmin, $types_result;
    global $TBDB_IMAGEID_IMAGENAMELEN, $TBDB_NODEIDLEN;
    
    if ($errors) {
52 53
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
54
              <tr>
55
                 <th align=center colspan=2>
56
                   <font size=+1 color=red>
57
                      &nbsp;Oops, please fix the following errors!&nbsp;
58 59 60 61 62 63
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
64 65 66 67
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
68 69 70 71 72 73 74 75 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
                  </tr>\n";
	}
	echo "</table><br>\n";
    }

    #
    # Get the OSID list that the user has access to. Do not allow shared OSIDs,
    # since we want to force mere users to create thier own versions.
    #
    if ($isadmin) {
	$osid_result =
	    DBQueryFatal("select * from os_info ".
			 "where (path='' or path is NULL) and ".
			 "      version!='' and version is not NULL ".
			 "order by osid");
    }
    else {
	$osid_result =
	    DBQueryFatal("select distinct o.* from os_info as o ".
			 "left join group_membership as m ".
			 " on m.pid=o.pid ".
			 "where m.uid='$uid' and ".
			 "      (path='' or path is NULL) and ".
			 "      version!='' and version is not NULL ".
			 "order by o.pid,o.osid");
    }
    if (! mysql_num_rows($osid_result)) {
	USERERROR("There are no OS Descriptors that you are able to use!", 1);
    }

    echo "<SCRIPT LANGUAGE=JavaScript>
              function SetPrefix(theform) 
              {
101 102 103 104 105 106 107 108 109 110 111 112 113
                  var pidx   = theform['formfields[pid]'].selectedIndex;
                  var pid    = theform['formfields[pid]'].options[pidx].value;
                  var gidx   = theform['formfields[gid]'].selectedIndex;
                  var gid    = theform['formfields[gid]'].options[gidx].value;
                  var shared = theform['formfields[shared]'].checked;
          \n";
    if ($isadmin)
	echo     "var global = theform['formfields[global]'].checked;";
    else
	echo     "var global = 0;";

    echo         "if (pid == '') {
                      theform['formfields[path]'].value = '/proj';
114 115 116
                  }
                  else if (theform['formfields[imagename]'].value == '') {
		      theform['formfields[imagename]'].defaultValue = '';
117 118 119 120 121 122 123 124 125 126 127

                      if (global) {
    	                  theform['formfields[path]'].value =
                                  '/usr/testbed/images/';
                      }
		      else if (gid == '' || gid == pid || shared) {
    	                  theform['formfields[path]'].value =
                                  '/proj/' + pid + '/images/';
                      }
                      else {
    	                  theform['formfields[path]'].value =
128
                                  '/groups/' + pid + '/' + gid + '/images/';
129
                      }
130 131
                  }
                  else if (theform['formfields[imagename]'].value != '') {
132 133 134 135 136 137 138 139 140 141 142 143 144
                      var filename = theform['formfields[imagename]'].value +
                                     '.ndz';

                      if (global) {
    	                  theform['formfields[path]'].value =
                                  '/usr/testbed/images/' + filename;
                      }
		      else if (gid == '' || gid == pid || shared) {
    	                  theform['formfields[path]'].value =
                                  '/proj/' + pid + '/images/' + filename;
                      }
                      else {
    	                  theform['formfields[path]'].value =
145
                                  '/groups/' + pid + '/' + gid + '/images/' +
146 147
                                  filename;
                      }
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
                  }
              }
          </SCRIPT>\n";

    echo "<br>
          <table align=center border=1> 
          <tr>
             <td align=center colspan=2>
                 <em>(Fields marked with * are required)</em>
             </td>
          </tr>
          <form action='newimageid.php3' method=post name=idform>\n";

    #
    # Select Project
    #
    echo "<tr>
              <td>*Select Project:</td>
              <td><select name=\"formfields[pid]\"
                          onChange='SetPrefix(idform);'>
                      <option value=''>Please Select &nbsp</option>\n";
    
    while (list($project) = each($projlist)) {
	$selected = "";

	if ($formfields[pid] == $project)
	    $selected = "selected";
	
	echo "        <option $selected value='$project'>$project </option>\n";
    }
    echo "       </select>";
    echo "    </td>
          </tr>\n";

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
    #
    # Select a group
    # 
    echo "<tr>
              <td >Group:</td>
              <td><select name=\"formfields[gid]\"
                          onChange='SetPrefix(idform);'>
                    <option value=''>Default Group </option>\n";

    reset($projlist);
    while (list($project, $grouplist) = each($projlist)) {
	for ($i = 0; $i < count($grouplist); $i++) {
	    $group    = $grouplist[$i];

	    if (strcmp($project, $group)) {
		$selected = "";

		if (isset($formfields[gid]) &&
		    isset($formfields[pid]) &&
		    strcmp($formfields[pid], $project) == 0 &&
		    strcmp($formfields[gid], $group) == 0)
		    $selected = "selected";
		
		echo "<option $selected value=\"$group\">
                           $project/$group</option>\n";
	    }
	}
    }
    echo "     </select>
             </td>
          </tr>\n";

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
    #
    # Image Name:
    #
    echo "<tr>
              <td>*Descriptor Name (no blanks):</td>
              <td class=left>
                  <input type=text
                         onChange='SetPrefix(idform);'
                         name=\"formfields[imagename]\"
                         value=\"" . $formfields[imagename] . "\"
	                 size=$TBDB_IMAGEID_IMAGENAMELEN
                         maxlength=$TBDB_IMAGEID_IMAGENAMELEN>
              </td>
          </tr>\n";

    #
    # Description
    #
    echo "<tr>
              <td>*Description:<br>
                  (a short pithy sentence)</td>
              <td class=left>
                  <input type=text
                         name=\"formfields[description]\"
                         value=\"" . $formfields[description] . "\"
	                 size=50>
              </td>
          </tr>\n";

    #
    # Load Partition
    #
    echo "<tr>
              <td>*Starting DOS Partition:<br>
                  (1-4, 0 means entire disk)</td>
              <td><select name=\"formfields[loadpart]\">
                          <option value=X>Please Select </option>\n";

    for ($i = 0; $i <= 4; $i++) {
	$selected = "";

	if (strcmp($formfields[loadpart], "$i") == 0)
	    $selected = "selected";
	
	echo "        <option $selected value=$i>$i </option>\n";
    }
    echo "       </select>";
    echo "    </td>
          </tr>\n";

    #
    # Load length
    #
    echo "<tr>
              <td>*Number of DOS Partitions[<b>1</b>]:<br>
                  (1, 2, 3, or 4)</td>
              <td><select name=\"formfields[loadlength]\">
                          <option value=X>Please Select </option>\n";

    for ($i = 0; $i <= 4; $i++) {
	$selected = "";

	if (strcmp($formfields[loadlength], "$i") == 0)
	    $selected = "selected";
	
	echo "        <option $selected value=$i>$i </option>\n";
    }
    echo "       </select>";
    echo "    </td>
          </tr>\n";

285 286 287 288 289 290 291 292 293 294
    WRITEOSIDMENU("Partition 1 OS[<b>2</b>]", "formfields[part1_osid]",
		  $osid_result, $formfields[part1_osid]);
    WRITEOSIDMENU("Partition 2 OS", "formfields[part2_osid]",
		  $osid_result, $formfields[part2_osid]);
    WRITEOSIDMENU("Partition 3 OS", "formfields[part3_osid]",
		  $osid_result, $formfields[part3_osid]);
    WRITEOSIDMENU("Partition 4 OS", "formfields[part4_osid]",
		  $osid_result, $formfields[part4_osid]);
    WRITEOSIDMENU("Boot OS[<b>3</b>]", "formfields[default_osid]",
		  $osid_result, $formfields[default_osid]);
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

    #
    # Path to image.
    #
    echo "<tr>
              <td>Filename (full path) of Image[<b>4</b>]:<br>
                  (must reside in /proj)</td>
              <td class=left>
                  <input type=text
                         name=\"formfields[path]\"
                         value=\"" . $formfields[path] . "\"
	                 size=50>
              </td>
          </tr>\n";

    #
    # Node Types.
    #
    echo "<tr>
              <td>Node Types[<b>5</b>]:</td>
              <td>\n";

    mysql_data_seek($types_result, 0);
    while ($row = mysql_fetch_array($types_result)) {
        $type    = $row[type];
        $checked = "";

        if (strcmp($formfields["mtype_$type"], "Yep") == 0)
	    $checked = "checked";
    
        echo "<input $checked type=checkbox
                     value=Yep name=\"formfields[mtype_$type]\">
                     $type &nbsp
              </input>\n";
    }
    echo "    </td>
          </tr>\n";

    #
    # Node to Create image from.
    #
    echo "<tr>
              <td>Node to Create Image from[<b>6</b>]:</td>
              <td class=left>
                  <input type=text
                         name=\"formfields[node]\"
                         value=\"" . $formfields[node] . "\"
	                 size=$TBDB_NODEIDLEN maxlength=$TBDB_NODEIDLEN>
              </td>
          </tr>\n";

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    #
    # Shared?
    #
    echo "<tr>
  	      <td>Shared?:<br>
                  (available to all subgroups)</td>
              <td class=left>
                  <input type=checkbox
                         onClick='SetPrefix(idform);'
                         name=\"formfields[shared]\"
                         value=Yep";

    if (isset($formfields[shared]) &&
	strcmp($formfields[shared], "Yep") == 0)
	echo "           checked";
	
    echo "                       > Yes
              </td>
          </tr>\n";

366 367
    if ($isadmin) {
        #
368
        # Global?
369 370
        #
	echo "<tr>
371
  	          <td>Global?:<br>
372 373 374
                      (available to all projects)</td>
                  <td class=left>
                      <input type=checkbox
375 376
                             onClick='SetPrefix(idform);'
                             name=\"formfields[global]\"
377 378
                             value=Yep";

379 380
	if (isset($formfields["global"]) &&
	    strcmp($formfields["global"], "Yep") == 0)
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
	    echo "           checked";
	
	echo "                       > Yes
                  </td>
              </tr>\n";

        #
        # Create mapping for this image.
        #
	echo "<tr>
  	          <td>Create Mapping?:<br>
                      (insert osid to imageid mappings)</td>
                  <td class=left>
                      <input type=checkbox
                             name=\"formfields[makedefault]\"
                             value=Yep";

	if (isset($formfields[makedefault]) &&
	    strcmp($formfields[makedefault], "Yep") == 0)
	    echo "           checked";
	
	echo "                       > Yes
                  </td>
              </tr>\n";
    }

    echo "<tr>
              <td align=center colspan=2>
                  <b><input type=submit name=submit value=Submit></b>
              </td>
          </tr>\n";

    echo "</form>
          </table>\n";

    echo "<h4><blockquote><blockquote><blockquote>
          <ol type=1 start=1>
             <li>Only single slices or partial disks are allowed.
  	         If you specify a non-zero starting load partition, the load
	         load length must be one. If you specify zero
	         for the starting load partition, then you can include any or
	         all of the slices (1-4). Note that '0' means to start at the
	         boot sector, while '1' means to start at the beginning of the
	         first partition (typically starting at sector 63).
             <li>If you are using a custom OS, you must
                  <a href='newosid_form.php3'>create the OS Descriptor
                  first!</a>
             <li>The OS (partition) that is active when the node boots up.
             <li>The image file must reside in the project directory.
             <li>Specify the node types that this image will be able
                  to work on (ie: can be loaded on and expected to work).
432 433 434 435 436
                  Typically, images will work on all of the \"pc\" types when
                  you are customizing one of the default images. However,
                  if you are installing your own OS from scratch, or you are
                  using DOS partition four, then this might not be true.
                  Feel free to ask us!
437 438 439 440 441
             <li>If you already have a node customized, enter that node
                 name (pcXXX) and the image will be auto created for you.
                 Notification of completion will be sent to you via email. 
          </ol>
          </blockquote></blockquote></blockquote></h4>\n";
442
}
443 444 445 446 447 448 449 450
    
#
# On first load, display a virgin form and exit.
#
if (! $submit) {
    $defaults = array();
    $defaults[loadpart] = "X";
    $defaults[path]     = "/proj/";
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

    #
    # For users that are in one project and one subgroup, it is usually
    # the case that they should use the subgroup, and since they also tend
    # to be in the clueless portion of our users, give them some help.
    # 
    if (count($projlist) == 1) {
	list($project, $grouplist) = each($projlist);
	
	if (count($grouplist) <= 2) {
	    $defaults[pid] = $project;
	    if (count($grouplist) == 1 || strcmp($project, $grouplist[0]))
		$group = $grouplist[0];
	    else {
		$group = $grouplist[1];
	    }
	    $defaults[gid] = $group;
	    
	    if (!strcmp($project, $group))
		$defaults[path]     = "/proj/$project/images/";
	    else
472
		$defaults[path]     = "/groups/$project/$group/images/";
473 474 475 476
	}
	reset($projlist);
    }
 
477 478 479
    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
480
}
481 482 483 484 485 486 487 488 489

#
# Otherwise, must validate and redisplay if errors
#
$errors = array();

if (!isset($formfields[pid]) ||
    strcmp($formfields[pid], "") == 0) {
    $errors["Project"] = "Not Selected";
490
}
491 492
elseif (!TBValidProject($formfields[pid])) {
    $errors["Project"] = "No such project";
493
}
494 495
elseif (!TBProjAccessCheck($uid, $formfields[pid],
			   $formfields[pid], $TB_PROJECT_MAKEIMAGEID)) {
496
    $errors["Project"] = "Not enough permission";
497 498
}

499 500 501
if (!isset($formfields[imagename]) ||
    strcmp($formfields[imagename], "") == 0) {
    $errors["Descriptor Name"] = "Missing Field";
502
}
503
else {
Leigh Stoller's avatar
Leigh Stoller committed
504
    if (! ereg("^[a-zA-Z0-9][-_a-zA-Z0-9\.\+]+$", $formfields[imagename])) {
505
	$errors["Descriptor Name"] =
Leigh Stoller's avatar
Leigh Stoller committed
506
	    "Must be alphanumeric (includes _, -, +, and .)<br>".
507 508 509 510
	    "and must begin with an alphanumeric";
    }
    elseif (strlen($formfields[imagename]) > $TBDB_IMAGEID_IMAGENAMELEN) {
	$errors["Descriptor Name"] =
511 512
	    "Too long! ".
	    "Must be less than or equal to $TBDB_IMAGEID_IMAGENAMELEN";
513
    }
514 515
}

516 517 518
if (!isset($formfields[description]) ||
    strcmp($formfields[description], "") == 0) {
    $errors["Description"] = "Missing Field";
519 520
}

521 522 523 524
if (!isset($formfields[loadpart]) ||
    strcmp($formfields[loadpart], "") == 0 ||
    strcmp($formfields[loadpart], "X") == 0) {
    $errors["Starting Partition"] = "Not Selected";
525
}
526 527 528
elseif (! ereg("^[0-9]+$", $formfields[loadpart]) ||
	$formfields[loadpart] < 0 || $formfields[loadpart] > 4) {
    $errors["Starting Partition"] = "Must be 0,1,2,3, or 4!";
529
}
530 531 532 533 534 535 536 537 538 539 540 541 542

if (!isset($formfields[loadlength]) ||
    strcmp($formfields[loadlength], "") == 0 ||
    strcmp($formfields[loadlength], "X") == 0) {
    $errors["#of Partitions"] = "Not Selected";
}
elseif (! ereg("^[0-9]+$", $formfields[loadlength]) ||
	$formfields[loadlength] < 1 || $formfields[loadlength] > 4) {
    $errors["#of Partitions"] = "Must be 1,2,3, or 4!";
}
elseif ($formfields[loadpart] != 0 && $formfields[loadlength] != 1) {
    $errors["#of Partitions"] =
	"Only single slices or<br> partial disks are allowed";
543 544 545 546
}

#
# Check sanity of the OSIDs for each slice. Permission checks not needed.
547 548
# Store the ones we care about and silently forget about the extraneous
# OSIDs by setting the locals to NULL.
549
#
550 551
# XXX This loops creates locals part1_osid, part2_osid, part3_osid, and
#     part4_osid on the fly. Look at $$foo. We use them below.
552
#
553 554
$osid_array = array();

555
for ($i = 1; $i <= 4; $i++) {
556 557
    $foo      = "part${i}_osid";	# Local variable dynamically created.
    $thisosid = $formfields[$foo];
558

559 560
    if (($formfields[loadpart] && $i == $formfields[loadpart]) ||
	(!$formfields[loadpart] && $i <= $formfields[loadlength])) {
561 562

	if (!isset($thisosid) ||
563 564 565
	    strcmp($thisosid, "") == 0 ||
	    strcmp($thisosid, "X") == 0) {
	    $errors["Partition $i OS"] = "Must select an OS";
566
	}
567 568 569 570 571 572 573 574 575 576 577 578 579 580
	elseif (strcmp($thisosid, "none") == 0) {
	    #
	    # Allow admins to specify no OS for a partition.
	    # 
	    if (!$isadmin)	    
		$errors["Partition $i OS"] = "Must select an OS";
	    $$foo = "NULL";	    
	}
	elseif (!TBValidOSID($thisosid)) {
	    $errors["Partition $i OS"] = "No such OS defined";
	}
	else {
	    $$foo = "'$thisosid'";
	    $osid_array[] = $thisosid;
581 582 583 584 585 586 587
	}
    }
    else {
	$$foo = "NULL";
    }
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
#
# Check the boot OS. Must be one of the OSes selected for a partition.
# 
if (!isset($formfields[default_osid]) ||
    strcmp($formfields[default_osid], "") == 0 ||
    strcmp($formfields[default_osid], "none") == 0) {
    $errors["Boot OS"] = "Not Selected";
}
elseif (!TBValidOSID($formfields[default_osid])) {
    $errors["Boot OS"] = "No such OS defined";
}
else {
    for ($i = 0; $i < count($osid_array); $i++) {
	if (strcmp($osid_array[$i], $formfields[default_osid]) == 0)
	    break;
    }
    if ($i == count($osid_array)) 
	$errors["Boot OS"] = "Invalid; Must be one of the partitions";
}

608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
#
# Only admin types can set the global bit for an image. Ignore silently.
#
$global = 0;
if ($isadmin &&
    isset($formfields["global"]) &&
    strcmp($formfields["global"], "Yep") == 0) {
    $global = 1;
}

$shared = 0;
if (isset($formfields[shared]) &&
    strcmp($formfields[shared], "Yep") == 0) {
    $shared = 1;
}
# Does not make sense to do this. 
if ($global && $shared) {
    $errors["Global"] = "Image declared both shared and global";
}

628 629 630 631 632 633 634 635
#
# The path must not contain illegal chars and it must be more than
# the original /proj/$pid we gave the user. We allow admins to specify
# a path outside of /proj though.
# 
if (!isset($formfields[path]) ||
    strcmp($formfields[path], "") == 0) {
    $errors["Path"] = "Missing Field";
636
}
637 638 639
elseif (! ereg("^[-_a-zA-Z0-9\/\.+]+$", $formfields[path])) {
    $errors["Path"] = "Contains invalid characters";
}
640 641 642 643 644 645 646 647 648 649 650
elseif (! $isadmin) {
    $pdef = "";
    
    if (!$shared &&
	isset($formfields[gid]) &&
	strcmp($formfields[gid], "") &&
	strcmp($formfields[gid], $formfields[pid])) {
	$pdef = "/groups/" . $formfields[pid] . "/" . $formfields[gid] . "/";
    }
    else {
	$pdef = "/proj/" . $formfields[pid] . "/images/";
651
    }
652 653

    if (strpos($formfields[path], $pdef) === false) {
654 655
	$errors["Path"] = "Invalid Path";
    }
656 657 658
}

#
659 660
# See what node types this image will work on. Must be at least one!
# Store the valid types in a new array for simplicity.
661
#
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
$mtypes_array = array();

while ($row = mysql_fetch_array($types_result)) {
    $type = $row[type];
    $foo  = $formfields["mtype_$type"];

    #
    # Look for a post variable with name.
    # 
    if (isset($foo) &&
	strcmp($foo, "Yep") == 0) {
	$mtypes_array[] = $type;
    }
}
if (! count($mtypes_array)) {
    $errors["Node Types"] = "Must select at least one type";
678 679 680
}

#
681
# Check sanity of node name and that user can create an image from it.
682
#
683 684 685 686 687 688 689 690 691 692 693 694
if (isset($formfields[node]) &&
    strcmp($formfields[node], "")) {

    if (! TBValidNodeName($formfields[node])) {
	$errors["Node"] = "Invalid node name";
    }
    
    if (! TBNodeAccessCheck($uid, $formfields[node],
			    $TB_NODEACCESS_LOADIMAGE)) {
	$errors["Node"] = "Not enough permission";
    }
    $node = $formfields[node];
695 696 697
}

#
698 699
# Only admins have this option. Always on for mereusers, but default off
# for admins. 
700
#
701 702 703 704 705 706
$makedefault = 0;

if (! $isadmin ||
    (isset($formfields[makedefault]) &&
     strcmp($formfields[makedefault], "Yep") == 0)) {
    $makedefault = 1;
707
}
708 709

#
710 711 712 713 714 715 716
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
# 
if (count($errors)) {
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
717 718
}

719 720 721 722 723
#
# For the rest, sanitize and convert to locals to make life easier.
# 
$description = addslashes($formfields[description]);
$pid         = $formfields[pid];
724
$gid         = $formfields[gid];
725 726 727 728 729
$imagename   = $formfields[imagename];
$loadpart    = $formfields[loadpart];
$loadlength  = $formfields[loadlength];
$default_osid= $formfields[default_osid];
$path        = $formfields[path];
730 731 732
if (!isset($gid) || !strcmp($gid, "")) {
    $gid = $pid;
}
733

734 735 736
#
# And insert the record!
#
737 738 739 740 741 742 743 744 745 746 747 748 749

DBQueryFatal("lock tables images write, osidtoimageid write");

#
# Of course, the Image record may not already exist in the DB.
#
if (TBValidImage($pid, $imagename)) {
    DBQueryFatal("unlock tables");

    $errors["Descriptor Name"] = "Already in use in selected project";
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
750
}
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

#
# Just concat them to form a unique imageid. 
# 
$imageid = "$pid-$imagename";
if (TBValidImageID($imageid)) {
    DBQueryFatal("unlock tables");
    TBERROR("Could not form a unique imageid for $pid/$imagename!", 1);
}

#
# Mereusers are not allowed to create more than one osid/imageid mapping
# for each machinetype. They cannot actually do that through the EZ form
# since the osid/imageid has to be unique, but it can happen by mixed
# use of the long form and the short form, or with multiple uses of the
# long form. 
#
$typeclause = "type=" . "'$mtypes_array[0]'";

for ($i = 1; $i < count($mtypes_array); $i++) {
    $typeclause = "$typeclause or type=" . "'$mtypes_array[$i]'";
772
}
773 774 775 776 777

$osidclause = "osid=" . "'$osid_array[0]'";
    
for ($i = 1; $i < count($osid_array); $i++) {
    $osidclause = "$osidclause or osid=" . "'$osid_array[$i]'";
778
}
779 780 781 782 783 784 785 786 787 788 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 822 823 824 825 826 827
    
$query_result =
    DBQueryFatal("select osidtoimageid.*,images.pid,images.imagename ".
		 " from osidtoimageid ".
		 "left join images on ".
		 " images.imageid=osidtoimageid.imageid ".
		 "where ($osidclause) and ($typeclause)");

if (mysql_num_rows($query_result)) {
    if (!$isadmin || $makedefault) {
	DBQueryFatal("unlock tables");

	echo "<center>
              There are other image descriptors that specify the 
	      same OS descriptors for the same node types.<br>
              There must be a
	      unique mapping of OS descriptor to Image descriptor for
              each node type! Perhaps you need to delete one of the
              images below, or create a new OS descriptor to use in
              this new Image descriptor.
              </center><br>\n";

	echo "<table border=1 cellpadding=2 cellspacing=2 align='center'>\n";

	echo "<tr>
                  <td align=center>OSID</td>
                  <td align=center>Type</td>
                  <td align=center>ImageID</td>
             </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
	    $imageid   = $row['imageid'];
	    $url       = rawurlencode($imageid);
	    $osid      = $row[osid];
	    $type      = $row[type];
	    $imagename = $row[imagename];
	    
	    echo "<tr>
                      <td>$osid</td>
	              <td>$type</td>
                      <td><A href='showimageid.php3?&imageid=$url'>
                             $imagename</A></td>
	          </tr>\n";
	}
	echo "</table><br><br>\n";
    
	USERERROR("Please check the other Image descriptors and make the ".
		  "necessary changes!", 1);
    }
828 829 830 831
}

$query_result =
    DBQueryFatal("INSERT INTO images ".
832
		 "(imagename, imageid, description, loadpart, loadlength, ".
833
		 " part1_osid, part2_osid, part3_osid, part4_osid, ".
834 835
		 " default_osid, path, pid, gid, shared, global, creator, ".
		 " created) ".
836
		 "VALUES ".
837 838
		 "  ('$imagename', '$imageid', '$description', $loadpart, ".
		 "   $loadlength, ".
839
		 "   $part1_osid, $part2_osid, $part3_osid, $part4_osid, ".
840 841
		 "   '$default_osid', '$path', '$pid', '$gid', $shared, ".
	         "   $global, '$uid', now())");
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865

if (!$isadmin || $makedefault) {
    for ($i = 0; $i < count($mtypes_array); $i++) {
	for ($j = 0; $j < count($osid_array); $j++) {
	    DBQueryFatal("REPLACE INTO osidtoimageid ".
			 "(osid, type, imageid) ".
			 "VALUES ('$osid_array[$j]', '$mtypes_array[$i]', ".
			 "        '$imageid')");
	}
    }
}

DBQueryFatal("unlock tables");

SUBPAGESTART();
SUBMENUSTART("More Options");
if (! isset($node)) {
    $fooid = rawurlencode($imageid);
    WRITESUBMENUBUTTON("Edit this Image Descriptor",
		       "editimageid_form.php3?imageid=$fooid");
    WRITESUBMENUBUTTON("Delete this Image Descriptor",
		       "deleteimageid.php3?imageid=$fooid");
}
WRITESUBMENUBUTTON("Create a new Image Descriptor",
866
		   "newimageid_ez.php3");
867 868
WRITESUBMENUBUTTON("Create a new OS Descriptor",
		   "newosid_form.php3");
Leigh Stoller's avatar
Leigh Stoller committed
869
WRITESUBMENUBUTTON("Image Descriptor list",
870
		   "showimageid_list.php3");
Leigh Stoller's avatar
Leigh Stoller committed
871
WRITESUBMENUBUTTON("OS Descriptor list",
872 873
		   "showosid_list.php3");
SUBMENUEND();
874 875 876 877 878

#
# Dump os_info record.
# 
SHOWIMAGEID($imageid, 0);
879 880 881 882 883 884 885 886 887 888 889 890 891
SUBPAGEEND();

if (isset($node)) {
    #
    # Create the image.
    #
    # XXX There is no locking of the descriptor or the node. Not really a
    # problem for the descriptor; the script is done with it by the time
    # it returns. However, if the node is freed up, things are going to go
    # awry. 
    #
    # Grab the unix GID for running script.
    #
892
    TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);
893

894 895 896 897
    echo "<br>
          Creating image using node '$node' ...
          <br><br>\n";
    flush();
898

899
    SUEXEC($uid, $unix_gid, "webcreateimage -p $pid $imagename $node", 1);
900

901 902 903
    echo "This will take 10 minutes or more; you will receive email
          notification when the image is complete. In the meantime,
          <b>PLEASE DO NOT</b> delete the imageid or the experiment
904
          $node is in. In fact, it is best if you do not mess with 
905 906
          the node at all!<br>\n";
}
907 908 909 910 911 912

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>