newimageid.php3 27.5 KB
Newer Older
1
<?php
Leigh Stoller's avatar
Leigh Stoller committed
2
#
3
# Copyright (c) 2000-2015 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
23
#
24
include("defs.php3");
25 26
include_once("imageid_defs.php");
include_once("osinfo_defs.php");
27
include_once("node_defs.php");
28
include_once("osiddefs.php3");
29 30

#
31
# Only known and logged in users.
32
#
33 34
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
35
$dbid      = $this_user->dbid();
36
$isadmin   = ISADMIN();
37

38 39 40
#
# Verify page arguments.
#
41 42
$optargs = OptionalPageArguments("submit",     PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY);
43

44
#
45
# See what projects the uid can do this in.
46
#
47
$projlist = $this_user->ProjectAccessList($TB_PROJECT_MAKEIMAGEID);
48 49 50 51

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);
52
}
53

54 55 56 57 58
#
# Standard Testbed Header
#
PAGEHEADER("Create a new Image Descriptor (long form)");

59
#
60 61 62
# 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.
63 64
#
$types_result =
65
    DBQueryFatal("select distinct n.type from nodes as n ".
66 67
		 "left join node_type_attributes as a on a.type=n.type ".
		 "where a.attrkey='imageable' and ".
68
		 "      a.attrvalue!='0' and n.role='testnode'");
69 70

#
71 72
# Spit the form out using the array of data.
#
73 74
function SPITFORM($formfields, $errors)
{
75 76 77
    global $this_user, $projlist, $isadmin, $types_result;
    global $TBDB_IMAGEID_IMAGENAMELEN, $TBDB_NODEIDLEN;
    global $TBPROJ_DIR, $TBGROUP_DIR;
78

79
    if ($errors) {
80 81
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
82
              <tr>
83
                 <th align=center colspan=2>
84
                   <font size=+1 color=red>
85
                      &nbsp;Oops, please fix the following errors!&nbsp;
86 87 88 89 90
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
91 92
            # XSS prevention.
	    $message = CleanString($message);
93
	    echo "<tr>
94 95 96 97
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
98 99 100 101
                  </tr>\n";
	}
	echo "</table><br>\n";
    }
102 103 104 105
    # XSS prevention.
    while (list ($key, $val) = each ($formfields)) {
	$formfields[$key] = CleanString($val);
    }
106 107 108 109 110 111 112

    #
    # 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 =
113
	    DBQueryFatal("select v.* from os_info as o ".
114
			 "left join os_info_versions as v on ".
115
			 "     v.osid=o.osid and v.vers=o.version ".
116
			 "where (v.path='' or v.path is NULL) and ".
117 118
			 "      v.version!='' and v.version is not NULL ".
			 "order by o.pid,o.osname");
119 120
    }
    else {
121 122
	$uid_idx = $this_user->uid_idx();
	
123 124
	$osid_result =
	    DBQueryFatal("select distinct o.* from os_info as o ".
125
			 "left join os_info_versions as v on ".
126
			 "     v.osid=o.osid and v.vers=o.version ".
127
			 "left join group_membership as m ".
128
			 "     on m.pid=o.pid ".
129
			 "where m.uid_idx='$uid_idx' and ".
130 131
			 "      (v.path='' or v.path is NULL) and ".
			 "      v.version!='' and v.version is not NULL ".
132
			 "order by o.pid,o.osname");
133 134 135 136 137 138 139 140
    }
    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) 
              {
141 142 143 144 145 146 147 148 149 150 151 152
                  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 == '') {
153
                      theform['formfields[path]'].value = '$TBPROJ_DIR';
154 155 156
                  }
                  else if (theform['formfields[imagename]'].value == '') {
		      theform['formfields[imagename]'].defaultValue = '';
157 158 159 160 161 162 163

                      if (global) {
    	                  theform['formfields[path]'].value =
                                  '/usr/testbed/images/';
                      }
		      else if (gid == '' || gid == pid || shared) {
    	                  theform['formfields[path]'].value =
164
                                  '$TBPROJ_DIR/' + pid + '/images/';
165 166 167
                      }
                      else {
    	                  theform['formfields[path]'].value =
168
                                  '$TBGROUP_DIR/' + pid + '/' + gid + '/images/';
169
                      }
170 171
                  }
                  else if (theform['formfields[imagename]'].value != '') {
172 173 174 175 176 177 178 179 180
                      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 =
181
                                  '$TBPROJ_DIR/' + pid + '/images/' + filename;
182 183 184
                      }
                      else {
    	                  theform['formfields[path]'].value =
185
                                  '$TBGROUP_DIR/' + pid + '/' + gid + '/images/' +
186 187
                                  filename;
                      }
