コード例 #1
0
ファイル: kvm-vm.edit.class.php プロジェクト: kelubo/OpenQRM
 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'] = '&#160;';
     $h['data']['sortable'] = false;
     $h['appliance']['title'] = '&#160;';
     $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'] = '&#160;';
     $h['plugins']['sortable'] = false;
     $h['action']['title'] = '&#160;';
     $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;
 }
コード例 #2
0
function openqrm_hybrid_cloud_monitor()
{
    global $event;
    global $OPENQRM_SERVER_BASE_DIR;
    global $OPENQRM_SERVER_IP_ADDRESS;
    global $OPENQRM_EXEC_PORT;
    global $openqrm_server;
    global $BaseDir;
    global $RootDir;
    $now = $_SERVER['REQUEST_TIME'];
    // $event->log("hybrid_cloud_monitor", $_SERVER['REQUEST_TIME'], 2, "hybrid-cloud-monitor-hook", "Hybrid Cloud monitor hook DISABLED for now!!!", "", "", 0, 0, 0);
    // return;
    // $event->log("hybrid_cloud_monitor", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-monitor-hook", "Hybrid Cloud monitor hook", "", "", 0, 0, 0);
    $last_stats = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/hybrid-cloud/web/hybrid-cloud-stat/last_statistics";
    if (file_exists($last_stats)) {
        $last_host_stats = file_get_contents($last_stats);
        $secs_after_last_host_stat = $now - $last_host_stats;
        if ($secs_after_last_host_stat > 35) {
            file_put_contents($last_stats, $now);
            $server = new openqrm_server();
            $hc = new hybrid_cloud();
            $hc_account_arr = $hc->get_ids();
            foreach ($hc_account_arr as $id) {
                $hc_account_id = $id['hybrid_cloud_id'];
                $hc->get_instance_by_id($hc_account_id);
                $hc_authentication = '';
                // for every ec2/euca cloud account monitor every configured region
                if ($hc->account_type == 'aws' || $hc->account_type == 'euca') {
                    $hybrid_cloud_conf = $OPENQRM_SERVER_BASE_DIR . '/openqrm/plugins/hybrid-cloud/etc/openqrm-plugin-hybrid-cloud.conf';
                    $hybrid_cloud_conf_arr = openqrm_parse_conf($hybrid_cloud_conf);
                    $region_arr = explode(",", $hybrid_cloud_conf_arr['OPENQRM_PLUGIN_HYBRID_CLOUD_REGIONS']);
                    $hc_authentication .= ' -O ' . $hc->access_key;
                    $hc_authentication .= ' -W ' . $hc->secret_key;
                }
                // one region for openstack
                if ($hc->account_type == 'lc-openstack') {
                    $region_arr = array("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;
                }
                foreach ($region_arr as $region) {
                    $event->log("hybrid_cloud_monitor", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-monitor-hook", "Hybrid Cloud monitor - checking Cloud statistics for Account " . $hc->account_name . " - " . $region, "", "", 0, 0, 0);
                    $statfile = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/hybrid-cloud/web/hybrid-cloud-stat/" . $hc_account_id . ".instances_statistics.log";
                    $command = $OPENQRM_SERVER_BASE_DIR . '/openqrm/plugins/hybrid-cloud/bin/openqrm-hybrid-cloud-vm describe --statistics true';
                    $command .= ' -i ' . $hc->id;
                    $command .= ' -n ' . $hc->account_name;
                    $command .= ' -t ' . $hc->account_type;
                    $command .= ' -ir ' . $region;
                    $command .= $hc_authentication;
                    $command .= ' --openqrm-cmd-mode background';
                    if (file_exists($statfile)) {
                        unlink($statfile);
                    }
                    $server->send_command($command, NULL, true);
                    while (!file_exists($statfile)) {
                        usleep(10000);
                        clearstatcache();
                    }
                    $content = file_get_contents($statfile);
                    $content = explode("\n", $content);
                    $b = array();
                    foreach ($content as $k => $v) {
                        if ($v !== '') {
                            $tmp = explode('@', $v);
                            $name = $tmp[1];
                            $ami = $tmp[2];
                            $public_hostname = $tmp[3];
                            $private_hostname = $tmp[4];
                            $state = $tmp[5];
                            $keypair = $tmp[6];
                            $unknown1 = $tmp[7];
                            $unknown2 = $tmp[8];
                            $type = $tmp[9];
                            $date = $tmp[10];
                            $region = $tmp[11];
                            $unknown4 = $tmp[12];
                            $unknown5 = $tmp[13];
                            $unknown6 = $tmp[14];
                            $monitoring = $tmp[15];
                            $public_ip = $hc->format_ip_address($tmp[16]);
                            $private_ip = $hc->format_ip_address($tmp[17]);
                            $unknown7 = $tmp[18];
                            $unknown8 = $tmp[19];
                            $store = $tmp[20];
                            $unknown9 = $tmp[21];
                            $unknown10 = $tmp[22];
                            $unknown11 = $tmp[23];
                            $unknown12 = $tmp[24];
                            $hvm = $tmp[25];
                            $virt_type = $tmp[26];
                            $mac = '';
                            // check for idle instances
                            $resource = new resource();
                            if ($state == 'idle') {
                                $mac = $tmp[30];
                                $resource->get_instance_by_mac($mac);
                                $resource_fields["resource_state"] = 'active';
                                $resource_fields["resource_lastgood"] = $now;
                                $resource_fields["resource_cpunumber"] = $hc->translate_resource_components('cpu', $type);
                                $resource_fields["resource_nics"] = $hc->translate_resource_components('net', $type);
                                $resource_fields["resource_memtotal"] = $hc->translate_resource_components('mem', $type);
                                $resource_fields["resource_memused"] = "0";
                                $resource_fields["resource_load"] = "0";
                                // restore mgmt ip
                                $resource_fields["resource_ip"] = $resource->network;
                                $resource->update_info($resource->id, $resource_fields);
                            } else {
                                if ($state == 'running') {
                                    // check if existing, if not auto-create resource, image and appliance
                                    if ($resource->exists_by_name($name)) {
                                        // update stats
                                        $resource->get_instance_id_by_hostname($name);
                                        $resource->get_instance_by_id($resource->id);
                                        $resource_fields["resource_state"] = 'active';
                                        $resource_fields["resource_lastgood"] = $now;
                                        $resource_fields["resource_cpunumber"] = $hc->translate_resource_components('cpu', $type);
                                        $resource_fields["resource_nics"] = $hc->translate_resource_components('net', $type);
                                        $resource_fields["resource_memtotal"] = $hc->translate_resource_components('mem', $type);
                                        $resource_fields["resource_memused"] = $resource_fields["resource_memtotal"];
                                        $resource_fields["resource_load"] = "1";
                                        if (strlen($public_ip) && $resource->ip != $public_ip) {
                                            // set public ip, update early and run nagios hook
                                            $resource_fields["resource_ip"] = $public_ip;
                                            $resource->update_info($resource->id, $resource_fields);
                                            // nagios enabled and started ?
                                            if (file_exists($RootDir . "/plugins/nagios3/.running")) {
                                                $virtualization = new virtualization();
                                                $virtualization->get_instance_by_type("hybrid-cloud-vm-local");
                                                $hc_appliance = new appliance();
                                                $hc_appliance->get_instance_by_virtualization_and_resource($virtualization->id, $resource->id);
                                                if (strlen($hc_appliance->name)) {
                                                    // special nagios classes
                                                    require_once $RootDir . "/plugins/nagios3/class/nagios3_service.class.php";
                                                    require_once $RootDir . "/plugins/nagios3/class/nagios3_host.class.php";
                                                    // get the nagios service checks
                                                    $nagios_host = new nagios3_host();
                                                    $nagios_host->get_instance_by_appliance_id($hc_appliance->id);
                                                    $active_nagios_services = explode(',', $nagios_host->appliance_services);
                                                    $nagios_service_list = '';
                                                    foreach ($active_nagios_services as $service_id) {
                                                        $nagios_service = new nagios3_service();
                                                        $nagios_service->get_instance_by_id($service_id);
                                                        $nagios_service_list = $nagios_service_list . "," . $nagios_service->port;
                                                    }
                                                    $nagios_service_list = substr($nagios_service_list, 1);
                                                    if (strlen($nagios_service_list)) {
                                                        // appliance has nagios service checks configured
                                                        $nagios_appliance_stop_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/nagios3/bin/openqrm-nagios-manager remove_host -n " . $hc_appliance->name . " --openqrm-cmd-mode background";
                                                        $server->send_command($nagios_appliance_stop_cmd, NULL, true);
                                                        sleep(2);
                                                        $nagios_appliance_start_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/nagios3/bin/openqrm-nagios-manager add -n " . $hc_appliance->name . " -i " . $public_ip . " -p " . $nagios_service_list . " --openqrm-cmd-mode background";
                                                        $server->send_command($nagios_appliance_start_cmd, NULL, true);
                                                    }
                                                }
                                            }
                                            // nagios finished
                                        }
                                        $resource->update_info($resource->id, $resource_fields);
                                    } else {
                                        // through error for now
                                        $event->log("hybrid_cloud_monitor", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-monitor-hook", "New Resource detected with name " . $name . "!", "", "", 0, 0, 0);
                                    }
                                }
                            }
                        }
                    }
                    unlink($statfile);
                }
            }
        }
    } else {
        file_put_contents($last_stats, $now);
    }
}
コード例 #3
0
function fence_hybrid_cloud_vm_local($host_resource_id, $mac)
{
    global $OPENQRM_SERVER_BASE_DIR;
    global $OPENQRM_SERVER_IP_ADDRESS;
    global $OPENQRM_EXEC_PORT;
    global $RESOURCE_INFO_TABLE;
    global $event;
    $event->log("fence_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-ha-hook", "Fencing Cloud VM " . $mac, "", "", 0, 0, 0);
    $origin_resource = new resource();
    $origin_resource->get_instance_by_mac($mac);
    // get hybrid-cloud account from origin resource
    $hybrid_acl_id = $origin_resource->get_resource_capabilities("HCACL");
    $hc = new hybrid_cloud();
    $hc->get_instance_by_id($hybrid_acl_id);
    $hc_authentication = '';
    if ($hc->account_type == 'aws' || $hc->account_type == 'euca') {
        $hc_authentication .= ' -O ' . $hc->access_key;
        $hc_authentication .= ' -W ' . $hc->secret_key;
    }
    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;
    }
    $openqrm = new openqrm_server();
    $command = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/hybrid-cloud/bin/openqrm-hybrid-cloud-vm fence ";
    $command .= ' -i ' . $hc->id;
    $command .= ' -n ' . $hc->account_name;
    $command .= ' -t ' . $hc->account_type;
    $command .= $hc_authentication;
    $command .= ' -im ' . $mac;
    $command .= ' --openqrm-cmd-mode background';
    $event->log("fence_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-ha-hook", "Running {$command}", "", "", 0, 0, 0);
    $openqrm->send_command($command, NULL, true);
    $rufields["resource_hostname"] = $hc->account_type . $origin_resource->id;
    $origin_resource->update_info($origin_resource->id, $rufields);
    // debug
    // echo "1:".$command;
}
コード例 #4
0
 function remove()
 {
     $response = $this->get_response();
     $instance_name = $response->html->request()->get('instance_name');
     $form = $response->form;
     $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');
     $d['param_f']['label'] = $instance_name;
     $d['param_f']['object']['type'] = 'htmlobject_input';
     $d['param_f']['object']['attrib']['type'] = 'checkbox';
     $d['param_f']['object']['attrib']['name'] = 'instance_name';
     $d['param_f']['object']['attrib']['value'] = $instance_name;
     $d['param_f']['object']['attrib']['checked'] = true;
     $form->add($d);
     if (!$form->get_errors() && $response->submit()) {
         $errors = array();
         $message = array();
         $resource = new resource();
         $resource->get_instance_by_mac($this->instance_mac);
         $appliance = new appliance();
         $appliances_using_resource = $appliance->get_ids_per_resource($resource->id);
         if (count($appliances_using_resource) > 0) {
             $appliances_using_resource_str = implode(",", $appliances_using_resource[0]);
             $errors[] = sprintf($this->lang['msg_vm_resource_still_in_use'], $instance_name, $resource->id, $appliances_using_resource_str);
         } else {
             $resource->remove($resource->id, $this->instance_mac);
             $hc_authentication = '';
             if ($this->hc->account_type == 'aws' || $this->hc->account_type == 'euca') {
                 $hc_authentication .= ' -O ' . $this->hc->access_key;
                 $hc_authentication .= ' -W ' . $this->hc->secret_key;
                 $hc_authentication .= ' -ir ' . $this->region;
             }
             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 remove';
             $command .= ' -i ' . $this->hc->id;
             $command .= ' -n ' . $this->hc->account_name;
             $command .= ' -t ' . $this->hc->account_type;
             $command .= ' -in ' . $instance_name;
             $command .= $hc_authentication;
             $command .= ' --openqrm-ui-user ' . $this->user->name;
             $command .= ' --openqrm-cmd-mode background';
             $openqrm = new openqrm_server();
             $openqrm->send_command($command, NULL, true);
             $message[] = sprintf($this->lang['msg_removed'], $instance_name);
         }
         if (count($errors) === 0) {
             $response->msg = join('<br>', $message);
         } else {
             $msg = array_merge($errors, $message);
             $response->error = join('<br>', $msg);
         }
     }
     return $response;
 }
コード例 #5
0
ファイル: kvm-vm.stop.class.php プロジェクト: kelubo/OpenQRM
 function stop()
 {
     $response = '';
     $vms = $this->response->html->request()->get($this->identifier_name);
     if ($vms !== '') {
         $appliance_id = $this->response->html->request()->get('appliance_id');
         $appliance = new appliance();
         $resource = new resource();
         $errors = array();
         $message = array();
         foreach ($vms as $key => $vm) {
             $appliance->get_instance_by_id($appliance_id);
             $resource->get_instance_by_id($appliance->resources);
             $file = $this->openqrm->get('basedir') . '/plugins/kvm/web/kvm-stat/' . $resource->id . '.vm_list';
             $command = $this->openqrm->get('basedir') . '/plugins/kvm/bin/openqrm-kvm-vm stop -n ' . $vm;
             $command .= ' -u ' . $this->openqrm->admin()->name . ' -p ' . $this->openqrm->admin()->password;
             $command .= ' --openqrm-ui-user ' . $this->user->name;
             $command .= ' --openqrm-cmd-mode background';
             $resource->send_command($resource->ip, $command);
             $message[] = sprintf($this->lang['msg_stoped'], $vm);
             $file = $this->openqrm->get('basedir') . '/plugins/kvm/web/kvm-stat/' . $resource->id . '.vm_list';
             if ($this->file->exists($file)) {
                 $lines = explode("\n", $this->file->get_contents($file));
                 foreach ($lines as $line) {
                     if ($line !== '') {
                         $line = explode('@', $line);
                         if ($line[1] === $vm) {
                             $tmp = explode(':', $line[5]);
                             $server = $tmp[0];
                             $port = $tmp[1];
                             $mac = $line[2];
                             $resource->get_instance_by_mac($mac);
                             $rid = $resource->id;
                         }
                     }
                     $event = new event();
                     $plugin = new plugin();
                     $enabled = $plugin->enabled();
                     foreach ($enabled as $index => $name) {
                         $running = $this->openqrm->get('webdir') . '/plugins/' . $name . '/.running';
                         $hook = $this->openqrm->get('webdir') . '/plugins/' . $name . '/openqrm-' . $name . '-remote-console-hook.php';
                         if (file_exists($hook)) {
                             if (file_exists($running)) {
                                 $event->log("console", $_SERVER['REQUEST_TIME'], 5, "kvm-vm.console.class.php", 'Found plugin ' . $name . ' providing a remote console.', "", "", 0, 0, $resource->id);
                                 require_once $hook;
                                 $console_function = 'openqrm_' . $name . '_disable_remote_console';
                                 $console_function = str_replace("-", "_", $console_function);
                                 $console_function($server, $port, $rid, $mac, $vm);
                             }
                         }
                     }
                 }
             }
         }
         if (count($errors) === 0) {
             $response = join('<br>', $message);
         } else {
             $msg = array_merge($errors, $message);
             $response = join('<br>', $msg);
         }
     } else {
         $response = '';
     }
     return $response;
 }
コード例 #6
0
ファイル: resource-monitor.php プロジェクト: kelubo/OpenQRM
         }
         $event->log("get_parameter", $_SERVER['REQUEST_TIME'], 5, "resource-monitor", "Adding new resource {$new_resource_id} ({$resource_mac})", "", "", 0, 1, $resource->id);
         # send add resource to openQRM-server
         $openqrm_server->send_command("openqrm_server_add_resource {$new_resource_id} {$resource_mac} {$resource_ip}");
         # add resource to db
         $resource_fields["resource_id"] = $new_resource_id;
         $resource_fields["resource_localboot"] = 0;
         $resource_fields["resource_vtype"] = 1;
         $resource_fields["resource_vhostid"] = $new_resource_id;
         $resource_fields["resource_subnet"] = $resource_subnet;
         $resource->add($resource_fields);
     }
 }
 if (strlen($resource_mac)) {
     $resource = new resource();
     $resource->get_instance_by_mac("{$resource_mac}");
     // update the resource parameter in any way
     $resource->update_info($resource->id, $resource_fields);
     $resource->get_parameter($resource->id);
 } else {
     if (strlen($resource_id)) {
         $resource = new resource();
         $resource->get_instance_by_id($resource_id);
         // update the resource parameter in any way
         $resource->update_info($resource->id, $resource_fields);
         $resource->get_parameter($resource->id);
     }
 }
 exit;
 break;
 // update_info requires :
