newosid.php3 9.78 KB
Newer Older
1
<?php
Leigh Stoller's avatar
Leigh Stoller committed
2
#
3
# Copyright (c) 2000-2014 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
include_once("osinfo_defs.php");
26
include_once("osiddefs.php3");
27
include("form_defs.php");
28 29

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

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

43 44 45 46
#
# See what projects the uid can do this in.
#
$projlist = $this_user->ProjectAccessList($TB_PROJECT_MAKEOSID);
47

48 49 50
if (! count($projlist)) {
    USERERROR("You do not appear to be a member of any Projects in which ".
	      "you have permission to create new OS Descriptors!", 1);
51
}
52 53 54 55
$projselection = array();
while (list($project) = each($projlist)) {
    $projselection[$project] = $project;
}
56

57 58 59 60 61
#
# Standard Testbed Header
#
PAGEHEADER("Create a new OS Descriptor");

62
#
63 64 65 66 67
# Define the form.
#
$form = array('#id'	  => 'myform',
	      '#action'   => "newosid.php3");
$fields = array();
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#
# Project Name:
#
$fields['pid'] = array('#type'	     => 'select',
		       '#label'      => 'Project',
		       '#options'    => $projselection,
		       '#required'   => TRUE);
#
# OS Name
#
$fields['osname'] = array('#type'	=> 'textfield',
			  '#label'      => 'Descriptor Name',
			  '#description'=> 'alphanumeric, no spaces',
			  '#size'	=> $TBDB_OSID_OSNAMELEN,
			  '#maxlength'  => $TBDB_OSID_OSNAMELEN,
			  '#checkslot'  => 'os_info:osname',
			  '#required'   => TRUE);
86

87 88 89 90 91 92 93 94 95 96
#
# Description
#
$fields['description'] =
    array('#type'	=> 'textfield',
	  '#label'      => 'Description',
	  '#description'=> 'a short pithy sentence',
	  '#size'	=> 50,
	  '#checkslot'  => 'os_info:description',
	  '#required'   => TRUE);
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111
#
# Select an OS
#
$OSselection = array();
while (list ($os, $userokay) = each($osid_oslist)) {
    if (!$userokay && !$isadmin)
	continue;
    $OSselection[$os] = $os;
}
$fields['OS'] = array('#type'	    => 'select',
		      '#label'      => 'Select OS',
		      '#options'    => $OSselection,
		      '#checkslot'  => 'os_info:OS',
		      '#required'   => TRUE);
112

113 114 115 116 117 118 119 120 121
#
# Version String
#
$fields['version'] =
    array('#type'	=> 'textfield',
	  '#label'      => 'Version',
	  '#size'	=> $TBDB_OSID_VERSLEN,
	  '#maxlength'	=> $TBDB_OSID_VERSLEN,
	  '#checkslot'  => 'os_info:version',
122
	  '#required'   => FALSE);
123

124 125 126 127 128 129 130 131
#
# Path to Multiboot image.
#
$fields['path'] =
    array('#type'	=> 'textfield',
	  '#label'      => 'Path',
	  '#checkslot'  => 'os_info:path',
	  '#size'	=> 40);
132

133 134 135 136 137 138 139 140 141
#
# Magic string
#
$fields['magic'] =
    array('#type'	=> 'textfield',
	  '#label'      => 'Magic',
	  '#description'=> 'ie: uname -r -s',
	  '#checkslot'  => 'os_info:magic',
	  '#size'	=> 30);
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156
#
# OS Features
#
$FeatureBoxes = array();
while (list ($feature, $userokay) = each($osid_featurelist)) {
    if (!$userokay && !$isadmin)
	continue;
    $FeatureBoxes["os_feature_$feature"] = array('#return_value'=> "checked",
						 '#label'       => $feature);
}
$fields['features'] =
    array('#type'	=> 'checkboxes',
	  '#label'      => 'OS Features',
	  '#boxes'      => $FeatureBoxes);
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#
# Op Mode
#
$OpmodeSelection = array();
while (list ($mode, $userokay) = each($osid_opmodes)) {
    if (!$userokay && !$isadmin)
	continue;
    $OpmodeSelection[$mode] = $mode;
}
$fields['op_mode'] =
    array('#type'	=> 'select',
	  '#label'      => 'Operational Mode',
	  '#options'    => $OpmodeSelection,
	  '#checkslot'  => 'os_info:op_mode',
	  '#required'   => TRUE);

if ($isadmin) {
175
    #
176
    # Shared?
177
    #
178 179 180 181 182 183
    $fields['shared'] =
	array('#type'        => 'checkbox',
	      '#return_value'=> 1,
	      '#label'       => 'Global?',
	      '#checkslot'   => 'os_info:shared',
	      '#description' => 'available to all projects');
184 185 186 187 188 189 190 191 192 193 194

    #
    # MFS?
    #
    $fields['mfs'] =
	array('#type'        => 'checkbox',
	      '#return_value'=> 1,
	      '#label'       => 'MFS?',
	      '#checkslot'   => 'os_info:mfs',
	      '#description' => 'this OS is an MFS');

195
    #
196
    # Mustclean?
197
    #
198 199 200 201 202 203 204
    $fields['mustclean'] =
	array('#type'        => 'checkbox',
	      '#return_value'=> 1,
	      '#label'       => 'Clean?',
	      '#checkslot'   => 'os_info:mustclean',
	      '#description' => 'no disk load required');
    
205
    #
206
    # Reboot Waittime. 
207
    #
208 209 210 211 212 213
    $fields['reboot_waittime'] =
	array('#type'	     => 'textfield',
	      '#label'       => 'Reboot Waittime',
	      '#description' => 'seconds',
	      '#checkslot'   => 'os_info:reboot_waittime',
	      '#size'	     => 6);
214

215
    # NextOsid
216
    #
217
    $osid_result =
218 219 220 221 222 223
	DBQueryFatal("select v.* from os_info as o ".
		     "left join os_info_versions as v on ".
		     "     v.osid=o.osid and v.vers=o.version ".
		     "where (v.path='' or v.path is NULL) and ".
		     "      v.version!='' and v.version is not NULL ".
		     "order by o.pid,o.osname");
224 225 226 227 228 229 230

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

Leigh Stoller's avatar
Leigh Stoller committed
231
	$NextOsidSelection["$osid"] = "$pid - $osname";
232
    }