188 189 190 191
                  }
              }
          </SCRIPT>\n";

192
    $url = CreateURL("newimageid");
193
    echo "<br>
194
          <table align=center border=1>
195 196 197 198 199
          <tr>
             <td align=center colspan=2>
                 <em>(Fields marked with * are required)</em>
             </td>
          </tr>
200
          <form action='$url' method=post name=idform>\n";
201 202 203 204 205 206 207 208 209 210 211 212 213

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

214
	if ($formfields["pid"] == $project)
215 216 217 218 219 220 221 222
	    $selected = "selected";
	
	echo "        <option $selected value='$project'>$project </option>\n";
    }
    echo "       </select>";
    echo "    </td>
          </tr>\n";

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    #
    # 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 = "";

240 241 242 243
		if (isset($formfields["gid"]) &&
		    isset($formfields["pid"]) &&
		    strcmp($formfields["pid"], $project) == 0 &&
		    strcmp($formfields["gid"], $group) == 0)
244 245 246 247 248 249 250 251 252 253 254
		    $selected = "selected";
		
		echo "<option $selected value=\"$group\">
                           $project/$group</option>\n";
	    }
	}
    }
    echo "     </select>
             </td>
          </tr>\n";

255
    #
256
    # Image Name
257 258 259 260 261 262 263
    #
    echo "<tr>
              <td>*Descriptor Name (no blanks):</td>
              <td class=left>
                  <input type=text
                         onChange='SetPrefix(idform);'
                         name=\"formfields[imagename]\"
264
                         value=\"" . $formfields["imagename"] . "\"
265 266 267 268 269 270 271 272 273 274 275 276 277 278
	                 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]\"
279
                         value=\"" . $formfields["description"] . "\"
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	                 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 = "";

296
	if (strcmp($formfields["loadpart"], "$i") == 0)
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	    $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 = "";

317
	if (strcmp($formfields["loadlength"], "$i") == 0)
318 319 320 321 322 323 324 325
	    $selected = "selected";
	
	echo "        <option $selected value=$i>$i </option>\n";
    }
    echo "       </select>";
    echo "    </td>
          </tr>\n";

326
    WRITEOSIDMENU("Partition 1 OS[<b>2</b>]", "formfields[part1_osid]",
327
		  $osid_result, $formfields["part1_osid"]);
328
    WRITEOSIDMENU("Partition 2 OS", "formfields[part2_osid]",
329
		  $osid_result, $formfields["part2_osid"]);
330
    WRITEOSIDMENU("Partition 3 OS", "formfields[part3_osid]",
331
		  $osid_result, $formfields["part3_osid"]);
332
    WRITEOSIDMENU("Partition 4 OS", "formfields[part4_osid]",
333
		  $osid_result, $formfields["part4_osid"]);
334
    WRITEOSIDMENU("Boot OS[<b>3</b>]", "formfields[default_osid]",
335
		  $osid_result, $formfields["default_osid"]);
336 337 338 339 340 341

    #
    # Path to image.
    #
    echo "<tr>
              <td>Filename (full path) of Image[<b>4</b>]:<br>
342
                  (must reside in $TBPROJ_DIR)</td>
343 344 345
              <td class=left>
                  <input type=text
                         name=\"formfields[path]\"
346
                         value=\"" . $formfields["path"] . "\"