コード例 #7
0
 function migrate()
 {
     $response = $this->get_response();
     if (isset($response->msg)) {
         return $response;
     }
     $form = $response->form;
     if (!$form->get_errors() && $this->response->submit()) {
         $target = $form->get_request('target');
         $vm_resource = new resource();
         $vm_resource->get_instance_by_mac($this->mac);
         $dest_host_resource = new resource();
         $dest_host_resource->get_instance_by_id($target);
         $source_host_resource = new resource();
         $source_host_resource->get_instance_by_id($vm_resource->vhostid);
         // first transfer the VM config from source to destination
         $tstatfile = $this->openqrm->get('basedir') . '/plugins/kvm/web/kvm-stat/' . $this->vm . '.transfer_status';
         if ($this->file->exists($tstatfile)) {
             $this->file->remove($tstatfile);
         }
         $t_command = $this->openqrm->get('basedir') . '/plugins/kvm/bin/openqrm-kvm-vm transfer_vm_config';
         $t_command .= ' -n ' . $this->vm;
         $t_command .= ' -k ' . $dest_host_resource->ip;
         $t_command .= ' -k1 ' . $source_host_resource->ip;
         $t_command .= ' -u ' . $this->openqrm->admin()->name . ' -p ' . $this->openqrm->admin()->password;
         $t_command .= ' --openqrm-ui-user ' . $this->user->name;
         $t_command .= ' --openqrm-cmd-mode background';
         $openqrm = new openqrm_server();
         $openqrm->send_command($t_command, NULL, true);
         while (!$this->file->exists($tstatfile)) {
             usleep(10000);
             // sleep 10ms to unload the CPU
             clearstatcache();
         }
         $msg = trim($this->file->get_contents($tstatfile));
         if ($msg !== "ok") {
             $response->error = $msg;
             return $response;
         }
         // calcuate the migration port
         list($o1, $o2, $o3, $o4) = explode(".", $vm_resource->ip, 4);
         $kvm_vm_migration_port = $o4 + 6000;
         // start as incoming on destination
         $s_command = $this->openqrm->get('basedir') . '/plugins/kvm/bin/openqrm-kvm-vm start_as_incoming';
         $s_command .= ' -n ' . $this->vm;
         $s_command .= ' -j ' . $kvm_vm_migration_port;
         $s_command .= ' -u ' . $this->openqrm->admin()->name . ' -p ' . $this->openqrm->admin()->password;
         $s_command .= ' --openqrm-ui-user ' . $this->user->name;
         $s_command .= ' --openqrm-cmd-mode background';
         $statfile = $this->openqrm->get('basedir') . '/plugins/kvm/web/kvm-stat/' . $this->vm . '.vm_migrated_successfully';
         if ($this->file->exists($statfile)) {
             $this->file->remove($statfile);
         }
         $dest_host_resource->send_command($dest_host_resource->ip, $s_command);
         sleep(5);
         $m_command = $this->openqrm->get('basedir') . '/plugins/kvm/bin/openqrm-kvm-vm migrate';
         $m_command .= ' -n ' . $this->vm;
         $m_command .= ' -k ' . $dest_host_resource->ip;
         $m_command .= ' -j ' . $kvm_vm_migration_port;
         $m_command .= ' -u ' . $this->openqrm->admin()->name . ' -p ' . $this->openqrm->admin()->password;
         $m_command .= ' --openqrm-ui-user ' . $this->user->name;
         $m_command .= ' --openqrm-cmd-mode background';
         $this->resource->send_command($this->resource->ip, $m_command);
         $fields = array();
         $fields["resource_vhostid"] = $dest_host_resource->id;
         $vm_resource->update_info($vm_resource->id, $fields);
         $response->msg = sprintf($this->lang['msg_migrated'], $this->vm, $dest_host_resource->id . ' / ' . $dest_host_resource->ip);
     }
     return $response;
 }
