profile_defs.php 15.5 KB
Newer Older
1
2
<?php
#
3
# Copyright (c) 2006-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
23
24
# 
# {{{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/>.
# 
# }}}
#
#
25
26
27
28
29
30

function TBvalid_rspec($token) {
    return TBcheck_dbslot($token, "apt_profiles", "rspec",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}

31
32
33
34
35
36
37
38
class Profile
{
    var	$profile;
    var $project;

    #
    # Constructor by lookup on unique index.
    #
39
40
    function Profile($token, $version = null) {
	$safe_profileid = addslashes($token);
Leigh B Stoller's avatar
Leigh B Stoller committed
41
	
42
	if (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $token)) {
43
44
45
46
47
	    #
	    # First look to see if the uuid is for the profile itself,
	    # which means current version. Otherwise look for a
	    # version with the uuid.
	    #
48
	    $query_result =
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.uuid='$token' and v.deleted is null");

	    if (!$query_result || !mysql_num_rows($query_result)) {
		$query_result =
		    DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
				"  from apt_profile_versions as v ".
				"left join apt_profiles as i on ".
				"     v.profileid=i.profileid ".
				"where v.uuid='$token' and ".
				"      v.deleted is null");
	    }
Leigh B Stoller's avatar
Leigh B Stoller committed
65
	}
66
	elseif (is_null($version)) {
Leigh B Stoller's avatar
Leigh B Stoller committed
67
	    $query_result =
68
69
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profiles as i ".
70
71
72
73
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.profileid='$safe_profileid'");
Leigh B Stoller's avatar
Leigh B Stoller committed
74
	}
75
76
77
	else {
	    $safe_version = addslashes($version);
	    $query_result =
78
79
	        DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profile_versions as v ".
80
81
82
83
84
			    "left join apt_profiles as i on ".
			    "     i.profileid=v.profileid ".
			    "where v.profileid='$safe_profileid' and ".
			    "      v.version='$safe_version' and ".
			    "      v.deleted is null");
Leigh B Stoller's avatar
Leigh B Stoller committed
85
	}
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->profile = null;
	    return;
	}
	$this->profile = mysql_fetch_array($query_result);

	# Load lazily;
	$this->project    = null;
    }
    # accessors
    function field($name) {
	return (is_null($this->profile) ? -1 : $this->profile[$name]);
    }
    function name()	    { return $this->field('name'); }
100
101
    function profileid()    { return $this->field('profileid'); }
    function version()      { return $this->field('version'); }
102
103
104
105
106
    function creator()	    { return $this->field('creator'); }
    function creator_idx()  { return $this->field('creator_idx'); }
    function pid()	    { return $this->field('pid'); }
    function pid_idx()	    { return $this->field('pid_idx'); }
    function created()	    { return $this->field('created'); }
107
108
    function published()    { return $this->field('published'); }
    function deleted()	    { return $this->field('deleted'); }
109
    function uuid()	    { return $this->field('uuid'); }
110
    function profile_uuid() { return $this->field('profile_uuid'); }
111
112
113
114
    function ispublic()	    { return $this->field('public'); }
    function shared()	    { return $this->field('shared'); }
    function listed()	    { return $this->field('listed'); }
    function rspec()	    { return $this->field('rspec'); }
115
    function script()	    { return $this->field('script'); }
116
    function paramdefs()    { return $this->field('paramdefs'); }
117
118
    function locked()	    { return $this->field('status'); }
    function status()	    { return $this->field('locked'); }
119
    function topdog()	    { return $this->field('topdog'); }
120
121
    function parent_profileid()    { return $this->field('parent_profileid'); }
    function parent_version()      { return $this->field('parent_version'); }
122
123
124
125
126

    # Private means only in the same project.
    function IsPrivate() {
	return !($this->ispublic() || $this->shared());
    }
127
128
129
130
    # PP profiles have parameter defs.
    function isParameterized() {
	return ($this->paramdefs() != "" ? 1 : 0);
    }
131
132
133
134
135
136
137
    
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->profile);
    }

    # Lookup up a single profile by idx. 
138
139
    function Lookup($token, $version = null) {
	$foo = new Profile($token, $version);
140
141
142
143
144
145
146
147

	if ($foo->IsValid()) {
            # Insert into cache.
	    return $foo;
	}	
	return null;
    }

148
    function LookupByName($project, $name, $version = null) {
149
	$pid = $project->pid();
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
	$safe_name = addslashes($name);

	if (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $name)) {
	    return Profile::Lookup($name);
	}
	elseif (is_null($version)) {
	    $query_result =
		DBQueryWarn("select i.profileid,i.version ".
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.pid='$pid' and ".
			    "      i.name='$safe_name'");
	}
	else {
	    $safe_version = addslashes($version);
	    $query_result =
		DBQueryWarn("select i.profileid,i.version ".
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid ".
			    "where i.pid='$pid' and ".
			    "      i.name='$safe_name' and ".
			    "      v.version='$safe_version'");
	}