347 348 349 350 351 352 353 354 355 356 357 358 359
	                 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)) {
360
        $type    = $row["type"];
361 362
        $checked = "";

363 364
        if (isset($formfields["mtype_$type"]) &&
	    $formfields["mtype_$type"] == "Yep") {
365
	    $checked = "checked";
366
	}
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    
        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
383 384
                         name=\"formfields[node_id]\"
                         value=\"" . $formfields["node_id"] . "\"
385 386 387 388
	                 size=$TBDB_NODEIDLEN maxlength=$TBDB_NODEIDLEN>
              </td>
          </tr>\n";

389 390 391 392 393 394 395 396 397 398 399 400
    #
    # 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";

401 402
    if (isset($formfields["shared"]) &&
	strcmp($formfields["shared"], "Yep") == 0)
403 404 405 406 407 408
	echo "           checked";
	
    echo "                       > Yes
              </td>
          </tr>\n";

409 410
    if ($isadmin) {
        #
411
        # Global?
412 413
        #
	echo "<tr>
414
  	          <td>Global?:<br>
415 416 417
                      (available to all projects)</td>
                  <td class=left>
                      <input type=checkbox
418 419
                             onClick='SetPrefix(idform);'
                             name=\"formfields[global]\"
420 421
                             value=Yep";

422 423
	if (isset($formfields["global"]) &&
	    strcmp($formfields["global"], "Yep") == 0)
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
	    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";

441 442
	if (isset($formfields["makedefault"]) &&
	    strcmp($formfields["makedefault"], "Yep") == 0)
443 444 445 446 447
	    echo "           checked";
	
	echo "                       > Yes
                  </td>
              </tr>\n";
448 449 450 451 452 453 454 455 456 457

	echo "<tr>
	          <td>MBR Version:<br>
		  <td class=left>
		      <input type=text
			     name=\"formfields[mbr_version]\"
			     value=\"" . $formfields["mbr_version"] . "\"
			     size=2 maxlength=2>
		  </td>
	      </tr>\n";
458 459 460 461
    }

    echo "<tr>
              <td align=center colspan=2>
462
                 <b><input type=submit name=submit value=Submit></b>
463 464 465 466 467 468 469 470 471
              </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.
Russ Fish's avatar
Russ Fish committed
472
  	         If you specify a non-zero starting load partition, the
473 474 475 476 477 478
	         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
479
                  <a href='newosid.php3'>create the OS Descriptor
480 481 482 483 484
                  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).
485 486 487 488 489
                  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!
490 491 492 493 494
             <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";
495
}
496 497 498 499
    
#
# On first load, display a virgin form and exit.
#
500
if (!isset($submit)) {
501
    $defaults = array();
502 503 504 505 506 507 508 509 510 511 512
    $defaults["pid"]         = "";
    $defaults["gid"]         = "";
    $defaults["imagename"]   = "";
    $defaults["description"] = "";
    $defaults["loadpart"]    = "X";
    $defaults["loadlength"]  = "X";
    $defaults["part1_osid"]  = "";
    $defaults["part2_osid"]  = "";
    $defaults["part3_osid"]  = "";
    $defaults["part4_osid"]  = "";
    $defaults["default_osid"]= "";
513
    $defaults["path"]        = "$TBPROJ_DIR/";
514 515 516 517
    $defaults["node_id"]     = "";
    $defaults["shared"]      = "No";
    $defaults["global"]      = "No";
    $defaults["makedefault"] = "No";
518
    $defaults["mbr_version"] = "";
519 520 521 522

    #
    # 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
523
    # to be in the naive portion of our users, give them some help.
524 525 526
    # 
    if (count($projlist) == 1) {
	list($project, $grouplist) = each($projlist);
527

528
	if (count($grouplist) <= 2) {
529
	    $defaults["pid"] = $project;
530 531 532 533 534
	    if (count($grouplist) == 1 || strcmp($project, $grouplist[0]))
		$group = $grouplist[0];
	    else {
		$group = $grouplist[1];
	    }
535
	    $defaults["gid"] = $group;
536 537
	    
	    if (!strcmp($project, $group))
538
		$defaults["path"]     = "$TBPROJ_DIR/$project/images/";
539
	    else
540
		$defaults["path"]     = "$TBGROUP_DIR/$project/$group/images/";
541 542 543
	}
	reset($projlist);
    }
544

545 546 547
    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
548
}
549 550 551 552

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