コード例 #8
0
function remove_hybrid_cloud_vm_local($host_resource_id, $name, $mac)
{
    global $OPENQRM_SERVER_BASE_DIR;
    global $OPENQRM_SERVER_IP_ADDRESS;
    global $OPENQRM_EXEC_PORT;
    global $RESOURCE_INFO_TABLE;
    global $event;
    $event->log("remove_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-cloud-hook", "Removing Cloud VM {$name}/{$mac} from Host resource {$host_resource_id}", "", "", 0, 0, 0);
    $openqrm = new openqrm_server();
    $cloud_resource = new resource();
    $cloud_resource->get_instance_by_mac($mac);
    $hc_default_account_id = $cloud_resource->get_resource_capabilities("HCACL");
    $cloud_resource_hostname = $cloud_resource->hostname;
    if (strlen($hc_default_account_id)) {
        // $event->log("remove_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 2, "hybrid-cloud-cloud-hook", "!!! found Cloud Account ".$hc_default_account_id." from resource config", "", "", 0, 0, 0);
    } else {
        $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'];
        // $event->log("remove_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 2, "hybrid-cloud-cloud-hook", "!!! got Cloud Account ".$hc_default_account_id." from plugin config", "", "", 0, 0, 0);
    }
    $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;
    }
    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;
    }
    $openqrm = new openqrm_server();
    $command = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/hybrid-cloud/bin/openqrm-hybrid-cloud-vm remove ";
    $command .= ' -i ' . $hc->id;
    $command .= ' -n ' . $hc->account_name;
    $command .= ' -t ' . $hc->account_type;
    $command .= $hc_authentication;
    $command .= ' -in ' . $cloud_resource_hostname;
    $command .= ' --openqrm-cmd-mode background';
    // $event->log("remove_hybrid_cloud_vm_local", $_SERVER['REQUEST_TIME'], 2, "hybrid-cloud-cloud-hook", "Running $command", "", "", 0, 0, 0);
    $openqrm->send_command($command, NULL, true);
}
コード例 #9
0
 function add()
 {
     $response = $this->get_response();
     $form = $response->form;
     $errors = array();
     if (!$form->get_errors() && $this->response->submit()) {
         $image_id = $form->get_request('ami_image_id');
         $image = new image();
         if (isset($image_id) && $image_id !== '') {
             $image->get_instance_by_id($image_id);
         } else {
             $errors[] = $this->lang['error_boot'];
         }
         if (count($errors) > 0 || $form->get_errors()) {
             $response->error = join('<br>', $errors);
         } else {
             $tables = $this->openqrm->get('table');
             $custom_script_parameter = '';
             $custom_script = $form->get_request('custom_script');
             if (strlen($custom_script)) {
                 $custom_script_parameter = ' -ic ' . $custom_script;
             }
             $custom_name = $form->get_request('name');
             $resource = new resource();
             $id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
             if (strlen($custom_name)) {
                 $name = $custom_name;
             } else {
                 $name = $this->hc->account_type . $id;
             }
             $ip = "0.0.0.0";
             $resource->generate_mac();
             $mac = $resource->mac;
             // send command to the openQRM-server
             $openqrm = new openqrm_server();
             $openqrm->send_command('openqrm_server_add_resource ' . $id . ' ' . $mac . ' ' . $ip);
             // set resource type
             $virtualization = new virtualization();
             $virtualization->get_instance_by_type("hybrid-cloud-vm-local");
             // add to openQRM database
             $fields["resource_id"] = $id;
             $fields["resource_ip"] = $ip;
             $fields["resource_mac"] = $mac;
             $fields["resource_hostname"] = $name;
             $fields["resource_localboot"] = 0;
             $fields["resource_vtype"] = $virtualization->id;
             $fields["resource_vhostid"] = 0;
             $fields["resource_image"] = $image->name;
             $fields["resource_imageid"] = $image->id;
             $rfields["resource_kernel"] = 'default';
             $rfields["resource_kernelid"] = 1;
             $resource->add($fields);
             $resource->get_instance_by_mac($mac);
             // set account id in resource capabilities
             $resource->set_resource_capabilities("HCACL", $this->hc->id);
             $hc_authentication = '';
             if ($this->hc->account_type == 'aws' || $this->hc->account_type == 'euca') {
                 $hc_authentication .= ' -O ' . $this->hc->access_key;
                 $hc_authentication .= ' -W ' . $this->hc->secret_key;
                 $hc_authentication .= ' -ir ' . $this->region;
                 $hc_authentication .= ' -iz ' . $form->get_request('availability_zone');
             }
             if ($this->hc->account_type == 'lc-openstack') {
                 $hc_authentication .= ' -u ' . $this->hc->username;
                 $hc_authentication .= ' -p ' . $this->hc->password;
                 $hc_authentication .= ' -q ' . $this->hc->host;
                 $hc_authentication .= ' -x ' . $this->hc->port;
                 $hc_authentication .= ' -g ' . $this->hc->tenant;
                 $hc_authentication .= ' -e ' . $this->hc->endpoint;
             }
             $command = $this->openqrm->get('basedir') . '/plugins/hybrid-cloud/bin/openqrm-hybrid-cloud-vm create';
             $command .= ' -i ' . $this->hc->id;
             $command .= ' -n ' . $this->hc->account_name;
             $command .= ' -t ' . $this->hc->account_type;
             $command .= $hc_authentication;
             $command .= ' -in ' . $name;
             $command .= ' -im ' . $mac;
             $command .= ' -a ' . $image->name;
             $command .= ' -it ' . $form->get_request('type');
             $command .= ' -ik ' . $form->get_request('keypair');
             if ($this->hc->account_type == 'aws') {
                 $command .= ' -subnet ' . $form->get_request('subnet');
             } else {
                 $command .= ' -ig ' . $form->get_request('group');
             }
             $command .= ' --openqrm-ui-user ' . $this->user->name;
             $command .= ' --openqrm-cmd-mode background';
             $command .= $custom_script_parameter;
             $openqrm->send_command($command, NULL, true);
             // check to have a ip from the dhcpd-resource hook
             while ($resource->ip == "0.0.0.0") {
                 sleep(1);
                 clearstatcache();
                 $resource->get_instance_by_mac($mac);
             }
             // save the mgmt ip in the resource network field
             $rufields["resource_network"] = $resource->ip;
             $resource->update_info($resource->id, $rufields);
             $response->resource_id = $id;
             $response->msg = sprintf($this->lang['msg_added'], $name);
             // auto create the appliance for this VM if we are not coming from the wizard
             if (isset($this->user->wizard_name) && $this->user->wizard_name === 'appliance' && $this->user->wizard_step == 2) {
                 $now = 1;
             } else {
                 $now = $_SERVER['REQUEST_TIME'];
                 $appliance = new appliance();
                 $new_appliance_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
                 $afields['appliance_id'] = $new_appliance_id;
                 $afields['appliance_name'] = $name;
                 $afields['appliance_resources'] = $id;
                 $afields['appliance_kernelid'] = '1';
                 $afields['appliance_imageid'] = $image->id;
                 $afields["appliance_virtual"] = 0;
                 $afields["appliance_virtualization"] = $virtualization->id;
                 $afields['appliance_wizard'] = '';
                 $afields['appliance_comment'] = 'Cloud VM Server for Resource ' . $id;
                 $appliance->add($afields);
                 // update state/start+stoptime
                 $aufields['appliance_stoptime'] = $now;
                 $aufields['appliance_starttime'] = '';
                 $aufields['appliance_state'] = 'stopped';
                 $appliance->update($new_appliance_id, $aufields);
                 // set image active
                 $image_fields["image_id"] = $image->id;
                 $image_fields['image_isactive'] = 1;
                 $image->update($image->id, $image_fields);
             }
         }
     }
     return $response;
 }
