osinfo_defs.php 14.1 KB
Newer Older
1
2
3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006, 2007, 2009, 2010 University of Utah and the Flux Group.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
73
74
75
76
77
78
79
# All rights reserved.
#

class OSinfo
{
    var	$osinfo;

    #
    # Constructor by lookup on unique ID
    #
    function OSinfo($id) {
	$safe_id = addslashes($id);

	$query_result =
	    DBQueryWarn("select * from os_info ".
			"where osid='$safe_id'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->osinfo = NULL;
	    return;
	}
	$this->osinfo = mysql_fetch_array($query_result);
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->osinfo);
    }

    # Lookup by osid
    function Lookup($id) {
	$foo = new OSinfo($id);

	if (! $foo->IsValid())
	    return null;

	return $foo;
    }

    # Lookup by osname in a project
    function LookupByName($project, $name) {
	$pid       = $project->pid();
	$safe_name = addslashes($name);
	
	$query_result =
	    DBQueryFatal("select osid from os_info ".
			 "where pid='$pid' and osname='$safe_name'");

	if (mysql_num_rows($query_result) == 0) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	return OSInfo::Lookup($row["osid"]);
    }

    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

	$osid = $this->osid();

	$query_result =
	    DBQueryWarn("select * from os_info where imageid='$osid'");
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->osinfo = NULL;
	    return -1;
	}
	$this->osinfo = mysql_fetch_array($query_result);
	return 0;
    }

80
81
82
83
84
85
86
87
88
89
90
91
    #
    # Class function to create new osid and return object.
    #
    function NewOSID($user, $project, $osname, $args, &$errors) {
	global $suexec_output, $suexec_output_array;

        #
        # Generate a temporary file and write in the XML goo.
        #
	$xmlname = tempnam("/tmp", "newosid");
	if (! $xmlname) {
	    TBERROR("Could not create temporary filename", 0);
92
	    $errors[] = "Transient error(1); please try again later.";
93
94
95
96
	    return null;
	}
	if (! ($fp = fopen($xmlname, "w"))) {
	    TBERROR("Could not open temp file $xmlname", 0);
97
	    $errors[] = "Transient error(2); please try again later.";
98
99
100
101
102
	    return null;
	}

	# Add these. Maybe caller should do this?
	$args["osname"]  = $osname;
103
	$args["pid"]     = $project->pid();
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

	fwrite($fp, "<osid>\n");
	foreach ($args as $name => $value) {
	    fwrite($fp, "<attribute name=\"$name\">");
	    fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	    fwrite($fp, "</attribute>\n");
	}
	fwrite($fp, "</osid>\n");
	fclose($fp);
	chmod($xmlname, 0666);

	$retval = SUEXEC("nobody", "nobody", "webnewosid $xmlname",
			 SUEXEC_ACTION_IGNORE);

	if ($retval) {
	    if ($retval < 0) {
120
		$errors[] = "Transient error(3, $retval); please try again later.";
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
		SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    }
	    else {
		# unlink($xmlname);
		if (count($suexec_output_array)) {
		    for ($i = 0; $i < count($suexec_output_array); $i++) {
			$line = $suexec_output_array[$i];
			if (preg_match("/^([-\w]+):\s*(.*)$/",
				       $line, $matches)) {
			    $errors[$matches[1]] = $matches[2];
			}
			else
			    $errors[] = $line;
		    }
		}
		else
137
		    $errors[] = "Transient error(4, $retval); please try again later.";
138
139
140
141
142
143
144
145
146
147
148
149
	    }
	    return null;
	}

        #
        # Parse the last line of output. Ick.
        #
	unset($matches);
	
	if (!preg_match("/^OSID\s+([^\/]+)\/(\d+)\s+/",
			$suexec_output_array[count($suexec_output_array)-1],
			$matches)) {
150
	    $errors[] = "Transient error(5); please try again later.";
151
152
153
154
155
156
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    return null;
	}
	$osid = $matches[2];
	$newosid = OSinfo::Lookup($osid);
	if (! $newosid) {
157
	    $errors[] = "Transient error(6); please try again later.";
158
159
160
161
162
163
164
165
166
	    TBERROR("Could not lookup new osid $osid", 0);
	    return null;
	}
	# Unlink this here, so that the file is left behind in case of error.
	# We can then create the osid by hand from the xmlfile, if desired.
	unlink($xmlname);
	return $newosid; 
    }

