/** * Lock cache index * * @return boolean True on success, false otherwise. * @since Nooku Server 0.7s */ private function lockindex() { $looptime = 300; $data_lock = $this->_db->add($this->_hash.'-index_lock', 1, false, 30); if ($data_lock === FALSE) { $lock_counter = 0; // loop until you find that the lock has been released. that implies that data get from other thread has finished while ( $data_lock === FALSE ) { if ( $lock_counter > $looptime ) { return false; break; } usleep(100); $data_lock = $this->_db->add($this->_hash.'-index_lock', 1, false, 30); $lock_counter++; } } return true; }
/** * Draw a rectangle filled with a gradient from $color1 to * $color2. * * @param integer $x The left x-coordinate of the rectangle. * @param integer $y The top y-coordinate of the rectangle. * @param integer $width The width of the rectangle. * @param integer $height The height of the rectangle. * @param string $color The outline color of the rectangle. * @param string $fill1 The name of the start color for the gradient. * @param string $fill2 The name of the end color for the gradient. */ function gradientRectangle($x, $y, $width, $height, $color = 'black', $fill1 = 'black', $fill2 = 'white') { $s = new SWFShape(); if ($color != 'none') { $color = $this->allocateColor($color); $s->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']); } $fill1 = $this->allocateColor($fill1); $fill2 = $this->allocateColor($fill2); $gradient = new SWFGradient(); $gradient->addEntry(0.0, $fill1['red'], $fill1['green'], $fill1['blue'], $fill1['alpha']); $gradient->addEntry(1.0, $fill2['red'], $fill2['green'], $fill2['blue'], $fill2['alpha']); $f = $s->addFill($gradient, SWFFILL_LINEAR_GRADIENT); $f->scaleTo($width / $this->_width); $f->moveTo($x, $y); $s->setRightFill($f); $verts[0] = array('x' => $x, 'y' => $y); $verts[1] = array('x' => $x + $width, 'y' => $y); $verts[2] = array('x' => $x + $width, 'y' => $y + $height); $verts[3] = array('x' => $x, 'y' => $y + $height); $first_done = false; foreach ($verts as $vert) { if (!$first_done) { $s->movePenTo($vert['x'], $vert['y']); $first_done = true; $first_x = $vert['x']; $first_y = $vert['y']; } $s->drawLineTo($vert['x'], $vert['y']); } $s->drawLineTo($first_x, $first_y); return $this->_movie->add($s); }
/** * Acquires a lock on the given $key. * * @param string $key * @param int $waitTime usleep() * @param int $maxTime seconds */ public function acquireLock($key, $waitTime, $maxTime) { if (strlen($key) > self::MAX_KEY_LENGTH) { throw new ezcCacheInvalidKeyException($key, 'Length > ' . self::MAX_KEY_LENGTH . '.'); } // add() does not replace and returns true on success. $maxTime is // obeyed by Memcache expiry. while ($this->memcache->add($key, $key, false, $maxTime) === false) { // Wait for next check usleep($waitTime); } }
/** * 对资源加锁 * @param string $key 缓存锁键 * @param int $lockTime 缓存锁失效时间 * @param string $server_key 指定服务器 * @return bool */ private function _getLock($key, $lockTime = 3, $server_key = NULL) { if (is_null($server_key)) { $ret = $this->mcd->add($this->_buildKey($key, 'LOCK_CTL'), 1, $lockTime); } else { $ret = $this->mcd->addByKey($server_key, $this->_buildKey($key, 'LOCK_CTL'), 1, $lockTime); } $this->_checkStats(__FUNCTION__, 0, $native = true); BaseModelCommon::addStatInfo('mc', 0, 1); defined('DAGGER_DEBUG') && BaseModelCommon::debug(intval($ret), "mcd_lock_time({$key})({$lockTime})({$server_key})"); return $ret; }
/** * add * @param string $key * @param string|array|object $value * @param int $ttl * @param string $tableName * @param resource $connectionResource * @return boolean */ public function add($key, $value, $ttl = 0, $tableName = '', $connectionResource = null) { if (0 != $ttl) { $ttl += time(); } if (true == $connectionResource->add($this->getRealKey($tableName, $key), array("ttl" => $ttl, "value" => $value))) { return true; } else { if ($this->get($key, $tableName, $connectionResource)) { return false; } else { $this->del($key, $tableName, $connectionResource); return $connectionResource->add($this->getRealKey($tableName, $key), array("ttl" => $ttl, "value" => $value)); } } }
/** * Output the end tag * */ public function endTag() { $stackelem = array_pop($this->_stack); if ($stackelem['sent']) { $this->_endTag(); if (count($this->_stack) == 0 && $this->multipart) { $this->_stream->rewind(); $len = $this->_stream->length(); $totalCount = count($this->_parts) + 1; $header = pack('i', $totalCount); $offset = $totalCount * 2 * 4 + 4; $header .= pack('ii', $offset, $len); $offset += $len; // start/length of parts foreach ($this->_parts as $bp) { if (is_resource($bp)) { rewind($bp); $stat = fstat($bp); $len = $stat['size']; } else { $len = strlen(bin2hex($bp)) / 2; } $header .= pack('ii', $offset, $len); $offset += $len; } // Output $this->_tempStream->add($header); $this->_stream->rewind(); $this->_tempStream->add($this->_stream); foreach ($this->_parts as $bp) { if (is_resource($bp)) { rewind($bp); $this->_tempStream->add($bp); fclose($bp); } else { $this->_tempStream->add($bp); } } $this->_stream->close(); $this->_stream = $this->_tempStream; } } }
/** * Clone the cell collection * * @param PHPExcel_Worksheet $parent The new worksheet * @return void */ public function copyCellCollection(PHPExcel_Worksheet $parent) { parent::copyCellCollection($parent); // Get a new id for the new file name $baseUnique = $this->_getUniqueID(); $newCachePrefix = substr(md5($baseUnique), 0, 8) . '.'; $cacheList = $this->getCellList(); foreach ($cacheList as $cellID) { if ($cellID != $this->_currentObjectID) { $obj = $this->_memcache->get($this->_cachePrefix . $cellID . '.cache'); if ($obj === false) { // Entry no longer exists in Memcache, so clear it from the cache array parent::deleteCacheData($cellID); throw new PHPExcel_Exception('Cell entry ' . $cellID . ' no longer exists in MemCache'); } if (!$this->_memcache->add($newCachePrefix . $cellID . '.cache', $obj, NULL, $this->_cacheTime)) { $this->__destruct(); throw new PHPExcel_Exception('Failed to store cell ' . $cellID . ' in MemCache'); } } } $this->_cachePrefix = $newCachePrefix; }
function create($cu_id, $virtualization_type, $name, $mac, $additional_nics, $cpu, $memory, $disk, $timeout, $vncpassword, $source_image_id = null) { global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; global $RESOURCE_INFO_TABLE; global $host_start_from_off_timeout; global $RootDir; $this->init($timeout); global $event; $vmware_mac_address_space = "00:50:56"; $vtype = new virtualization(); $vtype->get_instance_by_id($virtualization_type); $virtualization_plugin_name = $vtype->get_plugin_name(); $event->log("create", $_SERVER['REQUEST_TIME'], 5, "cloudvm.class.php", "Trying to create new VM type {$virtualization_type} ({$virtualization_plugin_name}) {$mac}/{$cpu}/{$memory}/{$disk}", "", "", 0, 0, 0); // here we need to find out if we have a virtualization host providing the type of VMs as requested // find out the host virtualization type via the plugin name $vhost_type = new virtualization(); $vhost_type->get_instance_by_type($virtualization_plugin_name); $event->log("create", $_SERVER['REQUEST_TIME'], 5, "cloudvm.class.php", "Trying to find a virtualization host from type {$vhost_type->type} {$vhost_type->name}", "", "", 0, 0, 0); // check if resource-pooling is enabled $cp_conf = new cloudconfig(); $show_resource_pools = $cp_conf->get_value(25); // resource_pools enabled ? $vm_provision_delay = $cp_conf->get_value(40); // delay provisioning of VMs ? $vm_loadbalance_algorithm = $cp_conf->get_value(41); // which LB to select ? // for all in appliance list, find virtualization host appliances $appliance_tmp = new appliance(); $appliance_id_list = $appliance_tmp->get_all_ids(); $active_appliance_list = array(); $active_appliance_resource_list = array(); foreach ($appliance_id_list as $id_arr) { foreach ($id_arr as $id) { $appliance = new appliance(); $appliance->get_instance_by_id($id); // active ? if ($appliance->stoptime == 0 || $appliance->resources == 0) { if ($appliance->virtualization == $vhost_type->id) { // we have found an active appliance from the right virtualization type // Now we check that its resource is active and not in error $cvm_resource = new resource(); $cvm_resource->get_instance_by_id($appliance->resources); if (strcmp($cvm_resource->state, "active")) { continue; } // here we check if there is still enough space // to create the new VM -> max_vm setting per resource $res_hostlimit = new cloudhostlimit(); $res_hostlimit->get_instance_by_resource($appliance->resources); if (strlen($res_hostlimit->id)) { if ($res_hostlimit->max_vms >= 0) { $new_current_vms = $res_hostlimit->current_vms + 1; if ($new_current_vms > $res_hostlimit->max_vms) { $event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "Hostlimit max_vm is reached for resource {$appliance->resources}", "", "", 0, 0, $appliance->resources); continue; } } } // resource pooling enabled ? if (strcmp($show_resource_pools, "true")) { // disabled, add any appliance from the right virtualization type $active_appliance_list[] .= $id; $active_appliance_resource_list[] .= $appliance->resources; //$event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "------- resource pooling is disabled", "", "", 0, 0, 0); } else { //$event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "------- resource pooling is enabled $appliance->resources", "", "", 0, 0, 0); // resource pooling enabled, check to which user group the resource belongs to $private_resource = new cloudrespool(); $private_resource->get_instance_by_resource($appliance->resources); // is this resource configured in the resource pools ? //$event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "------- resource pool id $private_resource->id !", "", "", 0, 0, 0); if (strlen($private_resource->id)) { //$event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "------- resource $appliance->resources is in a resource pool", "", "", 0, 0, 0); // is it hidden ? if ($private_resource->cg_id >= 0) { //$event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "------- resource $appliance->resources is also configured in resource pool (not hidden)", "", "", 0, 0, 0); $cloud_user = new clouduser(); $cloud_user->get_instance_by_id($cu_id); $cloud_user_group = new cloudusergroup(); $cloud_user_group->get_instance_by_id($cloud_user->cg_id); //$event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "------- we have found the users group $cloud_user_group->id", "", "", 0, 0, 0); // does it really belongs to the users group ? if ($private_resource->cg_id == $cloud_user_group->id) { // resource belongs to the users group, add appliance to list //$event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "------- adding appliance $id ", "", "", 0, 0, 0); $active_appliance_list[] .= $id; $active_appliance_resource_list[] .= $appliance->resources; //} else { // $event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "Appliance $id (resource $appliance->resources) is NOT in dedicated for the users group", "", "", 0, 0, 0); } //} else { // $event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "Appliance $id (resource $appliance->resources) is marked as hidden", "", "", 0, 0, 0); } } } } } } } // did we found any active host ? if (count($active_appliance_list) < 1) { $event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "Warning ! There is no active virtualization host type {$vhost_type->name} available to bring up a new VM", "", "", 0, 0, 0); $event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "Notice : Trying to find a Host which can start-from-off .....", "", "", 0, 0, 0); // if this method finds a host it will block until the host is up + active $cloud_host_start_from_off = new cloudhoststartfromoff(); $start_from_off_appliance_id = $cloud_host_start_from_off->find_host_to_start_from_off($vhost_type->id, $show_resource_pools, $cu_id, $host_start_from_off_timeout); if ($start_from_off_appliance_id > 0) { //$event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "------- adding appliance $id ", "", "", 0, 0, 0); $active_appliance_list[] .= $start_from_off_appliance_id; // add to active resource list $start_from_off_appliance = new appliance(); $start_from_off_appliance->get_instance_by_id($start_from_off_appliance_id); $active_appliance_resource_list[] .= $start_from_off_appliance->resources; } else { // here we did not found any host to start-from-off $event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "Warning ! Could not find any virtualization host type {$vhost_type->name} to start-from-off", "", "", 0, 0, 0); $event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "Warning ! Giving up trying to start a new VM type {$vhost_type->name}", "", "", 0, 0, 0); return false; } } // ! for all virt-localboot VMs we need to make sure the VM is created on // ! the same host as the image is located, for all others we try to lb $less_load_resource_id = -1; if (strstr($vtype->type, "-vm-local")) { $origin_appliance = new appliance(); $origin_appliance->get_instance_by_name($name); // if we have a cloudappliance already this create is coming from unpause // The host to create the new VM on must be the image storage resource $vstorage_cloud_app = new cloudappliance(); $vstorage_cloud_app->get_instance_by_appliance_id($origin_appliance->id); if (strlen($vstorage_cloud_app->id)) { $vstorage_image = new image(); $vstorage_image->get_instance_by_id($origin_appliance->imageid); $vstorage = new storage(); $vstorage->get_instance_by_id($vstorage_image->storageid); $vstorage_host_res_id = $vstorage->resource_id; // check if the origin host is in the active appliances we have found if (in_array($vstorage_host_res_id, $active_appliance_resource_list)) { $event->log("create", $_SERVER['REQUEST_TIME'], 5, "cloudvm.class.php", "Origin host {$vstorage_host_res_id} is active. Creating the new VM", "", "", 0, 0, 0); $resource = new resource(); $resource->get_instance_by_id($vstorage_host_res_id); $less_load_resource_id = $vstorage_host_res_id; } else { $event->log("create", $_SERVER['REQUEST_TIME'], 2, "cloudvm.class.php", "Origin host {$vstorage_host_res_id} is not active. Not creating the new VM", "", "", 0, 0, 0); } } else { // if we do not have a cloudappliance yet we can (should) loadbalance the create VM request $event->log("create", $_SERVER['REQUEST_TIME'], 5, "cloudvm.class.php", "Using Loadbalancing Algorithm " . $vm_loadbalance_algorithm . " for creating the new VM", "", "", 0, 0, 0); // the cloud-deployment hook of the virt-localboot VM will adapt the image storage id to the host id switch ($vm_loadbalance_algorithm) { case '0': $less_load_resource_id = $this->vm_balance_load($active_appliance_list); break; case '1': $less_load_resource_id = $this->vm_balance_memory($active_appliance_list, $memory); break; case '2': $less_load_resource_id = $this->vm_balance_random($active_appliance_list); break; case '3': $less_load_resource_id = $this->vm_balance_first_available($active_appliance_list); break; default: $less_load_resource_id = $this->vm_balance_load($active_appliance_list); break; } } } else { $event->log("create", $_SERVER['REQUEST_TIME'], 5, "cloudvm.class.php", "Using Loadbalancing Algorithm " . $vm_loadbalance_algorithm . " for creating the new VM", "", "", 0, 0, 0); switch ($vm_loadbalance_algorithm) { case '0': $less_load_resource_id = $this->vm_balance_load($active_appliance_list); break; case '1': $less_load_resource_id = $this->vm_balance_memory($active_appliance_list, $memory); break; case '2': $less_load_resource_id = $this->vm_balance_random($active_appliance_list); break; case '3': $less_load_resource_id = $this->vm_balance_first_available($active_appliance_list); break; default: $less_load_resource_id = $this->vm_balance_load($active_appliance_list); break; } } if ($less_load_resource_id >= 0) { $event->log("create", $_SERVER['REQUEST_TIME'], 5, "cloudvm.class.php", "Found Virtualization host resource {$less_load_resource_id} as the target for the new VM ", "", "", 0, 0, 0); } // additional network cards if ($additional_nics > 0) { $anic = 1; $additional_nic_str = ""; $mac_gen_res = new resource(); while ($anic <= $additional_nics) { $mac_gen_res->generate_mac(); switch ($virtualization_plugin_name) { # VMware VMs need to get special macs case 'vmware-esx': $nic_nr = $anic; $suggested_mac = $mac_gen_res->mac; $new_forth_byte_first_bit = rand(1, 3); $mac_gen_res_vmw = strtolower($vmware_mac_address_space . ":" . substr($suggested_mac, 9)); $mac_gen_res_vmw = substr_replace($mac_gen_res_vmw, $new_forth_byte_first_bit, 9, 1); $additional_nic_str .= " -m" . $nic_nr . " " . $mac_gen_res_vmw; break; # VMs network parameter starts with -m1 # VMs network parameter starts with -m1 default: $nic_nr = $anic; $additional_nic_str .= " -m" . $nic_nr . " " . $mac_gen_res->mac; break; } $anic++; } } // swap, for the cloud VMs we simply calculate memory * 2 $swap = $memory * 2; // start the VM on the appliance resource $host_resource = new resource(); $host_resource->get_instance_by_id($less_load_resource_id); $host_resource_ip = $host_resource->ip; // we need to have an openQRM server object too since some of the // virtualization commands are sent from openQRM directly $openqrm = new openqrm_server(); // create the new resource + setting the virtualization type $vm_resource_ip = "0.0.0.0"; // add to openQRM database $vm_resource_fields["resource_ip"] = $vm_resource_ip; $vm_resource_fields["resource_mac"] = $mac; $vm_resource_fields["resource_localboot"] = 0; $vm_resource_fields["resource_vtype"] = $vtype->id; $vm_resource_fields["resource_vhostid"] = $less_load_resource_id; $vm_resource_fields["resource_vname"] = $name; $new_resource_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $vm_resource_fields["resource_id"] = $new_resource_id; $host_resource->add($vm_resource_fields); // send new-resource command now after the resource is created logically $openqrm->send_command("openqrm_server_add_resource {$new_resource_id} {$mac} {$vm_resource_ip}"); // let the new resource commands settle sleep(10); // plug in the virtualization cloud hook $virtualization_cloud_hook = "{$RootDir}/plugins/{$virtualization_plugin_name}/openqrm-{$virtualization_plugin_name}-cloud-hook.php"; if (file_exists($virtualization_cloud_hook)) { $event->log("create", $_SERVER['REQUEST_TIME'], 5, "cloudvm.class", "Found plugin {$virtualization_plugin_name} handling to create the VM.", "", "", 0, 0, $new_resource_id); require_once "{$virtualization_cloud_hook}"; $virtualization_method = "create_" . $vtype->type; $virtualization_method = str_replace("-", "_", $virtualization_method); $virtualization_method($less_load_resource_id, $name, $mac, $memory, $cpu, $swap, $additional_nic_str, $vncpassword, $source_image_id); } else { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudvm.class", "Do not know how to create VM from type {$virtualization_plugin_name}.", "", "", 0, 0, 0); // remove resource object $vm_resource = new resource(); $vm_resource->get_instance_by_id($new_resource_id); $vm_resource->remove($new_resource_id, $mac); return false; } // update hostlimits quite early to avoid overloading a Host with non-starting VMs // add or update hostlimits $res_hostlimit = new cloudhostlimit(); $res_hostlimit->get_instance_by_resource($host_resource->id); if (strlen($res_hostlimit->id)) { // update $current_vms = $res_hostlimit->current_vms + 1; $cloud_hostlimit_fields["hl_current_vms"] = $current_vms; $res_hostlimit->update($res_hostlimit->id, $cloud_hostlimit_fields); } else { // add $cloud_hostlimit_fields["hl_id"] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $cloud_hostlimit_fields["hl_resource_id"] = $host_resource->id; $cloud_hostlimit_fields["hl_max_vms"] = -1; $cloud_hostlimit_fields["hl_current_vms"] = 1; $res_hostlimit->add($cloud_hostlimit_fields); } $event->log("create", $_SERVER['REQUEST_TIME'], 5, "cloudvm.class.php", "New VM created with resource id " . $new_resource_id . " and started. Waiting now until it is active/idle", "", "", 0, 0, 0); if ($vm_provision_delay > 0) { $event->log("create", $_SERVER['REQUEST_TIME'], 5, "cloudvm.class.php", "Delaying the provisioning of resource id " . $new_resource_id . " for " . $vm_provision_delay . " seconds.", "", "", 0, 0, 0); sleep($vm_provision_delay); } // setting this object resource id as return state $this->resource_id = $new_resource_id; }
/** * add * @param string $key * @param string|array|object $value * @param int $ttl * @param string $tableName * @param resource $connectionResource * @return boolean */ public function add($key, $value, $ttl = 0, $tableName = '', $connectionResource = null) { return $connectionResource->add($this->getRealKey($tableName, $key), $value, $ttl); }
/** * 对资源加锁 * @param string $key 缓存锁键 * @param int $lockTime 缓存锁失效时间 */ public function getLock($key, $lockTime = 3) { defined('DAGGER_DEBUG') && BaseModelCommon::debug($lockTime, 'mc_lock_time'); return $this->mc->add($key . self::CACHE_LOCK_CTL, 1, false, $lockTime); }
// check if resource_id is free if (!$resource->is_id_free($resource->id)) { $event->log("get_parameter", $_SERVER['REQUEST_TIME'], 3, "resource-monitor", "Given resource id {$resource->id} is already in use!", "", "", 0, 1, $resource->id); echo "Given resource id {$resource->id} is already in use!"; exit; } $event->log("get_parameter", $_SERVER['REQUEST_TIME'], 5, "resource-monitor", "Adding new resource {$new_resource_id} ({$resource_mac})", "", "", 0, 1, $resource->id); # send add resource to openQRM-server $openqrm_server->send_command("openqrm_server_add_resource {$new_resource_id} {$resource_mac} {$resource_ip}"); # add resource to db $resource_fields["resource_id"] = $new_resource_id; $resource_fields["resource_localboot"] = 0; $resource_fields["resource_vtype"] = 1; $resource_fields["resource_vhostid"] = $new_resource_id; $resource_fields["resource_subnet"] = $resource_subnet; $resource->add($resource_fields); } } if (strlen($resource_mac)) { $resource = new resource(); $resource->get_instance_by_mac("{$resource_mac}"); // update the resource parameter in any way $resource->update_info($resource->id, $resource_fields); $resource->get_parameter($resource->id); } else { if (strlen($resource_id)) { $resource = new resource(); $resource->get_instance_by_id($resource_id); // update the resource parameter in any way $resource->update_info($resource->id, $resource_fields); $resource->get_parameter($resource->id);
function add() { $response = $this->get_response(); $form = $response->form; $errors = array(); if (!$form->get_errors() && $this->response->submit()) { $image_id = $form->get_request('ami_image_id'); $image = new image(); if (isset($image_id) && $image_id !== '') { $image->get_instance_by_id($image_id); } else { $errors[] = $this->lang['error_boot']; } if (count($errors) > 0 || $form->get_errors()) { $response->error = join('<br>', $errors); } else { $tables = $this->openqrm->get('table'); $custom_script_parameter = ''; $custom_script = $form->get_request('custom_script'); if (strlen($custom_script)) { $custom_script_parameter = ' -ic ' . $custom_script; } $custom_name = $form->get_request('name'); $resource = new resource(); $id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); if (strlen($custom_name)) { $name = $custom_name; } else { $name = $this->hc->account_type . $id; } $ip = "0.0.0.0"; $resource->generate_mac(); $mac = $resource->mac; // send command to the openQRM-server $openqrm = new openqrm_server(); $openqrm->send_command('openqrm_server_add_resource ' . $id . ' ' . $mac . ' ' . $ip); // set resource type $virtualization = new virtualization(); $virtualization->get_instance_by_type("hybrid-cloud-vm-local"); // add to openQRM database $fields["resource_id"] = $id; $fields["resource_ip"] = $ip; $fields["resource_mac"] = $mac; $fields["resource_hostname"] = $name; $fields["resource_localboot"] = 0; $fields["resource_vtype"] = $virtualization->id; $fields["resource_vhostid"] = 0; $fields["resource_image"] = $image->name; $fields["resource_imageid"] = $image->id; $rfields["resource_kernel"] = 'default'; $rfields["resource_kernelid"] = 1; $resource->add($fields); $resource->get_instance_by_mac($mac); // set account id in resource capabilities $resource->set_resource_capabilities("HCACL", $this->hc->id); $hc_authentication = ''; if ($this->hc->account_type == 'aws' || $this->hc->account_type == 'euca') { $hc_authentication .= ' -O ' . $this->hc->access_key; $hc_authentication .= ' -W ' . $this->hc->secret_key; $hc_authentication .= ' -ir ' . $this->region; $hc_authentication .= ' -iz ' . $form->get_request('availability_zone'); } if ($this->hc->account_type == 'lc-openstack') { $hc_authentication .= ' -u ' . $this->hc->username; $hc_authentication .= ' -p ' . $this->hc->password; $hc_authentication .= ' -q ' . $this->hc->host; $hc_authentication .= ' -x ' . $this->hc->port; $hc_authentication .= ' -g ' . $this->hc->tenant; $hc_authentication .= ' -e ' . $this->hc->endpoint; } $command = $this->openqrm->get('basedir') . '/plugins/hybrid-cloud/bin/openqrm-hybrid-cloud-vm create'; $command .= ' -i ' . $this->hc->id; $command .= ' -n ' . $this->hc->account_name; $command .= ' -t ' . $this->hc->account_type; $command .= $hc_authentication; $command .= ' -in ' . $name; $command .= ' -im ' . $mac; $command .= ' -a ' . $image->name; $command .= ' -it ' . $form->get_request('type'); $command .= ' -ik ' . $form->get_request('keypair'); if ($this->hc->account_type == 'aws') { $command .= ' -subnet ' . $form->get_request('subnet'); } else { $command .= ' -ig ' . $form->get_request('group'); } $command .= ' --openqrm-ui-user ' . $this->user->name; $command .= ' --openqrm-cmd-mode background'; $command .= $custom_script_parameter; $openqrm->send_command($command, NULL, true); // check to have a ip from the dhcpd-resource hook while ($resource->ip == "0.0.0.0") { sleep(1); clearstatcache(); $resource->get_instance_by_mac($mac); } // save the mgmt ip in the resource network field $rufields["resource_network"] = $resource->ip; $resource->update_info($resource->id, $rufields); $response->resource_id = $id; $response->msg = sprintf($this->lang['msg_added'], $name); // auto create the appliance for this VM if we are not coming from the wizard if (isset($this->user->wizard_name) && $this->user->wizard_name === 'appliance' && $this->user->wizard_step == 2) { $now = 1; } else { $now = $_SERVER['REQUEST_TIME']; $appliance = new appliance(); $new_appliance_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $afields['appliance_id'] = $new_appliance_id; $afields['appliance_name'] = $name; $afields['appliance_resources'] = $id; $afields['appliance_kernelid'] = '1'; $afields['appliance_imageid'] = $image->id; $afields["appliance_virtual"] = 0; $afields["appliance_virtualization"] = $virtualization->id; $afields['appliance_wizard'] = ''; $afields['appliance_comment'] = 'Cloud VM Server for Resource ' . $id; $appliance->add($afields); // update state/start+stoptime $aufields['appliance_stoptime'] = $now; $aufields['appliance_starttime'] = ''; $aufields['appliance_state'] = 'stopped'; $appliance->update($new_appliance_id, $aufields); // set image active $image_fields["image_id"] = $image->id; $image_fields['image_isactive'] = 1; $image->update($image->id, $image_fields); } } } return $response; }
function import() { $response = ''; $errors = array(); $message = array(); $event = new event(); $instance_command = $this->response->html->request()->get('instance_command'); if ($instance_command !== '') { switch ($instance_command) { case 'add': // instance_command=add // &instance_name='.$name.' // &instance_mac='.$mac.' // &instance_public_ip='.$public_ip.' // &instance_type='.$type.' // &instance_keypair='.$keypair.' // &instance_region='.$region.' // &instance_ami='.$ami; $now = $_SERVER['REQUEST_TIME']; $openqrm = new openqrm_server(); $instance_name = $this->response->html->request()->get('instance_name'); $instance_mac = $this->response->html->request()->get('instance_mac'); $instance_public_ip = $this->response->html->request()->get('instance_public_ip'); $instance_type = $this->response->html->request()->get('instance_type'); $instance_keypair = $this->response->html->request()->get('instance_keypair'); $instance_region = $this->response->html->request()->get('instance_region'); $instance_ami = $this->response->html->request()->get('instance_ami'); // create resource, image and appliance $event->log("import", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-vm-import", "Importing " . $instance_name . " - " . $instance_mac . " - " . $instance_public_ip . " - " . $instance_type . " - " . $instance_keypair . " - " . $instance_region . " - " . $instance_ami . ".", "", "", 0, 0, 0); $import_resource = new resource(); $deployment = new deployment(); $deployment->get_instance_by_name('ami-deployment'); $virtualization = new virtualization(); $virtualization->get_instance_by_type("hybrid-cloud-vm-local"); // create resource $resid = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); // send command to the openQRM-server $openqrm->send_command('openqrm_server_add_resource ' . $resid . ' ' . $instance_mac . ' ' . $instance_public_ip); // add to openQRM database $resource_fields["resource_id"] = $resid; $resource_fields["resource_ip"] = $instance_public_ip; $resource_fields["resource_mac"] = $instance_mac; $resource_fields["resource_kernel"] = 'local'; $resource_fields["resource_kernelid"] = 0; $resource_fields["resource_localboot"] = 0; $resource_fields["resource_hostname"] = $this->hc->account_type . $resid; $resource_fields["resource_vtype"] = $virtualization->id; $resource_fields["resource_vhostid"] = 0; $import_resource->add($resource_fields); $import_resource->get_instance_by_mac($instance_mac); // update stats #if ($state == 'running') { $rfields["resource_state"] = 'idle'; #$rfields["resource_lastgood"]=$now; #} else { # $rfields["resource_state"]='off'; #} #$import_resource->update_info($import_resource->id, $rfields); // set account id in resource capabilities $import_resource->set_resource_capabilities("HCACL", $this->id); // auto create image object $storage = new storage(); $storage->get_instance_by_name('ami-image-storage'); $image = new image(); $image->get_instance_by_name($instance_ami); if (isset($image->id) && $image->id > 0) { $image_exists = true; } else { $image_fields = array(); $vm_image_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $image_fields["image_id"] = $vm_image_id; $image_fields['image_name'] = $instance_ami; $image_fields['image_type'] = 'ami-deployment'; $image_fields['image_rootfstype'] = 'local'; $image_fields['image_isactive'] = 0; $image_fields['image_storageid'] = $storage->id; $image_fields['image_comment'] = "Image Object for AMI {$instance_ami}"; $image_fields['image_rootdevice'] = $instance_ami; $image->add($image_fields); # update image object $image->get_instance_by_id($vm_image_id); // update resource with image infos $rfields["resource_id"] = $resid; $rfields["resource_image"] = $image->name; $rfields["resource_imageid"] = $image->id; $import_resource->update_info($import_resource->id, $rfields); $import_resource->get_instance_by_mac($instance_mac); } // create the appliance $appliance = new appliance(); $appliance->get_instance_by_name($instance_name); if (isset($appliance->id) && $appliance->id > 0) { $appliance_exists = true; } else { $new_appliance_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $afields['appliance_id'] = $new_appliance_id; $afields['appliance_name'] = $this->hc->account_type . $resid; $afields['appliance_resources'] = $resid; $afields['appliance_kernelid'] = '1'; $afields['appliance_imageid'] = $image->id; $afields["appliance_virtual"] = 0; $afields["appliance_virtualization"] = $virtualization->id; $afields['appliance_wizard'] = ''; $afields['appliance_comment'] = 'Cloud VM Appliance for Resource ' . $resid; $appliance->add($afields); // update state/start+stoptime $aufields['appliance_stoptime'] = $now; $aufields['appliance_starttime'] = ''; $aufields['appliance_state'] = 'stopped'; $appliance->update($new_appliance_id, $aufields); } $hc_authentication = ''; if ($this->hc->account_type == 'aws' || $this->hc->account_type == 'euca') { $hc_authentication .= ' -O ' . $this->hc->access_key; $hc_authentication .= ' -W ' . $this->hc->secret_key; $hc_authentication .= ' -ir ' . $this->response->html->request()->get('region'); $hc_authentication .= ' -iz ' . $instance_region; } if ($this->hc->account_type == 'lc-openstack') { $hc_authentication .= ' -u ' . $this->hc->username; $hc_authentication .= ' -p ' . $this->hc->password; $hc_authentication .= ' -q ' . $this->hc->host; $hc_authentication .= ' -x ' . $this->hc->port; $hc_authentication .= ' -g ' . $this->hc->tenant; $hc_authentication .= ' -e ' . $this->hc->endpoint; } $command = $this->openqrm->get('basedir') . '/plugins/hybrid-cloud/bin/openqrm-hybrid-cloud-vm create'; $command .= ' -i ' . $this->hc->id; $command .= ' -n ' . $this->hc->account_name; $command .= ' -t ' . $this->hc->account_type; $command .= $hc_authentication; $command .= ' -in ' . $this->hc->account_type . $resid; $command .= ' -im ' . $instance_mac; $command .= ' -a ' . $instance_ami; $command .= ' -it ' . $instance_type; $command .= ' -ik ' . $instance_keypair; if ($this->hc->account_type == 'aws') { $command .= ' -subnet ' . $this->response->html->request()->get('instance_subnet'); } else { # TODO $command .= ' -ig ' . $form->get_request('group'); } $command .= ' --openqrm-ui-user ' . $this->user->name; $command .= ' --openqrm-cmd-mode background'; $openqrm->send_command($command, NULL, true); $message[] = sprintf($this->lang['msg_imported'], $instance_name); break; } if (count($errors) === 0) { $response = join('<br>', $message); } else { $msg = array_merge($errors, $message); $response = join('<br>', $msg); } } else { $response = ''; } return $response; }
function resource_new() { $response = $this->get_response(); $form = $response->form; $name = $form->get_request('name'); $ip = $form->get_request('ip'); $mac = $form->get_request('mac'); if (strlen($name)) { if (!$form->get_errors() && $this->response->submit()) { // check that mac adress + ip does not yet exists $resource = new resource(); $resource->get_instance_by_mac($mac); if (strlen($resource->id)) { $response->error = sprintf($this->lang['msg_mac_in_use']); return $response; } $resource->get_instance_by_ip($ip); if (strlen($resource->id)) { $response->error = sprintf($this->lang['msg_ip_in_use']); return $response; } $tables = $this->openqrm->get('table'); $id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $mac = strtolower($mac); // send command to the openQRM-server $openqrm = new openqrm_server(); $openqrm->send_command('openqrm_server_add_resource ' . $id . ' ' . $mac . ' ' . $ip); // add to openQRM database $fields["resource_id"] = $id; $fields["resource_hostname"] = $name; $fields["resource_ip"] = $ip; $fields["resource_mac"] = $mac; $fields["resource_localboot"] = 0; $fields["resource_vtype"] = 1; $fields["resource_vhostid"] = $id; $fields["resource_state"] = 'unknown'; $fields["resource_lastgood"] = '-1'; $resource->add($fields); // set lastgood to -1 to prevent automatic checking the state $resource->update_info($id, $fields); $response->msg = sprintf($this->lang['msg'], $name); $response->resource_id = $id; } else { $response->error = sprintf($this->lang['msg_add_failed']); } } return $response; }