コード例 #10
0
 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;
 }
コード例 #11
0
ファイル: resource.new.class.php プロジェクト: kelubo/OpenQRM
 function resource_new()
 {
     $response = $this->get_response();
     $form = $response->form;
     $name = $form->get_request('name');
     $ip = $form->get_request('ip');
     $mac = $form->get_request('mac');
     if (strlen($name)) {
         if (!$form->get_errors() && $this->response->submit()) {
             // check that mac adress + ip does not yet exists
             $resource = new resource();
             $resource->get_instance_by_mac($mac);
             if (strlen($resource->id)) {
                 $response->error = sprintf($this->lang['msg_mac_in_use']);
                 return $response;
             }
             $resource->get_instance_by_ip($ip);
             if (strlen($resource->id)) {
                 $response->error = sprintf($this->lang['msg_ip_in_use']);
                 return $response;
             }
             $tables = $this->openqrm->get('table');
             $id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
             $mac = strtolower($mac);
             // send command to the openQRM-server
             $openqrm = new openqrm_server();
             $openqrm->send_command('openqrm_server_add_resource ' . $id . ' ' . $mac . ' ' . $ip);
             // add to openQRM database
             $fields["resource_id"] = $id;
             $fields["resource_hostname"] = $name;
             $fields["resource_ip"] = $ip;
             $fields["resource_mac"] = $mac;
             $fields["resource_localboot"] = 0;
             $fields["resource_vtype"] = 1;
             $fields["resource_vhostid"] = $id;
             $fields["resource_state"] = 'unknown';
             $fields["resource_lastgood"] = '-1';
             $resource->add($fields);
             // set lastgood to -1 to prevent automatic checking the state
             $resource->update_info($id, $fields);
             $response->msg = sprintf($this->lang['msg'], $name);
             $response->resource_id = $id;
         } else {
             $response->error = sprintf($this->lang['msg_add_failed']);
         }
     }
     return $response;
 }
