instance_defs.php 7.92 KB
Newer Older
Leigh B Stoller's avatar
Leigh B Stoller committed
1 2
<?php
#
3
# Copyright (c) 2006-2015 University of Utah and the Flux Group.
Leigh B Stoller's avatar
Leigh B Stoller committed
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 31 32
if ($ISCLOUD) {
    $DEFAULT_AGGREGATE = "Utah Cloudlab";
}
else {
    $DEFAULT_AGGREGATE = "Utah APT";
}

Leigh B Stoller's avatar
Leigh B Stoller committed
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
class Instance
{
    var	$instance;
    
    #
    # Constructor by lookup on unique index.
    #
    function Instance($uuid) {
	$safe_uuid = addslashes($uuid);

	$query_result =
	    DBQueryWarn("select * from apt_instances ".
			"where uuid='$safe_uuid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->instance = null;
	    return;
	}
	$this->instance = mysql_fetch_array($query_result);
    }
    # accessors
    function field($name) {
	return (is_null($this->instance) ? -1 : $this->instance[$name]);
    }
    function uuid()	    { return $this->field('uuid'); }
    function slice_uuid()   { return $this->field('slice_uuid'); }
    function creator()	    { return $this->field('creator'); }
    function creator_idx()  { return $this->field('creator_idx'); }
    function creator_uuid() { return $this->field('creator_uuid'); }
    function created()	    { return $this->field('created'); }
63 64
    function profile_id()   { return $this->field('profile_id'); }
    function profile_version() { return $this->field('profile_version'); }
Leigh B Stoller's avatar
Leigh B Stoller committed
65
    function status()	    { return $this->field('status'); }
Leigh B Stoller's avatar
Leigh B Stoller committed
66
    function public_url()   { return $this->field('public_url'); }
Leigh B Stoller's avatar
Leigh B Stoller committed
67
    function manifest()	    { return $this->field('manifest'); }
68 69 70
    function admin_lockdown() { return $this->field('admin_lockdown'); }
    function user_lockdown(){ return $this->field('user_lockdown'); }
    function extension_reason() { return $this->field('extension_reason'); }
71 72 73 74 75 76 77
    function servername()   { return $this->field('servername'); }
    function IsAPT() {
	return preg_match('/aptlab/', $this->servername());
    }
    function IsCloud() {
	return preg_match('/cloudlab/', $this->servername());
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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
    
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->instance);
    }

    # Lookup up an instance by idx. 
    function Lookup($idx) {
	$foo = new Instance($idx);

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

    function LookupByCreator($token) {
	$safe_token = addslashes($token);

	$query_result =
	    DBQueryFatal("select uuid from apt_instances ".
			 "where creator_uuid='$safe_token'");

	if (! ($query_result && mysql_num_rows($query_result))) {
	    return null;
	}
	$row = mysql_fetch_row($query_result);
	$uuid = $row[0];
 	return Instance::Lookup($uuid);
    }

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

	$uuid = $this->uuid();

	$query_result =
	    DBQueryWarn("select * from apt_instances where uuid='$uuid'");
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->instance  = NULL;
	    return -1;
	}
	$this->instance = mysql_fetch_array($query_result);
	return 0;
    }
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    #
    # Class function to create a new Instance
    #
    function Instantiate($creator, $options, $args, &$errors) {
	global $suexec_output, $suexec_output_array;

	# So we can look up the slice after the backend creates it.
	$uuid = NewUUID();

	#
        # Generate a temporary file and write in the XML goo. 
	#
	$xmlname = tempnam("/tmp", "quickvm");
	if (! $xmlname) {
	    TBERROR("Could not create temporary filename", 0);
	    $errors["error"] = "Transient error(1); please try again later.";
	    return null;
	}
	elseif (! ($fp = fopen($xmlname, "w"))) {
	    TBERROR("Could not open temp file $xmlname", 0);
	    $errors["error"] = "Transient error(2); please try again later.";
	    return null;
	}
	else {
	    fwrite($fp, "<quickvm>\n");
	    foreach ($args as $name => $value) {
		fwrite($fp, "<attribute name=\"$name\">");
		fwrite($fp, "  <value>" . htmlspecialchars($value) .
		       "</value>");
		fwrite($fp, "</attribute>\n");
	    }
	    fwrite($fp, "</quickvm>\n");
	    fclose($fp);
	    chmod($xmlname, 0666);
	}
164 165
	# 
	# With a real user, run as that user. 
166
	#
167
	$uid = ($creator ? $creator->uid() : "nobody");
Leigh B Stoller's avatar
Leigh B Stoller committed
168 169 170 171
	$pid = "nobody";
	if ($creator && $creator->FirstApprovedProject()) {
	    $pid = $creator->FirstApprovedProject()->pid();
	}
172 173 174
	if (isset($_SERVER['REMOTE_ADDR'])) { 
	    putenv("REMOTE_ADDR=" . $_SERVER['REMOTE_ADDR']);
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
175 176 177
	if (isset($_SERVER['SERVER_NAME'])) { 
	    putenv("SERVER_NAME=" . $_SERVER['SERVER_NAME']);
	}
178
	$retval = SUEXEC($uid, $pid,
179
			 "webcreate_instance $options -u $uuid $xmlname",
180
			 SUEXEC_ACTION_IGNORE);
181 182 183

	if ($retval != 0) {
	    if ($retval < 0) {
184
		SUEXECERROR(SUEXEC_ACTION_CONTINUE);
185 186 187 188 189 190 191 192 193
		$errors["error"] =
		    "Transient error(3); please try again later.";
	    }
	    else {
		if (count($suexec_output_array)) {
		    $line = $suexec_output_array[0];
		    $errors["error"] = $line;
		}
		else {
194
		    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
		    $errors["error"] =
			"Transient error(4); please try again later.";
		}
	    }
	    return null;
	}
	unlink($xmlname);

	$instance = Instance::Lookup($uuid);
	if (!$instance) {
	    $errors["error"] = "Transient error(5); please try again later.";
	    return null;
	}
	if (!$creator) {
	    $creator = GeniUser::Lookup("sa", $instance->creator_uuid());
	}
	if (!$creator) {
	    $errors["error"] = "Transient error(6); please try again later.";
	    return null;
	}
	return array($instance, $creator);
    }
217 218 219 220 221 222 223 224 225 226

    function UserHasInstances($user) {
	$uuid = $user->uuid();

	$query_result =
	    DBQueryFatal("select uuid from apt_instances ".
			 "where creator_uuid='$uuid'");

	return mysql_num_rows($query_result);
    }
227 228 229 230

    function SendEmail($to, $subject, $msg, $headers) {
	TBMAIL($to, $subject, $msg, $headers);
    }
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

    #
    # How many experiments has a guest user created
    #
    function GuestInstanceCount($geniuser) {
        $uid = $geniuser->uid();
        
        $query_result =
            DBQueryFatal("select count(h.uuid) from apt_instance_history as h ".
                         "left join geni.geni_users as u on ".
                         "     u.uuid=h.creator_uuid ".
                         "where h.creator='$uid' and u.email is not null");
        
	$row = mysql_fetch_row($query_result);
	return $row[0];
    }
247 248 249 250 251

    #
    # Return aggregate based on the current user.
    #
    function DefaultAggregateList() {
Leigh B Stoller's avatar
Leigh B Stoller committed
252 253
        global $ISCLOUD;
            
254 255 256 257 258 259 260 261 262
        $am_array = array('Utah APT' =>
                          "urn:publicid:IDN+apt.emulab.net+authority+cm",
                          'Utah Cloudlab' =>
                          "urn:publicid:IDN+utah.cloudlab.us+authority+cm",
                          'Utah DDC' =>
                          "urn:publicid:IDN+utahddc.geniracks.net+authority+cm",
                          'Utah PG'  =>
                          "urn:publicid:IDN+emulab.net+authority+cm");

Leigh B Stoller's avatar
Leigh B Stoller committed
263
        if ($ISCLOUD) {
Leigh B Stoller's avatar
Leigh B Stoller committed
264
            $am_array["Wisc Cloudlab (Alpha)"] =
265 266 267 268
                "urn:publicid:IDN+wisc.cloudlab.us+authority+cm";
        }
        return $am_array;
    }
269 270 271 272 273 274 275 276 277 278

    function SetExtensionReason($reason)
    {
	$uuid = $this->uuid();
        $safe_reason = mysql_escape_string($reason);

        DBQueryWarn("update apt_instances set ".
                    "  extension_reason='$safe_reason' ".
                    "where uuid='$uuid'");
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
279 280
}
?>