function get_ami_deployment_image_rootdevice_identifier($id) { global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_ADMIN; global $event; $rootdevice_identifier_array = array(); $image = new image(); $image_id_list = $image->get_ids(); foreach ($image_id_list as $id => $ikey) { $image_tmp = new image(); $image_tmp->get_instance_by_id($id); if ($image_tmp->type === "ami-deployment") { $rootdevice_identifier_array[] = array("value" => $image_tmp->rootdevice, "label" => $image_tmp->name); } } return $rootdevice_identifier_array; }
function send_command($resource_ip, $resource_command, $command_timeout = NULL) { global $OPENQRM_EXEC_PORT; global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_EXECUTION_LAYER; global $event; global $RootDir; // here we assume that we are the resource // plugin hook in case a resource gets rebooted or halted switch ($resource_command) { case 'reboot': // start the hook $plugin = new plugin(); $enabled_plugins = $plugin->enabled(); foreach ($enabled_plugins as $index => $plugin_name) { $plugin_start_resource_hook = "{$RootDir}/plugins/{$plugin_name}/openqrm-{$plugin_name}-resource-hook.php"; if (file_exists($plugin_start_resource_hook)) { $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found plugin {$plugin_name} handling start-resource event.", "", "", 0, 0, $this->id); // prepare resource_fields array $resource_fields = array(); $resource_fields = $this->get_fields($this->id); // include the plugin function file and run it require_once "{$plugin_start_resource_hook}"; $resource_function = "openqrm_" . "{$plugin_name}" . "_resource"; $resource_function = str_replace("-", "_", $resource_function); $resource_function("start", $resource_fields); } } // here we check if the resource is virtual or a host and if // the virtualization plugin wants to reboot it via the host $virtualization = new virtualization(); $virtualization->get_instance_by_id($this->vtype); $virtualization_plugin_name = $virtualization->get_plugin_name(); $plugin_resource_virtual_command_hook_vm_type = "{$RootDir}/plugins/{$virtualization_plugin_name}/openqrm-{$virtualization_plugin_name}-resource-virtual-command-hook.php"; // we also give the deployment type a chance to implement virtual commands $deployment_plugin_name = ''; if ($this->imageid != 1) { $image = new image(); $image->get_instance_by_id($this->imageid); $storage = new storage(); $storage->get_instance_by_id($image->storageid); $deployment = new deployment(); $deployment->get_instance_by_id($storage->type); $deployment_plugin_name = $deployment->storagetype; } $plugin_resource_virtual_command_hook = ''; $plugin_resource_virtual_command_hook_image_type = "{$RootDir}/plugins/{$deployment_plugin_name}/openqrm-{$deployment_plugin_name}-resource-virtual-command-hook.php"; // $plugin_resource_virtual_command_hook_image_type = "$RootDir/plugins/sanboot-storage/openqrm-sanboot-storage-resource-virtual-command-hook.php"; if (file_exists($plugin_resource_virtual_command_hook_vm_type)) { $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found virtualization {$virtualization_plugin_name} managing virtual command.", "", "", 0, 0, $this->id); $plugin_resource_virtual_command_hook = $plugin_resource_virtual_command_hook_vm_type; } else { if (file_exists($plugin_resource_virtual_command_hook_image_type)) { // check if IMAGE_VIRTUAL_RESOURCE_COMMAND=true $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found deploymetn {$deployment_plugin_name} managing virtual command.", "", "", 0, 0, $this->id); $image = new image(); $image->get_instance_by_id($this->imageid); $virtual_command_enabled = $image->get_deployment_parameter("IMAGE_VIRTUAL_RESOURCE_COMMAND"); if (!strcmp($virtual_command_enabled, "true")) { $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found IMAGE_VIRTUAL_RESOURCE_COMMAND enabled, using virtual command.", "", "", 0, 0, $this->id); $plugin_resource_virtual_command_hook = $plugin_resource_virtual_command_hook_image_type; $virtualization_plugin_name = $deployment_plugin_name; } else { $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found IMAGE_VIRTUAL_RESOURCE_COMMAND disabled, using regular command.", "", "", 0, 0, $this->id); } } } if (strlen($plugin_resource_virtual_command_hook)) { $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found plugin {$virtualization_plugin_name} virtually handling the reboot command.", "", "", 0, 0, $this->id); // prepare resource_fields array $resource_fields = array(); $resource_fields = $this->get_fields($this->id); // include the plugin function file and run it require_once "{$plugin_resource_virtual_command_hook}"; $resource_function = "openqrm_" . "{$virtualization_plugin_name}" . "_resource_virtual_command"; $resource_function = str_replace("-", "_", $resource_function); $resource_function("reboot", $resource_fields); // the virtual reboot function can only be // implemented by a single plugin depending on the // resource type. So we return after that and // do not try to reboot the resource via its ip return; } break; case 'halt': // stop hook $plugin = new plugin(); $enabled_plugins = $plugin->enabled(); foreach ($enabled_plugins as $index => $plugin_name) { $plugin_start_resource_hook = "{$RootDir}/plugins/{$plugin_name}/openqrm-{$plugin_name}-resource-hook.php"; if (file_exists($plugin_start_resource_hook)) { $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found plugin {$plugin_name} handling start-resource event.", "", "", 0, 0, $this->id); // prepare resource_fields array $resource_fields = array(); $resource_fields = $this->get_fields($this->id); // include the plugin function file and run it require_once "{$plugin_start_resource_hook}"; $resource_function = "openqrm_" . "{$plugin_name}" . "_resource"; $resource_function = str_replace("-", "_", $resource_function); $resource_function("stop", $resource_fields); } } // here we check if the resource is virtual or a host and if // the virtualization plugin wants to reboot it via the host $virtualization = new virtualization(); $virtualization->get_instance_by_id($this->vtype); $virtualization_plugin_name = $virtualization->get_plugin_name(); $plugin_resource_virtual_command_hook_vm_type = "{$RootDir}/plugins/{$virtualization_plugin_name}/openqrm-{$virtualization_plugin_name}-resource-virtual-command-hook.php"; // we also give the deployment type a chance to implement virtual commands $deployment_plugin_name = ''; if ($this->imageid != 1) { $image = new image(); $image->get_instance_by_id($this->imageid); $storage = new storage(); $storage->get_instance_by_id($image->storageid); $deployment = new deployment(); $deployment->get_instance_by_id($storage->type); $deployment_plugin_name = $deployment->storagetype; } $plugin_resource_virtual_command_hook = ''; $plugin_resource_virtual_command_hook_image_type = "{$RootDir}/plugins/{$deployment_plugin_name}/openqrm-{$deployment_plugin_name}-resource-virtual-command-hook.php"; if (file_exists($plugin_resource_virtual_command_hook_vm_type)) { $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found virtualization {$virtualization_plugin_name} managing virtual command.", "", "", 0, 0, $this->id); $plugin_resource_virtual_command_hook = $plugin_resource_virtual_command_hook_vm_type; } else { if (file_exists($plugin_resource_virtual_command_hook_image_type)) { // check if IMAGE_VIRTUAL_RESOURCE_COMMAND=true $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found deploymetn {$deployment_plugin_name} managing virtual command.", "", "", 0, 0, $this->id); $image = new image(); $image->get_instance_by_id($this->imageid); $virtual_command_enabled = $image->get_deployment_parameter("IMAGE_VIRTUAL_RESOURCE_COMMAND"); if (!strcmp($virtual_command_enabled, "true")) { $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found IMAGE_VIRTUAL_RESOURCE_COMMAND enabled, using virtual command.", "", "", 0, 0, $this->id); $plugin_resource_virtual_command_hook = $plugin_resource_virtual_command_hook_image_type; $virtualization_plugin_name = $deployment_plugin_name; } else { $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found IMAGE_VIRTUAL_RESOURCE_COMMAND disabled, using regular command.", "", "", 0, 0, $this->id); } } } if (strlen($plugin_resource_virtual_command_hook)) { $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found plugin {$virtualization_plugin_name} virtually handling the reboot command.", "", "", 0, 0, $this->id); // prepare resource_fields array $resource_fields = array(); $resource_fields = $this->get_fields($this->id); // include the plugin function file and run it require_once "{$plugin_resource_virtual_command_hook}"; $resource_function = "openqrm_" . "{$virtualization_plugin_name}" . "_resource_virtual_command"; $resource_function = str_replace("-", "_", $resource_function); $resource_function("halt", $resource_fields); // the virtual halt function can only be // implemented by a single plugin depending on the // resource type. So we return after that and // do not try to halt the resource via its ip return; } break; } // check which execution layer to use switch ($OPENQRM_EXECUTION_LAYER) { case 'dropbear': // generate a random token for the cmd $cmd_token = md5(uniqid(rand(), true)); // custom timeout ? if (!is_null($command_timeout)) { $cmd_token .= "." . $command_timeout; } $final_resource_command = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/sbin/openqrm-exec -i {$resource_ip} -t {$cmd_token} -c \"{$resource_command}\""; //$event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Running : $final_resource_command", "", "", 0, 0, 0); shell_exec($final_resource_command); break; case 'openqrm-execd': $fp = fsockopen($resource_ip, $OPENQRM_EXEC_PORT, $errno, $errstr, 30); if (!$fp) { $event->log("send_command", $_SERVER['REQUEST_TIME'], 2, "resource.class.php", "Could not send the command to resource {$resource_ip}", "", "", 0, 0, 0); $event->log("send_command", $_SERVER['REQUEST_TIME'], 2, "resource.class.php", "{$errstr} ({$errno})", "", "", 0, 0, 0); } else { // $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Running : $resource_command", "", "", 0, 0, $resource->id); fputs($fp, "{$resource_command}"); fclose($fp); } break; } }
function get_response() { $response = $this->response; $form = $response->get_form($this->actions_name, 'install2'); $id = $this->response->html->request()->get('image_id'); $install_server = $this->response->html->request()->get('install_server'); $submit = $form->get_elements('submit'); $submit->handler = 'onclick="wait();"'; $form->add($submit, 'submit'); $submit = $form->get_elements('cancel'); $submit->handler = 'onclick="cancel();"'; $form->add($submit, 'cancel'); $image = new image(); $image->get_instance_by_id($id); $storage = new storage(); $storage->get_instance_by_id($install_server); $deployment = new deployment(); $deployment->get_instance_by_id($storage->type); // require template-deployment file $local_deployment_templates_identifier_hook = $this->openqrm->get('basedir') . "/web/boot-service/template." . $deployment->type . ".php"; if ($this->file->exists($local_deployment_templates_identifier_hook)) { require_once "{$local_deployment_templates_identifier_hook}"; $get_deployment_templates_function = "get_" . "{$deployment->type}" . "_templates"; $get_deployment_templates_function = str_replace("-", "_", $get_deployment_templates_function); $local_deployment_templates_arr = $get_deployment_templates_function($install_server); // get additional optional local-deployment parameters from the template hook $get_additional_parameters_function = "get_" . "{$deployment->type}" . "_additional_parameters"; $get_additional_parameters_function = str_replace("-", "_", $get_additional_parameters_function); $additional_local_deployment_parameter = $get_additional_parameters_function(); } // persistent deployment ? $local_deployment_persistent_arr = array(); $local_deployment_persistent_arr[] = array("value" => "0", "label" => "First boot"); $local_deployment_persistent_arr[] = array("value" => "1", "label" => "Persistent"); $d['template']['label'] = $this->lang['form_install_template']; $d['template']['required'] = false; $d['template']['object']['type'] = 'htmlobject_select'; $d['template']['object']['attrib']['index'] = array('value', 'label'); $d['template']['object']['attrib']['id'] = 'template'; $d['template']['object']['attrib']['name'] = 'template'; $d['template']['object']['attrib']['options'] = $local_deployment_templates_arr; $d['persistent']['label'] = $this->lang['form_install_persistent']; $d['persistent']['required'] = false; $d['persistent']['object']['type'] = 'htmlobject_select'; $d['persistent']['object']['attrib']['index'] = array('value', 'label'); $d['persistent']['object']['attrib']['id'] = 'persistent'; $d['persistent']['object']['attrib']['name'] = 'persistent'; $d['persistent']['object']['attrib']['options'] = $local_deployment_persistent_arr; $n = 1; foreach ($additional_local_deployment_parameter as $paramters) { if (!isset($paramters['label'])) { continue; } $d['parameter' . $n]['label'] = $paramters['label']; $d['parameter' . $n]['required'] = false; $d['parameter' . $n]['object']['type'] = 'htmlobject_input'; $d['parameter' . $n]['object']['attrib']['id'] = 'parameter' . $n; $d['parameter' . $n]['object']['attrib']['name'] = 'parameter' . $n; $n++; } for ($j = $n; $j <= 4; $j++) { $d['parameter' . $j] = ''; } $form->add($d); $response->form = $form; return $response; }
function imtarget() { $h = array(); $h['image_icon']['title'] = ' '; $h['image_icon']['sortable'] = false; $h['image_id']['title'] = $this->lang['table_id']; $h['image_name']['title'] = $this->lang['table_name']; $h['image_version']['title'] = $this->lang['table_version']; $h['image_type']['title'] = $this->lang['table_deployment']; $h['image_isactive']['title'] = $this->lang['table_isactive']; $h['image_comment']['title'] = $this->lang['table_comment']; $h['image_comment']['sortable'] = false; $h['edit']['title'] = ' '; $h['edit']['sortable'] = false; $image = new image(); $params = $this->response->get_array($this->actions_name, 'target'); $b = array(); $table = $this->response->html->tablebuilder('hybridcloud_imtarget', $params); $table->offset = 0; $table->sort = 'image_id'; $table->limit = 10; $table->order = 'ASC'; $table->max = $image->get_count(); $table->init(); $image_arr = $image->display_overview(0, 10000, $table->sort, $table->order); $image_icon = "/openqrm/base/img/image.png"; foreach ($image_arr as $index => $image_db) { // prepare the values for the array $image = new image(); $image->get_instance_by_id($image_db["image_id"]); if ($image->type === 'lvm-nfs-deployment' || $image->type === 'nfs-deployment') { $image_comment = $image_db["image_comment"]; if (!strlen($image_comment)) { $image_comment = "-"; } $image_version = $image_db["image_version"]; if (!strlen($image_version)) { $image_version = " "; } // edit $a = $this->response->html->a(); $a->title = $this->lang['action_import']; $a->label = $this->lang['action_import']; $a->handler = 'onclick="wait();"'; $a->css = 'edit'; $a->href = $this->response->get_url($this->actions_name, 'imparams') . '&image_id=' . $image->id; $image_edit = $a->get_string(); // set the active icon $isactive_icon = "/openqrm/base/img/enable.png"; if ($image_db["image_isactive"] == 1) { $isactive_icon = "/openqrm/base/img/disable.png"; } $image_isactive_icon = "<img src=" . $isactive_icon . " width='24' height='24' alt='State'>"; $b[] = array('image_icon' => "<img width='24' height='24' src='" . $image_icon . "'>", 'image_id' => $image_db["image_id"], 'image_name' => $image_db["image_name"], 'image_version' => $image_version, 'image_type' => $image_db["image_type"], 'image_isactive' => $image_isactive_icon, 'image_comment' => $image_comment, 'edit' => $image_edit); } } $table->id = 'Tabelle'; $table->css = 'htmlobject_table'; $table->border = 1; $table->cellspacing = 0; $table->cellpadding = 3; $table->autosort = false; $table->sort_link = false; $table->max = count($b); $table->head = $h; $table->body = $b; $table->limit_select = array(array("value" => 10, "text" => 10), array("value" => 20, "text" => 20), array("value" => 30, "text" => 30), array("value" => 50, "text" => 50), array("value" => 100, "text" => 100)); require_once $this->openqrm->get('basedir') . '/plugins/hybrid-cloud/web/class/hybrid-cloud.class.php'; $hc = new hybrid_cloud(); $hc->get_instance_by_id($this->id); $d['name'] = $hc->account_name; $d['form'] = $this->response->get_form($this->actions_name, 'target', false)->get_elements(); $d['table'] = $table; return $d; }
function select() { $head['cr_status']['title'] = $this->lang['cloud_ui_request_status']; $head['cr_id']['title'] = $this->lang['cloud_ui_request_id']; $head['cr_id']['hidden'] = true; $head['cr_appliance_hostname']['title'] = $this->lang['cloud_ui_request_appliance_name']; $head['cr_appliance_hostname']['hidden'] = true; #$head['cr_start']['title'] = $this->lang['cloud_ui_request_start']; #$head['cr_stop']['title'] = $this->lang['cloud_ui_request_stop']; #$head['cr_kernel_id']['title'] = $this->lang['cloud_ui_request_os']; #$head['cr_image_id']['title'] = $this->lang['cloud_ui_request_template']; #$head['cr_resource_type_req']['title'] = $this->lang['cloud_ui_request_system_type']; $head['cr_req']['title'] = ' '; $head['cr_req']['sortable'] = false; #$head['cr_appliance_id']['title'] = $this->lang['cloud_ui_request_appliance_id']; $head['cr_details']['title'] = $this->lang['cloud_ui_request_details']; $head['cr_details']['sortable'] = false; $table = $this->response->html->tablebuilder('cloud_request_table', $this->response->get_array($this->actions_name, 'requests')); $table->css = 'htmlobject_table'; $table->border = 0; $table->id = 'cloud_requests'; $table->head = $head; $table->sort = 'cr_id'; $table->order = 'DESC'; $table->limit = 5; $table->sort_link = false; $table->autosort = false; $table->max = $this->cloudrequest->get_count_per_user($this->clouduser->id); $table->identifier = 'cr_id'; $table->identifier_name = $this->identifier_name; $table->actions = array('deprovision'); $table->actions_name = $this->actions_name; $table->form_action = $this->response->html->thisfile; $table->limit_select = array(array("value" => 5, "text" => 5), array("value" => 10, "text" => 10), array("value" => 20, "text" => 20), array("value" => 30, "text" => 30), array("value" => 40, "text" => 40), array("value" => 50, "text" => 50)); // $table->form_action = $this->response->html->thisfile; $table->init(); $kernel = new kernel(); $image = new image(); $virtualization = new virtualization(); $cloud_request_array = $this->cloudrequest->display_overview_per_user($this->clouduser->id, $table->offset, $table->limit, $table->sort, $table->order); $ta = ''; foreach ($cloud_request_array as $index => $cz) { $this->cloudrequest->get_instance_by_id($cz['cr_id']); $cr_status = $this->cloudrequest->getstatus($this->cloudrequest->id); if (isset($this->cloudrequest->kernel_id)) { $kernel->get_instance_by_id($this->cloudrequest->kernel_id); } if (isset($this->cloudrequest->image_id)) { $image->get_instance_by_id($this->cloudrequest->image_id); } if (isset($this->cloudrequest->resource_type_req)) { $virtualization->get_instance_by_id($this->cloudrequest->resource_type_req); } #echo '<pre>'; #print_r($this->cloudrequest); #echo '</pre>'; // hostname $appliance_hostname = '-'; if (strlen($this->cloudrequest->appliance_hostname)) { $appliance_hostname = $this->cloudrequest->appliance_hostname; } $cr_req = '<b>' . $this->lang['cloud_ui_request_id'] . '</b>: ' . $this->cloudrequest->id . "<br>"; $cr_req .= '<b>' . $this->lang['cloud_ui_request_appliance_name'] . '</b>: ' . $appliance_hostname . "<br>"; $cr_req .= '<b>' . $this->lang['cloud_ui_request_start'] . '</b>: ' . date("d-m-Y H:i", $this->cloudrequest->start) . "<br>"; $cr_req .= '<b>' . $this->lang['cloud_ui_request_stop'] . '</b>: ' . date("d-m-Y H:i", $this->cloudrequest->stop) . "<br>"; $cr_req .= '<b>' . $this->lang['cloud_ui_request_cpu'] . '</b>: ' . $this->cloudrequest->cpu_req . "<br>"; $cr_req .= '<b>' . $this->lang['cloud_ui_request_memory'] . '</b>: ' . $this->cloudrequest->ram_req . " MB<br>"; $cr_req .= '<b>' . $this->lang['cloud_ui_request_disk'] . '</b>: ' . $this->cloudrequest->disk_req . " MB<br>"; $cr_req .= '<b>' . $this->lang['cloud_ui_request_network'] . '</b>: ' . $this->cloudrequest->network_req . "<br>"; // details action $a = $this->response->html->a(); $a->title = $this->lang['cloud_ui_request_components_details']; $a->label = $this->lang['cloud_ui_request_components_details']; $a->handler = 'onclick="javascript:cloudopenPopup(' . $this->cloudrequest->id . ');"'; $a->css = 'edit'; $a->href = '#'; $ta[] = array('cr_status' => $cr_status, 'cr_id' => $this->cloudrequest->id, 'cr_appliance_hostname' => $appliance_hostname, 'cr_req' => $cr_req, 'cr_details' => $a->get_string()); } $table->body = $ta; return $table; }
function remove_nfs_deployment($cloud_image_id) { global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; global $RESOURCE_INFO_TABLE; global $event; $event->log("remove_nfs_deployment", $_SERVER['REQUEST_TIME'], 5, "nfs-deployment-cloud-hook", "Removing image on storage", "", "", 0, 0, 0); $cloudimage = new cloudimage(); $cloudimage->get_instance_by_id($cloud_image_id); // get image $image = new image(); $image->get_instance_by_id($cloudimage->image_id); $image_id = $image->id; $image_name = $image->name; $image_type = $image->type; $image_version = $image->version; $image_rootdevice = $image->rootdevice; $image_rootfstype = $image->rootfstype; $image_storageid = $image->storageid; $image_isshared = $image->isshared; $image_comment = $image->comment; $image_capabilities = $image->capabilities; $image_deployment_parameter = $image->deployment_parameter; // get image storage $storage = new storage(); $storage->get_instance_by_id($image_storageid); $storage_resource_id = $storage->resource_id; // get storage resource $resource = new resource(); $resource->get_instance_by_id($storage_resource_id); $resource_id = $resource->id; $resource_ip = $resource->ip; // nfs-storage $image_location_name = basename($image_rootdevice); $image_remove_clone_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/nfs-storage/bin/openqrm-nfs-storage remove -n " . $image_location_name . " --openqrm-cmd-mode background"; $event->log("remove_nfs_deployment", $_SERVER['REQUEST_TIME'], 5, "nfs-deployment-cloud-hook", "Running : {$image_remove_clone_cmd}", "", "", 0, 0, 0); $resource->send_command($resource_ip, $image_remove_clone_cmd); }
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; }
function create_private_template_deployment($cloud_image_id, $private_disk, $private_image_name) { global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; global $RESOURCE_INFO_TABLE; global $event; $event->log("create_private_template_deployment", $_SERVER['REQUEST_TIME'], 5, "template_deployment-cloud-hook", "Creating private image on storage", "", "", 0, 0, 0); // we got the cloudimage id here, get the image out of it $cloudimage = new cloudimage(); $cloudimage->get_instance_by_id($cloud_image_id); // get image $image = new image(); $image->get_instance_by_id($cloudimage->image_id); $image_id = $image->id; $image_name = $image->name; $image_type = $image->type; $image_version = $image->version; $image_rootdevice = $image->rootdevice; $image_rootfstype = $image->rootfstype; $image_storageid = $image->storageid; $image_isshared = $image->isshared; $image_comment = $image->comment; $image_capabilities = $image->capabilities; $image_deployment_parameter = $image->deployment_parameter; // get image storage $storage = new storage(); $storage->get_instance_by_id($image_storageid); $storage_resource_id = $storage->resource_id; // get storage resource $resource = new resource(); $resource->get_instance_by_id($storage_resource_id); $resource_id = $resource->id; $resource_ip = $resource->ip; // create an admin user to post when cloning has finished $openqrm_admin_user = new user("openqrm"); $openqrm_admin_user->set_user(); // parse the volume group info in the identifier $volume_group_location = dirname($image_rootdevice); $volume_group = basename($volume_group_location); $image_location_name = basename($image_rootdevice); // For template vms we assume that the image is located on the vm-host // so we send the auth command to the vm-host instead of the image storage. // This enables using a SAN backend with dedicated volumes per vm-host which all // contain all "golden-images" which are used for snapshotting. // We do this to overcome the current lvm limitation of not supporting cluster-wide snapshots // // Still we need to send the remove command to the storage resource since the // create-phase automatically adapted the image->storageid, we cannot use the vm-resource here // because cloudimage->resource_id will be set to -1 when the cloudapp is in paused/resize/private state // if ($cloudimage->resource_id > 0) { $vm_resource = new resource(); $vm_resource->get_instance_by_id($cloudimage->resource_id); // get the openvz host $vm_host_resource = new resource(); $vm_host_resource->get_instance_by_id($vm_resource->vhostid); // san backend ? if ($vm_host_resource->id != $resource->id) { $event->log("create_private_template_deployment", $_SERVER['REQUEST_TIME'], 5, "template_deployment-cloud-hook", "Image {$image_id} IS NOT available on this template host, {$resource->id} not equal {$vm_host_resource->id} !! Assuming SAN Backend", "", "", 0, 0, $appliance_id); } else { $event->log("create_private_template_deployment", $_SERVER['REQUEST_TIME'], 5, "template_deployment-cloud-hook", "Image {$image_id} IS available on this template host, {$resource->id} equal {$vm_host_resource->id}", "", "", 0, 0, $appliance_id); } } $image_resize_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/template/bin/openqrm-template clone -n " . $image_location_name . " -s " . $private_image_name . " -v " . $volume_group . " -m " . $private_disk . " -u " . $openqrm_admin_user->name . " -p " . $openqrm_admin_user->password . " --openqrm-cmd-mode background"; $event->log("create_private_template_deployment", $_SERVER['REQUEST_TIME'], 5, "template_deployment-cloud-hook", "Running : {$image_resize_cmd}", "", "", 0, 0, 0); $resource->send_command($resource_ip, $image_resize_cmd); // set the storage specific image root_device parameter $new_rootdevice = str_replace($image_location_name, $private_image_name, $image->rootdevice); return $new_rootdevice; }
function openqrm_kvm_appliance($cmd, $appliance_fields) { global $event; global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; $appliance_id = $appliance_fields["appliance_id"]; $appliance_name = $appliance_fields["appliance_name"]; $resource = new resource(); $resource->get_instance_by_id($appliance_fields["appliance_resources"]); $appliance_ip = $resource->ip; $appliance = new appliance(); $appliance->get_instance_by_id($appliance_id); // check appliance values, maybe we are in update and they are incomplete if ($appliance->imageid == 1) { return; } if ($resource->id == "-1" || $resource->id == "" || !isset($resource->vtype)) { return; } $openqrm_admin_user = new user("openqrm"); $openqrm_admin_user->set_user(); $event->log("openqrm_kvm_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-appliance-hook.php", "Handling {$cmd} event {$appliance_id}/{$appliance_name}/{$appliance_ip}", "", "", 0, 0, $appliance_id); switch ($cmd) { case "start": // send command to assign image and start vm // NOTICE : please enable this hook only if you are using the ip-mgmt plugin with vlans // check if resource type -> kvm-vm-net // $virtualization = new virtualization(); // $virtualization->get_instance_by_type("kvm-vm-net"); // $kvm_host_resource = new resource(); // $kvm_host_resource->get_instance_by_id($resource->vhostid); // if ($resource->vtype != $virtualization->id) { // $kvm_command="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/kvm/bin/openqrm-kvm-vm reset_vlans_by_mac -b start -m $resource->mac"; // $kvm_host_resource->send_command($kvm_host_resource->ip, $kvm_command); // return; // } // check resource type -> kvm-vm-local $virtualization = new virtualization(); $virtualization->get_instance_by_type("kvm-vm-local"); if ($resource->vtype != $virtualization->id) { $event->log("openqrm_kvm_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-appliance-hook.php", "{$appliance_id} is not from type kvm-vm, skipping .. {$appliance_name}/{$appliance_ip}", "", "", 0, 0, $appliance_id); return; } // check image is on the same storage server // get the kvm host resource $kvm_host_resource = new resource(); $kvm_host_resource->get_instance_by_id($resource->vhostid); // get the kvm resource $image = new image(); $image->get_instance_by_id($appliance->imageid); $storage = new storage(); $storage->get_instance_by_id($image->storageid); $kvm_resource = new resource(); $kvm_resource->get_instance_by_id($storage->resource_id); if ($kvm_host_resource->id != $kvm_resource->id) { $event->log("openqrm_kvm_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-appliance-hook.php", "Appliance {$appliance_id} image is not available on this kvm host. Assuming SAN-Backend", "", "", 0, 0, $appliance_id); } $kvm_command = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/kvm/bin/openqrm-kvm-vm start_by_mac -m " . $resource->mac . " -d " . $image->rootdevice . " -u " . $openqrm_admin_user->name . " -p " . $openqrm_admin_user->password; $kvm_host_resource->send_command($kvm_host_resource->ip, $kvm_command); break; case "stop": // send command to stop the vm and deassign image // NOTICE : please enable this hook only if you are using the ip-mgmt plugin with vlans // check if resource type -> kvm-vm-net // $virtualization = new virtualization(); // $virtualization->get_instance_by_type("kvm-vm-net"); // $kvm_host_resource = new resource(); // $kvm_host_resource->get_instance_by_id($resource->vhostid); // if ($resource->vtype != $virtualization->id) { // $kvm_command="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/kvm/bin/openqrm-kvm reset_vlans_by_mac -b stop -m $resource->mac"; // $kvm_host_resource->send_command($kvm_host_resource->ip, $kvm_command); // return; // } // check resource type -> kvm-vm-local $virtualization = new virtualization(); $virtualization->get_instance_by_type("kvm-vm-local"); if ($resource->vtype != $virtualization->id) { $event->log("openqrm_kvm_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-appliance-hook.php", "{$appliance_id} is not from type kvm-vm, skipping .. {$appliance_name}/{$appliance_ip}", "", "", 0, 0, $appliance_id); return; } // check image is on the same storage server // get the kvm host resource $kvm_host_resource = new resource(); $kvm_host_resource->get_instance_by_id($resource->vhostid); // get the kvm resource $image = new image(); $image->get_instance_by_id($appliance->imageid); $storage = new storage(); $storage->get_instance_by_id($image->storageid); $kvm_resource = new resource(); $kvm_resource->get_instance_by_id($storage->resource_id); if ($kvm_host_resource->id != $kvm_resource->id) { $event->log("openqrm_kvm_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-appliance-hook.php", "Appliance {$appliance_id} image is not available on this kvm host. Assuming SAN-Backend", "", "", 0, 0, $appliance_id); } $kvm_command = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/kvm/bin/openqrm-kvm-vm restart_by_mac -m " . $resource->mac . " -u " . $openqrm_admin_user->name . " -p " . $openqrm_admin_user->password . " --openqrm-cmd-mode background"; $kvm_host_resource->send_command($kvm_host_resource->ip, $kvm_command); break; case "update": // check if the appliance was set to a kvm Host, if yes, auto-create the storage objects $virtualization = new virtualization(); $virtualization->get_instance_by_type("kvm"); if ($appliance->virtualization == $virtualization->id) { // KVM LVM Storage $deployment = new deployment(); $deployment->get_instance_by_name('kvm-lvm-deployment'); $storage = new storage(); $kvm_id_list = $storage->get_ids_by_storage_type($deployment->id); $found_kvm = false; $found_kvm_id = -1; foreach ($kvm_id_list as $list) { foreach ($list as $kvm_id) { $storage->get_instance_by_id($kvm_id); if ($storage->resource_id == $appliance->resources) { $found_kvm = true; $found_kvm_id = $storage->id; break; } } } if (!$found_kvm) { $found_kvm_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $storage_fields['storage_id'] = $found_kvm_id; $storage_fields['storage_name'] = $appliance->name . "-lvm"; $storage_fields['storage_type'] = $deployment->id; $storage_fields['storage_comment'] = 'KVM LVM Storage Object for Appliance ' . $appliance->name; $storage_fields['storage_resource_id'] = $appliance->resources; $storage_fields['storage_capabilities'] = ''; $storage->add($storage_fields); $event->log("openqrm_kvm_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-appliance-hook.php", "Created KVM LVM Storage Object for Appliance " . $appliance_id . "!", "", "", 0, 0, $appliance_id); } else { $event->log("openqrm_kvm_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-appliance-hook.php", "KVM LVM Storage Object for Appliance " . $appliance_id . " already existing.", "", "", 0, 0, $appliance_id); } // KVM Blockfile Storage $deployment = new deployment(); $deployment->get_instance_by_name('kvm-bf-deployment'); $storage = new storage(); $kvm_id_list = $storage->get_ids_by_storage_type($deployment->id); $found_kvm = false; $found_kvm_id = -1; foreach ($kvm_id_list as $list) { foreach ($list as $kvm_id) { $storage->get_instance_by_id($kvm_id); if ($storage->resource_id == $appliance->resources) { $found_kvm = true; $found_kvm_id = $storage->id; break; } } } if (!$found_kvm) { $found_kvm_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $storage_fields['storage_id'] = $found_kvm_id; $storage_fields['storage_name'] = $appliance->name . "-bf"; $storage_fields['storage_type'] = $deployment->id; $storage_fields['storage_comment'] = 'KVM Blockfile Storage Object for Appliance ' . $appliance->name; $storage_fields['storage_resource_id'] = $appliance->resources; $storage_fields['storage_capabilities'] = ''; $storage->add($storage_fields); $event->log("openqrm_kvm_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-appliance-hook.php", "Created KVM Blockfile Storage Object for Appliance " . $appliance_id . "!", "", "", 0, 0, $appliance_id); } else { $event->log("openqrm_kvm_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-appliance-hook.php", "KVM Blockfile Storage Object for Appliance " . $appliance_id . " already existing.", "", "", 0, 0, $appliance_id); } } break; } }
function get_response() { $size_select_arr = array(); $availability_zones_select_arr = array(); $security_group_select_arr = array(); $subnet_select_arr = array(); $keypair_select_arr = array(); // get the datastore and vswitchlist for the selects if (file_exists($this->statfile)) { $lines = explode("\n", file_get_contents($this->statfile)); if (count($lines) >= 1) { foreach ($lines as $line) { if ($line !== '') { $line = explode('@', $line); switch ($line[0]) { case 'SIZES': $size_select_arr[] = array($line[1], $line[1]); break; case 'KEYPAIR': $keypair_select_arr[] = array($line[1], $line[1]); break; case 'GROUP': $security_group_select_arr[] = array($line[1], $line[1]); break; case 'ZONES': $availability_zones_select_arr[] = array($line[1], $line[1]); break; case 'SUBNET': $subnet_select_arr[] = array($line[1], $line[2] . " (" . $line[3] . ")"); break; } } } } } if ($this->hc->account_type == 'aws' || $this->hc->account_type == 'euca') { $instance_types[] = array("t1.micro", "t1.micro"); $instance_types[] = array("m1.small", "m1.small"); $instance_types[] = array("m1.medium", "m1.medium"); $instance_types[] = array("m1.large", "m1.large"); $instance_types[] = array("m1.xlarge", "m1.xlarge"); $instance_types[] = array("m3.xlarge", "m3.xlarge"); $instance_types[] = array("m3.2xlarge", "m3.2xlarge"); $instance_types[] = array("c1.medium", "c1.medium"); $instance_types[] = array("c1.xlarge", "c1.xlarge"); $instance_types[] = array("m2.xlarge", "m2.xlarge"); $instance_types[] = array("m2.2xlarge", "m2.2xlarge"); $instance_types[] = array("m2.4xlarge", "m2.4xlarge"); $instance_types[] = array("cr1.8xlarge", "cr1.8xlarge"); $instance_types[] = array("hi1.4xlarge", "hi1.4xlarge"); $instance_types[] = array("hs1.8xlarge", "hs1.8xlarge"); $instance_types[] = array("cc1.4xlarge", "cc1.4xlarge"); $instance_types[] = array("cc2.8xlarge", "cc2.8xlarge"); $instance_types[] = array("cg1.4xlarge", "cg1.4xlarge"); $instance_type_selected = "t1.micro"; } if ($this->hc->account_type == 'lc-openstack') { $instance_types = $size_select_arr; $instance_type_selected = "m1.nano"; } // AMIs $ami_select_arr = array(); $image = new image(); $image_id_list = $image->get_ids(); foreach ($image_id_list as $id => $ikey) { $image_tmp = new image(); $image_tmp->get_instance_by_id($ikey['image_id']); if ($image_tmp->type === "ami-deployment") { $ami_select_arr[] = array($image_tmp->id, $image_tmp->comment); } } $response = $this->response; $form = $response->get_form($this->actions_name, 'add'); $submit = $form->get_elements('submit'); $submit->handler = 'onclick="wait();"'; $form->add($submit, 'submit'); $submit = $form->get_elements('cancel'); $submit->handler = 'onclick="cancel();"'; $form->add($submit, 'cancel'); if ($this->hc->account_type == 'lc-openstack') { $d['name']['label'] = $this->lang['form_name']; $d['name']['required'] = false; $d['name']['validate']['regex'] = '/^[a-z0-9._:\\/-]+$/i'; $d['name']['validate']['errormsg'] = sprintf($this->lang['error_name'], 'a-z0-9._:\\/-'); $d['name']['object']['type'] = 'htmlobject_input'; $d['name']['object']['attrib']['id'] = 'name'; $d['name']['object']['attrib']['name'] = 'name'; $d['name']['object']['attrib']['type'] = 'text'; $d['name']['object']['attrib']['value'] = ''; $d['name']['object']['attrib']['maxlength'] = 255; } if ($this->hc->account_type == 'aws' || $this->hc->account_type == 'euca') { $d['name'] = ''; } $d['instance_type']['label'] = $this->lang['form_instance_type']; $d['instance_type']['required'] = true; $d['instance_type']['object']['type'] = 'htmlobject_select'; $d['instance_type']['object']['attrib']['name'] = 'type'; $d['instance_type']['object']['attrib']['index'] = array(0, 1); $d['instance_type']['object']['attrib']['options'] = $instance_types; $d['instance_type']['object']['attrib']['selected'] = array($instance_type_selected); $d['ami']['label'] = $this->lang['form_ami']; $d['ami']['required'] = true; $d['ami']['object']['type'] = 'htmlobject_select'; $d['ami']['object']['attrib']['name'] = 'ami_image_id'; $d['ami']['object']['attrib']['index'] = array(0, 1); $d['ami']['object']['attrib']['options'] = $ami_select_arr; $a = $this->response->html->a(); $a->label = $this->lang['form_add_volume']; $a->handler = 'onclick="wait();"'; $a->css = 'add'; $a->href = 'index.php?plugin=hybrid-cloud&controller=hybrid-cloud-ami&hybrid_cloud_id=' . $this->id; $d['add_image'] = $a->get_string(); if ($this->hc->account_type == 'aws' || $this->hc->account_type == 'euca') { $d['availability_zone']['label'] = $this->lang['form_availability_zone']; $d['availability_zone']['required'] = true; $d['availability_zone']['object']['type'] = 'htmlobject_select'; $d['availability_zone']['object']['attrib']['name'] = 'availability_zone'; $d['availability_zone']['object']['attrib']['index'] = array(0, 1); $d['availability_zone']['object']['attrib']['options'] = $availability_zones_select_arr; } if ($this->hc->account_type == 'lc-openstack') { $d['availability_zone'] = ''; } if ($this->hc->account_type == 'aws') { $d['group'] = ''; $d['subnet']['label'] = $this->lang['form_subnet']; $d['subnet']['required'] = true; $d['subnet']['object']['type'] = 'htmlobject_select'; $d['subnet']['object']['attrib']['name'] = 'subnet'; $d['subnet']['object']['attrib']['index'] = array(0, 1); $d['subnet']['object']['attrib']['options'] = $subnet_select_arr; } else { $d['subnet'] = ''; $d['group']['label'] = $this->lang['form_security_group']; $d['group']['required'] = true; $d['group']['object']['type'] = 'htmlobject_select'; $d['group']['object']['attrib']['name'] = 'group'; $d['group']['object']['attrib']['index'] = array(0, 1); $d['group']['object']['attrib']['options'] = $security_group_select_arr; $d['group']['object']['attrib']['selected'] = array('default'); } $d['keypair']['label'] = $this->lang['form_keypair']; $d['keypair']['required'] = true; $d['keypair']['object']['type'] = 'htmlobject_select'; $d['keypair']['object']['attrib']['name'] = 'keypair'; $d['keypair']['object']['attrib']['index'] = array(0, 1); $d['keypair']['object']['attrib']['options'] = $keypair_select_arr; $d['custom_script']['label'] = $this->lang['form_custom_script']; $d['custom_script']['required'] = false; $d['custom_script']['validate']['regex'] = '/^[a-z0-9._:\\/-]+$/i'; $d['custom_script']['validate']['errormsg'] = sprintf($this->lang['error_name'], 'a-z0-9._:\\/-'); $d['custom_script']['object']['type'] = 'htmlobject_input'; $d['custom_script']['object']['attrib']['id'] = 'custom_script'; $d['custom_script']['object']['attrib']['name'] = 'custom_script'; $d['custom_script']['object']['attrib']['type'] = 'text'; $d['custom_script']['object']['attrib']['value'] = ''; $d['custom_script']['object']['attrib']['title'] = $this->lang['form_custom_script_title']; $d['custom_script']['object']['attrib']['maxlength'] = 255; $form->add($d); $response->form = $form; return $response; }
function select() { $h = array(); $h['appliance_state']['title'] = ' '; $h['appliance_state']['sortable'] = false; /* $h['appliance_icon']['title'] =' '; $h['appliance_icon']['sortable'] = false; */ $h['appliance_id']['title'] = $this->lang['table_id']; $h['appliance_name']['title'] = $this->lang['table_name']; $h['appliance_values']['title'] = ' '; $h['appliance_values']['sortable'] = false; $h['login']['title'] = ' '; $h['login']['sortable'] = false; // use the appliance link hook require_once $this->openqrm->get('webdir') . "/plugins/sshterm/openqrm-sshterm-appliance-link-hook.php"; $appliance = new appliance(); $params = $this->response->get_array($this->actions_name, 'select'); $b = array(); $table = $this->response->html->tablebuilder('login', $params); $table->offset = 0; $table->sort = 'appliance_id'; $table->limit = 10; $table->order = 'ASC'; $table->init(); $appliances = $appliance->display_overview($table->offset, 100000, $table->sort, $table->order); foreach ($appliances as $index => $appliance_db) { $appliance = new appliance(); $appliance->get_instance_by_id($appliance_db["appliance_id"]); $resource = new resource(); $resource->get_instance_by_id($appliance->resources); $kernel = new kernel(); $kernel->get_instance_by_id($appliance_db["appliance_kernelid"]); $image = new image(); $image->get_instance_by_id($appliance_db["appliance_imageid"]); $virtualization = new virtualization(); $virtualization->get_instance_by_id($appliance_db["appliance_virtualization"]); $resource_icon_default = "/openqrm/base/img/appliance.png"; $active_state_icon = "active"; $inactive_state_icon = "inactive"; $login = ''; if ($appliance->stoptime == 0 || $appliance->resources == 0) { $state_icon = $active_state_icon; // login $login = get_sshterm_appliance_link($appliance->id); if (is_object($login)) { $login = $login->get_string(); } } else { $state_icon = $inactive_state_icon; $login = ''; } $str = '<strong>Kernel:</strong> ' . $kernel->name . '<br> <strong>Image:</strong> ' . $image->name . '<br> <strong>Resource:</strong> ' . $resource->id . " / " . $resource->ip . '<br> <strong>Type:</strong> <span class="pill">' . $virtualization->name . "</span>"; $state_pill = $this->response->html->span(); $state_pill->css = "pill"; $state_pill->add($state_icon); $b[] = array('appliance_state' => $state_pill->get_string(), 'appliance_id' => $appliance->id, 'appliance_name' => $appliance->name, 'appliance_values' => $str, 'login' => $login); } $table->id = 'Tabelle'; $table->css = 'htmlobject_table'; $table->border = 1; $table->cellspacing = 0; $table->cellpadding = 3; $table->form_action = $this->response->html->thisfile; $table->autosort = true; $table->sort_link = false; $table->max = count($b); $table->head = $h; $table->body = $b; $table->limit_select = array(array("value" => 5, "text" => 5), array("value" => 10, "text" => 10), array("value" => 20, "text" => 20), array("value" => 30, "text" => 30), array("value" => 50, "text" => 50), array("value" => 100, "text" => 100)); $d = array(); $d['table'] = $table; return $d; }
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 create_private_kvm_bf_deployment($cloud_image_id, $private_disk, $private_image_name) { global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; global $RESOURCE_INFO_TABLE; global $event; $cloudimage = new cloudimage(); $cloudimage->get_instance_by_id($cloud_image_id); $event->log("create_private_kvm_bf_deployment", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-bf-deployment-cloud-hook.php", "Creating private image " . $cloudimage->image_id . " on storage.", "", "", 0, 0, 0); // get image $image = new image(); $image->get_instance_by_id($cloudimage->image_id); $image_id = $image->id; $image_name = $image->name; $image_type = $image->type; $image_version = $image->version; $image_rootdevice = $image->rootdevice; $image_rootfstype = $image->rootfstype; $imageid = $image->storageid; $image_isshared = $image->isshared; $image_comment = $image->comment; $image_capabilities = $image->capabilities; $image_deployment_parameter = $image->deployment_parameter; // get image storage $storage = new storage(); $storage->get_instance_by_id($imageid); $storage_resource_id = $storage->resource_id; // get deployment type $deployment = new deployment(); $deployment->get_instance_by_id($storage->type); // get storage resource $resource = new resource(); $resource->get_instance_by_id($storage_resource_id); $resource_id = $resource->id; $resource_ip = $resource->ip; // create an admin user to post when cloning has finished $openqrm_admin_user = new user("openqrm"); $openqrm_admin_user->set_user(); // parse the identifiers // origin image volume name $origin_volume_name = basename($image_rootdevice); // location of the volume (path) $image_location_name = dirname($image_rootdevice); // For kvm vms we assume that the image is located on the vm-host // so we send the auth command to the vm-host instead of the image storage. // This enables using a NAS/Glusterfs backend with all volumes accessible for all hosts // // Still we need to send the remove command to the storage resource since the // create-phase automatically adapted the image->storageid, we cannot use the vm-resource here // because cloudimage->resource_id will be set to -1 when the cloudapp is in paused/resize/private state // if ($cloudimage->resource_id > 0) { // try to get the vm resource $vm_resource = new resource(); $vm_resource->get_instance_by_id($cloudimage->resource_id); // get the lxc host $vm_host_resource = new resource(); $vm_host_resource->get_instance_by_id($vm_resource->vhostid); // san backend ? if ($vm_host_resource->id != $resource->id) { $event->log("create_private_kvm_bf_deployment", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-bf-deployment-cloud-hook.php", "Image " . $image_id . " IS NOT available on this kvm host, " . $resource->id . " not equal " . $vm_host_resource->id . " !! Assuming SAN Backend", "", "", 0, 0, 0); } else { $event->log("create_private_kvm_bf_deployment", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-bf-deployment-cloud-hook.php", "Image " . $image_id . " IS available on this kvm host, " . $resource->id . " equal " . $vm_host_resource->id . ".", "", "", 0, 0, 0); } } $image_resize_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/kvm/bin/openqrm-kvm clone -n " . $origin_volume_name . " -s " . $private_image_name . " -v " . $image_location_name . " -m " . $private_disk . " -u " . $openqrm_admin_user->name . " -p " . $openqrm_admin_user->password . " -t " . $deployment->type . " --openqrm-cmd-mode background"; $event->log("create_private_kvm_bf_deployment", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-bf-deployment-cloud-hook.php", "Running : {$image_resize_cmd}", "", "", 0, 0, 0); $resource->send_command($resource_ip, $image_resize_cmd); // set the storage specific image root_device parameter $new_rootdevice = str_replace($origin_volume_name, $private_image_name, $image->rootdevice); return $new_rootdevice; }
function get_response() { $response = $this->response; $form = $response->get_form($this->actions_name, 'install1'); $id = $this->response->html->request()->get('image_id'); $submit = $form->get_elements('submit'); $submit->handler = 'onclick="wait();"'; $form->add($submit, 'submit'); $submit = $form->get_elements('cancel'); $submit->handler = 'onclick="cancel();"'; $form->add($submit, 'cancel'); $image = new image(); $image->get_instance_by_id($id); $storage = new storage(); $deployment = new deployment(); $install_template_type = $this->response->html->request()->get('install_from_template'); if (strlen($install_template_type)) { $deployment->get_instance_by_type($install_template_type); $storage_select_arr = $storage->get_ids_by_storage_type($deployment->id); } else { $storage_select_arr = array(); } $storage_select = array(); foreach ($storage_select_arr as $storage_db) { $storage_id = $storage_db['storage_id']; $storage->get_instance_by_id($storage_id); $storage_select[] = array("value" => $storage_id, "label" => $storage_id . " - " . $storage->name); } $d['install_server']['label'] = $this->lang['form_install_server']; $d['install_server']['required'] = true; $d['install_server']['object']['type'] = 'htmlobject_select'; $d['install_server']['object']['attrib']['index'] = array('value', 'label'); $d['install_server']['object']['attrib']['id'] = 'install_server'; $d['install_server']['object']['attrib']['name'] = 'install_server'; $d['install_server']['object']['attrib']['options'] = $storage_select; $form->add($d); $response->form = $form; return $response; }
function select() { $d = array(); $id = $this->response->html->request()->get('image_id'); if ($id !== '') { $image = new image(); $image->get_instance_by_id($id); $storage = new storage(); $storage->get_instance_by_id($image->storageid); $resource = new resource(); $resource->get_instance_by_id($storage->resource_id); require_once $this->openqrm->get('basedir') . '/plugins/image-shelf/web/class/imageshelf.class.php'; $imageshelf = new imageshelf(); $imageshelf->get_instance_by_id($this->id); $file = $this->openqrm->get('basedir') . '/plugins/image-shelf/web/image-lists/' . $imageshelf->name . '/image-shelf.conf'; if ($this->file->exists($file)) { $this->file->remove($file); } $command = $this->openqrm->get('basedir') . '/plugins/image-shelf/bin/openqrm-image-shelf list'; $command .= ' -n ' . $imageshelf->name; $command .= ' -i ' . $imageshelf->uri; $command .= ' -u ' . $imageshelf->user; $command .= ' -p ' . $imageshelf->password; $command .= ' --openqrm-ui-user ' . $this->user->name; $command .= ' --openqrm-cmd-mode background'; $openqrm_server = new openqrm_server(); $openqrm_server->send_command($command); while (!$this->file->exists($file)) { usleep(10000); // sleep 10ms to unload the CPU clearstatcache(); } sleep(1); $distribution = ''; $lines = explode("\n", $this->file->get_contents($file)); foreach ($lines as $line) { $tmp = explode('|', $line); if (isset($tmp[0]) && $tmp[0] === $this->image) { $distribution = $tmp[1]; break; } } $command = $this->openqrm->get('basedir') . '/plugins/image-shelf/bin/openqrm-image-shelf get'; $command .= ' -n ' . $imageshelf->name; $command .= ' -i ' . $imageshelf->uri; $command .= ' -f ' . $this->image; $command .= ' -s ' . $resource->ip . ':' . $image->rootdevice; $command .= ' -d ' . $distribution; $command .= ' -u ' . $imageshelf->user; $command .= ' -p ' . $imageshelf->password; $command .= ' -o ' . $this->openqrm->admin()->name . ' -q ' . $this->openqrm->admin()->password; $command .= ' --openqrm-ui-user ' . $this->user->name; $command .= ' --openqrm-cmd-mode background'; $openqrm_server->send_command($command); $d = sprintf($this->lang['msg'], $image->name, $this->image); } else { $h = array(); $h['image_icon']['title'] = ' '; $h['image_icon']['sortable'] = false; $h['image_id']['title'] = $this->lang['table_id']; $h['image_name']['title'] = $this->lang['table_name']; $h['image_version']['title'] = $this->lang['table_version']; $h['image_type']['title'] = $this->lang['table_deployment']; $h['image_isactive']['title'] = $this->lang['table_isactive']; $h['image_comment']['title'] = $this->lang['table_comment']; $h['image_comment']['sortable'] = false; $h['edit']['title'] = ' '; $h['edit']['sortable'] = false; $image = new image(); $params = $this->response->get_array($this->actions_name, 'target'); $b = array(); $table = $this->response->html->tablebuilder('imageshelf_target', $params); $table->offset = 0; $table->sort = 'image_id'; $table->limit = 10; $table->order = 'ASC'; $table->max = $image->get_count(); $table->init(); $image_arr = $image->display_overview(0, 10000, $table->sort, $table->order); $image_icon = "/openqrm/base/img/image.png"; foreach ($image_arr as $index => $image_db) { // prepare the values for the array $image = new image(); $image->get_instance_by_id($image_db["image_id"]); if ($image->type === 'lvm-nfs-deployment' || $image->type === 'nfs-deployment') { $image_comment = $image_db["image_comment"]; if (!strlen($image_comment)) { $image_comment = "-"; } $image_version = $image_db["image_version"]; if (!strlen($image_version)) { $image_version = "-"; } // edit $a = $this->response->html->a(); $a->title = $this->lang['action_edit']; $a->handler = 'onclick="wait();"'; $a->css = 'edit'; $a->href = $this->response->get_url($this->actions_name, 'target') . '&image_id=' . $image->id; $image_edit = $a->get_string(); // set the active icon $isactive_icon = "/openqrm/base/img/enable.png"; if ($image_db["image_isactive"] == 1) { $isactive_icon = "/openqrm/base/img/disable.png"; } $image_isactive_icon = "<img src=" . $isactive_icon . " width='24' height='24' alt='State'>"; $b[] = array('image_icon' => "<img width='24' height='24' src='" . $image_icon . "'>", 'image_id' => $image_db["image_id"], 'image_name' => $image_db["image_name"], 'image_version' => $image_version, 'image_type' => $image_db["image_type"], 'image_isactive' => $image_isactive_icon, 'image_comment' => $image_comment, 'edit' => $image_edit); } } $table->id = 'Tabelle'; $table->css = 'htmlobject_table'; $table->border = 1; $table->cellspacing = 0; $table->cellpadding = 3; $table->autosort = false; $table->sort_link = false; $table->max = count($b); $table->head = $h; $table->body = $b; $table->form_action = $this->response->html->thisfile; $table->limit_select = array(array("value" => 10, "text" => 10), array("value" => 20, "text" => 20), array("value" => 30, "text" => 30), array("value" => 50, "text" => 50), array("value" => 100, "text" => 100)); $d['form'] = $this->response->get_form($this->actions_name, 'target', false)->get_elements(); $d['table'] = $table; } return $d; }
function get_response() { $response = $this->response; $form = $response->get_form($this->actions_name, 'step3'); $resource = new resource(); $resource->get_instance_by_id($this->appliance->resources); // if not openQRM resource if ($resource->id != 0) { $virtualization = new virtualization(); $virtualization->get_instance_by_id($resource->vtype); } $submit = $form->get_elements('submit'); $submit->handler = 'onclick="wait();"'; $form->add($submit, 'submit'); $submit = $form->get_elements('cancel'); $submit->handler = 'onclick="cancel();"'; $form->add($submit, 'cancel'); // prepare image list according to the resource capabilities + vtype $image = new image(); $list = $image->get_list(); unset($list[0]); unset($list[1]); $images = array(); // openQRM if ($resource->id == 0) { $images[] = array(0, 'Local openQRM Installation'); // local-server integrated resource } else { if (strstr($resource->capabilities, "TYPE=local-server")) { $local_image = new image(); $local_image->get_instance_by_id($resource->imageid); $images[] = array($local_image->id, 'Local OS Installation'); // local-deployment VMs } else { if (strstr($virtualization->type, "-vm-local")) { $virtualization_plugin_name = $virtualization->get_plugin_name(); $deployment = new deployment(); $deployment_id_arr = $deployment->get_deployment_ids(); $possible_deployment_types_arr = ''; foreach ($deployment_id_arr as $deployment_id_db) { $deployment_id = $deployment_id_db['deployment_id']; $deployment->get_instance_by_id($deployment_id); if ($deployment->storagetype === $virtualization_plugin_name) { $possible_deployment_types_arr[] = $deployment->type; } } // filter image list with only the images from the VM deployment type foreach ($list as $value) { $image_id = $value['value']; $image->get_instance_by_id($image_id); // is image active ? then do not show it here if ($image->isactive == 1) { continue; } if (!in_array($image->type, $possible_deployment_types_arr)) { continue; } // filter local-server images $images[] = array($image_id, $image->id . ' / ' . $image->name . ' (' . $image->type . ')'); } // network-deployment - show only network-boot images } else { if (strstr($virtualization->type, "-vm-net")) { foreach ($list as $value) { $image_id = $value['value']; $image->get_instance_by_id($image_id); // is image active ? then do not show it here if ($image->isactive == 1) { continue; } // filter local-server images if (strstr($image->capabilities, "TYPE=local-server")) { continue; } $is_network_deployment = false; if ($image->is_network_deployment() === true) { $is_network_deployment = true; } if ($is_network_deployment) { $images[] = array($image_id, $image->id . ' / ' . $image->name . ' (' . $image->type . ')'); } } // network deployment - physical systems - show only network-boot images } else { foreach ($list as $value) { $image_id = $value['value']; $image->get_instance_by_id($image_id); // is image active ? then do not show it here if ($image->isactive == 1) { continue; } // filter local-server images if (strstr($image->capabilities, "TYPE=local-server")) { continue; } $is_network_deployment = false; if ($image->is_network_deployment() === true) { $is_network_deployment = true; } if ($is_network_deployment) { $images[] = array($image_id, $image->id . ' / ' . $image->name . ' (' . $image->type . ')'); } } } } } } // handle appliance is new or edited $selected = $this->response->html->request()->get('image_id'); if ($selected === '' && isset($this->appliance->imageid)) { $selected = $this->appliance->imageid; } $d['image']['label'] = $this->lang['form_image']; $d['image']['required'] = true; $d['image']['object']['type'] = 'htmlobject_select'; $d['image']['object']['attrib']['index'] = array(0, 1); $d['image']['object']['attrib']['id'] = 'image'; $d['image']['object']['attrib']['name'] = 'image'; $d['image']['object']['attrib']['options'] = $images; $d['image']['object']['attrib']['selected'] = array($selected); $d['image_edit'] = ''; if ($this->appliance->resources != 0) { $d['image_edit']['label'] = $this->lang['form_image_edit']; $d['image_edit']['object']['type'] = 'htmlobject_input'; $d['image_edit']['object']['attrib']['type'] = 'checkbox'; $d['image_edit']['object']['attrib']['id'] = 'image_edit'; $d['image_edit']['object']['attrib']['name'] = 'image_edit'; $d['image_edit']['object']['attrib']['checked'] = true; } $form->add($d); $response->form = $form; return $response; }
function storage_auth_deployment_stop($image_id) { global $event; global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; $image = new image(); $image->get_instance_by_id($image_id); $image_name = $image->name; $image_rootdevice = $image->rootdevice; $storage = new storage(); $storage->get_instance_by_id($image->storageid); $storage_resource = new resource(); $storage_resource->get_instance_by_id($storage->resource_id); $storage_ip = $storage_resource->ip; $deployment = new deployment(); $deployment->get_instance_by_type($image->type); $deployment_type = $deployment->type; $deployment_plugin_name = $deployment->storagetype; }
// resource_id // resource_mac // resource_ip // kernel_id // kernel_name // image_id // image_name // appliance_id case 'assign': $kernel_id = $_REQUEST["resource_kernelid"]; $kernel = new kernel(); $kernel->get_instance_by_id($kernel_id); $kernel_name = $kernel->name; $image_id = $_REQUEST["resource_imageid"]; $image = new image(); $image->get_instance_by_id($image_id); $image_name = $image->name; // send command to the openQRM-server $openqrm_server->send_command("openqrm_assign_kernel {$resource_id} {$resource_mac} {$kernel_name}"); // update openQRM database $resource = new resource(); $resource->assign($resource_id, $kernel_id, $kernel_name, $image_id, $image_name); $resource->send_command($resource_ip, "reboot"); break; // reboot requires : // resource_ip // reboot requires : // resource_ip case 'reboot': $resource = new resource(); $resource->send_command("{$resource_ip}", "reboot");
function create_private_lvm_nfs_deployment($cloud_image_id, $private_disk, $private_image_name) { global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; global $RESOURCE_INFO_TABLE; global $event; $event->log("create_private_lvm_nfs_deployment", $_SERVER['REQUEST_TIME'], 5, "lvm-nfs-deployment-cloud-hook", "Creating private image on storage", "", "", 0, 0, 0); $cloudimage = new cloudimage(); $cloudimage->get_instance_by_id($cloud_image_id); // get image $image = new image(); $image->get_instance_by_id($cloudimage->image_id); $image_id = $image->id; $image_name = $image->name; $image_type = $image->type; $image_version = $image->version; $image_rootdevice = $image->rootdevice; $image_rootfstype = $image->rootfstype; $image_storageid = $image->storageid; $image_isshared = $image->isshared; $image_comment = $image->comment; $image_capabilities = $image->capabilities; $image_deployment_parameter = $image->deployment_parameter; // get image storage $storage = new storage(); $storage->get_instance_by_id($image_storageid); $storage_resource_id = $storage->resource_id; // get storage resource $resource = new resource(); $resource->get_instance_by_id($storage_resource_id); $resource_id = $resource->id; $resource_ip = $resource->ip; // create an admin user to post when cloning has finished $openqrm_admin_user = new user("openqrm"); $openqrm_admin_user->set_user(); $full_vol_name = $image_rootdevice; $vol_dir = dirname($full_vol_name); $vol = str_replace("/", "", $vol_dir); $image_location_name = basename($full_vol_name); $image_resize_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/lvm-storage/bin/openqrm-lvm-storage clone -n " . $image_location_name . " -s " . $private_image_name . " -v " . $vol . " -m " . $private_disk . " -t lvm-nfs-deployment -u " . $openqrm_admin_user->name . " -p " . $openqrm_admin_user->password . " --openqrm-cmd-mode background"; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "lvm-nfs-deployment-cloud-hook", "Running : {$image_resize_cmd}", "", "", 0, 0, 0); $resource->send_command($resource_ip, $image_resize_cmd); // set the storage specific image root_device parameter $new_rootdevice = "/" . $vol . "/" . $private_image_name; return $new_rootdevice; }
function create_hybrid_cloud_vm_local($host_resource_id, $name, $mac, $memory, $cpu, $swap, $additional_nic_str) { global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; global $RESOURCE_INFO_TABLE; global $event; $max_loop = 30; $ip_wait_loop = 0; $event->log("create_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-cloud-hook", "Creating Cloud VM {$name} on Host resource {$host_resource_id}", "", "", 0, 0, 0); $file = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/hybrid-cloud/etc/openqrm-plugin-hybrid-cloud.conf"; $ini = openqrm_parse_conf($file); $hc_default_account_id = $ini['OPENQRM_PLUGIN_HYBRID_CLOUD_DEFAULT_ACCOUNT']; $hc = new hybrid_cloud(); $hc->get_instance_by_id($hc_default_account_id); $hc_authentication = ''; if ($hc->account_type == 'aws' || $hc->account_type == 'euca') { $hc_authentication .= ' -O ' . $hc->access_key; $hc_authentication .= ' -W ' . $hc->secret_key; $hc_authentication .= ' -iz ' . $ini['OPENQRM_PLUGIN_HYBRID_CLOUD_DEFAULT_AVAILABILITY_ZONE']; $hc_authentication .= ' -ir ' . $ini['OPENQRM_PLUGIN_HYBRID_CLOUD_DEFAULT_REGION']; $hc_authentication .= ' -it ' . $hc->translate_resource_components_to_instance_type($cpu, $memory); } if ($hc->account_type == 'lc-openstack') { $hc_authentication .= ' -u ' . $hc->username; $hc_authentication .= ' -p ' . $hc->password; $hc_authentication .= ' -q ' . $hc->host; $hc_authentication .= ' -x ' . $hc->port; $hc_authentication .= ' -g ' . $hc->tenant; $hc_authentication .= ' -e ' . $hc->endpoint; $hc_authentication .= ' -it ' . $hc->translate_resource_components_to_instance_type($cpu, $memory); } // set hybrid-cloud account in new resource $cloud_resource = new resource(); $cloud_resource->get_instance_by_mac($mac); $cloud_resource->set_resource_capabilities("HCACL", $hc_default_account_id); // $event->log("create_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 2, "hybrid-cloud-cloud-hook", "!!! Found new resource ".$cloud_resource->id." and set HC account id", "", "", 0, 0, 0); // set resource hostname + ip config // check to have a ip from the dhcpd-resource hook while ($cloud_resource->ip == "0.0.0.0") { sleep(1); clearstatcache(); $cloud_resource->get_instance_by_mac($cloud_resource->mac); $ip_wait_loop++; if ($ip_wait_loop > $max_loop) { break; } // $event->log("create_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 2, "hybrid-cloud-cloud-hook", "!!! Waiting for ip address ".$ip_wait_loop."!", "", "", 0, 0, 0); } // save the mgmt ip in the resource network field $rufields["resource_network"] = $cloud_resource->ip; $rufields["resource_hostname"] = $hc->account_type . $cloud_resource->id; $cloud_resource->update_info($cloud_resource->id, $rufields); // $event->log("create_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 2, "hybrid-cloud-cloud-hook", "!!! Updated resource with hostname + ip", "", "", 0, 0, 0); // we need to find the ami name from the appliance->image->root-device $appliance = new appliance(); $appliance->get_instance_by_name($name); $image = new image(); $image->get_instance_by_id($appliance->imageid); // $event->log("create_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 2, "hybrid-cloud-cloud-hook", "!!! Found AMI ".$image->name." root-dev: ".$image->rootdevice, "", "", 0, 0, 0); $openqrm = new openqrm_server(); $command = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/hybrid-cloud/bin/openqrm-hybrid-cloud-vm create "; $command .= ' -i ' . $hc->id; $command .= ' -n ' . $hc->account_name; $command .= ' -t ' . $hc->account_type; $command .= $hc_authentication; $command .= ' -in ' . $hc->account_type . $cloud_resource->id; $command .= ' -im ' . $mac; $command .= ' -a ' . $image->rootdevice; $command .= ' -ig ' . $ini['OPENQRM_PLUGIN_HYBRID_CLOUD_DEFAULT_SECURITY_GROUP']; $command .= ' -ik ' . $ini['OPENQRM_PLUGIN_HYBRID_CLOUD_DEFAULT_KEYPAIR']; $command .= ' --openqrm-cmd-mode background'; // $event->log("create_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 2, "hybrid-cloud-cloud-hook", "Running $command", "", "", 0, 0, 0); $openqrm->send_command($command, NULL, true); }
function openqrm_cloud_monitor() { global $event; global $APPLIANCE_INFO_TABLE; global $IMAGE_INFO_TABLE; global $CLOUD_IMAGE_TABLE; global $CLOUD_APPLIANCE_TABLE; global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; global $openqrm_server; global $BaseDir; global $RootDir; global $vm_create_timout; $vmware_mac_address_space = "00:50:56"; $cloud_monitor_lock = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/web/action/cloud-conf/cloud-monitor.lock"; $cloud_monitor_timeout = "600"; $cloud_volume_clone_timeout = "4800"; // lock to prevent running multiple times in parallel if (file_exists($cloud_monitor_lock)) { // check from when it is, if it is too old we remove it and start $cloud_monitor_lock_date = file_get_contents($cloud_monitor_lock); $now = $_SERVER['REQUEST_TIME']; if ($now - $cloud_monitor_lock_date > $cloud_monitor_timeout) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "monitor-hook", "Timeout for the cloud-monitor-lock reached, creating new lock", "", "", 0, 0, 0); $cloud_lock_fp = fopen($cloud_monitor_lock, 'w'); fwrite($cloud_lock_fp, $now); fclose($cloud_lock_fp); } else { return 0; } } else { $now = $_SERVER['REQUEST_TIME']; $cloud_lock_fp = fopen($cloud_monitor_lock, 'w'); fwrite($cloud_lock_fp, $now); fclose($cloud_lock_fp); } // prepare performance parameter $cloud_performance_config = new cloudconfig(); $max_parallel_phase_one_actions = $cloud_performance_config->get_value(27); // 27 max-parallel-phase-one-actions $max_parallel_phase_two_actions = $cloud_performance_config->get_value(28); // 28 max-parallel-phase-two-actions $max_parallel_phase_three_actions = $cloud_performance_config->get_value(29); // 29 max-parallel-phase-three-actions $max_parallel_phase_four_actions = $cloud_performance_config->get_value(30); // 30 max-parallel-phase-four-actions $max_parallel_phase_five_actions = $cloud_performance_config->get_value(31); // 31 max-parallel-phase-five-actions $max_parallel_phase_six_actions = $cloud_performance_config->get_value(32); // 32 max-parallel-phase-six-actions $max_parallel_phase_seven_actions = $cloud_performance_config->get_value(33); // 33 max-parallel-phase-seven-actions $parallel_phase_one_actions = 0; $parallel_phase_two_actions = 0; $parallel_phase_three_actions = 0; $parallel_phase_four_actions = 0; $parallel_phase_five_actions = 0; $parallel_phase_six_actions = 0; $parallel_phase_seven_actions = 0; // appliance hostname $cloud_appliance_hostname_enabled = $cloud_performance_config->get_value(34); // 34 appliance-hostname // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "Cloud Phase I - Image actions, VM-removal", "", "", 0, 0, 0); // #################### clone-on-deploy image resize / remove ################################ // here we check if we have any clone-on-deploy images to resize or to remove // get cloudimage ids $cil = new cloudimage(); $cloud_image_list = $cil->get_all_ids(); foreach ($cloud_image_list as $ci_list) { $phase_one_actions = 0; $ci_id = $ci_list['ci_id']; $ci = new cloudimage(); $ci->get_instance_by_id($ci_id); $ci_state = $ci->state; $ci_image_id = $ci->image_id; $ci_appliance_id = $ci->appliance_id; $ci_resource_id = $ci->resource_id; $ci_cr_id = $ci->cr_id; $ci_resource = new resource(); $ci_resource->get_instance_by_id($ci_resource_id); $ci_appliance = new appliance(); $ci_appliance->get_instance_by_id($ci->appliance_id); // not the openQRM server resource, accept 0 only for private image remove if ($ci_cr_id != 0) { if ($ci_resource_id == 0) { continue; } // not when the cr is in starting phase $ci_request = new cloudrequest(); $ci_request->get_instance_by_id($ci_cr_id); if ($ci_request->status == 8) { continue; } } // image still in use ? if ($ci_state == 1) { // its resource its active with the idle image ? sounds like pause if (!strcmp($ci_resource->state, "active") && $ci_resource->imageid == 1) { // ####################### remove auto createed vm ################# // check for auto-create vms, if yes remove the resource if it is virtual $app_stop_autovm_remove_conf = new cloudconfig(); $app_stop_auto_remove_vms = $app_stop_autovm_remove_conf->get_value(7); // 7 is auto_create_vms if (!strcmp($app_stop_auto_remove_vms, "true")) { // we only remove virtual machines if ($ci_resource->vtype != 1) { // check if we still wait for the image_authentication stop hook unset($ci_image_authentication); $ci_image_authentication = new image_authentication(); $ci_image_authentication->get_instance_by_image_id($ci_image_id); if (strlen($ci_image_authentication->id)) { // we still wait for the image_authentication hook to run continue; } // cloudvm->remove ..... $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "Auto-removing resource {$ci_resource_id}", "", "", 0, 0, 0); $auto_cloudvm = new cloudvm(); $auto_cloudvm->remove($ci_resource_id, $ci_resource->vtype, $ci_appliance->name, $ci_resource->mac); // update cloudimage with resource -1 $ar_ci_update = array('ci_resource_id' => "-1"); $ci->update($ci->id, $ar_ci_update); $phase_one_actions = 1; } } // ####################### end remove auto createed vm ############# } // the image is still in use continue; } // image not in use any more and resource active (idle) again ? if ($ci_resource_id > 0) { if (strcmp($ci_resource->state, "active")) { // not yet active again continue; } if ($ci_resource->imageid != 1) { // not yet idle continue; } } // get image definition $image = new image(); $image->get_instance_by_id($ci_image_id); $image_name = $image->name; $image_type = $image->type; $image_rootdevice = $image->rootdevice; $image_storageid = $image->storageid; $image_deployment_parameter = $image->deployment_parameter; // get image storage $storage = new storage(); $storage->get_instance_by_id($image_storageid); $storage_resource_id = $storage->resource_id; // get storage resource $resource = new resource(); $resource->get_instance_by_id($storage_resource_id); $resource_id = $resource->id; $resource_ip = $resource->ip; // resize ? if ($ci_state == 2) { // calculate the resize $resize_value = $ci->disk_rsize; $storage_clone_timeout = 60; $cloudstorage = new cloudstorage(); $cloudstorage->resize($ci_id, $resize_value, $storage_clone_timeout); // re-set the cloudimage state to active $ci->set_state($ci->id, "active"); $phase_one_actions = 1; } // private ? if ($ci_state == 3) { // calculate the private disk size $private_disk = $ci->disk_rsize; $private_image_name = $ci->clone_name; $storage_private_timeout = 60; // private storage method returns new rootdevice $cloudstorage = new cloudstorage(); $clone_image_fields["image_rootdevice"] = $cloudstorage->create_private($ci_id, $private_disk, $private_image_name, $storage_private_timeout); // here we logical create the image in openQRM, we have all data available // the private image relation will be created after this step in the private lc if (strlen($clone_image_fields["image_rootdevice"])) { $clone_image = new image(); $clone_image_fields["image_id"] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $clone_image_fields["image_name"] = $ci->clone_name; $clone_image_fields["image_version"] = "Private Cloud"; $clone_image_fields["image_type"] = $image->type; $clone_image_fields["image_rootfstype"] = $image->rootfstype; $clone_image_fields["image_storageid"] = $image->storageid; $clone_image_fields["image_deployment_parameter"] = $image->deployment_parameter; // !! we create the private image as non-shared // this will prevent cloning when it is requested $clone_image_fields["image_isshared"] = 0; $clone_image_fields["image_comment"] = $image->comment; $clone_image_fields["image_capabilities"] = $image->capabilities; $clone_image->add($clone_image_fields); $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Created new private Cloud image {$ci->clone_name}", "", "", 0, 0, 0); } // re-set the cloudimage state to active $ci->set_state($ci->id, "active"); $phase_one_actions = 1; } // remove ? if ($ci_state == 0) { $physical_remove = false; // only remove physically if the cr was set to shared $ci_cr = new cloudrequest(); $ci_cr->get_instance_by_id($ci->cr_id); if ($ci_cr->shared_req == 1) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Cloud request set to shared, removing Image " . $ci_image_id . ".", "", "", 0, 0, 0); $physical_remove = true; } // or if the remove request came from a user for a private image if ($ci_cr_id == 0) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Cloud user requested to remove Image " . $ci_image_id . ".", "", "", 0, 0, 0); $physical_remove = true; } else { // re-check if this is a private image with clone-on-deploy set to false if ($ci_cr->image_id > 0) { $ci_private_image = new cloudprivateimage(); if ($ci_private_image->exists_by_image_id($ci_cr->image_id)) { $ci_private_image->get_instance_by_image_id($ci_cr->image_id); if ($ci_private_image->clone_on_deploy == 0) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Image " . $ci_image_id . " is private and set to NOT clone-on-deploy. Not removing.", "", "", 0, 0, 0); $physical_remove = false; } } } } if ($physical_remove) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Removing Image {$ci_image_id} !", "", "", 0, 0, 0); $storage_remove_timeout = 60; $cloudstorage = new cloudstorage(); $cloudstorage->remove($ci_id, $storage_remove_timeout); // remove any image_authentication for the image // since we remove the image a image_authentication won't // find it anyway $image_authentication = new image_authentication(); $ia_id_ar = $image_authentication->get_all_ids(); foreach ($ia_id_ar as $ia_list) { $ia_auth_id = $ia_list['ia_id']; $ia_auth = new image_authentication(); $ia_auth->get_instance_by_id($ia_auth_id); if ($ia_auth->image_id == $ci_image_id) { // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Removing image_authentication $ia_auth_id for cloud image $ci_image_id since we are on going to remove the image itself", "", "", 0, 0, $resource_id); $ia_auth->remove($ia_auth_id); } } // remove the image in openQRM $image->remove($ci_image_id); $phase_one_actions = 1; // we do not remove non-shared images but just its cloudimage } // ####################### remove auto createed vm ################# // check for auto-create vms, if yes remove the resource if it is virtual $cc_autovm_remove_conf = new cloudconfig(); $cc_auto_remove_vms = $cc_autovm_remove_conf->get_value(7); // 7 is auto_create_vms if (!strcmp($cc_auto_remove_vms, "true")) { // if it had a resource, it has none e.g. in case of cloudappliance pause if ($ci_resource_id > 0) { // check virtualization type $auto_resource = new resource(); $auto_resource->get_instance_by_id($ci_resource_id); $auto_vm_virtualization = $auto_resource->vtype; // we only remove virtual machines if ($auto_vm_virtualization != 1) { // gather name $auto_remove_appliance = new appliance(); $auto_remove_appliance->get_instance_by_id($ci_appliance_id); // cloudvm->remove ..... $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "Auto-removing resource {$ci_resource_id}", "", "", 0, 0, 0); $auto_cloudvm = new cloudvm(); $auto_cloudvm->remove($ci_resource_id, $auto_vm_virtualization, $auto_remove_appliance->name, $auto_resource->mac); } } } // ####################### end remove auto createed vm ############# // remove the appliance if ($ci_appliance_id > 0) { $rapp = new appliance(); $rapp->remove($ci_appliance_id); } // remove the image in the cloud $ci->remove($ci_id); $phase_one_actions = 1; // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Removing the cloned image $ci_image_id and the appliance $ci_appliance_id !", "", "", 0, 0, 0); } // check if we continue or go on if ($phase_one_actions == 1) { $parallel_phase_one_actions++; if ($max_parallel_phase_one_actions > 0 && $parallel_phase_one_actions >= $max_parallel_phase_one_actions) { break; } } // end remove } // end cloudimage loop // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "Cloud Phase II - Main provisioning loop", "", "", 0, 0, 0); // #################### main cloud request loop ################################ $crl = new cloudrequest(); $cr_list = $crl->get_all_new_and_approved_ids(); foreach ($cr_list as $list) { $cr_id = $list['cr_id']; $cr = new cloudrequest(); $cr->get_instance_by_id($cr_id); $cr_status = $cr->status; // source image // Set Source Image ID $SOURCE_IMAGE_ID = $cr->image_id; $cu = new clouduser(); $cr_cu_id = $cr->cu_id; $cu->get_instance_by_id($cr_cu_id); $cu_name = $cu->name; // #################### auto-provisioning ################################ // here we only care about the requests status new and set them to approved (2) if ($cr_status == 1) { // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Found new request ID $cr_id. Checking if Auto-provisioning is enabled", "", "", 0, 0, 0); $cc_conf = new cloudconfig(); $cc_auto_provision = $cc_conf->get_value(2); // 2 is auto_provision if (!strcmp($cc_auto_provision, "true")) { // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Found new request ID $cr_id. Auto-provisioning is enabled! Approving the request", "", "", 0, 0, 0); $cr->setstatus($cr_id, "approve"); $cr_status = 2; } } // care about the next approved cr in the list if ($cr_status != 2) { continue; } // check for start time $now = $_SERVER['REQUEST_TIME']; $cr_start = $cr->start; if ($cr_start > $now) { continue; } // #################### provisioning ################################ // provision, only care about approved requests $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Provisioning request ID {$cr_id}", "", "", 0, 0, 0); // ################################## quantity loop provisioning ############################### // from 4.9 on we do not support more than one appliance per request. // $resource_quantity = $cr->resource_quantity; $resource_quantity = 1; // check for max_apps_per_user $cloud_user_apps_arr = array(); $cloud_user_app = new cloudappliance(); $cloud_user_apps_arr = $cloud_user_app->get_all_ids(); $users_appliance_count = 0; foreach ($cloud_user_apps_arr as $capp) { $tmp_cloud_app = new cloudappliance(); $tmp_cloud_app_id = $capp['ca_id']; $tmp_cloud_app->get_instance_by_id($tmp_cloud_app_id); // active ? if ($tmp_cloud_app->state == 0) { continue; } // check if the cr is ours $rc_tmp_cr = new cloudrequest(); $rc_tmp_cr->get_instance_by_id($tmp_cloud_app->cr_id); if ($rc_tmp_cr->cu_id != $cr_cu_id) { continue; } $users_appliance_count++; } // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "User $cr_cu_id has already $users_appliance_count appliance(s) running.", "", "", 0, 0, 0); $cc_max_app = new cloudconfig(); $max_apps_per_user = $cc_max_app->get_value(13); // 13 is max_apps_per_user if ($users_appliance_count + $resource_quantity > $max_apps_per_user) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Not provisining CR {$cr_id} from user {$cr_cu_id} who has already {$users_appliance_count} appliance(s) running.", "", "", 0, 0, 0); $cr->setstatus($cr_id, 'deny'); continue; } $cr_resource_number = 1; // ################################## create appliance ############################### // set hostname $appliance_name = "cloud-" . $cr_id . "-" . $cr_resource_number . "-x"; if (!strcmp($cloud_appliance_hostname_enabled, "true")) { if (strlen($cr->appliance_hostname)) { $appliance_requested_hostname = $cr->appliance_hostname; // check if the hostname is free, if not return to regular naming convention $appliance_chk_hostname = new appliance(); $appliance_chk_hostname->get_instance_by_name($appliance_requested_hostname); if ($appliance_chk_hostname->id > 0) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Requested hostname " . $appliance_requested_hostname . " already in use. Reverting to " . $appliance_name . " (CR ID " . $cr_id . ").", "", "", 0, 0, 0); } else { // set requested hostname $appliance_name = $appliance_requested_hostname; } } } // update cr with new hostname $cr_update_hostname_fields = array(); $cr_update_hostname_fields["cr_appliance_hostname"] = $appliance_name; $cr->update($cr->id, $cr_update_hostname_fields); $cr->get_instance_by_id($cr->id); // get new appliance id $appliance_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); // we $user_network_cards = $cr->network_req + 1; // prepare array to add appliance $ar_request = array('appliance_id' => $appliance_id, 'appliance_resources' => "-1", 'appliance_name' => $appliance_name, 'appliance_kernelid' => $cr->kernel_id, 'appliance_imageid' => $cr->image_id, 'appliance_virtualization' => $cr->resource_type_req, 'appliance_cpunumber' => $cr->cpu_req, 'appliance_memtotal' => $cr->ram_req, 'appliance_nics' => $user_network_cards, 'appliance_comment' => "Requested by user {$cu_name}", 'appliance_ssi' => $cr->shared_req, 'appliance_highavailable' => $cr->ha_req, 'appliance_capabilities' => $cr->appliance_capabilities); // create + start the appliance :) $appliance = new appliance(); $appliance->add($ar_request); // first get admin email $cc_acr_conf = new cloudconfig(); $cc_acr_admin_email = $cc_acr_conf->get_value(1); // 1 is admin_email // and the user details $cu_name = $cu->name; $cu_forename = $cu->forename; $cu_lastname = $cu->lastname; $cu_email = $cu->email; // now lets find a resource for this new appliance $appliance->get_instance_by_id($appliance_id); $appliance_virtualization = $cr->resource_type_req; // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "#### Cloud Phase II-1 - Getting a resource", "", "", 0, 0, 0); // ################################## phys. res. ############################### if ($appliance_virtualization == 1) { $cloud_app_physical_resource = new cloudappliance(); if (!$cloud_app_physical_resource->find_existing_resource($appliance, $appliance_virtualization, $cu->id)) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Could not find a resource (type physical system) for request ID {$cr_id}!", "", "", 0, 0, 0); $appliance->remove($appliance_id); $cr->setstatus($cr_id, 'no-res'); // send mail to user $rmail = new cloudmailer(); $rmail->to = "{$cu_email}"; $rmail->from = "{$cc_acr_admin_email}"; $rmail->subject = "openQRM Cloud: Not enough resources to provision a system for request " . $cr_id; $rmail->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "{$cu_forename}", '@@LASTNAME@@' => "{$cu_lastname}", '@@RESNUMBER@@' => "{$cr_resource_number}", '@@YOUR@@' => "your", '@@CLOUDADMIN@@' => $cc_acr_admin_email); $rmail->var_array = $arr; $rmail->send(); // send mail to admin $rmail_admin = new cloudmailer(); $rmail_admin->to = "{$cc_acr_admin_email}"; $rmail_admin->from = "{$cc_acr_admin_email}"; $rmail_admin->subject = "openQRM Cloud: Not enough resources to provision a system for request " . $cr_id; $rmail_admin->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "Cloudadmin", '@@LASTNAME@@' => "", '@@RESNUMBER@@' => "{$cr_resource_number}", '@@YOUR@@' => "the", '@@CLOUDADMIN@@' => $cc_acr_admin_email); $rmail_admin->var_array = $arr; $rmail_admin->send(); continue; } // we have a phys. resource, update object $appliance->get_instance_by_id($appliance_id); } else { // ################################## auto create vm ############################### // check if we should try to create one // request type vm $cc_autovm_conf = new cloudconfig(); $cc_auto_create_vms = $cc_autovm_conf->get_value(7); // 7 is auto_create_vms if (!strcmp($cc_auto_create_vms, "true")) { // check if createvmlc exists for this cr + res-quantity unset($cvmlc); $cvmlc = new cloudcreatevmlc(); $cvmlc->get_instance_by_cr_details($cr_id, $cr_resource_number); if (!strlen($cvmlc->request_time)) { // if no createvmlc exists so far create it and the vm // generate a mac address $mac_res = new resource(); // check if we need to generate the first nics mac address in the vmware address space $new_vm_mac = ""; $vm_virt = new virtualization(); $vm_virt->get_instance_by_id($cr->resource_type_req); $virt_name = $vm_virt->get_plugin_name(); switch ($virt_name) { case 'vmware-esx': case 'vmware-server': case 'vmware-server2': $mac_res->generate_mac(); $suggested_mac = $mac_res->mac; $new_forth_byte_first_bit = rand(1, 3); $new_vm_mac = strtolower($vmware_mac_address_space . ":" . substr($suggested_mac, 9)); $new_vm_mac = substr_replace($new_vm_mac, $new_forth_byte_first_bit, 9, 1); break; default: $mac_res->generate_mac(); $new_vm_mac = $mac_res->mac; break; } // additional_nics $new_additional_nics = $cr->network_req; // cpu $new_vm_cpu = $cr->cpu_req; // memory $new_vm_memory = 256; if ($cr->ram_req != 0) { $new_vm_memory = $cr->ram_req; } // disk size $new_vm_disk = 5000; if ($cr->disk_req != 0) { $new_vm_disk = $cr->disk_req; } // here we generate the password for the Image and optional vnc of the VM resource $image = new image(); $vm_vnc_password = trim($image->generatePassword(12)); // put the password in the cr for the image later $cr_update_fields = array(); $cr_update_fields['cr_image_password'] = $vm_vnc_password; $cr->update($cr_id, $cr_update_fields); // Create VM // here we start the new vm ! $cloudvm = new cloudvm(); // this method returns the resource-id $cloudvm->create($cr_cu_id, $appliance_virtualization, $appliance_name, $new_vm_mac, $new_additional_nics, $new_vm_cpu, $new_vm_memory, $new_vm_disk, $vm_create_timout, $vm_vnc_password, $SOURCE_IMAGE_ID); $new_vm_resource_id = $cloudvm->resource_id; // check if creating the new VM resource failed if ($new_vm_resource_id == 0) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Failed creating a new resource for request ID {$cr_id}!", "", "", 0, 0, 0); $cr->setstatus($cr_id, 'no-res'); // send mail to user $rmail = new cloudmailer(); $rmail->to = "{$cu_email}"; $rmail->from = "{$cc_acr_admin_email}"; $rmail->subject = "openQRM Cloud: Not enough resources to provision a system for request " . $cr_id; $rmail->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "{$cu_forename}", '@@LASTNAME@@' => "{$cu_lastname}", '@@RESNUMBER@@' => "{$cr_resource_number}", '@@YOUR@@' => "your", '@@CLOUDADMIN@@' => $cc_acr_admin_email); $rmail->var_array = $arr; $rmail->send(); // send mail to admin $rmail_admin = new cloudmailer(); $rmail_admin->to = "{$cc_acr_admin_email}"; $rmail_admin->from = "{$cc_acr_admin_email}"; $rmail_admin->subject = "openQRM Cloud: Error creating new resource for request " . $cr_id; $rmail_admin->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "Cloudadmin", '@@LASTNAME@@' => "", '@@RESNUMBER@@' => "{$cr_resource_number}", '@@YOUR@@' => "the", '@@CLOUDADMIN@@' => $cc_acr_admin_email); $rmail_admin->var_array = $arr; $rmail_admin->send(); // refresh object $appliance->get_instance_by_id($appliance_id); // remove app and cvmlc $appliance->remove($appliance_id); continue; } $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Created VM with resource_id {$new_vm_resource_id}", "", "", 0, 0, 0); // create cvmlc after we got a resource_id $vm_create_time = $_SERVER['REQUEST_TIME']; $cvmlc_resource_fields["vc_resource_id"] = $new_vm_resource_id; $cvmlc_resource_fields["vc_cr_id"] = $cr_id; $cvmlc_resource_fields["vc_cr_resource_number"] = $cr_resource_number; $cvmlc_resource_fields["vc_request_time"] = $vm_create_time; $cvmlc_resource_fields["vc_vm_create_timeout"] = $vm_create_timout; $cvmlc_resource_fields["vc_state"] = 0; // get the new resource id from the db $new_vc_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $cvmlc_resource_fields["vc_id"] = $new_vc_id; $cvmlc->add($cvmlc_resource_fields); // here we go on to the next cr or resource_number, remove app before $appliance->remove($appliance_id); continue; } else { // we have a cvmlc, check its resource and set its state $cvm_resource = new resource(); $cvm_resource->get_instance_by_id($cvmlc->resource_id); // idle ? if ($cvm_resource->imageid == 1 && $cvm_resource->state == 'active' && strcmp($cvm_resource->ip, "0.0.0.0")) { // we have a new idle vm as resource :) update it in the appliance $new_vm_resource_id = $cvmlc->resource_id; $appliance_fields = array(); $appliance_fields['appliance_resources'] = $new_vm_resource_id; // update and refresh the appliance object $appliance->update($appliance->id, $appliance_fields); $appliance->get_instance_by_id($appliance_id); $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Created resource {$new_vm_resource_id} /cr {$cr_id} now idle, continue provisioning.", "", "", 0, 0, 0); // remove cvmlc $cvmlc->remove($cvmlc->id); } else { // check timeout $vm_check_time = $_SERVER['REQUEST_TIME']; $vm_c_timeout = $cvmlc->request_time + $cvmlc->vm_create_timeout; if ($vm_check_time > $vm_c_timeout) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Could not create a new resource for request ID {$cr_id}!", "", "", 0, 0, 0); $cr->setstatus($cr_id, 'no-res'); // send mail to user $rmail = new cloudmailer(); $rmail->to = "{$cu_email}"; $rmail->from = "{$cc_acr_admin_email}"; $rmail->subject = "openQRM Cloud: Not enough resources to provision a system for request " . $cr_id; $rmail->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "{$cu_forename}", '@@LASTNAME@@' => "{$cu_lastname}", '@@RESNUMBER@@' => "{$cr_resource_number}", '@@YOUR@@' => "your", '@@CLOUDADMIN@@' => $cc_acr_admin_email); $rmail->var_array = $arr; $rmail->send(); // send mail to admin $rmail_admin = new cloudmailer(); $rmail_admin->to = "{$cc_acr_admin_email}"; $rmail_admin->from = "{$cc_acr_admin_email}"; $rmail_admin->subject = "openQRM Cloud: Not enough resources to provision a system from request " . $cr_id; $rmail_admin->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "Cloudadmin", '@@LASTNAME@@' => "", '@@RESNUMBER@@' => "{$cr_resource_number}", '@@YOUR@@' => "the", '@@CLOUDADMIN@@' => $cc_acr_admin_email); $rmail_admin->var_array = $arr; $rmail_admin->send(); // refresh object $appliance->get_instance_by_id($appliance_id); // try removing the cloudvm $clean_cloudvm = new cloudvm(); $clean_cloudvm->remove($cvm_resource->id, $cvm_resource->vtype, $appliance->name, $cvm_resource->mac); // remove app and cvmlc $appliance->remove($appliance_id); $cvmlc->remove($cvmlc->id); continue; } // still waiting within the timeout // update state to 1 (starting) // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Still waiting for cr $cr_id / res. ".$cvmlc->resource_id." to get idle", "", "", 0, 0, 0); $cvm_state_fields['vc_state'] = 1; $cvmlc->update($cvmlc->id, $cvm_state_fields); // remove app $appliance->remove($appliance_id); // continue with the next cr/res-nr continue; } } // ################################## no auto create vm ############################### } else { // not set to auto-create vms // try to find a fitting idle vm $cloud_app_vm_resource = new cloudappliance(); if (!$cloud_app_vm_resource->find_existing_resource($appliance, $appliance_virtualization, $cu->id)) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Not creating a new resource for request ID " . $cr_id . " since auto-create-vms is disabled.", "", "", 0, 0, 0); $appliance->remove($appliance_id); $cr->setstatus($cr_id, 'no-res'); // send mail to user $rmail = new cloudmailer(); $rmail->to = "{$cu_email}"; $rmail->from = "{$cc_acr_admin_email}"; $rmail->subject = "openQRM Cloud: Not enough resources to provision a system for request " . $cr_id; $rmail->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "{$cu_forename}", '@@LASTNAME@@' => "{$cu_lastname}", '@@RESNUMBER@@' => "{$cr_resource_number}", '@@YOUR@@' => "your", '@@CLOUDADMIN@@' => $cc_acr_admin_email); $rmail->var_array = $arr; $rmail->send(); // send mail to admin $rmail_admin = new cloudmailer(); $rmail_admin->to = "{$cc_acr_admin_email}"; $rmail_admin->from = "{$cc_acr_admin_email}"; $rmail_admin->subject = "openQRM Cloud: Not enough resources to provision a system for request " . $cr_id; $rmail_admin->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "Cloudadmin", '@@LASTNAME@@' => "", '@@RESNUMBER@@' => "{$cr_resource_number}", '@@YOUR@@' => "the", '@@CLOUDADMIN@@' => $cc_acr_admin_email); $rmail_admin->var_array = $arr; $rmail_admin->send(); continue; } $appliance->get_instance_by_id($appliance_id); $appliance->remove($appliance_id); } } // ################################## end auto create vm ############################### // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Found resource ".$appliance->resources." (type $appliance_virtualization) for request ID $cr_id", "", "", 0, 0, 0); // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "#### Cloud Phase II-2 - Got resource, Clone Image", "", "", 0, 0, 0); // ################################## clone on deploy ############################### // here we have a resource but // do we have to clone the image before deployment ? // get image definition $image = new image(); $image->get_instance_by_id($cr->image_id); $image_name = $image->name; $image_type = $image->type; $image_version = $image->version; $image_rootdevice = $image->rootdevice; $image_rootfstype = $image->rootfstype; $image_storageid = $image->storageid; $image_isshared = $image->isshared; $image_comment = $image->comment; $image_capabilities = $image->capabilities; $image_deployment_parameter = $image->deployment_parameter; // check if this is a private cloud image belonging to the user // if yes, check if clone_on_deploy is enabled $provision_private_image = new cloudprivateimage(); $provision_private_image->get_instance_by_image_id($image->id); if ($provision_private_image->cu_id == $cr_cu_id) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Image " . $image_name . " belongs to cloud user " . $cr_cu_id . ".", "", "", 0, 0, 0); if ($provision_private_image->clone_on_deploy == 0) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Image " . $image_name . " is private and set to NOT clone-on-deploy.", "", "", 0, 0, 0); $cr->shared_req = 0; // update in the db $cr_update_fields = array(); $cr_update_fields["cr_shared_req"] = "0"; $cr->update($cr->id, $cr_update_fields); } else { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Image " . $image_name . " is private and set to clone-on-deploy.", "", "", 0, 0, 0); } } // we clone ? if ($cr->shared_req == 1) { // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Request ID $cr_id has clone-on-deploy activated. Cloning the image", "", "", 0, 0, 0); // assign new name $image_clone_name = $cr->image_id . ".cloud_" . $cr_id . "_" . $cr_resource_number . "_"; // get new image id $image_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); // add the new image to the openQRM db $ar_request = array('image_id' => $image_id, 'image_name' => $image_clone_name, 'image_version' => $image_version, 'image_type' => $image_type, 'image_rootdevice' => $image_rootdevice, 'image_rootfstype' => $image_rootfstype, 'image_storageid' => $image_storageid, 'image_isshared' => $image_isshared, 'image_comment' => "Requested by user {$cu_name}", 'image_capabilities' => $image_capabilities, 'image_deployment_parameter' => $image_deployment_parameter); $image->add($ar_request); $image->get_instance_by_id($image_id); // set the new image in the appliance ! // prepare array to update appliance $ar_appliance_update = array('appliance_imageid' => $image_id); $appliance->update($appliance_id, $ar_appliance_update); // refresh the appliance object $appliance->get_instance_by_id($appliance_id); // here we put the image + resource definition into an cloudimage // this cares e.g. later to remove the image after the resource gets idle again // -> the check for the resource-idle state happens at the beginning // of every cloud-monitor loop $ci_disk_size = 5000; if (strlen($cr->disk_req)) { $ci_disk_size = $cr->disk_req; } // get a new ci_id $cloud_image_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $cloud_image_arr = array('ci_id' => $cloud_image_id, 'ci_cr_id' => $cr->id, 'ci_image_id' => $appliance->imageid, 'ci_appliance_id' => $appliance->id, 'ci_resource_id' => $appliance->resources, 'ci_disk_size' => $ci_disk_size, 'ci_state' => 1); $cloud_image = new cloudimage(); $cloud_image->add($cloud_image_arr); // get image storage $storage = new storage(); $storage->get_instance_by_id($image_storageid); $storage_resource_id = $storage->resource_id; // get storage resource $resource = new resource(); $resource->get_instance_by_id($storage_resource_id); $resource_id = $resource->id; $resource_ip = $resource->ip; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Sending clone command to {$resource_ip} to create Image {$image_clone_name}", "", "", 0, 0, 0); $storage_clone_timeout = 60; $cloudstorage = new cloudstorage(); $cloudstorage->create_clone($cloud_image_id, $image_clone_name, $ci_disk_size, $storage_clone_timeout); // be sure to have the create command run before appliance start / storage auth hook sleep(5); } else { // non shared ! $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Not cloning Image " . $image_name . ".", "", "", 0, 0, 0); // we put it into an cloudimage too but it won't get removed $ci_disk_size = 5000; if (strlen($cr->disk_req)) { $ci_disk_size = $cr->disk_req; } // get a new ci_id $cloud_image_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $cloud_image_arr = array('ci_id' => $cloud_image_id, 'ci_cr_id' => $cr->id, 'ci_image_id' => $appliance->imageid, 'ci_appliance_id' => $appliance->id, 'ci_resource_id' => $appliance->resources, 'ci_disk_size' => $ci_disk_size, 'ci_state' => 1); $cloud_image = new cloudimage(); $cloud_image->add($cloud_image_arr); } // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "#### Cloud Phase II-3 - Appliance start", "", "", 0, 0, 0); // ################################## start appliance ############################### // assign the resource $kernel = new kernel(); $kernel->get_instance_by_id($appliance->kernelid); $resource = new resource(); $resource->get_instance_by_id($appliance->resources); // in case we do not have an external ip-config send the resource ip to the user $resource_external_ip = $resource->ip; // ################################## ip-mgmt assing ############################### // check ip-mgmt $cc_conf = new cloudconfig(); $show_ip_mgmt = $cc_conf->get_value(26); // ip-mgmt enabled ? if (!strcmp($show_ip_mgmt, "true")) { if (file_exists("{$RootDir}/plugins/ip-mgmt/.running")) { require_once "{$RootDir}/plugins/ip-mgmt/class/ip-mgmt.class.php"; $ip_mgmt_array = explode(",", $cr->ip_mgmt); $ip_mgmt_assign_loop = 1; foreach ($ip_mgmt_array as $ip_mgmt_config_str) { $collon_pos = strpos($ip_mgmt_config_str, ":"); $nic_id = substr($ip_mgmt_config_str, 0, $collon_pos); $ip_mgmt_id = substr($ip_mgmt_config_str, $collon_pos + 1); if (!strlen($ip_mgmt_id)) { continue; } $orginal_ip_mgmt_id = $ip_mgmt_id; $ip_mgmt_assign = new ip_mgmt(); $ip_mgmt_id_final = $ip_mgmt_id; // we need to check if the ip is still free $ip_mgmt_object_arr = $ip_mgmt_assign->get_instance('id', $ip_mgmt_id); $ip_app_id = $ip_mgmt_object_arr['ip_mgmt_appliance_id']; if ($ip_app_id > 0) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "WARNING: ip-mgmt id " . $ip_mgmt_id . " is already in use. Trying to find the next free ip..", "", "", 0, 0, 0); $ip_mgmt_id = -2; } else { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "SUCCESS: ip-mgmt id " . $ip_mgmt_id . " is free.", "", "", 0, 0, 0); } // if ip_mgmt_id == auto (-2) search the next free ip for the user if ($ip_mgmt_id == -2) { $ip_mgmt_list_per_user = $ip_mgmt_assign->get_list_by_user($cu->cg_id); $next_free_ip_mgmt_id = 0; foreach ($ip_mgmt_list_per_user as $list) { $possible_next_ip_mgmt_id = $list['ip_mgmt_id']; $possible_next_ip_mgmt_object_arr = $ip_mgmt_assign->get_instance('id', $possible_next_ip_mgmt_id); if ($possible_next_ip_mgmt_object_arr['ip_mgmt_appliance_id'] == NULL) { // we have found the next free ip-mgmt id $next_free_ip_mgmt_id = $possible_next_ip_mgmt_id; $ip_mgmt_id_final = $possible_next_ip_mgmt_id; break; } } if ($next_free_ip_mgmt_id == 0) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "WARNING: Could not find the next free ip-mgmt id for appliance " . $appliance_id . ".", "", "", 0, 0, 0); continue; } else { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "SUCCESS: Found the next free ip-mgmt id " . $next_free_ip_mgmt_id . " for appliance " . $appliance_id . ".", "", "", 0, 0, 0); $ip_mgmt_id = $next_free_ip_mgmt_id; // here we have to update the cr with the new ip-mgmt-id $new_cr_ip_mgmt_str = str_replace($nic_id . ":" . $orginal_ip_mgmt_id, $nic_id . ":" . $ip_mgmt_id, $cr->ip_mgmt); $new_cr_ip_mgmt_fields = array(); $new_cr_ip_mgmt_fields["cr_ip_mgmt"] = $new_cr_ip_mgmt_str; $cr->update($cr->id, $new_cr_ip_mgmt_fields); $cr->get_instance_by_id($cr->id); } } // here we have a valid ip-mgmt opbject to update $ip_mgmt_fields = array(); $ip_mgmt_fields["ip_mgmt_appliance_id"] = $appliance_id; $ip_mgmt_fields["ip_mgmt_nic_id"] = $nic_id; $ip_mgmt_assign->update_ip($ip_mgmt_id, $ip_mgmt_fields); // set resource_external_ip if ($ip_mgmt_assign_loop == 1) { $ip_mgmt_assign_arr = $ip_mgmt_assign->get_config_by_id($ip_mgmt_id_final); $resource_external_ip = $ip_mgmt_assign_arr[0]['ip_mgmt_address']; } $ip_mgmt_assign_loop++; } } } // ##################################################################################### // assign kernel $openqrm_server->send_command("openqrm_assign_kernel {$resource->id} {$resource->mac} {$kernel->name}"); //refresh the object before in case of clone-on-deploy $appliance->get_instance_by_id($appliance_id); // hybrid-cloud deployment needs image password + application classes set before appliance start // get the password to send to the user from the request $cr->get_instance_by_id($cr_id); $appliance_password = $cr->image_password; $image = new image(); $image->set_root_password($appliance->imageid, $appliance_password); $image->get_instance_by_id($appliance->imageid); $appliance_login_user = "******"; if ($image->version == 'Windows') { $appliance_login_user = "******"; } // add the user + image_password to the request $cr_update_fields = array(); $cr_update_fields['cr_image_password'] = $appliance_login_user . ":" . $appliance_password . ":" . $resource_external_ip; $cr->update($cr_id, $cr_update_fields); // ################################## apply application groups ############################### // check if application is enabled $application_conf = new cloudconfig(); $show_application_groups = $application_conf->get_value(11); // show_puppet_groups if (!strcmp($show_application_groups, "true")) { // check if we have a application config in the request $application_appliance = $appliance->name; if (strlen($cr->puppet_groups)) { $application_groups_str = $cr->puppet_groups; $application_appliance = $appliance->name; $application_debug = "Applying {$application_groups_str} to appliance {$application_appliance}"; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", $application_debug, "", "", 0, 0, 0); $application_group_array = explode(",", $cr->puppet_groups); $cloudapplication = new cloudapplication(); $cloudapplication->set_applications($appliance->name, $application_group_array); } } // update appliance id in request $cr->get_instance_by_id($cr->id); $cr->setappliance("add", $appliance_id); // update request status to starting $cr->setstatus($cr_id, "starting"); // wait until the resource got the new kernel and application assigned sleep(2); // check if we continue or go on $parallel_phase_two_actions++; if ($max_parallel_phase_two_actions > 0 && $parallel_phase_two_actions >= $max_parallel_phase_two_actions) { break; } } // #################### appliance starting phase ################################ $parallel_phase_two_actions = 0; $crl = new cloudrequest(); $cr_list = $crl->get_all_starting_ids(); foreach ($cr_list as $list) { $cr_id = $list['cr_id']; $cr = new cloudrequest(); $cr->get_instance_by_id($cr_id); $cr_status = $cr->status; $cu = new clouduser(); $cr_cu_id = $cr->cu_id; $cu->get_instance_by_id($cr_cu_id); $cu_name = $cu->name; // update appliance id in request unset($appliance_id); $appliance_id = $cr->appliance_id; if (!strlen($appliance_id)) { continue; } $appliance = new appliance(); $appliance->get_instance_by_id($appliance_id); $image = new image(); $image->get_instance_by_id($appliance->imageid); // run authblocker from plugin hook if existing $deployment = new deployment(); $deployment->get_instance_by_type($image->type); $deployment_type = $deployment->type; $deployment_plugin_name = $deployment->storagetype; $storage_auth_hook = $RootDir . "/plugins/" . $deployment_plugin_name . "/openqrm-" . $deployment_type . "-auth-hook.php"; if (file_exists($storage_auth_hook)) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Found deployment type " . $deployment_type . " handling the start auth hook.", "", "", 0, 0, $appliance->resources); if ($appliance->ssi == 2) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Already created authblocker for image " . $image->name . " / app id " . $appliance->id . ".", "", "", 0, 0, $appliance->resources); } else { // create storage_auth_blocker if not existing already unset($authblocker); $authblocker = new authblocker(); $authblocker->get_instance_by_image_name($image->name); if (!strlen($authblocker->id)) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Creating new authblocker for image " . $image->name . " / app id " . $appliance->id . ".", "", "", 0, 0, $appliance->resources); $ab_start_time = $_SERVER['REQUEST_TIME']; $ab_create_fields['ab_image_id'] = $image->id; $ab_create_fields['ab_image_name'] = $image->name; $ab_create_fields['ab_start_time'] = $ab_start_time; $ab_create_fields['ab_id'] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $authblocker->add($ab_create_fields); // update appliance->ssi saving that the authblocker was created $ab_appliance_update = array('appliance_ssi' => 2); $appliance->update($appliance_id, $ab_appliance_update); $appliance->get_instance_by_id($appliance_id); // run the auth hook require_once "{$storage_auth_hook}"; storage_auth_function("start", $appliance->id); continue; } else { $ab_appliance_update = array('appliance_ssi' => 2); $appliance->update($appliance_id, $ab_appliance_update); $appliance->get_instance_by_id($appliance_id); $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Found existing authblocker for image " . $image->name . " / app id " . $appliance->id . ".", "", "", 0, 0, $appliance->resources); } } $storage_auth_blocker_created = true; } else { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "No storage-auth hook (" . $storage_auth_hook . ") available for deployment type " . $deployment_type . " for start auth hook.", "", "", 0, 0, $appliance->resources); $storage_auth_blocker_created = false; } // if authblocker was created if ($storage_auth_blocker_created) { if ($appliance->ssi == 2) { unset($check_authblocker); $check_authblocker = new authblocker(); $check_authblocker->get_instance_by_image_name($image->name); if (strlen($check_authblocker->id)) { // ab still existing, check timeout $ab_check_now = $_SERVER['REQUEST_TIME']; $ab_check_timeout = $check_authblocker->start_time + $cloud_volume_clone_timeout; if ($ab_check_now > $ab_check_timeout) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Failed starting server " . $appliance->id . ". Storage-authentication timed out!", "", "", 0, 0, $appliance->resources); // remove authblocker $check_authblocker->remove($check_authblocker->id); // set cr to dreprovision to automatically clean up the created objects $cr->setstatus($cr_id, 'deprovision'); // send mail to user and admin $ab_check_conf = new cloudconfig(); $ab_check_admin_email = $ab_check_conf->get_value(1); // 1 is admin_email $rmail = new cloudmailer(); $rmail->to = "{$cu->email}"; $rmail->from = "{$ab_check_admin_email}"; $rmail->subject = "openQRM Cloud: Not enough resources to provision storage for request " . $cr_id . ". Deprovisioning request " . $cr_id . "!"; $rmail->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "{$cu->forename}", '@@LASTNAME@@' => "{$cu->lastname}", '@@YOUR@@' => "your", '@@CLOUDADMIN@@' => $ab_check_admin_email); $rmail->var_array = $arr; $rmail->send(); $rmail_admin = new cloudmailer(); $rmail_admin->to = "{$ab_check_admin_email}"; $rmail_admin->from = "{$ab_check_admin_email}"; $rmail_admin->subject = "openQRM Cloud: Not enough resources to provision storage for request " . $cr_id . ". Deprovisioning request " . $cr_id . "!"; $rmail_admin->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "Cloudadmin", '@@LASTNAME@@' => "", '@@YOUR@@' => "the", '@@CLOUDADMIN@@' => $ab_check_admin_email); $rmail_admin->var_array = $arr; $rmail_admin->send(); continue; } else { // we are still waiting for the auth-blocker to be removed $still_wait = $ab_check_timeout - $ab_check_now; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Still waiting " . $still_wait . " sec. for storage-auth for server " . $appliance->id . ".", "", "", 0, 0, $appliance->resources); continue; } } else { // here we got the remove-auth-blocker message from the storage-auth hook // now we can be sure that storage auth ran before rebooting the resource $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Storage authentication for image " . $image->name . " succeeded, assigning the resource now.", "", "", 0, 0, $appliance->resources); } } } // update request status to active $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Starting server " . $appliance_id . " for request ID " . $cr_id, "", "", 0, 0, 0); $cr->setstatus($cr_id, "active"); $ab_appliance_update = array('appliance_ssi' => $cr->shared_req); $appliance->update($appliance_id, $ab_appliance_update); $appliance->get_instance_by_id($appliance_id); // run appliance start with no auth hook $appliance->start(false); // here we insert the new appliance into the cloud-appliance table $cloud_appliance_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $cloud_appliance_arr = array('ca_id' => $cloud_appliance_id, 'ca_cr_id' => $cr->id, 'ca_appliance_id' => $appliance_id, 'ca_cmd' => 0, 'ca_state' => 1); $cloud_appliance = new cloudappliance(); $cloud_appliance->add($cloud_appliance_arr); // ################################## mail user provisioning ############################### // get the user + image_password from the request $appliance_login_credentials = explode(':', $cr->image_password); $login_user = $appliance_login_credentials[0]; $login_password = $appliance_login_credentials[1]; $login_ip = $appliance_login_credentials[2]; // remove user + pass from request $cr_update_fields = array(); $cr_update_fields['cr_image_password'] = ''; $cr->update($cr_id, $cr_update_fields); // send mail to user // get admin email $cc_conf = new cloudconfig(); $cc_admin_email = $cc_conf->get_value(1); // 1 is admin_email $cc_allow_vnc = $cc_conf->get_value(42); // 1 is admin_email // get user + request + appliance details $cu_id = $cr->cu_id; $cu = new clouduser(); $cu->get_instance_by_id($cu_id); $cu_name = $cu->name; $cu_forename = $cu->forename; $cu_lastname = $cu->lastname; $cu_email = $cu->email; // start/stop time $cr_start = $cr->start; $start = date("d-m-Y H-i", $cr_start); $cr_stop = $cr->stop; $stop = date("d-m-Y H-i", $cr_stop); // check for allow_vnc $vnc_informations = ""; if (!strcmp($cc_allow_vnc, "true")) { $vnc_resource = new resource(); $vnc_resource->get_instance_by_id($appliance->resources); $vnc_informations_file = $OPENQRM_SERVER_BASE_DIR . "/openqrm/web/action/cloud-conf/" . $vnc_resource->mac . ".vnc"; if (file_exists($vnc_informations_file)) { $vnc_infos = file_get_contents($vnc_informations_file); $vnc_informations = "You can login to the System console via VNC at " . trim($vnc_infos); unlink($vnc_informations_file); } else { $vnc_informations = "You can login to the System console via VNC using the above password"; } } $arr = array('@@ID@@' => $cr_id, '@@FORENAME@@' => $cu_forename, '@@LASTNAME@@' => $cu_lastname, '@@START@@' => $start, '@@STOP@@' => $stop, '@@PASSWORD@@' => $login_password, '@@LOGINUSER@@' => $login_user, '@@IP@@' => $login_ip, '@@RESNUMBER@@' => '1', '@@HOSTNAME@@' => $appliance->name, '@@VNC@@' => $vnc_informations, '@@CLOUDADMIN@@' => $cc_admin_email); // send mail to the user $rmail = new cloudmailer(); $rmail->to = "{$cu_email}"; $rmail->from = "{$cc_admin_email}"; $rmail->subject = "openQRM Cloud: Your resource for request " . $cr_id . " is now active"; $rmail->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/active_cloud_request.mail.tmpl"; $rmail->var_array = $arr; $rmail->send(); // mail the ip + root password to the cloud admin $rmail_admin = new cloudmailer(); $rmail_admin->to = "{$cc_admin_email}"; $rmail_admin->from = "{$cc_admin_email}"; $rmail_admin->subject = "openQRM Cloud: The resource for request " . $cr_id . " is now active"; $rmail_admin->template = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/etc/mail/active_cloud_request_admin.mail.tmpl"; $rmail_admin->var_array = $arr; $rmail_admin->send(); // ################################## setup access to collectd graphs #################### // check if collectd is enabled $collectd_conf = new cloudconfig(); $show_collectd_graphs = $collectd_conf->get_value(19); // show_collectd_graphs if (!strcmp($show_collectd_graphs, "true")) { // is collectd enabled ? if (file_exists("{$RootDir}/plugins/collectd/.running")) { // ldap or regular user ? $collectd_appliance = $appliance->name; if (file_exists("{$RootDir}/plugins/ldap/.running")) { $collectd_debug = "Setting up access to the collectd graphs of appliance {$collectd_appliance} for ldap Cloud user {$cu_name}"; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", $collectd_debug, "", "", 0, 0, 0); // get ldap from db config $ldap_conf = new ldapconfig(); $ldap_conf->get_instance_by_id(1); $ldap_host = $ldap_conf->value; $ldap_conf->get_instance_by_id(2); $ldap_port = $ldap_conf->value; $ldap_conf->get_instance_by_id(3); $ldap_base_dn = $ldap_conf->value; $ldap_conf->get_instance_by_id(4); $ldap_admin = $ldap_conf->value; $ldap_conf->get_instance_by_id(5); $ldap_password = $ldap_conf->value; // send command to the openQRM-server $setup_collectd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/bin/openqrm-cloud-manager setup-graph-ldap " . $collectd_appliance . " " . $cu_name . " " . $ldap_host . " " . $ldap_port . " " . $ldap_base_dn . " " . $ldap_password; $openqrm_server->send_command($setup_collectd); } else { // regular basic auth user $collectd_debug = "Setting up access to the collectd graphs of appliance {$collectd_appliance} for Cloud user {$cu_name}"; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", $collectd_debug, "", "", 0, 0, 0); // here we still have the valid user object, get the password $cu_pass = $cu->password; // send command to the openQRM-server $setup_collectd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/bin/openqrm-cloud-manager setup-graph " . $collectd_appliance . " " . $cu_name . " " . $cu_pass; $openqrm_server->send_command($setup_collectd); } } } // ################################## provision finished #################### // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "#### Cloud Phase II-4 - Provisioning $cr_resource_number finished", "", "", 0, 0, 0); // check if we continue or go on $parallel_phase_two_actions++; if ($max_parallel_phase_two_actions > 0 && $parallel_phase_two_actions >= $max_parallel_phase_two_actions) { break; } } // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "Cloud Phase III - Billing loop", "", "", 0, 0, 0); // new active cr loop $cr_list = $crl->get_all_active_ids(); foreach ($cr_list as $list) { $phase_three_actions = 0; $cr_id = $list['cr_id']; $cr = new cloudrequest(); $cr->get_instance_by_id($cr_id); $cr_status = $cr->status; // #################### monitoring for billing ################################ // billing, only care about active requests $cb_config = new cloudconfig(); $cloud_billing_enabled = $cb_config->get_value(16); // 16 is cloud_billing_enabled $cloud_deprovision_warning = $cb_config->get_value(38); // 38 is deprovision_warning $cloud_deprovision_pause = $cb_config->get_value(39); // 38 is deprovision_pause if ($cloud_billing_enabled != 'true') { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Cloud-billing is disabled. Not charging User {$cu->name} for request ID {$cr_id}", "", "", 0, 0, 0); } else { $one_hour = 3600; $one_day = 43200; // every 12 hours $now = $_SERVER['REQUEST_TIME']; $cu_id = $cr->cu_id; $cu = new clouduser(); $cu->get_instance_by_id($cu_id); $no_ccu_mail_conf = new cloudconfig(); $no_ccu_mail_admin_email = $no_ccu_mail_conf->get_value(1); // 1 is admin_email $cu_ccunits = $cu->ccunits; // be sure to get a value from the db if (!is_numeric($cu_ccunits)) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Got empty CCU value /" . $cu_ccunits . "/ for User " . $cu->name . "/" . $cu_id . " request ID " . $cr_id, "", "", 0, 0, 0); continue; } // in case the user has no ccunits any more we set the status to deprovision if ($cu_ccunits <= 0) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "No CCUs left for User {$cu->name}, setting CR {$cr_id} to deprovisioning", "", "", 0, 0, 0); $cr->setstatus($cr_id, "deprovision"); // send mail to user $deprovision_no_ccu_mail = new cloudmailer(); $deprovision_no_ccu_mail->to = $cu->email; $deprovision_no_ccu_mail->from = $no_ccu_mail_admin_email; $deprovision_no_ccu_mail->subject = "openQRM Cloud: No CCUs left! Deprovisioning request " . $cr_id; $deprovision_no_ccu_mail->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/deprovision_no_ccu.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "{$cu->forename}", '@@LASTNAME@@' => "{$cu->lastname}", '@@CLOUDADMIN@@' => $no_ccu_mail_admin_email); $deprovision_no_ccu_mail->var_array = $arr; $deprovision_no_ccu_mail->send(); continue; } else { if ($cu_ccunits <= $cloud_deprovision_pause) { $pause_app_array = explode(",", $cr->appliance_id); if (is_array($pause_app_array)) { foreach ($pause_app_array as $pause_app_id) { $pause_app = new appliance(); $pause_app->get_instance_by_id($pause_app_id); if (!strcmp($pause_app->state, "active")) { // inform the user $deprovision_pause_mail = new cloudmailer(); $deprovision_pause_mail->to = $cu->email; $deprovision_pause_mail->from = $no_ccu_mail_admin_email; $deprovision_pause_mail->subject = "openQRM Cloud: Warning! Pausing the Appliance of Cloud request " . $cr_id; $deprovision_pause_mail->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/deprovision_pause.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "{$cu->forename}", '@@LASTNAME@@' => "{$cu->lastname}", '@@CCUS@@' => "{$cu_ccunits}", '@@CLOUDADMIN@@' => $no_ccu_mail_admin_email); $deprovision_pause_mail->var_array = $arr; $deprovision_pause_mail->send(); // pause appliance $pause_cloud_appliance = new cloudappliance(); $pause_cloud_appliance->get_instance_by_appliance_id($pause_app_id); if ($pause_cloud_appliance->cmd == 0) { if ($pause_cloud_appliance->state == 1) { $pause_cloud_appliance->set_cmd($pause_cloud_appliance->id, "stop"); $pause_cloud_appliance->set_state($pause_cloud_appliance->id, "paused"); } } } } } } else { if ($cu_ccunits <= $cloud_deprovision_warning) { // once per day per cr we send mail for deprovisioning warning $check_cr_now = false; $cloud_user_lock_per_day = dirname($cloud_monitor_lock) . '/' . $cu->name . '.' . $cr_id . '.per_day.lock'; if (!file_exists($cloud_user_lock_per_day)) { file_put_contents($cloud_user_lock_per_day, time()); $check_cr_now = true; } $lastwarning = file_get_contents($cloud_user_lock_per_day); $currenttimestamp = time(); if ($currenttimestamp - $lastwarning >= $one_day || $check_cr_now == 'true') { file_put_contents($cloud_user_lock_per_day, time()); $deprovision_warning_mail = new cloudmailer(); $deprovision_warning_mail->to = $cu->email; $deprovision_warning_mail->from = $no_ccu_mail_admin_email; $deprovision_warning_mail->subject = "openQRM Cloud: Warning! Your CCUs are running low for Cloud request " . $cr_id; $deprovision_warning_mail->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/deprovision_warning.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "{$cu->forename}", '@@LASTNAME@@' => "{$cu->lastname}", '@@CCUS@@' => "{$cu_ccunits}", '@@CLOUDADMIN@@' => $no_ccu_mail_admin_email); $deprovision_warning_mail->var_array = $arr; $deprovision_warning_mail->send(); } } } } // check if to charge $charge = false; $cr_lastbill = $cr->lastbill; if (!strlen($cr_lastbill)) { // we set the last-bill time to now and bill $cr->set_requests_lastbill($cr_id, $now); $charge = true; } else { // we check if we need to bill according the last-bill var $active_cr_time = $now - $cr_lastbill; if ($active_cr_time >= $one_hour) { // set lastbill to now $cr->set_requests_lastbill($cr_id, $now); $charge = true; } } if ($charge) { // here we calculate what to charge // cloudselector enabled ? $show_cloud_selector = $cb_config->get_value(22); // cloud_selector if (!strcmp($show_cloud_selector, "true")) { $ct = new cloudtransaction(); $cloudselector = new cloudselector(); // we need to loop through all appliances of this request // and only charge for active ones $cs_active_apps = 0; $new_cu_ccunits = $cu_ccunits; $cs_app_array = explode(",", $cr->appliance_id); if (is_array($cs_app_array)) { foreach ($cs_app_array as $cs_app_id) { $cs_app = new appliance(); $cs_app->get_instance_by_id($cs_app_id); if (!strcmp($cs_app->state, "active")) { // cpu $cpu_cost = $cloudselector->get_price($cr->cpu_req, "cpu"); $new_cu_ccunits = substract_until_zero($new_cu_ccunits, $cpu_cost); $ct->push($cr->id, $cr->cu_id, $cpu_cost, $new_cu_ccunits, "Cloud Billing", "{$cpu_cost} CCUs for {$cr->cpu_req} CPU(s) Appliance {$cs_app_id} (CR {$cr->id})"); // disk $disk_cost = $cloudselector->get_price($cr->disk_req, "disk"); $new_cu_ccunits = substract_until_zero($new_cu_ccunits, $disk_cost); $ct->push($cr->id, $cr->cu_id, $disk_cost, $new_cu_ccunits, "Cloud Billing", "{$disk_cost} CCUs for {$cr->disk_req} MB Disk Space Appliance {$cs_app_id} (CR {$cr->id})"); // ha if (strlen($cr->ha_req)) { $ha_cost = $cloudselector->get_price($cr->ha_req, "ha"); $new_cu_ccunits = substract_until_zero($new_cu_ccunits, $ha_cost); $ct->push($cr->id, $cr->cu_id, $ha_cost, $new_cu_ccunits, "Cloud Billing", "{$ha_cost} CCUs for High-Availability Appliance {$cs_app_id} (CR {$cr->id})"); } // kernel $kernel_cost = $cloudselector->get_price($cr->kernel_id, "kernel"); $new_cu_ccunits = substract_until_zero($new_cu_ccunits, $kernel_cost); $ct->push($cr->id, $cr->cu_id, $kernel_cost, $new_cu_ccunits, "Cloud Billing", "{$kernel_cost} CCUs for Kernel {$cr->kernel_id} Appliance {$cs_app_id} (CR {$cr->id})"); // memory $memory_cost = $cloudselector->get_price($cr->ram_req, "memory"); $new_cu_ccunits = substract_until_zero($new_cu_ccunits, $memory_cost); $ct->push($cr->id, $cr->cu_id, $memory_cost, $new_cu_ccunits, "Cloud Billing", "{$memory_cost} CCUs for {$cr->ram_req} MB Memory Appliance {$cs_app_id} (CR {$cr->id})"); // network $network_cost = $cloudselector->get_price($cr->network_req, "network"); $new_cu_ccunits = substract_until_zero($new_cu_ccunits, $network_cost); $ct->push($cr->id, $cr->cu_id, $network_cost, $new_cu_ccunits, "Cloud Billing", "{$network_cost} CCUs for {$cr->network_req} Network Card(s) Appliance {$cs_app_id} (CR {$cr->id})"); // application $application_groups_array = explode(",", $cr->puppet_groups); if (is_array($application_groups_array)) { foreach ($application_groups_array as $application_group) { if (strlen($application_group)) { $application_group_cost = $cloudselector->get_price($application_group, "application"); $new_cu_ccunits = substract_until_zero($new_cu_ccunits, $application_group_cost); $ct->push($cr->id, $cr->cu_id, $application_group_cost, $new_cu_ccunits, "Cloud Billing", "{$application_group_cost} CCUs for Application {$application_group} Appliance {$cs_app_id} (CR {$cr->id})"); } } } // resource type $cs_virtualization = new virtualization(); $cs_virtualization->get_instance_by_id($cr->resource_type_req); $resource_cost = $cloudselector->get_price($cr->resource_type_req, "resource"); $new_cu_ccunits = substract_until_zero($new_cu_ccunits, $resource_cost); $ct->push($cr->id, $cr->cu_id, $resource_cost, $new_cu_ccunits, "Cloud Billing", "{$resource_cost} CCUs for Type {$cs_virtualization->name} Appliance {$cs_app_id} (CR {$cr->id})"); $cs_active_apps++; } else { // we only charge for the diskspace used // disk $disk_cost = $cloudselector->get_price($cr->disk_req, "disk"); $new_cu_ccunits = substract_until_zero($new_cu_ccunits, $disk_cost); $ct->push($cr->id, $cr->cu_id, $disk_cost, $new_cu_ccunits, "Cloud Billing", "{$disk_cost} CCUs for {$cr->disk_req} MB Disk Space Appliance {$cs_app_id} (CR {$cr->id})"); } } } } else { // or custom billing $new_cu_ccunits = openqrm_custom_cloud_billing($cr_id, $cu_id, $cu_ccunits); } $cu->set_users_ccunits($cu_id, $new_cu_ccunits); $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Charging User {$cu->name} for request ID {$cr_id}", "", "", 0, 0, 0); $phase_three_actions = 1; } } // #################### check for deprovisioning ################################ // de-provision, check if it is time or if status deprovisioning $cr = new cloudrequest(); $cr->get_instance_by_id($cr_id); // check for stop time $now = $_SERVER['REQUEST_TIME']; $cr_stop = $cr->stop; if ($cr_stop < $now) { // set to deprovisioning $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "CR {$cr_id} stop time reached, setting to deprovisioning", "", "", 0, 0, 0); $cr->setstatus($cr_id, "deprovision"); } // check if we continue or go on if ($phase_three_actions == 1) { $parallel_phase_three_actions++; if ($max_parallel_phase_three_actions > 0 && $parallel_phase_three_actions >= $max_parallel_phase_three_actions) { break; } } } // #################### deprovisioning ################################ // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "Cloud Phase IV - Deprovisioning", "", "", 0, 0, 0); // new deprovision cr loop $cr_list = $crl->get_all_deprovisioned_ids(); foreach ($cr_list as $list) { $cr_id = $list['cr_id']; $cr = new cloudrequest(); $cr->get_instance_by_id($cr_id); $cu_id = $cr->cu_id; $cu = new clouduser(); $cu->get_instance_by_id($cu_id); $cr_has_appliance = 1; $cloud_user_lock_per_day = dirname($cloud_monitor_lock) . '/' . $cu->name . '.' . $cr_id . '.per_day.lock'; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Deprovisioning of Cloud request ID {$cr_id}", "", "", 0, 0, 0); if (file_exists($cloud_user_lock_per_day)) { unlink($cloud_user_lock_per_day); } // get the requests appliance $cr_appliance_id = $cr->appliance_id; if (!strlen($cr_appliance_id)) { // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Request $cr_id does not have an active appliance!", "", "", 0, 0, 0); $cr_has_appliance = 0; } if ($cr_appliance_id == 0) { // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Request $cr_id does not have an active appliance!", "", "", 0, 0, 0); $cr_has_appliance = 0; } // in case a cr got deprovisioned with an active creaet-vm-lc but no cloud_appliance and/or cloud_image yet // this will remove the left over vm + resource if ($cr_has_appliance == 0) { // check if a vm was requested if ($cr->resource_type_req != 1) { // check if we have a create-vm-lc, if we have one auto-create-vm is true $deprovision_cr_resource = 1; $deprovision_cr_create_vm_lc = new cloudcreatevmlc(); $deprovision_cr_create_vm_lc->get_instance_by_cr_details($cr_id, $deprovision_cr_resource); if (strlen($deprovision_cr_create_vm_lc->id)) { if ($deprovision_cr_create_vm_lc->resource_id != 0) { // remove the vm $auto_deprovision_resource = new resource(); $auto_deprovision_resource->get_instance_by_id($deprovision_cr_create_vm_lc->resource_id); $auto_deprovision_resource_name = "cloud-" . $cr_id . "-" . $deprovision_cr_resource . "-x"; // cloudvm->remove ..... $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "Auto-removing resource {$deprovision_cr_create_vm_lc->resource_id} - {$cr->resource_type_req}, {$auto_deprovision_resource_name}, {$auto_deprovision_resource->mac}", "", "", 0, 0, 0); $auto_cloudvm = new cloudvm(); $auto_cloudvm->remove($deprovision_cr_create_vm_lc->resource_id, $cr->resource_type_req, $auto_deprovision_resource_name, $auto_deprovision_resource->mac); } // remove the create-vm-lx $deprovision_cr_create_vm_lc->remove($deprovision_cr_create_vm_lc->id); } } $cr->setstatus($cr_id, "done"); continue; } // ################################## de-provisioning ############################### // count the resource we deprovision for the request $deprovision_resource_number = 1; // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Deprovisioning appliance $cr_appliance_id from request ID $cr_id", "", "", 0, 0, 0); // stop the appliance, first de-assign its resource $appliance = new appliance(); $appliance->get_instance_by_id($cr_appliance_id); // .. only if active and not stopped already by the user $cloud_appliance = new cloudappliance(); $cloud_appliance->get_instance_by_appliance_id($appliance->id); if ($cloud_appliance->state == 0) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Appliance {$cr_appliance_id} from request ID {$cr_id} stopped already", "", "", 0, 0, 0); } else { if ($appliance->resources != -1) { $resource = new resource(); $resource->get_instance_by_id($appliance->resources); $resource_external_ip = $resource->ip; $openqrm_server->send_command("openqrm_assign_kernel {$resource->id} {$resource->mac} default"); // let the kernel assign command finish sleep(2); // now stop // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Stopping Appliance $cr_appliance_id from request ID $cr_id", "", "", 0, 0, 0); $appliance->stop(); } } // ################################## de-assign ip-mgmt ############################### // here we free up the ip addresses used by the appliance again // check ip-mgmt $cc_conf = new cloudconfig(); $show_ip_mgmt = $cc_conf->get_value(26); // ip-mgmt enabled ? if (!strcmp($show_ip_mgmt, "true")) { if (file_exists("{$RootDir}/plugins/ip-mgmt/.running")) { require_once "{$RootDir}/plugins/ip-mgmt/class/ip-mgmt.class.php"; $ip_mgmt_array = explode(",", $cr->ip_mgmt); foreach ($ip_mgmt_array as $ip_mgmt_config_str) { $collon_pos = strpos($ip_mgmt_config_str, ":"); $nic_id = substr($ip_mgmt_config_str, 0, $collon_pos); $ip_mgmt_id = substr($ip_mgmt_config_str, $collon_pos + 1); $ip_mgmt_fields = array(); $ip_mgmt_fields["ip_mgmt_appliance_id"] = NULL; $ip_mgmt_fields["ip_mgmt_nic_id"] = NULL; $ip_mgmt_assign = new ip_mgmt(); $ip_mgmt_assign->update_ip($ip_mgmt_id, $ip_mgmt_fields); } } } // ##################################################################################### // here we remove the appliance from the cloud-appliance table $cloud_appliance = new cloudappliance(); $cloud_appliance->get_instance_by_appliance_id($appliance->id); if (strlen($cloud_appliance->id)) { $cloud_appliance->remove($cloud_appliance->id); } // ################################## remove application groups ############################### // check if application is enabled $application_conf = new cloudconfig(); $show_application_groups = $application_conf->get_value(11); // show_puppet_groups if (!strcmp($show_application_groups, "true")) { $application_appliance = $appliance->name; if (strlen($cr->puppet_groups)) { $application_debug = "Removing applications from appliance " . $application_appliance . "."; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", $application_debug, "", "", 0, 0, 0); $application_group_array = explode(",", $cr->puppet_groups); $cloudapplication = new cloudapplication(); $cloudapplication->remove_applications($appliance->name, $application_group_array); } } // ################################## deprovisioning clone-on-deploy ############################### // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Removing cloudimage for request ID $cr_id", "", "", 0, 0, 0); // here we set the state of the cloud-image to remove // this will check the state of the resource which still has // the image as active rootfs. If the resource is idle again the // image will be removed. // The check for this mechanism is being executed at the beginning // of each cloud-monitor loop if ($appliance->imageid > 0) { $cloud_image = new cloudimage(); $cloud_image->get_instance_by_image_id($appliance->imageid); $cloud_image->set_state($cloud_image->id, "remove"); } // ################################## deprovisioning mail user ############################### // remove appliance_id from request $cr->get_instance_by_id($cr->id); $cr->setappliance("remove", $appliance->id); // when we are at the last resource for the request set status to 6 = done if ($deprovision_resource_number == $cr->resource_quantity) { $cr->setstatus($cr_id, "done"); // set lastbill empty $cr->set_requests_lastbill($cr_id, ''); } // send mail to user for deprovision started // get admin email $cc_conf = new cloudconfig(); $cc_admin_email = $cc_conf->get_value(1); // 1 is admin_email // get user + request + appliance details $cu_name = $cu->name; $cu_forename = $cu->forename; $cu_lastname = $cu->lastname; $cu_email = $cu->email; // start/stop time $cr_start = $cr->start; $start = date("d-m-Y H-i", $cr_start); $cr_stop = $cr->stop; $stop = date("d-m-Y H-i", $cr_stop); $eip = ''; if (isset($resource_external_ip)) { $eip = $resource_external_ip; } $rmail = new cloudmailer(); $rmail->to = "{$cu_email}"; $rmail->from = "{$cc_admin_email}"; $rmail->subject = "openQRM Cloud: Your resource from request {$cr_id} is fully deprovisioned now"; $rmail->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/done_cloud_request.mail.tmpl"; $arr = array('@@ID@@' => "{$cr_id}", '@@FORENAME@@' => "{$cu_forename}", '@@LASTNAME@@' => "{$cu_lastname}", '@@START@@' => "{$start}", '@@STOP@@' => "{$stop}", '@@IP@@' => "{$eip}", '@@RESNUMBER@@' => "{$deprovision_resource_number}", '@@CLOUDADMIN@@' => $cc_admin_email); $rmail->var_array = $arr; $rmail->send(); // ################################## remove access to collectd graphs #################### // check if collectd is enabled $collectd_conf = new cloudconfig(); $show_collectd_graphs = $collectd_conf->get_value(19); // show_collectd_graphs if (!strcmp($show_collectd_graphs, "true")) { // is collectd enabled ? if (file_exists("{$RootDir}/plugins/collectd/.running")) { // check if we have a collectd config in the request $collectd_appliance = $appliance->name; $collectd_debug = "Removing access to Collectd graphs of appliance {$collectd_appliance} for Cloud user {$cu_name}"; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", $collectd_debug, "", "", 0, 0, 0); // send command to the openQRM-server $remove_collectd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/cloud/bin/openqrm-cloud-manager remove-graph {$collectd_appliance} {$cu_name}"; $openqrm_server->send_command($remove_collectd); } } // ################################## finsihed de-provision #################### // we cannot remove the appliance here because its image is still in use // and the appliance (id) is needed for the removal // so the image-remove mechanism also cares to remove the appliance // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Deprovisioning request ID $cr_id finished", "", "", 0, 0, 0); $deprovision_resource_number++; // ################################## end de-provisioning ############################### // #################### end deprovisioning cr-loop ################################ // check if we continue or go on $parallel_phase_four_actions++; if ($max_parallel_phase_four_actions > 0 && $parallel_phase_four_actions >= $max_parallel_phase_four_actions) { break; } } // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "Cloud Phase V - Cloudappliance commands", "", "", 0, 0, 0); // ################################## run cloudappliance commands ############################### $cloudapp = new cloudappliance(); $cloudapp_list = $cloudapp->get_all_ids(); foreach ($cloudapp_list as $list) { $phase_five_actions = 0; $ca_id = $list['ca_id']; $ca = new cloudappliance(); $ca->get_instance_by_id($ca_id); $ca_appliance_id = $ca->appliance_id; $ca_cr_id = $ca->cr_id; $ca_cmd = $ca->cmd; $ca_state = $ca->state; switch ($ca_cmd) { case 1: // start // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Appliance start (ca $ca_id / app $ca_appliance_id / cr $ca_cr_id)", "", "", 0, 0, 0); $tappliance = new appliance(); $tappliance->get_instance_by_id($ca_appliance_id); $cloud_image_start = new cloudimage(); $cloud_image_start->get_instance_by_image_id($tappliance->imageid); // resource active (idle) again or ci resource set to -1 (removed) if ($cloud_image_start->resource_id != -1) { $ca_resource = new resource(); $ca_resource->get_instance_by_id($cloud_image_start->resource_id); $tcaid = $cloud_image_start->resource_id; if (strcmp($ca_resource->state, "active") || !strcmp($ca_resource->ip, "0.0.0.0")) { // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Appliance start (ca $ca_id / app $ca_appliance_id / cr $ca_cr_id) : resource $tcaid Not yet active again", "", "", 0, 0, 0); // resource not yet automatic removed in case it is ia vm or not yet active again continue; } } // prepare array to update appliance, be sure to set to auto-select resource $ar_update = array('appliance_resources' => "-1"); // update appliance $ca_appliance = new appliance(); $ca_appliance->update($ca_appliance_id, $ar_update); // lets find a resource for this new appliance according the cr, update the object first $ca_appliance->get_instance_by_id($ca_appliance_id); // get the cr $ca_cr = new cloudrequest(); $ca_cr->get_instance_by_id($ca_cr_id); $appliance_virtualization = $ca_cr->resource_type_req; // prepare mail data $unpause_ca_conf = new cloudconfig(); $unpause_ca_admin_email = $unpause_ca_conf->get_value(1); // 1 is admin_email // and the user details $unpause_cloud_user = new clouduser(); $unpause_cloud_user->get_instance_by_id($ca_cr->cu_id); $unpause_cu_forename = $unpause_cloud_user->forename; $unpause_cu_lastname = $unpause_cloud_user->lastname; $unpause_cu_email = $unpause_cloud_user->email; // ################################## phys. res. ############################### if ($appliance_virtualization == 1) { $ca_appliance->find_resource($appliance_virtualization); // check if we got a resource ! $ca_appliance->get_instance_by_id($ca_appliance_id); if ($ca_appliance->resources == -1) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Could not find a resource (type physical system) for request ID {$ca_cr_id}", "", "", 0, 0, 0); $ca_cr->setstatus($ca_cr_id, 'no-res'); // send mail to user $rmail = new cloudmailer(); $rmail->to = "{$unpause_cu_email}"; $rmail->from = "{$unpause_ca_admin_email}"; $rmail->subject = "openQRM Cloud: Not enough resources to unpause your Cloudappliance " . $ca_id . " from request " . $ca_cr_id; $rmail->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$ca_cr_id}", '@@FORENAME@@' => "{$unpause_cu_forename}", '@@LASTNAME@@' => "{$unpause_cu_lastname}", '@@RESNUMBER@@' => "0", '@@YOUR@@' => "your", '@@CLOUDADMIN@@' => $unpause_ca_admin_email); $rmail->var_array = $arr; $rmail->send(); // send mail to admin $rmail_admin = new cloudmailer(); $rmail_admin->to = "{$unpause_ca_admin_email}"; $rmail_admin->from = "{$unpause_ca_admin_email}"; $rmail_admin->subject = "openQRM Cloud: Not enough resources to unpause Cloudappliance " . $ca_id . " from request " . $ca_cr_id; $rmail_admin->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$ca_cr_id}", '@@FORENAME@@' => "Cloudadmin", '@@LASTNAME@@' => "", '@@RESNUMBER@@' => "0", '@@YOUR@@' => "the", '@@CLOUDADMIN@@' => $unpause_ca_admin_email); $rmail_admin->var_array = $arr; $rmail_admin->send(); continue; } // we have a phys. resource } else { // ################################## auto create vm ############################### // check if we should try to create one // request type vm $unpause_auto_create_vms = $unpause_ca_conf->get_value(7); // 7 is auto_create_vms if (!strcmp($unpause_auto_create_vms, "true")) { // check if createvmlc exists for this cr + res-quantity unset($cvmlc); // use ca id as res.no $cvmlc = new cloudcreatevmlc(); $cvmlc->get_instance_by_cr_details($ca_cr_id, $ca_appliance_id); if (!strlen($cvmlc->request_time)) { // if no createvmlc exists so far create it and the vm // generate a mac address $mac_res = new resource(); // check if we need to generate the first nics mac address in the vmware address space $new_vm_mac = ""; $vm_virt = new virtualization(); $vm_virt->get_instance_by_type($ca_cr->resource_type_req); $virt_name = $vm_virt->get_plugin_name(); switch ($virt_name) { case 'vmware-esx': case 'vmware-server': case 'vmware-server2': $mac_res->generate_mac(); $suggested_mac = $mac_res->mac; $new_forth_byte_first_bit = rand(1, 3); $new_vm_mac = strtolower($vmware_mac_address_space . ":" . substr($suggested_mac, 9)); $new_vm_mac = substr_replace($new_vm_mac, $new_forth_byte_first_bit, 9, 1); break; default: $mac_res->generate_mac(); $new_vm_mac = $mac_res->mac; break; } // additional_nics $new_additional_nics = $ca_cr->network_req; // cpu $new_vm_cpu = $ca_cr->cpu_req; // memory $new_vm_memory = 256; if ($ca_cr->ram_req != 0) { $new_vm_memory = $ca_cr->ram_req; } // disk size $new_vm_disk = 5000; if ($ca_cr->disk_req != 0) { $new_vm_disk = $ca_cr->disk_req; } // generate vnc password and store in cr $pass_gen_image = new image(); $vm_vnc_password = trim($pass_gen_image->generatePassword(12)); $ca_cr_update_fields = array(); $ca_cr_update_fields['cr_image_password'] = $vm_vnc_password; $ca_cr->update($ca_cr->id, $ca_cr_update_fields); // Create VM // here we start the new vm ! $cloudvm = new cloudvm(); // this method returns the resource-id $cloudvm->create($ca_cr->cu_id, $appliance_virtualization, $ca_appliance->name, $new_vm_mac, $new_additional_nics, $new_vm_cpu, $new_vm_memory, $new_vm_disk, $vm_create_timout, $vm_vnc_password, $SOURCE_IMAGE_ID); $new_vm_resource_id = $cloudvm->resource_id; // if we did not find a host resource id will be 0 if ($new_vm_resource_id == 0) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Could not find a VM Host for request ID " . $ca_cr_id . "(unpause)", "", "", 0, 0, 0); $ca_cr->setstatus($ca_cr_id, 'no-res'); // send mail to user $rmail = new cloudmailer(); $rmail->to = "{$unpause_cu_email}"; $rmail->from = "{$unpause_ca_admin_email}"; $rmail->subject = "openQRM Cloud: No VM Host available to unpause your Cloudappliance " . $ca_id . " from request " . $ca_cr_id; $rmail->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$ca_cr_id}", '@@FORENAME@@' => "{$unpause_cu_forename}", '@@LASTNAME@@' => "{$unpause_cu_lastname}", '@@RESNUMBER@@' => "0", '@@YOUR@@' => "your", '@@CLOUDADMIN@@' => $unpause_ca_admin_email); $rmail->var_array = $arr; $rmail->send(); // send mail to admin $rmail_admin = new cloudmailer(); $rmail_admin->to = "{$unpause_ca_admin_email}"; $rmail_admin->from = "{$unpause_ca_admin_email}"; $rmail_admin->subject = "openQRM Cloud: No VM Host available to unpause Cloudappliance " . $ca_id . " from request " . $ca_cr_id; $rmail_admin->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$ca_cr_id}", '@@FORENAME@@' => "Cloudadmin", '@@LASTNAME@@' => "", '@@RESNUMBER@@' => "0", '@@YOUR@@' => "the", '@@CLOUDADMIN@@' => $unpause_ca_admin_email); $rmail_admin->var_array = $arr; $rmail_admin->send(); // reset the cmd field $ca->set_cmd($ca_id, "noop"); // set state to paused $ca->set_state($ca_id, "paused"); continue; } $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Auto-created VM with resource_id {$new_vm_resource_id}", "", "", 0, 0, 0); // create cvmlc after we got a resource_id $vm_create_time = $_SERVER['REQUEST_TIME']; $cvmlc_resource_fields["vc_resource_id"] = $new_vm_resource_id; $cvmlc_resource_fields["vc_cr_id"] = $ca_cr_id; $cvmlc_resource_fields["vc_cr_resource_number"] = $ca_appliance_id; $cvmlc_resource_fields["vc_request_time"] = $vm_create_time; $cvmlc_resource_fields["vc_vm_create_timeout"] = $vm_create_timout; $cvmlc_resource_fields["vc_state"] = 0; // get the new resource id from the db $new_vc_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $cvmlc_resource_fields["vc_id"] = $new_vc_id; $cvmlc->add($cvmlc_resource_fields); // here we go on continue; } else { // we have a cvmlc, check its resource and set its state $cvm_resource = new resource(); $cvm_resource->get_instance_by_id($cvmlc->resource_id); // idle ? if ($cvm_resource->imageid == 1 && $cvm_resource->state == 'active' && strcmp($cvm_resource->ip, "0.0.0.0")) { // we have a new idle vm as resource :) update it in the appliance $new_vm_resource_id = $cvmlc->resource_id; unset($appliance_fields); $appliance_fields = array(); $appliance_fields['appliance_resources'] = $new_vm_resource_id; $ca_appliance->update($ca_appliance_id, $appliance_fields); $ca_appliance->get_instance_by_id($ca_appliance_id); // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Created new resource $new_vm_resource_id for appliance $ca_appliance->name start event", "", "", 0, 0, 0); // update cloudimage with resource -1 $ar_ci_update = array('ci_resource_id' => $new_vm_resource_id, 'ci_state' => 1); $cloud_image_start->update($cloud_image_start->id, $ar_ci_update); // remove cvmlc $cvmlc->remove($cvmlc->id); } else { // check timeout $vm_check_time = $_SERVER['REQUEST_TIME']; $vm_c_timeout = $cvmlc->request_time + $cvmlc->vm_create_timeout; if ($vm_check_time > $vm_c_timeout) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Could not create a new resource for request ID " . $ca_cr_id . "(unpause)", "", "", 0, 0, 0); $ca_cr->setstatus($ca_cr_id, 'no-res'); // send mail to user $rmail = new cloudmailer(); $rmail->to = "{$unpause_cu_email}"; $rmail->from = "{$unpause_ca_admin_email}"; $rmail->subject = "openQRM Cloud: Not enough resources to unpause your Cloudappliance " . $ca_id . " from request " . $ca_cr_id; $rmail->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$ca_cr_id}", '@@FORENAME@@' => "{$unpause_cu_forename}", '@@LASTNAME@@' => "{$unpause_cu_lastname}", '@@RESNUMBER@@' => "0", '@@YOUR@@' => "your", '@@CLOUDADMIN@@' => $unpause_ca_admin_email); $rmail->var_array = $arr; $rmail->send(); // send mail to admin $rmail_admin = new cloudmailer(); $rmail_admin->to = "{$unpause_ca_admin_email}"; $rmail_admin->from = "{$unpause_ca_admin_email}"; $rmail_admin->subject = "openQRM Cloud: Not enough resources to unpause Cloudappliance " . $ca_id . " from request " . $ca_cr_id; $rmail_admin->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$ca_cr_id}", '@@FORENAME@@' => "Cloudadmin", '@@LASTNAME@@' => "", '@@RESNUMBER@@' => "0", '@@YOUR@@' => "the", '@@CLOUDADMIN@@' => $unpause_ca_admin_email); $rmail_admin->var_array = $arr; $rmail_admin->send(); // refresh object $ca_appliance->get_instance_by_id($ca_appliance_id); // try removing the cloudvm $clean_cloudvm = new cloudvm(); $clean_cloudvm->remove($cvm_resource->id, $cvm_resource->vtype, $ca_appliance->name, $cvm_resource->mac); // remove cvmlc $cvmlc->remove($cvmlc->id); // reset the cmd field $ca->set_cmd($ca_id, "noop"); // set state to paused $ca->set_state($ca_id, "paused"); // go on continue; } // still waiting within the timeout // update state to 1 (starting) // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Still waiting for cr $ca_cr_id / res. ".$cvmlc->resource_id." to get idle (unpause)", "", "", 0, 0, 0); $cvm_state_fields['vc_state'] = 1; $cvmlc->update($cvmlc->id, $cvm_state_fields); // continue with the next cr/res-nr continue; } } // ################################## no auto create vm ############################### } else { // not set to auto-create vms // try to find a fitting idle vm $ca_appliance->find_resource($appliance_virtualization); // check if we got a resource ! $ca_appliance->get_instance_by_id($ca_appliance_id); if ($ca_appliance->resources == -1) { $event->log("cloud", $_SERVER['REQUEST_TIME'], 2, "cloud-monitor", "Not creating a new resource for request ID {$ca_cr_id}, auto-create-vms is disabled.", "", "", 0, 0, 0); $ca_cr->setstatus($ca_cr_id, 'no-res'); // send mail to user $rmail = new cloudmailer(); $rmail->to = "{$unpause_cu_email}"; $rmail->from = "{$unpause_ca_admin_email}"; $rmail->subject = "openQRM Cloud: Not enough resources to unpause your Cloudappliance " . $ca_id . " from request " . $ca_cr_id; $rmail->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$ca_cr_id}", '@@FORENAME@@' => "{$unpause_cu_forename}", '@@LASTNAME@@' => "{$unpause_cu_lastname}", '@@RESNUMBER@@' => "0", '@@YOUR@@' => "your", '@@CLOUDADMIN@@' => $unpause_ca_admin_email); $rmail->var_array = $arr; $rmail->send(); // send mail to admin $rmail_admin = new cloudmailer(); $rmail_admin->to = "{$unpause_ca_admin_email}"; $rmail_admin->from = "{$unpause_ca_admin_email}"; $rmail_admin->subject = "openQRM Cloud: Not enough resources to unpause Cloudappliance " . $ca_id . " from request " . $ca_cr_id; $rmail_admin->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/not_enough_resources.mail.tmpl"; $arr = array('@@ID@@' => "{$ca_cr_id}", '@@FORENAME@@' => "Cloudadmin", '@@LASTNAME@@' => "", '@@RESNUMBER@@' => "0", '@@YOUR@@' => "the", '@@CLOUDADMIN@@' => $unpause_ca_admin_email); $rmail_admin->var_array = $arr; $rmail_admin->send(); // reset the cmd field $ca->set_cmd($ca_id, "noop"); // set state to paused $ca->set_state($ca_id, "paused"); continue; } } } // ################################## end auto create vm ############################### // assign the resource $ca_appliance->get_instance_by_id($ca_appliance_id); $kernel = new kernel(); $kernel->get_instance_by_id($ca_appliance->kernelid); $resource = new resource(); $resource->get_instance_by_id($ca_appliance->resources); // in case we do not have an external ip-config send the resource ip to the user $resource_external_ip = $resource->ip; // send command to the openQRM-server $openqrm_server->send_command("openqrm_assign_kernel {$resource->id} {$resource->mac} {$kernel->name}"); // wait until the resource got the new kernel assigned sleep(2); //start the appliance, refresh the object before in case of clone-on-deploy $ca_appliance->get_instance_by_id($ca_appliance_id); $ca_appliance->start(); // ######################## ip-mgmt find users ips ############################### // here we check which ip to send to the user // check ip-mgmt $cc_conf = new cloudconfig(); $show_ip_mgmt = $cc_conf->get_value(26); // ip-mgmt enabled ? if (!strcmp($show_ip_mgmt, "true")) { if (file_exists("{$RootDir}/plugins/ip-mgmt/.running")) { require_once "{$RootDir}/plugins/ip-mgmt/class/ip-mgmt.class.php"; $ip_mgmt_array = explode(",", $ca_cr->ip_mgmt); foreach ($ip_mgmt_array as $ip_mgmt_config_str) { $collon_pos = strpos($ip_mgmt_config_str, ":"); $nic_id = substr($ip_mgmt_config_str, 0, $collon_pos); $ip_mgmt_id = substr($ip_mgmt_config_str, $collon_pos + 1); if (!strlen($ip_mgmt_id)) { continue; } $ip_mgmt_unpause = new ip_mgmt(); $ip_mgmt_config_arr = $ip_mgmt_unpause->get_config_by_id($ip_mgmt_id); $cloud_ip = $ip_mgmt_config_arr[0]['ip_mgmt_address']; $resource_external_ip = $cloud_ip . ","; } $resource_external_ip = rtrim($resource_external_ip, ","); } } // ################################################################################ // get the vnc console infos - check for allow_vnc $cc_allow_vnc = $cc_conf->get_value(42); // 1 is admin_email $vnc_informations = ""; if (!strcmp($cc_allow_vnc, "true")) { $vnc_resource = new resource(); $vnc_resource->get_instance_by_id($ca_appliance->resources); $vnc_informations_file = $OPENQRM_SERVER_BASE_DIR . "/openqrm/web/action/cloud-conf/" . $vnc_resource->mac . ".vnc"; if (file_exists($vnc_informations_file)) { $vnc_infos = file_get_contents($vnc_informations_file); $vnc_informations = "You can login to the System console via VNC at " . trim($vnc_infos) . " with the password: "******"You can login to the System console via VNC with the password: "******"noop"); // set state to active $ca->set_state($ca_id, "active"); // make sure the request is set to active again $ca_cr->setstatus($ca_cr_id, 'active'); // send mail to user // get admin email $cc_conf = new cloudconfig(); $cc_admin_email = $cc_conf->get_value(1); // 1 is admin_email // get user + request + appliance details $cu_id = $ca_cr->cu_id; $cu = new clouduser(); $cu->get_instance_by_id($cu_id); $cu_name = $cu->name; $unpause_cu_forename = $cu->forename; $unpause_cu_lastname = $cu->lastname; $unpause_cu_email = $cu->email; // start/stop time $cr_start = $ca_cr->start; $start = date("d-m-Y H-i", $cr_start); $cr_stop = $ca_cr->stop; $stop = date("d-m-Y H-i", $cr_stop); $rmail = new cloudmailer(); $rmail->to = "{$unpause_cu_email}"; $rmail->from = "{$unpause_ca_admin_email}"; $rmail->subject = "openQRM Cloud: Your unpaused appliance {$ca_appliance_id} from request {$ca_cr_id} is now active"; $rmail->template = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/cloud/etc/mail/active_cloud_request.mail.tmpl"; $arr = array('@@ID@@' => "{$ca_cr_id}", '@@FORENAME@@' => "{$unpause_cu_forename}", '@@LASTNAME@@' => "{$unpause_cu_lastname}", '@@START@@' => "{$start}", '@@STOP@@' => "{$stop}", '@@PASSWORD@@' => "(as before)", '@@LOGINUSER@@' => $cu_name, '@@IP@@' => "{$resource_external_ip}", '@@RESNUMBER@@' => "", '@@HOSTNAME@@' => $ca_appliance->name, '@@VNC@@' => $vnc_informations, '@@CLOUDADMIN@@' => $unpause_ca_admin_email); $rmail->var_array = $arr; $rmail->send(); $phase_five_actions = 1; break; case 2: // stop/pause $ca_appliance = new appliance(); $ca_appliance->get_instance_by_id($ca_appliance_id); $ca_resource_id = $ca_appliance->resources; $ca_resource_stop = new resource(); $ca_resource_stop->get_instance_by_id($ca_appliance->resources); $resource_external_ip = $ca_resource_stop->ip; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Pausing Appliance {$ca_appliance->name}", "", "", 0, 0, 0); $openqrm_server->send_command("openqrm_assign_kernel {$ca_resource_stop->id} {$ca_resource_stop->mac} default"); // now stop $ca_appliance->stop(); // remove resource $ar_update = array('appliance_resources' => "-1"); // update appliance $ca_appliance->update($ca_appliance_id, $ar_update); // reset the cmd field $ca->set_cmd($ca_id, "noop"); // set state to paused $ca->set_state($ca_id, "paused"); $phase_five_actions = 1; break; case 3: // restart $ca_appliance = new appliance(); $ca_appliance->get_instance_by_id($ca_appliance_id); $ca_resource_id = $ca_appliance->resources; $ca_resource_restart = new resource(); $ca_resource_restart->get_instance_by_id($ca_resource_id); $ca_resource_ip = $ca_resource_restart->ip; $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Restarting Appliance {$ca_appliance->name}", "", "", 0, 0, 0); $ca_resource_restart->send_command("{$ca_resource_ip}", "reboot"); // reset the cmd field $ca->set_cmd($ca_id, "noop"); sleep(2); // set state to transition $resource_fields = array(); $resource_fields["resource_state"] = "transition"; $ca_resource_restart->update_info($ca_resource_id, $resource_fields); $phase_five_actions = 1; break; } // check if we continue or go on if ($phase_five_actions == 1) { $parallel_phase_five_actions++; if ($max_parallel_phase_five_actions > 0 && $parallel_phase_five_actions >= $max_parallel_phase_five_actions) { break; } } } // ###################### end cloudappliance commands ###################### // ##################### start cloudimage-resize-life-cycle ################ // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "Cloud Phase VI - Cloud Image resize live-cycle", "", "", 0, 0, 0); $cirlc = new cloudirlc(); $cirlc_list = $cirlc->get_all_ids(); foreach ($cirlc_list as $cdlist) { $cd_id = $cdlist['cd_id']; $cd = new cloudirlc(); $cd->get_instance_by_id($cd_id); $cd_appliance_id = $cd->appliance_id; $cd_state = $cd->state; switch ($cd_state) { case 0: // remove $cd->remove($cd_id); // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudirlc", "(REMOVE) Resize life-cycle of Appliance $cd_appliance_id", "", "", 0, 0, 0); break; case 1: // pause // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudirlc", "(PAUSE) Resize life-cycle of Appliance $cd_appliance_id", "", "", 0, 0, 0); $cloud_app_resize = new cloudappliance(); $cloud_app_resize->get_instance_by_id($cd_appliance_id); $cloud_app_resize->set_cmd($cloud_app_resize->id, "stop"); $cloud_app_resize->set_state($cloud_app_resize->id, "paused"); $cd->set_state($cd_id, "start_resize"); break; case 2: // start_resize // set the cloudimage to state resize $cloud_app_resize = new cloudappliance(); $cloud_app_resize->get_instance_by_id($cd_appliance_id); $appliance = new appliance(); $appliance->get_instance_by_id($cloud_app_resize->appliance_id); $cloud_im = new cloudimage(); $cloud_im->get_instance_by_image_id($appliance->imageid); // make sure that we wait until the cloud image has no resource, // otherwise we risk doing things while the volume is still in use. if ($cloud_im->resource_id == -1) { $cloud_im->set_state($cloud_im->id, "resizing"); $cd->set_state($cd_id, "resizing"); // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudirlc", "(START_RESIZE) Resize life-cycle of Appliance $cd_appliance_id", "", "", 0, 0, 0); } break; case 3: // resizing // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudirlc", "(RESIZING) Resize life-cycle of Appliance $cd_appliance_id", "", "", 0, 0, 0); // remove any existing image-authentication to avoid kicking the auth into the resize phase $cloud_app_resize = new cloudappliance(); $cloud_app_resize->get_instance_by_id($cd_appliance_id); $appliance = new appliance(); $appliance->get_instance_by_id($cloud_app_resize->appliance_id); $image_auth = new image_authentication(); $image_auth->get_instance_by_image_id($appliance->imageid); $image_auth->remove($image_auth->id); $cd->set_state($cd_id, "end_resize"); break; case 4: // end_resize // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudirlc", "(END_RESIZE) Resize life-cycle of Appliance $cd_appliance_id", "", "", 0, 0, 0); $cd->set_state($cd_id, "unpause"); break; case 5: // unpause // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudirlc", "(UNPAUSE) Resize life-cycle of Appliance $cd_appliance_id", "", "", 0, 0, 0); // unpause appliance $cloud_app_resize = new cloudappliance(); $cloud_app_resize->get_instance_by_id($cd_appliance_id); $cloud_app_resize->set_cmd($cloud_app_resize->id, "start"); $cloud_app_resize->set_state($cloud_app_resize->id, "active"); // set new disk size in cloudimage $appliance = new appliance(); $appliance->get_instance_by_id($cloud_app_resize->appliance_id); $cloud_im = new cloudimage(); $cloud_im->get_instance_by_image_id($appliance->imageid); $ar_cl_image_update = array('ci_disk_size' => $cloud_im->disk_rsize, 'ci_disk_rsize' => ""); $cloud_im->update($cloud_im->id, $ar_cl_image_update); $cd->set_state($cd_id, "remove"); break; } // check if we continue or go on $parallel_phase_six_actions++; if ($max_parallel_phase_six_actions > 0 && $parallel_phase_six_actions >= $max_parallel_phase_six_actions) { break; } } // ##################### end cloudimage-resize-life-cycle ################## // ##################### start cloudimage-private-life-cycle ################ // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "monitor-hook", "Cloud Phase VII - Cloud Image private live-cycle", "", "", 0, 0, 0); $max_clone_time = 4800; $ciplc = new cloudiplc(); $ciplc_list = $ciplc->get_all_ids(); foreach ($ciplc_list as $cplist) { $cp_id = $cplist['cp_id']; $cp = new cloudiplc(); $cp->get_instance_by_id($cp_id); $cp_appliance_id = $cp->appliance_id; $cp_state = $cp->state; switch ($cp_state) { case 0: // remove $cp->remove($cp_id); $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudiplc", "(REMOVE) Private life-cycle of Appliance {$cp_appliance_id}", "", "", 0, 0, 0); break; case 1: // pause $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudiplc", "(PAUSE) Private life-cycle of Appliance {$cp_appliance_id}", "", "", 0, 0, 0); $cloud_app_private = new cloudappliance(); $cloud_app_private->get_instance_by_id($cp_appliance_id); $cloud_app_private->set_cmd($cloud_app_private->id, "stop"); $cloud_app_private->set_state($cloud_app_private->id, "paused"); $cp->set_state($cp_id, "start_private"); break; case 2: // start_private // set the cloudimage to state resize $cloud_app_private = new cloudappliance(); $cloud_app_private->get_instance_by_id($cp_appliance_id); $appliance = new appliance(); $appliance->get_instance_by_id($cloud_app_private->appliance_id); $cloud_im = new cloudimage(); $cloud_im->get_instance_by_image_id($appliance->imageid); // make sure that we wait until the cloud image has no resource, // otherwise we risk doing things while the volume is still in use. if ($cloud_im->resource_id == -1) { $cloud_im->set_state($cloud_im->id, "private"); $cp->set_state($cp_id, "cloning"); $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudiplc", "(START_PRIVATE) Private life-cycle of Appliance {$cp_appliance_id}", "", "", 0, 0, 0); } break; case 3: // cloning $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudiplc", "(CLONING) Private life-cycle of Appliance {$cp_appliance_id}", "", "", 0, 0, 0); // remove any existing image-authentication to avoid kicking the auth into the private phase $cloud_app_private = new cloudappliance(); $cloud_app_private->get_instance_by_id($cp_appliance_id); $appliance = new appliance(); $appliance->get_instance_by_id($cloud_app_private->appliance_id); $image_auth = new image_authentication(); $image_auth->get_instance_by_image_id($appliance->imageid); $image_auth->remove($image_auth->id); $cp->set_state($cp_id, "end_private"); break; case 4: // end_private // check timeout $start_private = $cp->start_private; $current_time = $_SERVER['REQUEST_TIME']; $private_runtime = $current_time - $start_private; // check notifcation from storage // get the cloudappliance $cloud_app_private = new cloudappliance(); $cloud_app_private->get_instance_by_id($cp_appliance_id); // get the real appliance $appliance = new appliance(); $appliance->get_instance_by_id($cloud_app_private->appliance_id); // get the cloudimage $cloud_im = new cloudimage(); $cloud_im->get_instance_by_image_id($appliance->imageid); // get image_id $pimage = new image(); $pimage->get_instance_by_name($cloud_im->clone_name); // get deployment type $pdeployment = new deployment(); if (strlen($pimage->type)) { $pdeployment->get_instance_by_type($pimage->type); } // notification filename $clone_notification_file = $_SERVER["DOCUMENT_ROOT"] . '/openqrm/base/plugins/' . $pdeployment->storagetype . '/storage/' . $cloud_im->clone_name . '.clone'; // start checking if ($private_runtime > $max_clone_time) { // ran too long $event->log("cloud", $_SERVER['REQUEST_TIME'], 3, "cloudiplc", "(END_PRIVATE) Time-out private life-cycle of Appliance {$cp_appliance_id}", "", "", 0, 0, 0); $cp->set_state($cp_id, "unpause"); } else { if (file_exists($clone_notification_file)) { // got notification from storage server $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudiplc", "(END_PRIVATE) Got notified to finsish private life-cycle of Appliance {$cp_appliance_id}", "", "", 0, 0, 0); unlink($clone_notification_file); $cp->set_state($cp_id, "unpause"); } } break; case 5: // unpause $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloudiplc", "(UNPAUSE) Private life-cycle of Appliance {$cp_appliance_id}", "", "", 0, 0, 0); // get the cloudappliance $cloud_app_private = new cloudappliance(); $cloud_app_private->get_instance_by_id($cp_appliance_id); // get the real appliance $appliance = new appliance(); $appliance->get_instance_by_id($cloud_app_private->appliance_id); // get the cloudimage $cloud_im = new cloudimage(); $cloud_im->get_instance_by_image_id($appliance->imageid); // here we create the private cloud image in openQRM after the clone procedure $private_cloud_image = new cloudprivateimage(); // get image_id $pimage = new image(); $pimage->get_instance_by_name($cloud_im->clone_name); // get cu_id $crequest = new cloudrequest(); $crequest->get_instance_by_id($cloud_app_private->cr_id); $cuser = new clouduser(); $cuser->get_instance_by_id($crequest->cu_id); // create array for add $private_cloud_image_fields["co_id"] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $private_cloud_image_fields["co_image_id"] = $pimage->id; $private_cloud_image_fields["co_cu_id"] = $cuser->id; $private_cloud_image_fields["co_state"] = 1; $private_cloud_image->add($private_cloud_image_fields); // unpause appliance $cloud_app_private->set_cmd($cloud_app_private->id, "start"); $cloud_app_private->set_state($cloud_app_private->id, "active"); // array for updating the cloudimage $ar_cl_image_update = array('ci_disk_rsize' => "", 'ci_clone_name' => ""); $cloud_im->update($cloud_im->id, $ar_cl_image_update); $cp->set_state($cp_id, "remove"); break; } // check if we continue or go on $parallel_phase_seven_actions++; if ($max_parallel_phase_seven_actions > 0 && $parallel_phase_seven_actions >= $max_parallel_phase_seven_actions) { break; } } // ##################### end cloudimage-private-life-cycle ################## // ##################### checking for power-saving ################## $cloudpowersaver = new cloudpowersaver(); $cloudpowersaver->trigger(); // $event->log("cloud", $_SERVER['REQUEST_TIME'], 5, "cloud-monitor", "Removing the cloud-monitor lock", "", "", 0, 0, 0); unlink($cloud_monitor_lock); }
function get_response() { $response = $this->response; $form = $response->get_form($this->actions_name, 'step4'); $resource = new resource(); $resource->get_instance_by_id($this->appliance->resources); // if not openQRM resource if ($resource->id != 0) { $virtualization = new virtualization(); $virtualization->get_instance_by_id($resource->vtype); } $image = new image(); $image->get_instance_by_id($this->appliance->imageid); $deployment = new deployment(); $deployment->get_instance_by_type($image->type); $submit = $form->get_elements('submit'); $submit->handler = 'onclick="wait();"'; $form->add($submit, 'submit'); $submit = $form->get_elements('cancel'); $submit->handler = 'onclick="cancel();"'; $form->add($submit, 'cancel'); $kernel = new kernel(); $list = $kernel->get_list(); unset($list[0]); $kernels = array(); if ($resource->id == 0) { $kernels[] = array(0, 'openQRM'); } else { if (strstr($resource->capabilities, "TYPE=local-server")) { $local_kernel = new kernel(); $local_kernel->get_instance_by_name("resource" . $resource->id); $kernels[] = array($local_kernel->id, 'Local OS Installation'); // local-deployment VMs } else { if (strstr($virtualization->type, "-vm-local")) { $kernels[] = array(1, 'Local OS Installation'); // network-deployment - show only network-boot images } else { if (strstr($virtualization->type, "-vm-net")) { foreach ($list as $value) { $id = $value['value']; $kernel->get_instance_by_id($id); if (!strstr($kernel->capabilities, "TYPE=local-server")) { $kernels[] = array($id, $kernel->id . ' / ' . $kernel->name . ' (' . $kernel->version . ')'); } } // network deployment - physical systems - show only network-boot images } else { foreach ($list as $value) { $id = $value['value']; $kernel->get_instance_by_id($id); if (!strstr($kernel->capabilities, "TYPE=local-server")) { $kernels[] = array($id, $kernel->id . ' / ' . $kernel->name . ' (' . $kernel->version . ')'); } } } } } } $d['kernel']['label'] = $this->lang['form_kernel']; $d['kernel']['required'] = true; $d['kernel']['object']['type'] = 'htmlobject_select'; $d['kernel']['object']['attrib']['index'] = array(0, 1); $d['kernel']['object']['attrib']['id'] = 'kernel'; $d['kernel']['object']['attrib']['name'] = 'kernel'; $d['kernel']['object']['attrib']['options'] = $kernels; $form->add($d); $response->form = $form; return $response; }
function create_private_kvm_gluster_deployment($cloud_image_id, $private_disk, $private_image_name) { global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; global $RESOURCE_INFO_TABLE; global $event; $cloudimage = new cloudimage(); $cloudimage->get_instance_by_id($cloud_image_id); $event->log("create_private_kvm_gluster_deployment", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-gluster-deployment-cloud-hook.php", "Creating private image " . $cloudimage->image_id . " on storage.", "", "", 0, 0, 0); // get image $image = new image(); $image->get_instance_by_id($cloudimage->image_id); $image_id = $image->id; $image_name = $image->name; $image_type = $image->type; $image_version = $image->version; $image_rootdevice = $image->rootdevice; $image_rootfstype = $image->rootfstype; $imageid = $image->storageid; $image_isshared = $image->isshared; $image_comment = $image->comment; $image_capabilities = $image->capabilities; $image_deployment_parameter = $image->deployment_parameter; // get image storage $storage = new storage(); $storage->get_instance_by_id($imageid); $storage_resource_id = $storage->resource_id; // get deployment type $deployment = new deployment(); $deployment->get_instance_by_id($storage->type); // get storage resource $resource = new resource(); $resource->get_instance_by_id($storage_resource_id); $resource_id = $resource->id; $resource_ip = $resource->ip; // create an admin user to post when cloning has finished $openqrm_admin_user = new user("openqrm"); $openqrm_admin_user->set_user(); $gluster_uri_arr = parse_url($image_rootdevice); // origin image volume name $origin_volume_name = basename($gluster_uri_arr['path']); // location of the volume (path) $image_location_name = str_replace('/', '', dirname($gluster_uri_arr['path'])); $image_clone_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/kvm/bin/openqrm-kvm clone -n " . $origin_volume_name . " -s " . $private_image_name . " -v " . $image_location_name . " -m " . $private_disk . " -u " . $openqrm_admin_user->name . " -p " . $openqrm_admin_user->password . " -t " . $deployment->type . " --openqrm-cmd-mode background"; $event->log("create_private_kvm_gluster_deployment", $_SERVER['REQUEST_TIME'], 5, "openqrm-kvm-gluster-deployment-cloud-hook.php", "Running : {$image_resize_cmd}", "", "", 0, 0, 0); $resource->send_command($resource->ip, $image_clone_cmd); // set the storage specific image root_device parameter $new_rootdevice = "gluster:" . $resource->ip . "//" . $image_location_name . "/" . $private_image_name; return $new_rootdevice; }
function remove() { $response = $this->get_response(); $remove_image_id = $this->response->html->request()->get($this->identifier_name); $force_remove = $response->html->request()->get('force_remove'); $form = $response->form; $image = new image(); if ($remove_image_id !== '') { $submit = $form->get_elements('submit'); $submit->handler = 'onclick="wait();"'; $form->add($submit, 'submit'); $submit = $form->get_elements('cancel'); $submit->handler = 'onclick="cancel();"'; $form->add($submit, 'cancel'); $image->get_instance_by_id($remove_image_id); $d['param_f' . $remove_image_id]['label'] = $image->name; $d['param_f' . $remove_image_id]['object']['type'] = 'htmlobject_input'; $d['param_f' . $remove_image_id]['object']['attrib']['type'] = 'checkbox'; $d['param_f' . $remove_image_id]['object']['attrib']['name'] = $this->identifier_name; $d['param_f' . $remove_image_id]['object']['attrib']['value'] = $remove_image_id; $d['param_f' . $remove_image_id]['object']['attrib']['checked'] = true; $d['force_remove']['label'] = $this->lang['msg_force_remove']; $d['force_remove']['object']['type'] = 'htmlobject_input'; $d['force_remove']['object']['attrib']['type'] = 'checkbox'; $d['force_remove']['object']['attrib']['name'] = 'force_remove'; $d['force_remove']['object']['attrib']['value'] = 1; $d['force_remove']['object']['attrib']['checked'] = false; $form->add($d); if (!$form->get_errors() && $response->submit()) { $errors = array(); $message = array(); if ($force_remove == 1) { $image->get_instance_by_id($remove_image_id); $image_is_used_by_appliance = ""; $remove_error = 0; $appliance = new appliance(); $appliance_id_list = $appliance->get_all_ids(); foreach ($appliance_id_list as $appliance_list) { $appliance_id = $appliance_list['appliance_id']; $app_image_remove_check = new appliance(); $app_image_remove_check->get_instance_by_id($appliance_id); if ($app_image_remove_check->imageid == $remove_image_id) { $image_is_used_by_appliance .= $appliance_id . " "; $remove_error = 1; } } if ($remove_error == 1) { $message[] = sprintf($this->lang['msg_not_removing_active'], $remove_image_id, $image_is_used_by_appliance); } else { $image_name = $image->name; $image->remove($remove_image_id); $form->remove($this->identifier_name . '[' . $remove_image_id . ']'); $message[] = sprintf($this->lang['msg'], $image_name); } } else { $message[] = sprintf($this->lang['msg_not_removing'], $image->name); } if (count($errors) === 0) { $response->msg = join('<br>', $message); } else { $msg = array_merge($errors, $message); $response->error = join('<br>', $msg); } } } else { $response->msg = ''; } return $response; }
function edit() { $resource_icon_default = "/img/resource.png"; $storage_icon = "/plugins/kvm/img/plugin.png"; //$state_icon = $this->openqrm->get('baseurl')."/img/".$this->resource->state.".png"; if ($this->file->exists($this->openqrm->get('webdir') . $storage_icon)) { $resource_icon_default = $storage_icon; } $resource_icon_default = $this->openqrm->get('baseurl') . $resource_icon_default; // check if we have a plugin implementing the remote console $remote_console = false; $plugin = new plugin(); $enabled_plugins = $plugin->enabled(); foreach ($enabled_plugins as $index => $plugin_name) { //$plugin_remote_console_running = $this->openqrm->get('webdir')."/plugins/".$plugin_name."/.running"; $plugin_remote_console_hook = $this->openqrm->get('webdir') . "/plugins/" . $plugin_name . "/openqrm-" . $plugin_name . "-remote-console-hook.php"; if ($this->file->exists($plugin_remote_console_hook)) { require_once "{$plugin_remote_console_hook}"; $link_function = str_replace("-", "_", "openqrm_" . "{$plugin_name}" . "_remote_console"); if (function_exists($link_function)) { $remote_functions[] = $link_function; $remote_console = true; } } } // prepare list of all Host resource id for the migration select // we need a select with the ids/ips from all resources which // are used by appliances with kvm capabilities $kvm_hosts = array(); $appliance_list = new appliance(); $appliance_list_array = $appliance_list->get_list(); foreach ($appliance_list_array as $index => $app) { $appliance_kvm_host_check = new appliance(); $appliance_kvm_host_check->get_instance_by_id($app["value"]); // only active appliances if (!strcmp($appliance_kvm_host_check->state, "active") || $appliance_kvm_host_check->resources == 0) { $virtualization = new virtualization(); $virtualization->get_instance_by_id($appliance_kvm_host_check->virtualization); if (!strcmp($virtualization->type, "kvm") && !strstr($virtualization->type, "kvm-vm")) { $kvm_host_resource = new resource(); $kvm_host_resource->get_instance_by_id($appliance_kvm_host_check->resources); // exclude source host #if ($kvm_host_resource->id == $this->resource->id) { # continue; #} // only active appliances if (!strcmp($kvm_host_resource->state, "active")) { $migration_select_label = "Res. " . $kvm_host_resource->id . "/" . $kvm_host_resource->ip; $kvm_hosts[] = array("value" => $kvm_host_resource->id, "label" => $migration_select_label); } } } } $d['state'] = '<span class="pill ' . $this->resource->state . '">' . $this->resource->state . '</span>'; $d['resource'] = $this->resource->id . ' / ' . $this->resource->ip; $d['name'] = $this->appliance->name; $d['id'] = $this->appliance->id; $a = $this->response->html->a(); $a->label = $this->lang['action_add_local_vm']; $a->css = 'add'; $a->handler = 'onclick="wait();"'; $a->href = $this->response->get_url($this->actions_name, "add") . '&vmtype=kvm-vm-local'; $d['add_local_vm'] = $a->get_string(); $a = $this->response->html->a(); $a->label = $this->lang['action_add_network_vm']; $a->css = 'add'; $a->handler = 'onclick="wait();"'; $a->href = $this->response->get_url($this->actions_name, "add") . '&vmtype=kvm-vm-net'; $d['add_network_vm'] = $a->get_string(); $body = array(); $identifier_disabled = array(); $file = $this->statfile; if ($this->file->exists($file)) { $lines = explode("\n", $this->file->get_contents($file)); if (count($lines) >= 1) { $i = 0; foreach ($lines as $line) { if ($line !== '') { $line = explode('@', $line); $state = $line[0]; $name = $line[1]; $mac = $line[2]; $resource = new resource(); $resource->get_instance_by_mac($mac); if ($resource->vhostid != $this->resource->id) { continue; } $res_virtualization = new virtualization(); $res_virtualization->get_instance_by_id($resource->vtype); $update = ''; $a = $this->response->html->a(); $a->title = $this->lang['action_update']; $a->label = $this->lang['action_update']; $a->handler = 'onclick="wait();"'; $a->css = 'edit'; $a->href = $this->response->get_url($this->actions_name, "update") . '&vm=' . $name . '&vmtype=' . $res_virtualization->type; $update_link = $a->get_string(); $clone = ''; $a = $this->response->html->a(); $a->title = $this->lang['action_clone']; $a->label = $this->lang['action_clone']; $a->handler = 'onclick="wait();"'; $a->css = 'clone'; $a->href = $this->response->get_url($this->actions_name, "clone") . '&vm=' . $name . '&mac=' . $mac; $clone_link = $a->get_string(); if ($res_virtualization->type == 'kvm-vm-local') { if ($state === '0' && $resource->image === 'idle') { $update = $update_link; $clone = $clone_link; } } if ($res_virtualization->type == 'kvm-vm-net') { if ($state !== '2' && $resource->image === 'idle') { $update = $update_link; $clone = $clone_link; } } $migrate = ''; $a = $this->response->html->a(); $a->title = $this->lang['action_migrate']; $a->label = $this->lang['action_migrate']; $a->handler = 'onclick="wait();"'; $a->css = 'migrate'; $a->href = $this->response->get_url($this->actions_name, "migrate") . '&vm=' . $name . '&mac=' . $mac; if (count($kvm_hosts) >= 1 && $state === '1') { $migrate = $a->get_string(); } $data = '<b>' . $this->lang['table_id'] . '</b>: ' . $resource->id . '<br>'; $data .= '<b>' . $this->lang['table_name'] . '</b>: ' . $name . '<br>'; $data .= '<b>' . $this->lang['table_type'] . '</b>: ' . $res_virtualization->name . '<br>'; $data .= '<b>' . $this->lang['table_ip'] . '</b>: ' . $resource->ip . '<br>'; $data .= '<b>' . $this->lang['table_mac'] . '</b>: ' . $mac . '<br>'; $data .= '<b>' . $this->lang['table_vnc'] . '</b>: ' . $line[5] . '<br>'; $data .= '<b>' . $this->lang['table_cpu'] . '</b>: ' . $line[3] . '<br>'; $data .= '<b>' . $this->lang['table_ram'] . '</b>: ' . $line[4] . '<br>'; $data .= '<b>' . $this->lang['table_nics'] . '</b>: ' . $resource->nics; $appliance = new appliance(); $appliance->get_instance_by_virtualization_and_resource($resource->vtype, $resource->id); $server = array(); $server[0] = ''; $server[1] = '1000000000000000000000000'; if ($appliance->id !== '') { $kernel = new kernel(); $kernel->get_instance_by_id($appliance->kernelid); $image = new image(); $image->get_instance_by_id($appliance->imageid); $storage = new storage(); $storage->get_instance_by_id($image->storageid); $s = '<b>' . $this->lang['table_appliance'] . '</b>: ' . $appliance->id . '<br>'; $s .= '<b>' . $this->lang['table_name'] . '</b>: ' . $appliance->name . '<br>'; $s .= '<b>' . $this->lang['table_kernel'] . '</b>: ' . $kernel->name . '<br>'; $s .= '<b>' . $this->lang['table_image'] . '</b>: ' . $image->name . '<br>'; $s .= '<b>' . $this->lang['table_storage'] . '</b>: ' . $storage->name . '<br>'; $server[0] = $s; $server[1] = $appliance->id; } $console = ''; if ($state === '2') { $t = $this->response->html->template($this->openqrm->get('webdir') . '/js/openqrm-progressbar.js'); $identifier_disabled[] = $name; // progressbar $t->add(uniqid('b'), 'id'); $t->add($this->openqrm->get('baseurl') . '/api.php?action=plugin&plugin=kvm&controller=kvm-vm&kvm_vm_action=progress&name=' . $name . '.vm_migration_progress', 'url'); $t->add($this->lang['action_migrate_in_progress'], 'lang_in_progress'); $t->add($this->lang['action_migrate_finished'], 'lang_finished'); $console = $t->get_string(); } else { if ($remote_console === true && $resource->imageid !== 1 && $state === '1') { foreach ($remote_functions as $function) { $a = $function($resource->id); if (is_object($a)) { $console .= $a->get_string(); } } } } $state = array(); $state[0] = '<span class="pill idle">idle</span>'; $state[1] = 'i'; if ($line[0] === '1' && $resource->image !== 'idle') { $state[0] = '<span class="pill active">active</span>'; $state[1] = 'a'; } if ($res_virtualization->type == 'kvm-vm-net' && $line[0] === '0') { $state[0] = '<span class="pill off">off</span>'; $state[1] = 'o'; } $action = ''; if (!in_array($name, $identifier_disabled)) { $action = $update . $clone . $migrate; } $body[$i] = array('state' => $state[0], 'state_s' => $state[1], 'name' => $name, 'id' => $resource->id, 'mac' => $mac, 'cpu' => $line[3], 'ram' => $line[4], 'ip' => $resource->ip, 'vnc' => $line[5], 'data' => $data, 'appliance' => $server[0], 'appliance_s' => $server[1], 'plugins' => $console, 'action' => $action); } $i++; } } } $h['state']['title'] = $this->lang['table_state']; $h['state']['sortable'] = false; $h['state_s']['title'] = $this->lang['table_state']; $h['state_s']['sortable'] = true; $h['state_s']['hidden'] = true; $h['id']['title'] = $this->lang['table_id']; $h['id']['hidden'] = true; $h['name']['title'] = $this->lang['table_name']; $h['name']['hidden'] = true; $h['ip']['title'] = $this->lang['table_ip']; $h['ip']['hidden'] = true; $h['mac']['title'] = $this->lang['table_mac']; $h['mac']['hidden'] = true; $h['vnc']['title'] = $this->lang['table_vnc']; $h['vnc']['hidden'] = true; $h['cpu']['title'] = $this->lang['table_cpu']; $h['cpu']['hidden'] = true; $h['ram']['title'] = $this->lang['table_ram']; $h['ram']['hidden'] = true; $h['nics']['title'] = $this->lang['table_nics']; $h['nics']['hidden'] = true; $h['data']['title'] = ' '; $h['data']['sortable'] = false; $h['appliance']['title'] = ' '; $h['appliance']['sortable'] = false; $h['appliance_s']['title'] = $this->lang['table_appliance']; $h['appliance_s']['sortable'] = true; $h['appliance_s']['hidden'] = true; $h['plugins']['title'] = ' '; $h['plugins']['sortable'] = false; $h['action']['title'] = ' '; $h['action']['sortable'] = false; $table = $this->response->html->tablebuilder('kvm_vm_edit', $this->response->get_array($this->actions_name, 'edit')); $table->sort = 'name'; $table->limit = 10; $table->offset = 0; $table->order = 'ASC'; $table->max = count($body); $table->autosort = true; $table->sort_link = false; $table->id = 'Tabelle'; $table->css = 'htmlobject_table'; $table->border = 1; $table->cellspacing = 0; $table->cellpadding = 3; $table->form_action = $this->response->html->thisfile; $table->head = $h; $table->body = $body; $table->identifier = 'name'; $table->identifier_name = $this->identifier_name; $table->identifier_disabled = $identifier_disabled; $table->actions_name = $this->actions_name; $table->actions = array(array('start' => $this->lang['action_start']), array('stop' => $this->lang['action_stop']), array('reboot' => $this->lang['action_reboot']), array('remove' => $this->lang['action_remove'])); $d['table'] = $table->get_string(); return $d; }
function openqrm_linuxcoe_appliance($cmd, $appliance_fields) { global $event; global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; $openqrm_server = new openqrm_server(); $appliance_id = $appliance_fields["appliance_id"]; $appliance_name = $appliance_fields["appliance_name"]; $resource = new resource(); $resource->get_instance_by_id($appliance_fields["appliance_resources"]); $resource_mac = $resource->mac; $resource_ip = $resource->ip; $appliance = new appliance(); $appliance->get_instance_by_id($appliance_id); $linuxcoe_install_timeout = 240; // check appliance values, maybe we are in update and they are incomplete if ($appliance->imageid == 1) { return; } if ($resource->id == "-1" || $resource->id == "") { return; } // check if image is type linuxcoe-deployment $image = new image(); $image->get_instance_by_id($appliance->imageid); // linuxcoe configured in image deployment parameters ? $linuxcoe_auto_install_enabled = false; $linuxcoe_deployment_parameters = trim($image->get_deployment_parameter("INSTALL_CONFIG")); if (strlen($linuxcoe_deployment_parameters)) { $linuxcoe_deployment_parameter_arr = explode(":", $linuxcoe_deployment_parameters); $local_deployment_persistent = $linuxcoe_deployment_parameter_arr[0]; $local_deployment_type = $linuxcoe_deployment_parameter_arr[1]; if (strcmp($local_deployment_type, "linuxcoe-deployment")) { $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Appliance " . $appliance_id . "/" . $appliance_name . " image is not from type linuxcoe-deployment", "", "", 0, 0, $resource->id); return; } $linuxcoe_server_storage_id = $linuxcoe_deployment_parameter_arr[2]; $linuxcoe_install_profile = $linuxcoe_deployment_parameter_arr[3]; $linuxcoe_auto_install_enabled = true; } $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Handling {$cmd} event {$appliance_id}/{$appliance_name}/{$resource_ip}/{$resource_mac}", "", "", 0, 0, $resource->id); switch ($cmd) { case "start": $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "START event {$appliance_id}/{$appliance_name}/{$resource_ip}/{$resource_mac}", "", "", 0, 0, $resource->id); if ($linuxcoe_auto_install_enabled) { // prepare automatic-installation / transfer client to linuxcoe server $linuxcoe_command = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/linuxcoe/bin/openqrm-linuxcoe-manager transfer_to_linuxcoe -x " . $resource->id . " -i " . $resource_ip . " -m " . $resource_mac . " -n " . $linuxcoe_install_profile . " --openqrm-cmd-mode background"; $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "transfer_to_linuxcoe {$resource_ip} / {$linuxcoe_install_profile}", "", "", 0, 0, $resource->id); $openqrm_server->send_command($linuxcoe_command, NULL, true); // Remove image-deployment paramters, if auto-install is a single-shot actions if (!strcmp($local_deployment_persistent, "0")) { $image->set_deployment_parameters("INSTALL_CONFIG", ""); } // create linuxcoestate-state object to allow to run a late setboot to local command on the vm host $linuxcoe_state = new linuxcoestate(); $linuxcoe_state->remove_by_resource_id($resource->id); $linuxcoe_state_fields = array(); $linuxcoe_state_fields["linuxcoe_id"] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0")); $linuxcoe_state_fields["linuxcoe_resource_id"] = $resource->id; $linuxcoe_state_fields["linuxcoe_install_start"] = $_SERVER['REQUEST_TIME']; $linuxcoe_state_fields["linuxcoe_timeout"] = $linuxcoe_install_timeout; $linuxcoe_state->add($linuxcoe_state_fields); } else { if (strcmp($image->type, "linuxcoe-deployment")) { $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Appliance {$appliance_id}/{$appliance_name} image is not from type linuxcoe-deployment", "", "", 0, 0, $resource->id); } else { // we have auto-installed already, if it is VM the linuxcoeresource object will care to set the boot-sequence on the VM Host to local boot $linuxcoeresource = new linuxcoeresource(); $linuxcoeresource->set_boot($resource->id, 1); // set pxe config to local-boot $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Setting resource {$resource_ip} to local-boot", "", "", 0, 0, $resource->id); $linuxcoe_command = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/linuxcoe/bin/openqrm-linuxcoe-manager set_linuxcoe_client_to_local_boot -m " . $resource_mac . " --openqrm-cmd-mode background"; $openqrm_server->send_command($linuxcoe_command, NULL, true); } } break; case "stop": if (strcmp($image->type, "linuxcoe-deployment")) { $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Appliance {$appliance_id}/{$appliance_name} image is not from type linuxcoe-deployment", "", "", 0, 0, $resource->id); } else { $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Stop event for appliance " . $appliance_id . "/" . $appliance->name . ".", "", "", 0, 0, $resource->id); // remove linuxcoestate-state object if existing $local_storage_state = new linuxcoestate(); $local_storage_state->remove_by_resource_id($resource->id); // if it is VM the linuxcoeresource object will care to set the boot-sequence on the VM Host to network boot $linuxcoeresource = new linuxcoeresource(); $linuxcoeresource->set_boot($resource->id, 0); } break; } }
function storage_auth_deployment_stop($image_id) { global $event; global $OPENQRM_SERVER_BASE_DIR; global $OPENQRM_SERVER_IP_ADDRESS; global $OPENQRM_EXEC_PORT; $image = new image(); $image->get_instance_by_id($image_id); $image_name = $image->name; $image_rootdevice = $image->rootdevice; $storage = new storage(); $storage->get_instance_by_id($image->storageid); $storage_resource = new resource(); $storage_resource->get_instance_by_id($storage->resource_id); $storage_ip = $storage_resource->ip; $deployment = new deployment(); $deployment->get_instance_by_type($image->type); $deployment_type = $deployment->type; $deployment_plugin_name = $deployment->storagetype; // just for sending the commands $resource = new resource(); // get install deployment params $install_from_nfs_param = trim($image->get_deployment_parameter("IMAGE_INSTALL_FROM_NFS")); if (strlen($install_from_nfs_param)) { // storage -> resource -> auth $ip_storage_id = $deployment->parse_deployment_parameter("id", $install_from_nfs_param); $ip_storage_ip = $deployment->parse_deployment_parameter("ip", $install_from_nfs_param); $ip_image_rootdevice = $deployment->parse_deployment_parameter("path", $install_from_nfs_param); $ip_storage = new storage(); $ip_storage->get_instance_by_id($ip_storage_id); $ip_storage_resource = new resource(); $ip_storage_resource->get_instance_by_id($ip_storage->resource_id); $op_storage_ip = $ip_storage_resource->ip; $ip_deployment = new deployment(); $ip_deployment->get_instance_by_id($ip_storage->type); $ip_deployment_type = $ip_deployment->type; $ip_deployment_plugin_name = $ip_deployment->storagetype; $event->log("storage_auth_function", $_SERVER['REQUEST_TIME'], 5, "openqrm-lvm-nfs-deployment-auth-hook.php", "Install-from-NFS: Authenticating {$resource_ip} on storage id {$ip_storage_id}:{$ip_storage_ip}:{$ip_image_rootdevice}", "", "", 0, 0, $resource_id); $auth_install_from_nfs_start_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/" . $ip_deployment_plugin_name . "/bin/openqrm-" . $ip_deployment_plugin_name . " auth -r " . $ip_image_rootdevice . " -i " . $OPENQRM_SERVER_IP_ADDRESS . " -t " . $ip_deployment_type . " --openqrm-cmd-mode background"; $resource->send_command($ip_storage_ip, $auth_install_from_nfs_start_cmd); } // get transfer deployment params $transfer_from_nfs_param = trim($image->get_deployment_parameter("IMAGE_TRANSFER_TO_NFS")); if (strlen($transfer_from_nfs_param)) { // storage -> resource -> auth $tp_storage_id = $deployment->parse_deployment_parameter("id", $transfer_from_nfs_param); $tp_storage_ip = $deployment->parse_deployment_parameter("ip", $transfer_from_nfs_param); $tp_image_rootdevice = $deployment->parse_deployment_parameter("path", $transfer_from_nfs_param); $tp_storage = new storage(); $tp_storage->get_instance_by_id($tp_storage_id); $tp_storage_resource = new resource(); $tp_storage_resource->get_instance_by_id($tp_storage->resource_id); $op_storage_ip = $tp_storage_resource->ip; $tp_deployment = new deployment(); $tp_deployment->get_instance_by_id($tp_storage->type); $tp_deployment_type = $tp_deployment->type; $tp_deployment_plugin_name = $tp_deployment->storagetype; $event->log("storage_auth_function", $_SERVER['REQUEST_TIME'], 5, "openqrm-lvm-nfs-deployment-auth-hook.php", "Install-from-NFS: Authenticating {$resource_ip} on storage id {$tp_storage_id}:{$tp_storage_ip}:{$tp_image_rootdevice}", "", "", 0, 0, $resource_id); $auth_install_from_nfs_start_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/" . $tp_deployment_plugin_name . "/bin/openqrm-" . $tp_deployment_plugin_name . " auth -r " . $tp_image_rootdevice . " -i " . $OPENQRM_SERVER_IP_ADDRESS . " -t " . $tp_deployment_type . " --openqrm-cmd-mode background"; $resource->send_command($tp_storage_ip, $auth_install_from_nfs_start_cmd); } }
function ImageGetList($method_parameters) { global $event; $parameter_array = explode(',', $method_parameters); $mode = $parameter_array[0]; $username = $parameter_array[1]; $password = $parameter_array[2]; $cloudusername = $parameter_array[3]; // check all user input for ($i = 0; $i <= 3; $i++) { if (!$this->check_param($parameter_array[$i])) { $event->log("cloudsoap->ImageGetList", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Not allowing user-intput with special-characters : {$parameter_array[$i]}", "", "", 0, 0, 0); return; } } // check parameter count $parameter_count = count($parameter_array); if ($parameter_count != 4) { $event->log("cloudsoap->ImageGetList", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Wrong parameter count {$parameter_count} ! Exiting.", "", "", 0, 0, 0); return; } // check authentication if (!$this->check_user($mode, $username, $password)) { $event->log("cloudsoap->ImageGetList", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "User authentication failed (mode {$mode})", "", "", 0, 0, 0); return; } $event->log("cloudsoap->ImageGetList", $_SERVER['REQUEST_TIME'], 5, "cloud-soap-server.php", "Providing list of available images", "", "", 0, 0, 0); $pcloud_user = new clouduser(); $pcloud_user->get_instance_by_name($cloudusername); // check if private image feature is enabled $image_name_list = array(); $cc_so_conf = new cloudconfig(); $show_private_image = $cc_so_conf->get_value(21); // show_private_image if (!strcmp($show_private_image, "true")) { // private image feature enabled $private_cimage = new cloudprivateimage(); $private_image_list = $private_cimage->get_all_ids(); foreach ($private_image_list as $index => $cpi) { $cpi_id = $cpi["co_id"]; $priv_image = new cloudprivateimage(); $priv_image->get_instance_by_id($cpi_id); if ($pcloud_user->id == $priv_image->cu_id) { $priv_im = new image(); $priv_im->get_instance_by_id($priv_image->image_id); // do not show active images if ($priv_im->isactive == 1) { continue; } // only show the non-shared image to the user if it is not attached to a resource // because we don't want users to assign the same image to two appliances $priv_cloud_im = new cloudimage(); $priv_cloud_im->get_instance_by_image_id($priv_image->image_id); if (!$priv_cloud_im->id) { if ($priv_cloud_im->resource_id == 0 || $priv_cloud_im->resource_id == -1) { $image_name_list[] = $priv_im->name; } } } else { if ($priv_image->cu_id == 0) { $priv_im = new image(); $priv_im->get_instance_by_id($priv_image->image_id); // do not show active images if ($priv_im->isactive == 1) { continue; } $image_name_list[] = $priv_im->name; } } } } else { // private image feature disabled $image = new image(); $image_list = $image->get_list(); foreach ($image_list as $images) { $iid = $images['value']; $iimage = new image(); $iimage->get_instance_by_id($iid); // do not show active images if ($iimage->isactive == 1) { continue; } $image_name_list[] = $images['label']; } // remove openqrm and idle image array_splice($image_name_list, 0, 1); array_splice($image_name_list, 0, 1); } return $image_name_list; }
$appliance_fields["appliance_starttime"] = $now; $appliance_fields["appliance_stoptime"] = 0; $appliance_fields['appliance_state'] = 'active'; // set resource type to physical $appliance_fields['appliance_virtualization'] = 1; $appliance->update($next_appliance_id, $appliance_fields); // set resource to localboot $resource = new resource(); $resource->get_instance_by_id($local_server_id); $openqrm_server->send_command("openqrm_server_set_boot local {$local_server_id} {$resource->mac} 0.0.0.0"); $resource->set_localboot($local_server_id, 1); // update resource fields with kernel + image $kernel->get_instance_by_id($kernel_fields["kernel_id"]); $resource_fields["resource_kernel"] = $kernel->name; $resource_fields["resource_kernelid"] = $kernel_fields["kernel_id"]; $image->get_instance_by_id($image_fields["image_id"]); $resource_fields["resource_image"] = $image->name; $resource_fields["resource_imageid"] = $image_fields["image_id"]; // set capabilites $resource_fields["resource_capabilities"] = 'TYPE=local-server'; $resource->update_info($local_server_id, $resource_fields); // add + start hook $appliance->get_instance_by_id($next_appliance_id); $now = $_SERVER['REQUEST_TIME']; $appliance_fields = array(); $appliance_fields['appliance_stoptime'] = $now; $appliance_fields['appliance_state'] = 'stopped'; // fill in the rest of the appliance info in the array for the plugin hook $appliance_fields["appliance_id"] = $next_appliance_id; $appliance_fields["appliance_name"] = $appliance->name; $appliance_fields["appliance_kernelid"] = $appliance->kernelid;
function get_response() { $response = $this->response; $form = $response->get_form($this->actions_name, 'edit'); $id = $this->response->html->request()->get('image_id'); $submit = $form->get_elements('submit'); $submit->handler = 'onclick="wait();"'; $form->add($submit, 'submit'); $submit = $form->get_elements('cancel'); $submit->handler = 'onclick="cancel();"'; $form->add($submit, 'cancel'); $image = $this->openqrm->image(); $image->get_instance_by_id($id); $storage = $this->openqrm->storage(); $storage->get_instance_by_id($image->storageid); $deployment = $this->openqrm->deployment(); $deployment->get_instance_by_id($storage->type); $storage_resource = $this->openqrm->resource(); $storage_resource->get_instance_by_id($storage->resource_id); // making the deployment parameters plugg-able $rootdevice_identifier_hook = $this->openqrm->get('basedir') . "/web/boot-service/image." . $deployment->type . ".php"; if (file_exists($rootdevice_identifier_hook)) { require_once "{$rootdevice_identifier_hook}"; // run function returning rootdevice array $get_rootfs_transfer_methods_function = "get_" . $deployment->type . "_rootfs_transfer_methods"; $get_rootfs_transfer_methods_function = str_replace("-", "_", $get_rootfs_transfer_methods_function); $rootfs_transfer_methods = $get_rootfs_transfer_methods_function(); $get_rootfs_set_password_method_function = "get_" . $deployment->type . "_rootfs_set_password_method"; $get_rootfs_set_password_method_function = str_replace("-", "_", $get_rootfs_set_password_method_function); $rootfs_set_password_method = $get_rootfs_set_password_method_function(); $get_rootfs_local_deployment_enabled_function = "get_" . $deployment->type . "_local_deployment_enabled"; $get_rootfs_local_deployment_enabled_function = str_replace("-", "_", $get_rootfs_local_deployment_enabled_function); $rootfs_local_deployment_enabled = $get_rootfs_local_deployment_enabled_function(); } else { $rootfs_transfer_methods = false; $rootfs_set_password_method = false; $rootfs_local_deployment_enabled = false; } // making the local deployment parameter plugg-able $local_deployment_methods_input = ""; if ($rootfs_local_deployment_enabled) { $local_deployment_methods_arr[] = array("value" => "", "label" => ""); $local_deployment = new deployment(); $deployment_id_arr = $local_deployment->get_deployment_ids(); foreach ($deployment_id_arr as $deployment_id) { $local_deployment->get_instance_by_id($deployment_id['deployment_id']); $local_deployment_templates_identifier_hook = $this->openqrm->get('basedir') . "/web/boot-service/template." . $local_deployment->type . ".php"; if (file_exists($local_deployment_templates_identifier_hook)) { require_once "{$local_deployment_templates_identifier_hook}"; $deployment_function = "get_" . "{$local_deployment->type}" . "_methods"; $deployment_function = str_replace("-", "_", $deployment_function); $local_deployment_methods_arr[] = $deployment_function(); } } } // in case the deployment method provides the rootfs-transfer options $nfs_image_identifier_array = array(); if ($rootfs_transfer_methods) { // prepare the install-from and transfer-to selects $nfs_image_identifier_array[] = array("value" => "", "label" => ""); $nfs_image = new image(); $image_arr = $nfs_image->get_ids(); foreach ($image_arr as $id) { $i_id = $id['image_id']; $timage = new image(); $timage->get_instance_by_id($i_id); if (strstr($timage->type, "nfs")) { $timage_name = $timage->name; $nfs_image_identifier_array[] = array("value" => "{$i_id}", "label" => "{$timage_name}"); } } } $html = new htmlobject_div(); $html->text = '<a href="../../plugins/' . $deployment->storagetype . '/' . $deployment->storagetype . '-about.php" target="_blank" class="doculink">' . $deployment->description . '</a>'; $html->id = 'htmlobject_image_type'; $storage_deploy_box = new htmlobject_box(); $storage_deploy_box->id = 'htmlobject_box_image_deploy'; $storage_deploy_box->css = 'htmlobject_box'; $storage_deploy_box->label = 'Deployment'; $storage_deploy_box->content = $html; $html = new htmlobject_div(); $html->text = $deployment->storagedescription; $html->id = 'htmlobject_storage_type'; $storage_type_box = new htmlobject_box(); $storage_type_box->id = 'htmlobject_box_storage_type'; $storage_type_box->css = 'htmlobject_box'; $storage_type_box->label = 'Storage'; $storage_type_box->content = $html; #$storage_resource->id / $html = new htmlobject_div(); $html->text = "{$storage_resource->ip}"; $html->id = 'htmlobject_storage_resource'; $storage_resource_box = new htmlobject_box(); $storage_resource_box->id = 'htmlobject_box_storage_resource'; $storage_resource_box->css = 'htmlobject_box'; $storage_resource_box->label = 'Resource'; $storage_resource_box->content = $html; // in case the deployment type allows to set the password in the image if ($rootfs_set_password_method) { $d['image_password']['label'] = $this->lang['form_image_password']; $d['image_password']['required'] = false; $d['image_password']['object']['type'] = 'htmlobject_input'; $d['image_password']['object']['attrib']['id'] = 'pass_1'; $d['image_password']['object']['attrib']['type'] = 'password'; $d['image_password']['object']['attrib']['name'] = 'image_password'; $d['image_password_2']['label'] = $this->lang['form_image_password_repeat']; $d['image_password_2']['required'] = false; $d['image_password_2']['object']['type'] = 'htmlobject_input'; $d['image_password_2']['object']['attrib']['id'] = 'pass_2'; $d['image_password_2']['object']['attrib']['type'] = 'password'; $d['image_password_2']['object']['attrib']['name'] = 'image_password_2'; } else { $d['image_password'] = ''; $d['image_password_2'] = ''; } if ($rootfs_transfer_methods) { $d['install_from_local']['label'] = $this->lang['form_install_from_local']; $d['install_from_local']['required'] = false; $d['install_from_local']['object']['type'] = 'htmlobject_input'; $d['install_from_local']['object']['attrib']['id'] = 'install_from_local'; $d['install_from_local']['object']['attrib']['name'] = 'install_from_local'; $d['transfer_to_local']['label'] = $this->lang['form_transfer_to_local']; $d['transfer_to_local']['required'] = false; $d['transfer_to_local']['object']['type'] = 'htmlobject_input'; $d['transfer_to_local']['object']['attrib']['id'] = 'transfer_to_local'; $d['transfer_to_local']['object']['attrib']['name'] = 'transfer_to_local'; $d['install_from_nfs']['label'] = $this->lang['form_install_from_nfs']; $d['install_from_nfs']['required'] = false; $d['install_from_nfs']['object']['type'] = 'htmlobject_select'; $d['install_from_nfs']['object']['attrib']['index'] = array('value', 'label'); $d['install_from_nfs']['object']['attrib']['id'] = 'install_from_nfs'; $d['install_from_nfs']['object']['attrib']['name'] = 'install_from_nfs'; $d['install_from_nfs']['object']['attrib']['options'] = $nfs_image_identifier_array; $d['transfer_to_nfs']['label'] = $this->lang['form_transfer_to_nfs']; $d['transfer_to_nfs']['required'] = false; $d['transfer_to_nfs']['object']['type'] = 'htmlobject_select'; $d['transfer_to_nfs']['object']['attrib']['index'] = array('value', 'label'); $d['transfer_to_nfs']['object']['attrib']['id'] = 'transfer_to_nfs'; $d['transfer_to_nfs']['object']['attrib']['name'] = 'transfer_to_nfs'; $d['transfer_to_nfs']['object']['attrib']['options'] = $nfs_image_identifier_array; } else { $d['install_from_local'] = ''; $d['transfer_to_local'] = ''; $d['install_from_nfs'] = ''; $d['transfer_to_nfs'] = ''; } if ($rootfs_local_deployment_enabled) { $d['install_from_template']['label'] = $this->lang['form_install_from_template']; $d['install_from_template']['required'] = false; $d['install_from_template']['object']['type'] = 'htmlobject_select'; $d['install_from_template']['object']['attrib']['index'] = array('value', 'label'); $d['install_from_template']['object']['attrib']['id'] = 'install_from_template'; $d['install_from_template']['object']['attrib']['name'] = 'install_from_template'; $d['install_from_template']['object']['attrib']['options'] = $local_deployment_methods_arr; } else { $d['install_from_template'] = ''; } $image_version_arr[] = array("value" => "Linux", "label" => "Linux"); $image_version_arr[] = array("value" => "Windows", "label" => "Windows"); $image_version_arr[] = array("value" => "Other", "label" => "Other"); $d['image_version']['label'] = $this->lang['form_image_version']; $d['image_version']['required'] = false; $d['image_version']['object']['type'] = 'htmlobject_select'; $d['image_version']['object']['attrib']['index'] = array('value', 'label'); $d['image_version']['object']['attrib']['id'] = 'image_version'; $d['image_version']['object']['attrib']['name'] = 'image_version'; $d['image_version']['object']['attrib']['options'] = $image_version_arr; $d['image_version']['object']['attrib']['selected'] = array($image->version); $d['image_comment']['label'] = $this->lang['form_comment']; $d['image_comment']['object']['type'] = 'htmlobject_textarea'; $d['image_comment']['object']['attrib']['id'] = 'comment'; $d['image_comment']['object']['attrib']['name'] = 'image_comment'; $d['image_comment']['object']['attrib']['value'] = $image->comment; $form->add($d); $response->form = $form; return $response; }