167
168
169
170
171
172
173
174
175
176
177
178
179
180
    #
    # Equality test.
    #
    function SameOS($osinfo) {
	return $this->osid() == $osinfo->osid();
    }

    # accessors
    function field($name) {
	return (is_null($this->osinfo) ? -1 : $this->osinfo[$name]);
    }
    function osname()		{ return $this->field("osname"); }
    function pid()		{ return $this->field("pid"); }
    function osid()		{ return $this->field("osid"); }
181
    function uuid()		{ return $this->field("uuid"); }
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    function creator()		{ return $this->field("creator"); }
    function creator_idx()	{ return $this->field("creator_idx"); }
    function created()		{ return $this->field("created"); }
    function description()	{ return $this->field("description"); }
    function OS()		{ return $this->field("OS"); }
    function version()		{ return $this->field("version"); }
    function path()		{ return $this->field("path"); }
    function magic()		{ return $this->field("magic"); }
    function machinetype()	{ return $this->field("machinetype"); }
    function osfeatures()	{ return $this->field("osfeatures"); }
    function ezid()		{ return $this->field("ezid"); }
    function shared()		{ return $this->field("shared"); }
    function mustclean()	{ return $this->field("mustclean"); }
    function op_mode()		{ return $this->field("op_mode"); }
    function nextosid()		{ return $this->field("nextosid"); }
    function max_concurrent()	{ return $this->field("max_concurrent"); }
    function mfs()		{ return $this->field("mfs"); }
    function reboot_waittime()  { return $this->field("reboot_waittime"); }

    #
    # Access Check, determines if $user can access $this record.
    # 
    function AccessCheck($user, $access_type) {
	global $TB_OSID_READINFO;
	global $TB_OSID_MODIFYINFO;
	global $TB_OSID_DESTROY;
	global $TB_OSID_MIN;
	global $TB_OSID_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_LOCALROOT;
	$mintrust = $TB_OSID_READINFO;

	if ($access_type < $TB_OSID_MIN || $access_type > $TB_OSID_MAX) {
	    TBERROR("Invalid access type $access_type!", 1);
	}

        #
        # Admins do whatever they want!
        # 
	if (ISADMIN()) {
	    return 1;
	}

        #
        # No GIDs yet.
        #
	$pid    = $this->pid();
	$shared = $this->shared();
	$uid    = $user->uid();

        #
        # Global OSIDs can be read by anyone.
        # 
	if ($shared) {
	    if ($access_type == $TB_OSID_READINFO) {
		return 1;
	    }
	    return 0;
	}
    
        #
        # Otherwise must have proper trust in the project.
        # 
	if ($access_type == $TB_OSID_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}

	#
	# Need the project object to complete this test.
	#
	if (! ($project = Project::Lookup($pid))) {
	    TBERROR("Could not map project $pid to its object", 1);
	}
	return TBMinTrust($project->UserTrust($user), $mintrust);
    }

    #
    # Spit out an OSID link in user format.
    #
    function SpitLink() {
	$osname = $this->osname();
	$url    = CreateURL("showosinfo", $this);
	
	echo "<a href='$url'>$osname</a>\n";
    }

    #
    # Show OS INFO record.
    #
    function Show() {
	$osid           = $this->osid();
	$os_description = $this->description();
	$os_OS          = $this->OS();
	$os_version     = $this->version();
	$os_path        = $this->path();
	$os_magic       = $this->magic();
	$os_osfeatures  = $this->osfeatures();
	$os_op_mode     = $this->op_mode();
	$os_pid         = $this->pid();
	$os_shared      = $this->shared();
	$os_osname      = $this->osname();
	$creator        = $this->creator();
	$created        = $this->created();
	$mustclean      = $this->mustclean();
	$nextosid       = $this->nextosid();
	$max_concurrent = $this->max_concurrent();
	$reboot_waittime= $this->reboot_waittime();
292
	$uuid           = $this->uuid();
293
	$ezid           = $this->ezid();
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401

	if (! ($creator_user = User::Lookup($creator))) {
	    TBERROR("Error getting object for user $creator", 1);
	}
	$showuser_url = CreateURL("showuser", $creator_user);

	if (!$os_description)
	    $os_description = "&nbsp;";
	if (!$os_version)
	    $os_version = "&nbsp;";
	if (!$os_path)
	    $os_path = "&nbsp;";
	if (!$os_magic)
	    $os_magic = "&nbsp;";
	if (!$os_osfeatures)
	    $os_osfeatures = "&nbsp;";
	if (!$os_op_mode)
	    $os_op_mode = "&nbsp;";
	if (!$created)
	    $created = "N/A";
	if (!$reboot_waittime)
	    $reboot_waittime = "&nbsp;";

        #
        # Generate the table.
        #
	echo "<table align=center border=1>\n";

	echo "<tr>
                <td>Name: </td>
                <td class=\"left\">$os_osname</td>
              </tr>\n";

	echo "<tr>
                <td>Project: </td>
                <td class=\"left\">
                  <a href='showproject.php3?pid=$os_pid'>$os_pid</a></td>
              </tr>\n";

	echo "<tr>
                <td>Creator: </td>
                <td class=left>
                  <a href='$showuser_url'>$creator</a></td>
 	      </tr>\n";

	echo "<tr>
                <td>Created: </td>
                <td class=left>$created</td>
    	      </tr>\n";

	echo "<tr>
                <td>Description: </td>
                <td class=\"left\">$os_description</td>
              </tr>\n";

        echo "<tr>
                <td>Operating System: </td>
                <td class=\"left\">$os_OS</td>
              </tr>\n";

	echo "<tr>
                <td>Version: </td>
                <td class=\"left\">$os_version</td>
             </tr>\n";

	echo "<tr>
                <td>Path: </td>
                <td class=\"left\">$os_path</td>
              </tr>\n";

	echo "<tr>
                <td>Magic (uname -r -s): </td>
                <td class=\"left\">$os_magic</td>
              </tr>\n";

	echo "<tr>
                <td>Features: </td>
                <td class=\"left\">$os_osfeatures</td>
              </tr>\n";

	echo "<tr>
                <td>Operational Mode: </td>
                <td class=\"left\">$os_op_mode</td>
              </tr>\n";

	if (isset($max_concurrent) and $max_concurrent > 0) {
	    echo "<tr>
                    <td>Max Concurrent Usage: </td>
                    <td class=\"left\">$max_concurrent</td>
                  </tr>\n";
	}

	echo "<tr>
                <td>Reboot Waittime: </td>
                <td class=\"left\">$reboot_waittime</td>
              </tr>\n";

	echo "<tr>
                <td>Shared?: </td>
                <td class=left>" . YesNo($os_shared) . "</td>
              </tr>\n";

	echo "<tr>
                <td>Must Clean?: </td>
                <td class=left>" . YesNo($mustclean) . "</td>
              </tr>\n";

	if ($nextosid) {
402
	    if ($nextosid == 0) {
403
404
405
		echo "<tr>
		        <td>Next Osid: </td>
		        <td class=left>
406
407
			    Mapped via DB table: osid_map</td></tr>\n";
	    }
408
409
410
411
412
413
414
415
	    else
		echo "<tr>
                        <td>Next Osid: </td>
                        <td class=left>
                            <a href='showosinfo.php3?osid=$nextosid'>
                                            $nextosid</a></td>
                      </tr>\n";
	}
416
417
418
419
420
421
422
423
	if ($ezid) {
		echo "<tr>
                        <td>Image Link: </td>
                        <td class=left>
                            <a href='showimageid.php3?imageid=$osid'>
                                            $os_osname</a></td>
                      </tr>\n";
	}
424
425
426
427
428
429

	echo "<tr>
                <td>Internal ID: </td>
                <td class=\"left\">$osid</td>
              </tr>\n";

430
431
432
433
434
	echo "<tr>
                <td>UUID: </td>
                <td class=left>$uuid</td>
              </tr>\n";

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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
	echo "</table>\n";
    }

    #
    # Display list of experiments using this osid.
    #
    function ShowExperiments($user) {
	global $TBOPSPID;
	global $TB_EXPT_READINFO;

	$uid = $user->uid();
	$pid = $this->pid();
	$osname = $this->osname();

        #
        # Due to the funny way we handle 'global' images in the emulab-ops 
        # project, we have to treat its images specially - namely, we 
        # have to make sure there is not an osname in that project, which 
        # takes priority over the global ones.
        #
	if ($pid == $TBOPSPID) {
	    $query_result =
		DBQueryFatal("select distinct v.pid, v.eid, e.state " .
			     "     from virt_nodes as v ".
			     "left join os_info as o on " .
			     "     v.osname=o.osname and v.pid=o.pid ".
			     "left join experiments as e on v.pid=e.pid and ".
			     "     v.eid=e.eid " .
			     "where v.osname='$osname' and o.osname is NULL " .
			     "order by v.pid, v.eid, e.state");
	}
	else {
	    $query_result =
		DBQueryFatal("select distinct v.pid, v.eid, e.state " .
			     "  from virt_nodes as v ".
			     "left join experiments as e " .
			     "     on v.pid=e.pid and v.eid=e.eid " .
			     "where v.pid='$pid' and v.osname='$osname' " .
			     "order by v.pid, v.eid, e.state");
	}

	if (mysql_num_rows($query_result) == 0) {
	    echo "<h4 align='center'>No experiments are using this OS</h3>";
	}
	else {
	    $other_exps = 0;
481
482

	    echo "<h3 align='center'>Experiments using this OS</h3>\n";
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
	    echo "<table align=center border=1>\n";
	    echo "  <tr> 
		        <th>PID</th>
   		        <th>EID</th>
		        <th>State</th>
		    </tr>\n";
	    while($row = mysql_fetch_array($query_result)) {
		$pid   = $row[0];
		$eid   = $row[1];
		$state = $row[2];

                #
	        # Gotta make sure that the user actually has the right to 
	        # see this experiment - summarize all the experiments that
	        # he/she cannot see at the bottom
	        #
		if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
		    continue;
		}
502
		if (! $experiment->AccessCheck($user, $TB_EXPT_READINFO)) {
503
504
505
506
507
508
509
		    $other_exps++;
		    continue;
		}
		$showexp_url = CreateURL("showexp", $experiment);

		echo "<tr>\n";
		echo "  <td>$pid</td>\n";
510
		echo "  <td><a href='$showexp_url'>$eid</td>\n";
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
		echo "  <td>$state</td>\n";
		echo "</tr>\n";
	    }
	    if ($other_exps) {
		echo "<tr><td colspan=3>
                        $other_exps experiments in other projects</td></tr>\n";
	    }
	    echo "</table>\n";
	}
    }
}

#
# Spit out an OSID link in user format.
#
function SpitOSIDLink($osid)
{
    $osinfo = OSInfo::Lookup($osid);

    if ($osinfo) {
	$osname = $osinfo->osname();
	$url    = CreateURL("showosinfo", $osinfo);
	
	echo "<a href='$url'>$osname</a>\n";
    }
}