コード例 #12
0
 function remove()
 {
     $response = $this->get_response();
     $vms = $response->html->request()->get($this->identifier_name);
     $form = $response->form;
     if ($vms !== '') {
         $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');
         $i = 0;
         foreach ($vms as $ex) {
             $d['param_f' . $i]['label'] = $ex;
             $d['param_f' . $i]['object']['type'] = 'htmlobject_input';
             $d['param_f' . $i]['object']['attrib']['type'] = 'checkbox';
             $d['param_f' . $i]['object']['attrib']['name'] = $this->identifier_name . '[' . $i . ']';
             $d['param_f' . $i]['object']['attrib']['value'] = $ex;
             $d['param_f' . $i]['object']['attrib']['checked'] = true;
             $i++;
         }
         $form->add($d);
         if (!$form->get_errors() && $response->submit()) {
             $appliance_id = $this->response->html->request()->get('appliance_id');
             $appliance = new appliance();
             $resource = new resource();
             $errors = array();
             $message = array();
             foreach ($vms as $key => $vm) {
                 $appliance->get_instance_by_id($appliance_id);
                 $resource->get_instance_by_id($appliance->resources);
                 $file = $this->openqrm->get('basedir') . '/plugins/kvm/web/kvm-stat/' . $resource->id . '.vm_list';
                 if ($this->file->exists($file)) {
                     $lines = explode("\n", $this->file->get_contents($file));
                     if (count($lines) >= 1) {
                         foreach ($lines as $line) {
                             if ($line !== '') {
                                 $line = explode('@', $line);
                                 if ($vm === $line[1]) {
                                     $kvm = new resource();
                                     $kvm->get_instance_by_mac($line[2]);
                                     // check if it is still in use
                                     $appliances_using_resource = $appliance->get_ids_per_resource($kvm->id);
                                     if (count($appliances_using_resource) > 0) {
                                         $appliances_using_resource_str = implode(",", $appliances_using_resource[0]);
                                         $errors[] = sprintf($this->lang['msg_vm_resource_still_in_use'], $vm, $kvm->id, $appliances_using_resource_str);
                                     } else {
                                         $kvm->remove($kvm->id, $line[2]);
                                         $command = $this->openqrm->get('basedir') . '/plugins/kvm/bin/openqrm-kvm-vm delete -n ' . $vm;
                                         $command .= ' -u ' . $this->openqrm->admin()->name . ' -p ' . $this->openqrm->admin()->password;
                                         $command .= ' --openqrm-ui-user ' . $this->user->name;
                                         $command .= ' --openqrm-cmd-mode background';
                                         $resource->send_command($resource->ip, $command);
                                         $form->remove($this->identifier_name . '[' . $key . ']');
                                         $message[] = sprintf($this->lang['msg_removed'], $vm);
                                         // stop remote console
                                         $tmp = explode(':', $line[5]);
                                         $server = $tmp[0];
                                         $port = $tmp[1];
                                         $mac = $line[2];
                                         $rid = $kvm->id;
                                         $event = new event();
                                         $plugin = new plugin();
                                         $enabled = $plugin->enabled();
                                         foreach ($enabled as $index => $name) {
                                             $running = $this->openqrm->get('webdir') . '/plugins/' . $name . '/.running';
                                             $hook = $this->openqrm->get('webdir') . '/plugins/' . $name . '/openqrm-' . $name . '-remote-console-hook.php';
                                             if (file_exists($hook)) {
                                                 if (file_exists($running)) {
                                                     $event->log("console", $_SERVER['REQUEST_TIME'], 5, "kvm-storge-vm.remove.class.php", 'Stopping ' . $name . ' remote console.', "", "", 0, 0, $kvm->id);
                                                     require_once $hook;
                                                     $console_function = 'openqrm_' . $name . '_disable_remote_console';
                                                     $console_function = str_replace("-", "_", $console_function);
                                                     $console_function($server, $port, $rid, $mac, $vm);
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             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;
 }
コード例 #13
0
ファイル: kvm-vm.clone.class.php プロジェクト: kelubo/OpenQRM
 function duplicate()
 {
     $response = $this->get_response();
     $form = $response->form;
     if (!$form->get_errors() && $this->response->submit()) {
         $mac_gen_resource = new resource();
         $mac_gen_resource->generate_mac();
         $name = $form->get_request('name');
         $command = $this->openqrm->get('basedir') . '/plugins/kvm/bin/openqrm-kvm-vm clone';
         $command .= ' -n ' . $this->vm;
         $command .= ' -w ' . $name;
         $command .= ' -m ' . $mac_gen_resource->mac;
         $command .= ' -u ' . $this->openqrm->admin()->name . ' -p ' . $this->openqrm->admin()->password;
         $command .= ' --openqrm-ui-user ' . $this->user->name;
         $command .= ' --openqrm-cmd-mode regular';
         $statfile = $this->statfile;
         if ($this->file->exists($statfile)) {
             $lines = explode("\n", $this->file->get_contents($statfile));
             if (count($lines) >= 1) {
                 foreach ($lines as $line) {
                     if ($line !== '') {
                         $line = explode('@', $line);
                         $check = $line[1];
                         if ($name === $check) {
                             $error = sprintf($this->lang['error_exists'], $name);
                         }
                     }
                 }
             }
         }
         if (isset($error)) {
             $response->error = $error;
         } else {
             $tables = $this->openqrm->get('table');
             $id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
             $ip = "0.0.0.0";
             $mac = $mac_gen_resource->mac;
             // set resource type, get virt type from origin vm
             $mac_gen_resource->get_instance_by_mac($this->vm_mac);
             $virtualization = new virtualization();
             $virtualization->get_instance_by_id($mac_gen_resource->vtype);
             // send command to the openQRM-server
             $openqrm = new openqrm_server();
             $openqrm->send_command('openqrm_server_add_resource ' . $id . ' ' . $mac . ' ' . $ip);
             // add to openQRM database
             $fields["resource_id"] = $id;
             $fields["resource_ip"] = $ip;
             $fields["resource_mac"] = $mac;
             $fields["resource_localboot"] = 0;
             $fields["resource_vtype"] = $virtualization->id;
             $fields["resource_vhostid"] = $this->resource->id;
             $this->resource->add($fields);
             $this->resource->send_command($this->resource->ip, $command);
             $response->resource_id = $id;
             $response->msg = sprintf($this->lang['msg_cloned'], $this->vm, $name);
         }
     }
     return $response;
 }