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

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

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

#
23
# See what projects the uid can do this in.
24
#
25 26 27 28 29
$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);
30
}
31 32

#
33 34 35
# 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.
36 37
#
$types_result =
38 39 40
    DBQueryFatal("select distinct n.type from nodes as n ".
		 "left join node_types as nt on n.type=nt.type ".
		 "where nt.imageable=1");
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 70 71 72

#
# Helper function to write out a menu.
#
function WRITEOSIDMENU($caption, $value, $osid_result, $previous)
{
    echo "<tr>
            <td>*$caption:</td>";
    echo "  <td><select name=\"formfields[$value]\">
                <option value=X>Please Select </option>\n";

    mysql_data_seek($osid_result, 0);

    while ($row = mysql_fetch_array($osid_result)) {
	$osid   = $row[osid];
	$osname = $row[osname];
	$pid    = $row[pid];
	$selected = "";

	if (strcmp($previous, "$osid") == 0)
	    $selected = "selected";

	echo "<option $selected value='$osid'>$pid - $osname</option>\n";
    }
    $selected = "";
    if (strcmp($previous, "none") == 0)
	$selected = "selected";
	
    echo "         <option $selected value=none>No OS</option>\n";
    echo "       </select>";
    echo "    </td>
          </tr>\n";
73
}
74 75 76 77 78 79 80 81 82 83

#
# 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) {
84 85
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
86
              <tr>
87
                 <th align=center colspan=2>
88
                   <font size=+1 color=red>
89
                      &nbsp;Oops, please fix the following errors!&nbsp;
90 91 92 93 94 95
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
96 97 98 99
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
                  </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) 
              {
133 134 135 136 137 138 139 140 141 142 143 144 145
                  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';
146 147 148
                  }
                  else if (theform['formfields[imagename]'].value == '') {
		      theform['formfields[imagename]'].defaultValue = '';
149 150 151 152 153 154 155 156 157 158 159

                      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 =
160
                                  '/groups/' + pid + '/' + gid + '/images/';
161
                      }
162 163
                  }
                  else if (theform['formfields[imagename]'].value != '') {
164 165 166 167 168 169 170 171 172 173 174 175 176
                      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 =
177
                                  '/groups/' + pid + '/' + gid + '/images/' +
178 179
                                  filename;
                      }
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
                  }
              }
          </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";

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
    #
    # 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";

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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    #
    # 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";

    WRITEOSIDMENU("Partition 1 OS[<b>2</b>]",
		  "part1_osid", $osid_result, $formfields[part1_osid]);
    WRITEOSIDMENU("Partition 2 OS",
		  "part2_osid", $osid_result, $formfields[part2_osid]);
    WRITEOSIDMENU("Partition 3 OS",
		  "part3_osid", $osid_result, $formfields[part3_osid]);
    WRITEOSIDMENU("Partition 4 OS",
		  "part4_osid", $osid_result, $formfields[part4_osid]);
    WRITEOSIDMENU("Boot OS[<b>3</b>]",
		  "default_osid", $osid_result, $formfields[default_osid]);

    #
    # 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";

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    #
    # 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";

398 399
    if ($isadmin) {
        #
400
        # Global?
401 402
        #
	echo "<tr>
403
  	          <td>Global?:<br>
404 405 406
                      (available to all projects)</td>
                  <td class=left>
                      <input type=checkbox
407 408
                             onClick='SetPrefix(idform);'
                             name=\"formfields[global]\"
409 410
                             value=Yep";

411 412
	if (isset($formfields["global"]) &&
	    strcmp($formfields["global"], "Yep") == 0)
413 414 415 416 417 418 419 420 421 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
	    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).
464 465 466 467 468
                  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!
469 470 471 472 473
             <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";
474
}
475 476 477 478 479 480 481 482
    
#
# On first load, display a virgin form and exit.
#
if (! $submit) {
    $defaults = array();
    $defaults[loadpart] = "X";
    $defaults[path]     = "/proj/";
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

    #
    # 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
504
		$defaults[path]     = "/groups/$project/$group/images/";
505 506 507 508
	}
	reset($projlist);
    }
 
509 510 511
    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
512
}
513 514 515 516 517 518 519 520 521

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

if (!isset($formfields[pid]) ||
    strcmp($formfields[pid], "") == 0) {
    $errors["Project"] = "Not Selected";
522
}
523 524
elseif (!TBValidProject($formfields[pid])) {
    $errors["Project"] = "No such project";
525
}
526 527
elseif (!TBProjAccessCheck($uid, $formfields[pid],
			   $formfields[pid], $TB_PROJECT_MAKEIMAGEID)) {
528
    $errors["Project"] = "Not enough permission";
529 530
}

531 532 533
if (!isset($formfields[imagename]) ||
    strcmp($formfields[imagename], "") == 0) {
    $errors["Descriptor Name"] = "Missing Field";
534
}
535
else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
536
    if (! ereg("^[a-zA-Z0-9][-_a-zA-Z0-9\.\+]+$", $formfields[imagename])) {
537
	$errors["Descriptor Name"] =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
538
	    "Must be alphanumeric (includes _, -, +, and .)<br>".
539 540 541 542
	    "and must begin with an alphanumeric";
    }
    elseif (strlen($formfields[imagename]) > $TBDB_IMAGEID_IMAGENAMELEN) {
	$errors["Descriptor Name"] =
543 544
	    "Too long! ".
	    "Must be less than or equal to $TBDB_IMAGEID_IMAGENAMELEN";
545
    }
546 547
}

548 549 550
if (!isset($formfields[description]) ||
    strcmp($formfields[description], "") == 0) {
    $errors["Description"] = "Missing Field";
551 552
}