555
# Be friendly about the required form field names.
556 557
if (!isset($formfields["pid"]) ||
    strcmp($formfields["pid"], "") == 0) {
558
    $errors["Project"] = "Missing Field";
559 560 561 562
}

if (!isset($formfields["imagename"]) ||
    strcmp($formfields["imagename"], "") == 0) {
563
    $errors["Descriptor Name"] = "Missing Field";
564 565
}

566 567
if (!isset($formfields["loadpart"]) ||
    strcmp($formfields["loadpart"], "X") == 0) {
568
    $errors["Starting DOS Partion"] = "Missing Field";
569
}
570

571 572
if (!isset($formfields["loadlength"]) ||
    strcmp($formfields["loadlength"], "X") == 0) {
573
    $errors["Number of DOS Partitions"] = "Missing Field";
574
}
575 576 577 578

if (!isset($formfields["default_osid"]) ||
    strcmp($formfields["default_osid"], "none") == 0) {
    $errors["Boot OS"] = "Missing Field";
579 580
}

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
$project = null;
$group   = null;

#
# Project:
#
if (!TBvalid_pid($formfields["pid"])) {
    $errors["Project"] = "Invalid project name";
}
elseif (! ($project = Project::Lookup($formfields["pid"]))) {
    $errors["Project"] = "Invalid project name";
}

if (isset($formfields["gid"]) && $formfields["gid"] != "") {
    if ($formfields["pid"] == $formfields["gid"] && $project) {
	$group = $project->DefaultGroup();
    }
    elseif (!TBvalid_gid($formfields["gid"])) {
	$errors["Group"] = "Invalid group name";
    }
    elseif ($project &&
	    ! ($group = $project->LookupSubgroupByName($formfields["gid"]))) {
	$errors["Group"] = "Invalid group name";
    }
}
elseif ($project) {
    $group = $project->DefaultGroup();
}

# Permission check if we managed to get a proper group above.
if ($group &&
    ! $group->AccessCheck($this_user, $TB_PROJECT_MAKEIMAGEID)) {
    $errors["Project"] = "Not enough permission";
}

616
#
617
# Build up argument array to pass along.
618
#
619
$args = array();
620

621 622 623
if (isset($formfields["pid"]) && $formfields["pid"] != "") {
    $args["pid"] = $pid = $formfields["pid"];
}
624

625 626 627
if (isset($formfields["gid"]) && $formfields["gid"] != "") {
    $args["gid"] = $gid = $formfields["gid"];
}
628

629 630
if (isset($formfields["imagename"]) && $formfields["imagename"] != "") {
    $args["imagename"] = $formfields["imagename"];
631 632
}

633 634
if (isset($formfields["description"]) && $formfields["description"] != "") {
    $args["description"] = $formfields["description"];
635
}
636 637

if (isset($formfields["loadpart"]) &&
Russ Fish's avatar
Russ Fish committed
638
    $formfields["loadpart"] != "X" && $formfields["loadpart"] != "") {
639
    $args["loadpart"] = $formfields["loadpart"];
640
}
641 642 643 644

if (isset($formfields["loadlength"]) &&
    $formfields["loadlength"] != "none" && $formfields["loadlength"] != "") {
    $args["loadlength"] = $formfields["loadlength"];
645
}
646 647 648 649

if (isset($formfields["part1_osid"]) &&
    $formfields["part1_osid"] != "none" && $formfields["part1_osid"] != "") {
    $args["part1_osid"] = $formfields["part1_osid"];
650 651
}