176
177
	if ($query_result && mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
178
	    return Profile::Lookup($row[0], $row[1]);
179
180
181
	}
	return null;
    }
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    #
    # Lookup the most recently published version of a profile.
    #
    function LookupMostRecentPublished() {
	$profileid = $this->profileid();

	$query_result = 
	    DBQueryWarn("select version from apt_profile_versions as v ".
			"where v.profileid='$profileid' and ".
			"      published is not null and ".
			"      deleted is null ".
			"order by published desc limit 1");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return null;
	}
	$row = mysql_fetch_row($query_result);
	return Profile::Lookup($profileid, $row[0]);
    }

203
204
205
206
207
208
209
    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

210
211
	$profileid = $this->profileid();
	$version   = $this->version();
212
213

	$query_result =
214
215
	    DBQueryWarn("select * from apt_profile_versions ".
			"where profileid='$profileid' and version='$version'");
216
217
218
219
220
221
222
223
224
225
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->profile    = NULL;
	    $this->project    = null;
	    return -1;
	}
	$this->profile    = mysql_fetch_array($query_result);
	$this->project    = null;
	return 0;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
226
227

    #
228
    # URL. To the specific version of the profile.
Leigh B Stoller's avatar
Leigh B Stoller committed
229
230
    #
    function URL() {
231
	global $APTBASE, $ISVSERVER;
Leigh B Stoller's avatar
Leigh B Stoller committed
232
233
234
	
	$uuid = $this->uuid();

235
236
237
	if ($this->ispublic()) {
	    $pid  = $this->pid();
	    $name = $this->name();
238
	    $vers = $this->version();
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
	    if ($ISVSERVER)
		return "$APTBASE/p/$pid/$name/$vers";
	    return "$APTBASE/instantiate.php?profile=$name".
		"&project=$pid&version=$vers";
	}
	else {
	    if ($ISVSERVER)
		return "$APTBASE/p/$uuid";	    
	    return "$APTBASE/instantiate.php?profile=$uuid";
	}
    }
    # And the URL of the profile itself.
    function ProfileURL() {
	global $APTBASE;
	
	$uuid = $this->profile_uuid();

	if ($this->ispublic()) {
	    $pid  = $this->pid();
	    $name = $this->name();
	    if ($ISVSERVER)
		return "$APTBASE/p/$pid/$name";
	    return "$APTBASE/instantiate.php?profile=$name&project=$pid";
262
263
	}
	else {
264
265
266
	    if ($ISVSERVER)
		return "$APTBASE/p/$uuid";	    
	    return "$APTBASE/instantiate.php?profile=$uuid";
267
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
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

    #
    # Is this profile the highest numbered version.
    # 
    function IsHead() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select max(version) from apt_profile_versions ".
			"where profileid='$profileid'");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	return ($this->version() == $row[0] ? 1 : 0);
    }
    #
    # Does this profile have more then one version (history).
    # 
    function HasHistory() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select count(*) from apt_profile_versions ".
			"where profileid='$profileid'");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	return ($row[0] > 1 ? 1 : 0);
    }
    #
    # A profile can be published if it is a > version then the most
    # recent published profile. 
    # 
    function CanPublish() {
	$profileid = $this->profileid();

	# Already published. Might support unpublish at some point.
	if ($this->published())
	    return 0;

	$query_result =
	    DBQueryWarn("select version from apt_profile_versions ".
			"where profileid='$profileid' and ".
			"      published is not null ".
			"order by version desc limit 1");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	$vers = $row[0];
	
	return ($this->version() > $row[0] ? 1 : 0);
    }
    #
    # A profile can be modified if it is a >= version then the most
    # recent published profile. 
    # 
    function CanModify() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select version from apt_profile_versions ".
			"where profileid='$profileid' and ".
			"      published is not null ".
			"order by version desc limit 1");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	$vers = $row[0];
	
	return ($this->version() >= $row[0] ? 1 : 0);
    }
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
    #
    # Has a profile been instantiated?
    #
    function HasActivity() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select count(h.uuid) from apt_instance_history as h ".
			"where h.profile_id='$profileid'");

	if (!$query_result) {
	    return 0;
	}
	if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
	    if ($row[0] > 0) {
		return 1;
	    }
	}
	$query_result =
	    DBQueryWarn("select count(uuid) from apt_instances ".
			"where profile_id='$profileid'");

	if (!$query_result) {
	    return 0;
	}
	if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
	    if ($row[0] > 0) {
		return 1;
	    }
	}
	return 0;
    }