553 554 555 556
if (!isset($formfields[loadpart]) ||
    strcmp($formfields[loadpart], "") == 0 ||
    strcmp($formfields[loadpart], "X") == 0) {
    $errors["Starting Partition"] = "Not Selected";
557
}
558 559 560
elseif (! ereg("^[0-9]+$", $formfields[loadpart]) ||
	$formfields[loadpart] < 0 || $formfields[loadpart] > 4) {
    $errors["Starting Partition"] = "Must be 0,1,2,3, or 4!";
561
}
562 563 564 565 566 567 568 569 570 571 572 573 574

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";
575 576 577 578
}

#
# Check sanity of the OSIDs for each slice. Permission checks not needed.
579 580
# Store the ones we care about and silently forget about the extraneous
# OSIDs by setting the locals to NULL.
581
#
582 583
# XXX This loops creates locals part1_osid, part2_osid, part3_osid, and
#     part4_osid on the fly. Look at $$foo. We use them below.
584
#
585 586
$osid_array = array();

587
for ($i = 1; $i <= 4; $i++) {
588 589
    $foo      = "part${i}_osid";	# Local variable dynamically created.
    $thisosid = $formfields[$foo];
590

591 592
    if (($formfields[loadpart] && $i == $formfields[loadpart]) ||
	(!$formfields[loadpart] && $i <= $formfields[loadlength])) {
593 594

	if (!isset($thisosid) ||
595 596 597
	    strcmp($thisosid, "") == 0 ||
	    strcmp($thisosid, "X") == 0) {
	    $errors["Partition $i OS"] = "Must select an OS";
598
	}
599 600 601 602 603 604 605 606 607 608 609 610 611 612
	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;
613 614 615 616 617 618 619
	}
    }
    else {
	$$foo = "NULL";
    }
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
#
# 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";
}

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
#
# 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";
}

660 661 662 663 664 665 666 667
#
# 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";
668
}
669 670 671
elseif (! ereg("^[-_a-zA-Z0-9\/\.+]+$", $formfields[path])) {
    $errors["Path"] = "Contains invalid characters";
}
672 673 674 675 676 677 678 679 680 681 682
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/";
683
    }
684 685

    if (strpos($formfields[path], $pdef) === false) {
686 687
	$errors["Path"] = "Invalid Path";
    }
688 689 690
}

#
691 692
# See what node types this image will work on. Must be at least one!
# Store the valid types in a new array for simplicity.
693
#
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
$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";
710 711 712
}

#
713
# Check sanity of node name and that user can create an image from it.
714
#
715 716 717 718 719 720 721 722 723 724 725 726
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];
727 728 729
}

#
730 731
# Only admins have this option. Always on for mereusers, but default off
# for admins. 
732
#
733 734 735 736 737 738
$makedefault = 0;

if (! $isadmin ||
    (isset($formfields[makedefault]) &&
     strcmp($formfields[makedefault], "Yep") == 0)) {
    $makedefault = 1;
739
}
740 741

#
742 743 744 745 746 747 748
# 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;
749 750
}

751 752 753 754 755
#
# For the rest, sanitize and convert to locals to make life easier.
# 
$description = addslashes($formfields[description]);
$pid         = $formfields[pid];
756
$gid         = $formfields[gid];
757 758 759 760 761
$imagename   = $formfields[imagename];
$loadpart    = $formfields[loadpart];
$loadlength  = $formfields[loadlength];
$default_osid= $formfields[default_osid];
$path        = $formfields[path];
762 763 764
if (!isset($gid) || !strcmp($gid, "")) {
    $gid = $pid;
}
765

766 767 768
#
# And insert the record!
#
769 770 771 772 773 774 775 776 777 778 779 780 781

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;
782
}
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

#
# 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]'";
804
}
805 806 807 808 809

$osidclause = "osid=" . "'$osid_array[0]'";
    
for ($i = 1; $i < count($osid_array); $i++) {
    $osidclause = "$osidclause or osid=" . "'$osid_array[$i]'";
810
}
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
    
$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);
    }
860 861 862 863
}

$query_result =
    DBQueryFatal("INSERT INTO images ".
864
		 "(imagename, imageid, description, loadpart, loadlength, ".
865
		 " part1_osid, part2_osid, part3_osid, part4_osid, ".
866 867
		 " default_osid, path, pid, gid, shared, global, creator, ".
		 " created) ".
868
		 "VALUES ".
869 870
		 "  ('$imagename', '$imageid', '$description', $loadpart, ".
		 "   $loadlength, ".
871
		 "   $part1_osid, $part2_osid, $part3_osid, $part4_osid, ".
872 873
		 "   '$default_osid', '$path', '$pid', '$gid', $shared, ".
	         "   $global, '$uid', now())");
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897

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",
898
		   "newimageid_ez.php3");
899 900
WRITESUBMENUBUTTON("Create a new OS Descriptor",
		   "newosid_form.php3");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
901
WRITESUBMENUBUTTON("Image Descriptor list",
902
		   "showimageid_list.php3");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
903
WRITESUBMENUBUTTON("OS Descriptor list",
904 905
		   "showosid_list.php3");
SUBMENUEND();
906 907 908 909 910

#
# Dump os_info record.
# 
SHOWIMAGEID($imageid, 0);
911 912 913 914 915 916 917 918 919 920 921 922 923
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.
    #
924
    TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);
925

926 927 928 929
    echo "<br>
          Creating image using node '$node' ...
          <br><br>\n";
    flush();
930

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

933 934 935
    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
936
          $node is in. In fact, it is best if you do not mess with 
937 938
          the node at all!<br>\n";
}
939 940 941 942 943 944

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