233 234 235 236 237 238
    $fields['nextosid'] =
	array('#type'	    => 'select',
	      '#label'      => 'NextOSid',
	      '#checkslot'  => 'os_info:nextosid',
	      '#options'    => $NextOsidSelection);
}
239

240 241 242 243 244 245 246 247 248
#
# Spit the form out using the array of data. 
# 
function SPITFORM($formfields, $errors)
{
    global $form, $fields;
    
    $fields['submit'] = array('#type'  => 'submit',
			      '#value' => "Submit");
249

250 251 252
    echo "<center>";
    FormRender($form, $errors, $fields, $formfields);
    echo "</center>";
253 254
}

255
#
256
# On first load, display a virgin form and exit.
257
#
258 259 260 261 262 263 264 265 266
if (!isset($submit)) {
    $defaults = array();
    $defaults["pid"]            = "";
    $defaults["osname"]         = "";
    $defaults["description"]    = "";
    $defaults["OS"]             = "";
    $defaults["version"]        = "";
    $defaults["path"]           = "";
    $defaults["magic"]          = "";
267
    $defaults["shared"]         = 0;
268
    $defaults["mfs"]            = 0;
269
    $defaults["mustclean"]      = 0;
270 271 272 273 274 275 276 277
    $defaults["path"]           = "";
    $defaults["op_mode"]             = TBDB_DEFAULT_OSID_OPMODE;
    $defaults["os_feature_ping"]     = "checked";
    $defaults["os_feature_ssh"]      = "checked";
    $defaults["os_feature_ipod"]     = "checked";
    $defaults["os_feature_isup"]     = "checked";
    $defaults["os_feature_linktest"] = "checked";
    $defaults["reboot_waittime"]     = "";
278
    $defaults["nextosid"]            = "";
279 280 281 282 283 284 285 286 287 288 289 290 291

    #
    # 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;
	}
	reset($projlist);
292
    }
293 294 295 296
 
    SPITFORM($defaults, null);
    PAGEFOOTER();
    return;
297
}
298 299 300 301
# Form submitted. Make sure we have a formfields array.
if (!isset($formfields)) {
    PAGEARGERROR("Invalid form arguments.");
}
302

303
#
304
# Otherwise, must validate and redisplay if errors
305
#
306 307 308 309 310 311
$errors  = array();
$project = null;

#
# Project:
#
Russ Fish's avatar
Russ Fish committed
312 313
if (!isset($formfields["pid"]) ||
    strcmp($formfields["pid"], "") == 0) {
314 315 316 317 318
    $errors["Project"] = "Not Selected";
}
elseif (!TBvalid_pid($formfields["pid"])) {
    $errors["Project"] = "Invalid project name";
}
Russ Fish's avatar
Russ Fish committed
319
elseif (!($project = Project::Lookup($formfields["pid"]))) {
320 321 322
    $errors["Project"] = "Invalid project name";
}
elseif (!$project->AccessCheck($this_user, $TB_PROJECT_MAKEOSID)) {
Russ Fish's avatar
Russ Fish committed
323
    $errors["Project"] = "Not enough permission";
324
}
325 326

FormValidate($form, $errors, $fields, $formfields);
327

328
#
329 330
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
331
# 
332 333 334 335 336
if (count($errors)) {
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
}
337 338

#
339 340
# Build up argument array to pass along. We pass only those form fields that
# have actual values and let the backend decide the rest.
341
#
342
$args = array();
343

344 345 346 347 348 349 350 351 352 353
while (list ($name, $attributes) = each ($fields)) {
    # features special. see below
    if ($name == "features")
	continue;
    # pid and osname are handled in NewOSID
    if ($name == "pid" || $name == "osname")
	continue;
    if (isset($formfields[$name]) && $formfields[$name] != "") {
	$args[$name] = $formfields[$name];
    }
354
}
355

356
#
357
# Form comma separated list of osfeatures.
358
#
359
$os_features_array = array();
360

361
reset($osid_featurelist);
362 363 364 365 366
while (list ($feature, $userokay) = each($osid_featurelist)) {
    if (isset($formfields["os_feature_$feature"]) &&
	$formfields["os_feature_$feature"] == "checked") {
	$os_features_array[] = $feature;
    }
367
}
368
$args["features"] = join(",", $os_features_array);
369

370
if (! ($osinfo = OSinfo::NewOSID($this_user, $project,
371
				 $formfields["osname"], $args, $errors))) {
372 373 374 375 376
    # 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;
377
}
378

379 380
echo "<center><h3>Done!</h3></center>\n";
PAGEREPLACE(CreateURL("showosinfo", $osinfo));
381 382 383 384 385 386

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