378
379
380
381
382
383
384
385

    #
    # Permission check; does user have permission to instantiate the
    # profile. At the moment, view/instantiate are the same.
    #
    function CanInstantiate($user) {
	$profileid = $this->profileid();

386
387
	if ($this->shared() || $this->ispublic() ||
	    $this->creator_idx() == $user->uid_idx()) {
388
389
390
	    return 1;
	}
	# Otherwise a project membership test.
391
	$project = Project::Lookup($this->pid_idx());
392
393
394
395
396
397
398
399
400
401
402
403
	if (!$project) {
	    return 0;
	}
	$isapproved = 0;
	if ($project->IsMember($user, $isapproved) && $isapproved) {
	    return 1;
	}
	return 0;
    }
    function CanView($user) {
	return $this->CanInstantiate($user);
    }
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
    function CanDelete($user) {
	# Want to know if the project is APT or Cloud/Emulab. APT projects
        # may not delete profiles (yet).
	$project = Project::Lookup($this->pid_idx());
	if (!$project) {
	    return 0;
	}
        if (!$this->IsHead()) {
            return 0;
        }
        if (ISADMIN() || STUDLY()) {
            return 1;
        }
        if (!$project->isAPT()) {
            return 1;
        }
        # APT profiles may not be deleted if published.
        if (!$this->published()) {
            return 1;
        }
        return 0;
    }
426

427
428
429
430
431
    function BestAggregate($rspec = null) {
	if (!$rspec) {
	    $rspec = $this->rspec();
	}
	$parsed_xml = simplexml_load_string($rspec);
432
433
434
435
436
437
438
439

	foreach ($parsed_xml->node as $node) {
	    # No XEN VMs on Cloudlab yet.
	    if ($node->sliver_type &&
		$node->sliver_type["name"] &&
		$node->sliver_type["name"] == "emulab-xen") {
		return "Utah APT";
	    }
440
	    if ($node->hardware_type &&
441
442
443
444
445
446
447
448
449
450
451
		$node->hardware_type["name"]) {
                if ($node->hardware_type["name"] == "m400") {
                    return "Utah Cloudlab";
                }
                elseif ($node->hardware_type["name"] == "dl360") {
                    return "Utah DDC";
                }
                elseif ($node->hardware_type["name"] == "r320" ||
                        $node->hardware_type["name"] == "c6220") {
                    return "Utah APT";
                }
452
	    }
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
	    # Check URL
	    if (! ($node->sliver_type &&
		   $node->sliver_type->disk_image &&
		   ($node->sliver_type->disk_image["url"] ||
		    $node->sliver_type->disk_image["name"]))) {
		continue;
	    }
	    if ($node->sliver_type->disk_image["name"]) {
		$name = $node->sliver_type->disk_image["name"];
		#
		# The only image that runs on Cloudlab is UBUNTU14-64-ARM
		#
		if (preg_match("/ARM/", $name)) {
		    return "Utah Cloudlab";
		}
		return "Utah APT";
	    }
	    else {
		$url = $node->sliver_type->disk_image["url"];
		if (preg_match("/utah\.cloudlab\.us/", $url)) {
		    return "Utah Cloudlab";
		}
		if (preg_match("/emulab\.net/", $url) ||
		    preg_match("/geniracks\.net/", $url) ||
		    preg_match("/instageni/", $url)) {
		    return "Utah APT";
		}
	    }
	}
	return null;
    }
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509

    function GenerateFormFragment() {
	$json_data = $this->paramdefs();
	
	if (!$json_data || $json_data == "") {
	    return "";
	}
	$fields    = json_decode($json_data);
	$defaults  = array();
	$form      = "";

	while (list ($name, $val) = each ($fields)) {
	    $type    = $val->type;
	    $prompt  = $val->description;
	    $defval  = $val->defaultValue;
	    $options = $val->legalValues;

	    $defaults[$name] = $defval;
	    if (!isset($prompt) || !$prompt) {
		$prompt = $name;
	    }

	    if ($type == "boolean") {
		$form .=
		    "<input name='$name' ".
		    "      <%- formfields.${name} %> ".
510
511
		    "      style='margin: 0px; height: 34px;' ".
		    "      class='format-me' ".
512
513
514
		    "      data-key='$name' ".
		    "      data-label='$prompt' ".
		    "      value='checked' ".
515
		    "      type='checkbox'>";
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
		if ($defval) {
		    $defaults[$name] = "checked";
		}
		else {
		    $defaults[$name] = "";
		}
	    }
	    elseif ($options) {
		$form .=
		    "<select name='$name' ".
		    "       class='form-control format-me' ".
		    "       data-key='$name' ".
		    "       data-label='$prompt' ".
		    "       placeholder='Please Select'> ";
		foreach ($options as $option) {
		    if (gettype($option) == "array") {
			$oval = $option[0];
			$okey = $option[1];
		    }
		    else {
			$okey = $oval = $option;
		    }
		    $form .= "<option";
		    $form .= 
			"<% if (_.has(formfields, '$name') && ".
			"       formfields.${name} == '$oval') { %> ".
			"   selected ".
			"<% } %> ".
			"value='$oval'>$okey</option>";
		}
		$form .= "</select>";
	    }
	    else {
		$form .=
		    "<input name='$name' ".
		    "value='<%- formfields.${name} %>' ".
		    "class='form-control format-me' ".
		    "data-key='$name' ".
		    "data-label='$prompt' ".
		    "type='text'>";
	    }
	}
	return array($form, $defaults);
    }
560
561
}
?>