652 653 654
if (isset($formfields["part2_osid"]) &&
    $formfields["part2_osid"] != "none" && $formfields["part2_osid"] != "") {
    $args["part2_osid"] = $formfields["part2_osid"];
655 656
}

657 658 659
if (isset($formfields["part3_osid"]) &&
    $formfields["part3_osid"] != "none" && $formfields["part3_osid"] != "") {
    $args["part3_osid"] = $formfields["part3_osid"];
660
}
661 662 663 664

if (isset($formfields["part4_osid"]) &&
    $formfields["part4_osid"] != "none" && $formfields["part4_osid"] != "") {
    $args["part4_osid"] = $formfields["part4_osid"];
665 666
}

667 668 669
if (isset($formfields["default_osid"]) &&
    $formfields["default_osid"] != "none" && $formfields["default_osid"] != "") {
    $args["default_osid"] = $formfields["default_osid"];
670
}
671 672 673

if (isset($formfields["path"]) && $formfields["path"] != "") {
    $args["path"] = $formfields["path"];
674
}
675

676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
if (isset($formfields["node_id"]) && $formfields["node_id"] != "") {
    $args["node_id"] = $node_id = $formfields["node_id"];
}

# Filter booleans from checkboxes to 0 or 1.
if (isset($formfields["shared"])) {
   $args["shared"] = strcmp($formfields["shared"], "Yep") ? 0 : 1;
}
if (isset($formfields["global"])) {
   $args["global"] = strcmp($formfields["global"], "Yep") ? 0 : 1;
}
$makedefault = 0;
if (isset($formfields["makedefault"])) {
   $args["makedefault"] = $makedefault = 
       strcmp($formfields["makedefault"], "Yep") ? 0 : 1;
691 692
}

693 694 695 696 697
if (isset($formfields["mbr_version"]) &&
    $formfields["mbr_version"] != "") {
    $args["mbr_version"] = $formfields["mbr_version"];
}

698
#
699 700
# See what node types this image will work on. Must be at least one!
# Store the valid types in a new array for simplicity.
701
#
702
$mtypes_array = array();
703
mysql_data_seek($types_result, 0);
704
while ($row = mysql_fetch_array($types_result)) {
705
    $type = $row["type"];
706 707 708 709

    #
    # Look for a post variable with name.
    # 
710 711
    if (isset($formfields["mtype_$type"]) &&
	$formfields["mtype_$type"] == "Yep") {
712 713 714 715 716
	$mtypes_array[] = $type;
    }
}
if (! count($mtypes_array)) {
    $errors["Node Types"] = "Must select at least one type";
717 718
}

719 720
# The mtype_* checkboxes are dynamically generated.
foreach ($mtypes_array as $type) {
721

722 723 724 725
    # Filter booleans from checkbox values.
    $checked = isset($formfields["mtype_$type"]) &&
	strcmp($formfields["mtype_$type"], "Yep") == 0;
    $args["mtype_$type"] = $checked ? "1" : "0";
726
}
727 728

#
729 730 731 732 733 734 735
# 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;
736 737
}

738
#
739 740 741 742 743
# 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.
744

745 746
# Can't check this unless we have at least one mtype!
if (!count($mtypes_array)) {
747 748 749
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
750
}
751
    
752 753 754
$typeclause = "type=" . "'$mtypes_array[0]'";
for ($i = 1; $i < count($mtypes_array); $i++) {
    $typeclause = "$typeclause or type=" . "'$mtypes_array[$i]'";
755
}
756

757 758 759 760 761 762 763 764 765 766 767 768 769
unset($osidclause);
for ($i = 1; $i <= 4; $i++) {
    # Local variable dynamically created.    
    $foo      = "part${i}_osid";

    if (isset($formfields[$foo])) {
	if (isset($osidclause))
	    $osidclause = "$osidclause or osid='" . $formfields[$foo] . "' ";
	else 
	    $osidclause = "osid='" . $formfields[$foo] . "' ";

	$osid_array[] = $formfields[$foo];
    }
770
}
771
    
772
DBQueryFatal("lock tables images write, os_info write, osidtoimageid write");
773 774 775 776 777 778
$query_result =
    DBQueryFatal("select osidtoimageid.*,images.pid,images.imagename ".
		 " from osidtoimageid ".
		 "left join images on ".
		 " images.imageid=osidtoimageid.imageid ".
		 "where ($osidclause) and ($typeclause)");
779
DBQueryFatal("unlock tables");
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797

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>
798 799 800
                  <td align=center>OS ID/name</td>
                  <td align=center>Node Type</td>
                  <td align=center>Image PID/ID/name</td>
801 802 803
             </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
804
	    $osid      = $row["osid"];
805 806
            $osinfo    = OSinfo::Lookup($osid);
            $osname    = $osinfo->osname();
807
	    $type      = $row["type"];
808 809 810
	    $imageid   = $row['imageid'];
	    $url       = CreateURL("showimageid", URLARG_IMAGEID, $imageid);
	    $pid       = $row['pid'];
811
	    $imagename = $row["imagename"];
812 813
	    
	    echo "<tr>
814
                      <td>$osid/$osname</td>
815
	              <td>$type</td>
816
                      <td>$pid/$imageid/<A href='$url'>$imagename</A></td>
817 818 819 820 821 822 823
	          </tr>\n";
	}
	echo "</table><br><br>\n";
    
	USERERROR("Please check the other Image descriptors and make the ".
		  "necessary changes!", 1);
    }
824 825
}

826 827
# Send to the backend for more checking, and eventually, to update the DB.
$imagename = $args["imagename"];
828 829
if (! ($image = Image::NewImageId(0, $imagename, $args, $this_user, $group,
				  $node_id, $errors))) {
830 831 832 833 834
    # Always respit the form so that the form fields are not lost.
    # I just hate it when that happens so lets not be guilty of it ourselves.
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
835 836 837 838
}

SUBPAGESTART();
SUBMENUSTART("More Options");
839 840
if (! isset($node_id)) {
    $imageid = $image->imageid();
841 842
    $fooid = rawurlencode($imageid);
    WRITESUBMENUBUTTON("Edit this Image Descriptor",
843
		       "editimageid.php3?imageid=$fooid");
844 845 846 847
    WRITESUBMENUBUTTON("Delete this Image Descriptor",
		       "deleteimageid.php3?imageid=$fooid");
}
WRITESUBMENUBUTTON("Create a new Image Descriptor",
848
		   "newimageid_ez.php3");
849
WRITESUBMENUBUTTON("Create a new OS Descriptor",
850
		   "newosid.php3");
Leigh Stoller's avatar
Leigh Stoller committed
851
WRITESUBMENUBUTTON("Image Descriptor list",
852
		   "showimageid_list.php3");
853 854 855 856
if ($isadmin) {
    WRITESUBMENUBUTTON("OS Descriptor list",
		       "showosid_list.php3");
}
857
SUBMENUEND();
858 859 860

#
# Dump os_info record.
861 862
#
$image->Show();
863 864
SUBPAGEEND();

865
if (isset($node_id)) {
866 867 868 869 870 871 872
    #
    # 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. 
873 874 875 876 877
    #

    $node = Node::Lookup($node_id); # Already been checked.
    $node_id = $node->node_id();    # XXX Why?

878 879 880
    #
    # Grab the unix GID for running script.
    #
881
    $unix_gid  = $group->unix_gid();
882
    $unix_pid  = $project->unix_gid();
883
    $safe_name = escapeshellarg($imagename);
884

885
    echo "<br>
886
          Creating image using node '$node_id'.
887 888
          <br><br>\n";
    flush();
889

890
    SUEXEC($uid, "$unix_pid,$unix_gid",
891
	   "webcreate_image -p $pid $safe_name $node_id",
892
	   SUEXEC_ACTION_DUPDIE);
893

894 895 896
    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
897 898
          $node_id is in. In fact, it is best if you do not mess with 
          the node or the experiment at all until you receive email.<br>\n";
899
}
900 901 902 903 904 905

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