function openqrm_puppet_resource($cmd, $resource_fields)
{
    global $event;
    global $OPENQRM_SERVER_BASE_DIR;
    global $OPENQRM_SERVER_IP_ADDRESS;
    global $OPENQRM_EXEC_PORT;
    $resource_id = $resource_fields["resource_id"];
    $resource = new resource();
    $resource->get_instance_by_id($resource_id);
    $resource_ip = $resource->ip;
    $resource_name = $resource->hostname;
    $event->log("openqrm_puppet_resource", $_SERVER['REQUEST_TIME'], 5, "openqrm-puppet-resource-hook.php", "Handling {$cmd} event {$resource_id}/{$resource_name}/{$resource_ip}", "", "", 0, 0, $resource_id);
    // we do only care if we serving an appliance
    $appliance = new appliance();
    $appliance_record_set = array();
    $appliance_id_array = array();
    $appliance_record_set = $appliance->get_all_ids();
    // the appliance_array from getlist is a 2-dimensional array
    foreach ($appliance_record_set as $index => $appliance_id_array) {
        foreach ($appliance_id_array as $index => $id) {
            $tapp = new appliance();
            $tapp->get_instance_by_id($id);
            $tapp_state = $tapp->state;
            $tapp_resources = $tapp->resources;
            if (!strcmp($tapp_state, "active")) {
                if ($tapp_resources == $resource_id) {
                    // we found the resources active appliance, running the cmd
                    $appliance_name = $tapp->name;
                    switch ($cmd) {
                        case "start":
                            $openqrm_server = new openqrm_server();
                            $openqrm_server->send_command("{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/puppet/bin/openqrm-puppet-manager start {$id} {$appliance_name} {$resource_ip} --openqrm-cmd-mode background");
                            break;
                        case "stop":
                            $openqrm_server = new openqrm_server();
                            $openqrm_server->send_command("{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/puppet/bin/openqrm-puppet-manager stop {$id} {$appliance_name} {$resource_ip} --openqrm-cmd-mode background");
                            break;
                    }
                }
            }
        }
    }
}
             $appliance_function = str_replace("-", "_", $appliance_function);
             // add
             $appliance_function("add", $appliance_fields);
             // start
             $appliance_fields['appliance_stoptime'] = '';
             $appliance_fields['appliance_starttime'] = $now;
             $appliance_fields['appliance_state'] = 'active';
             $appliance->update($next_appliance_id, $appliance_fields);
             $appliance_function("start", $appliance_fields);
         }
     }
     break;
 case 'remove':
     // remove all appliance with resource set to localserver id
     $appliance = new appliance();
     $appliance_id_list = $appliance->get_all_ids();
     foreach ($appliance_id_list as $appliance_list) {
         $appliance_id = $appliance_list['appliance_id'];
         $app_resource_remove_check = new appliance();
         $app_resource_remove_check->get_instance_by_id($appliance_id);
         if ($app_resource_remove_check->resources == $local_server_id) {
             // stop + remove hooks
             $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"] = $appliance_id;
             $appliance_fields["appliance_name"] = $app_resource_remove_check->name;
             $appliance_fields["appliance_kernelid"] = $app_resource_remove_check->kernelid;
             $appliance_fields["appliance_imageid"] = $app_resource_remove_check->imageid;
Exemple #3
0
 function image()
 {
     $response = '';
     $errors = array();
     $message = array();
     $image_command = $this->response->html->request()->get('image_command');
     if ($image_command !== '') {
         switch ($image_command) {
             case 'add':
                 $root_device = $this->response->html->request()->get('root_device');
                 if ($this->deployment->type == 'kvm-gluster-deployment') {
                     $image_name = $this->response->html->request()->get('image_name');
                 } else {
                     $image_name = basename($root_device);
                 }
                 // check if image name is not in use yet
                 $image = new image();
                 $image->get_instance_by_name($image_name);
                 if (strlen($image->id)) {
                     $errors[] = sprintf($this->lang['error_exists'], $image_name);
                 } else {
                     $tables = $this->openqrm->get('table');
                     $image_fields = array();
                     $image_fields["image_id"] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
                     $image_fields['image_name'] = $image_name;
                     $image_fields['image_type'] = $this->deployment->type;
                     $image_fields['image_rootfstype'] = 'local';
                     $image_fields['image_storageid'] = $this->storage->id;
                     $image_fields['image_comment'] = "Image Object for volume {$image_name}";
                     $image_fields['image_rootdevice'] = $root_device;
                     $image = new image();
                     $image->add($image_fields);
                     $message[] = sprintf($this->lang['msg_added_image'], $image_name);
                 }
                 break;
             case 'remove':
                 $image_id = $this->response->html->request()->get('image_id');
                 // check if image is not in use any more before removing
                 $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 == $image_id) {
                         $image_is_used_by_appliance .= $appliance_id . " ";
                         $remove_error = 1;
                     }
                 }
                 if ($remove_error == 1) {
                     $errors[] = sprintf($this->lang['error_image_still_in_use'], $image_id, $image_is_used_by_appliance);
                 } else {
                     $image_remove = new image();
                     $image_remove->remove($image_id);
                     $message[] = sprintf($this->lang['msg_removed_image'], $image_id);
                 }
                 break;
         }
         if (count($errors) === 0) {
             $response = join('<br>', $message);
         } else {
             $msg = array_merge($errors, $message);
             $response = join('<br>', $msg);
         }
     } else {
         $response = '';
     }
     return $response;
 }
Exemple #4
0
 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 image()
 {
     $response = '';
     $errors = array();
     $message = array();
     $image_command = $this->response->html->request()->get('image_command');
     if ($image_command !== '') {
         switch ($image_command) {
             case 'add':
                 $root_device = $this->response->html->request()->get('root_device');
                 $image_name = $this->response->html->request()->get('image_name');
                 // check if image name is not in use yet
                 $image = new image();
                 $image->get_instance_by_name($image_name);
                 if (strlen($image->id)) {
                     $errors[] = sprintf($this->lang['error_exists'], $image_name);
                 } else {
                     $image_rootfstype = '';
                     // for lvm-aoe deployment we need to get the shelf + slot from get_root_identifiert
                     $ident_file = $this->openqrm->get('basedir') . '/plugins/lvm-storage/web/storage/' . $this->resource->id . '.lvm.' . $image_name . '.adapt';
                     $get_ident_command = $this->openqrm->get('basedir') . '/plugins/lvm-storage/bin/openqrm-lvm-storage adapt';
                     $get_ident_command .= ' -n ' . $image_name;
                     $get_ident_command .= ' -t ' . $this->deployment->type . ' -v ' . $this->volgroup;
                     $get_ident_command .= ' -u ' . $this->openqrm->admin()->name . ' -p ' . $this->openqrm->admin()->password;
                     $get_ident_command .= ' --openqrm-ui-user ' . $this->user->name;
                     $get_ident_command .= ' --openqrm-cmd-mode background';
                     if (file_exists($ident_file)) {
                         unlink($ident_file);
                     }
                     $this->resource->send_command($this->resource->ip, $get_ident_command);
                     while (!file_exists($ident_file)) {
                         usleep(10000);
                         // sleep 10ms to unload the CPU
                         clearstatcache();
                     }
                     $found = false;
                     if ($this->deployment->type == 'lvm-aoe-deployment') {
                         $name = $root_device;
                         $root_device = '';
                         $ident_lines = explode("\n", file_get_contents($ident_file));
                         if (count($ident_lines) >= 1) {
                             foreach ($ident_lines as $ident_line) {
                                 if ($ident_line !== '') {
                                     $ident_line = explode(',', $ident_line);
                                     $ident_root_path = explode(':', $ident_line[1]);
                                     $ident_check = $ident_root_path[1];
                                     if ($name === $ident_check) {
                                         $root_device = $ident_line[1];
                                         $image_rootfstype = 'ext3';
                                         $found = true;
                                         break;
                                     }
                                 }
                             }
                         }
                     } else {
                         if ($this->deployment->type == 'lvm-iscsi-deployment') {
                             $image_rootfstype = 'ext3';
                             $found = true;
                         } else {
                             if ($this->deployment->type == 'lvm-nfs-deployment') {
                                 $image_rootfstype = 'nfs';
                                 $found = true;
                             }
                         }
                     }
                     if ($found) {
                         $tables = $this->openqrm->get('table');
                         $image_fields = array();
                         $image_fields["image_id"] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
                         $image_fields['image_name'] = $image_name;
                         $image_fields['image_type'] = $this->deployment->type;
                         $image_fields['image_rootfstype'] = $image_rootfstype;
                         $image_fields['image_storageid'] = $this->storage->id;
                         $image_fields['image_comment'] = "Image Object for volume {$image_name}";
                         $image_fields['image_rootdevice'] = $root_device;
                         $image = new image();
                         $image->add($image_fields);
                         $message[] = sprintf($this->lang['msg_added_image'], $image_name);
                     } else {
                         $message[] = sprintf($this->lang['msg_add_failed'], $image_name);
                     }
                 }
                 break;
             case 'remove':
                 $image_id = $this->response->html->request()->get('image_id');
                 // check if image is not in use any more before removing
                 $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 == $image_id) {
                         $image_is_used_by_appliance .= $appliance_id . " ";
                         $remove_error = 1;
                     }
                 }
                 if ($remove_error == 1) {
                     $errors[] = sprintf($this->lang['error_image_still_in_use'], $image_id, $image_is_used_by_appliance);
                 } else {
                     $image_remove = new image();
                     $image_remove->remove($image_id);
                     $message[] = sprintf($this->lang['msg_removed_image'], $image_id);
                 }
                 break;
         }
         if (count($errors) === 0) {
             $response = join('<br>', $message);
         } else {
             $msg = array_merge($errors, $message);
             $response = join('<br>', $msg);
         }
     } else {
         $response = '';
     }
     return $response;
 }
 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;
 }
Exemple #7
0
 function statistics()
 {
     $resources_all = 0;
     $resources_active = 0;
     $resources_available = 0;
     $dc_load_overall = 0;
     $appliance_load_overall = 0;
     $appliance_active = 0;
     $storage_load_overall = 0;
     $storage_active = 0;
     $cpu_total = 0;
     $mem_used = 0;
     $mem_total = 0;
     // run only each minute
     $db = openqrm_get_db_connection();
     $rs = $db->Execute("select MAX(datacenter_id) from {$this->_db_table}");
     foreach ($rs as $index => $dc) {
         if (isset($dc['MAX(datacenter_id)'])) {
             $this->last_statistics = $dc['MAX(datacenter_id)'];
         } else {
             if (isset($dc['max'])) {
                 $this->last_statistics = $dc['max'];
             }
         }
     }
     // get an array of resources which are assigned to an appliance
     $appliance_resources_array = array();
     $appliance = new appliance();
     $appliance_list = $appliance->get_all_ids();
     foreach ($appliance_list as $app) {
         $app_id = $app['appliance_id'];
         $g_appliance = new appliance();
         $g_appliance->get_instance_by_id($app_id);
         $g_appliance_resource = $g_appliance->resources;
         if (!strcmp($g_appliance->state, "active") || $g_appliance_resource == 0) {
             if ($g_appliance_resource != "-1") {
                 $appliance_resources_array[] .= $g_appliance_resource;
             }
         }
     }
     // get an array of resources which are a storage server
     $storage_resources_array = array();
     $storage = new storage();
     $storage_list = $storage->get_list();
     foreach ($storage_list as $store) {
         $storage_id = $store['value'];
         $g_storage = new storage();
         $g_storage->get_instance_by_id($storage_id);
         $g_storage_resource = $g_storage->resource_id;
         $storage_resources_array[] .= $g_storage_resource;
     }
     $resource = new resource();
     $resource_list = $resource->get_list();
     foreach ($resource_list as $res) {
         $res_id = $res['resource_id'];
         //echo "!! res_id $res_id <br>";
         $g_resource = new resource();
         $g_resource->get_instance_by_id($res_id);
         // start gathering
         $resources_all++;
         $cpu_total = $cpu_total + $g_resource->cpunumber;
         $mem_used = $mem_used + $g_resource->memused;
         $mem_total = $mem_total + $g_resource->memtotal;
         // resource load
         if ("{$g_resource->imageid}" == "1" && "{$g_resource->state}" == "active") {
             // idle
             $resources_available++;
         } else {
             if ("{$g_resource->state}" == "active") {
                 // active
                 $resources_active++;
                 $dc_load_overall = $dc_load_overall + $g_resource->load;
                 // is storage ?
                 if (in_array($g_resource->id, $storage_resources_array)) {
                     $storage_active++;
                     $storage_load_overall = $storage_load_overall + $g_resource->load;
                 }
                 // is appliance ?
                 if (in_array($g_resource->id, $appliance_resources_array)) {
                     $appliance_active++;
                     $appliance_load_overall = $appliance_load_overall + $g_resource->load;
                 }
             }
         }
     }
     if ($resources_active != 0) {
         $dc_load_overall = $dc_load_overall / $resources_active;
         $dc_load_overall = number_format($dc_load_overall, 2, '.', '');
     }
     if ($appliance_active != 0) {
         $appliance_load_overall = $appliance_load_overall / $appliance_active;
         $appliance_load_overall = number_format($appliance_load_overall, 2, '.', '');
     }
     if ($storage_active != 0) {
         $storage_load_overall = $storage_load_overall / $storage_active;
         $storage_load_overall = number_format($storage_load_overall, 2, '.', '');
     }
     $datacenter_fields = array();
     $datacenter_fields['datacenter_load_overall'] = $dc_load_overall;
     $datacenter_fields['datacenter_load_server'] = $appliance_load_overall;
     $datacenter_fields['datacenter_load_storage'] = $storage_load_overall;
     $datacenter_fields['datacenter_cpu_total'] = $cpu_total;
     $datacenter_fields['datacenter_mem_total'] = $mem_total;
     $datacenter_fields['datacenter_mem_used'] = $mem_used;
     $stats_count = $this->get_count();
     if ($stats_count >= $this->resolution) {
         $rs = $db->Execute("select MIN(datacenter_id) from {$this->_db_table}");
         foreach ($rs as $index => $dc) {
             if (isset($dc['MIN(datacenter_id)'])) {
                 $this->oldest_statistics_id = $dc['MIN(datacenter_id)'];
             } else {
                 if (isset($dc['min'])) {
                     $this->oldest_statistics_id = $dc['min'];
                 }
             }
         }
         $rs = $db->Execute("delete from {$this->_db_table} where datacenter_id={$this->oldest_statistics_id}");
         $stats_count = $this->get_count();
     }
     $datacenter_fields['datacenter_id'] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
     $result = $db->AutoExecute($this->_db_table, $datacenter_fields, 'INSERT');
     if (!$result) {
         $this->event->log("add", $_SERVER['REQUEST_TIME'], 2, "datacenter.class.php", "Failed updating datacenter " . $datacenter_fields['datacenter_id'], "", "", 0, 0, 0);
     }
 }
 function remove()
 {
     $response = $this->get_response();
     $kernels = $response->html->request()->get($this->identifier_name);
     $form = $response->form;
     $kernel = new kernel();
     if ($kernels !== '') {
         $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 ($kernels as $id) {
             $kernel = $kernel->get_instance_by_id($id);
             $d['param_f' . $i]['label'] = $kernel->name;
             $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'] = $id;
             $d['param_f' . $i]['object']['attrib']['checked'] = true;
             $i++;
         }
         $form->add($d);
         if (!$form->get_errors() && $response->submit()) {
             $errors = array();
             $message = array();
             foreach ($kernels as $key => $id) {
                 $kernel = $kernel->get_instance_by_id($id);
                 // check that this is not the default kernel
                 if ($id == 1) {
                     $message[] = sprintf($this->lang['msg_not_removing_default']);
                     continue;
                 }
                 // check that this kernel is not in use any more
                 $kernel_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_kernel_remove_check = new appliance();
                     $app_kernel_remove_check->get_instance_by_id($appliance_id);
                     if ($app_kernel_remove_check->kernelid == $id) {
                         $kernel_is_used_by_appliance .= $appliance_id . " ";
                         $remove_error = 1;
                     }
                 }
                 if ($remove_error == 1) {
                     $message[] = sprintf($this->lang['msg_not_removing_active'], $id, $kernel_is_used_by_appliance);
                     continue;
                 }
                 $kernel_name = $kernel->name;
                 $kernel->remove($id);
                 $form->remove($this->identifier_name . '[' . $key . ']');
                 $message[] = sprintf($this->lang['msg'], $kernel_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 remove()
 {
     $response = $this->get_response();
     $resources = $response->html->request()->get($this->identifier_name);
     $force_remove = $response->html->request()->get('force_remove');
     $form = $response->form;
     $resource = new resource();
     if ($resources !== '') {
         $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 ($resources as $id) {
             $resource = $resource->get_instance_by_id($id);
             $d['param_f' . $i]['label'] = $resource->hostname;
             $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'] = $id;
             $d['param_f' . $i]['object']['attrib']['checked'] = true;
             $i++;
         }
         $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();
             foreach ($resources as $key => $id) {
                 $resource = $resource->get_instance_by_id($id);
                 if ($force_remove != 1) {
                     // check that the state is poweroff or error
                     if ($resource->state != 'error' && $resource->state != 'off' && $resource->state != 'unknown') {
                         $message[] = sprintf($this->lang['msg_still_active'], $id);
                         continue;
                     }
                     // check that resource is not still used by an appliance
                     $resource_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_resource_remove_check = new appliance();
                         $app_resource_remove_check->get_instance_by_id($appliance_id);
                         if ($app_resource_remove_check->resources == $id) {
                             $resource_is_used_by_appliance .= $appliance_id . " ";
                             $remove_error = 1;
                         }
                     }
                     if ($remove_error == 1) {
                         $message[] = sprintf($this->lang['msg_not_removing'], $id, $resource_is_used_by_appliance);
                         continue;
                     }
                     // check that this is a physical resource, VMs are removed through their VM Manager
                     if ($resource->vtype != 1) {
                         $virtualization = new virtualization();
                         $virtualization->get_instance_by_id($resource->vtype);
                         $virtualization_name = str_replace('-vm', '', $virtualization->type);
                         $message[] = sprintf($this->lang['msg_not_removing_vm'], $id, $virtualization_name);
                         continue;
                     }
                 }
                 $resource->remove($id, $resource->mac);
                 $form->remove($this->identifier_name . '[' . $key . ']');
                 $message[] = sprintf($this->lang['msg'], $id);
             }
             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 dc_status()
 {
     // number of idle systems
     $resources_all = 0;
     // active deployed resources
     $resources_active = 0;
     // resources in error state
     $resources_error = 0;
     // physical resources
     $resources_physical = 0;
     // virtual resources
     $resources_virtual = 0;
     // number of idle systems
     $resources_available = 0;
     // physical resource available
     $resources_available_physical = 0;
     // virtal resource available
     $resources_available_virtual = 0;
     // overall load
     $dc_load_overall = 0;
     // active appliance load
     $appliance_load_overall = 0;
     // peak in appliance load
     $appliance_load_peak = 0;
     // active appliances
     $appliance_active = 0;
     // active appliance with resource in error state
     $appliance_error = 0;
     // storage load
     $storage_load_overall = 0;
     // storage peak
     $storage_load_peak = 0;
     // active storages
     $storage_active = 0;
     // storage with resource in error state
     $storage_error = 0;
     // get an array of resources which are assigned to an appliance
     $appliance_resources_array = array();
     $appliance = new appliance();
     $appliance_list = $appliance->get_all_ids();
     foreach ($appliance_list as $app) {
         $app_id = $app['appliance_id'];
         $g_appliance = new appliance();
         $g_appliance->get_instance_by_id($app_id);
         $g_appliance_resource = $g_appliance->resources;
         if (!strcmp($g_appliance->state, "active") || $g_appliance_resource == 0) {
             if ($g_appliance_resource != "-1") {
                 $appliance_resources_array[] .= $g_appliance_resource;
             }
         }
     }
     // get an array of resources which are a storage server
     $storage_resources_array = array();
     $storage = new storage();
     $storage_list = $storage->get_list();
     foreach ($storage_list as $store) {
         $storage_id = $store['value'];
         $g_storage = new storage();
         $g_storage->get_instance_by_id($storage_id);
         $g_storage_resource = $g_storage->resource_id;
         $storage_resources_array[] .= $g_storage_resource;
     }
     $restype = 0;
     $resource = new resource();
     $resource_list = $resource->get_list();
     foreach ($resource_list as $res) {
         $res_id = $res['resource_id'];
         //echo "!! res_id $res_id <br>";
         $g_resource = new resource();
         $g_resource->get_instance_by_id($res_id);
         // start gathering
         $resources_all++;
         // physical or virtual ?
         if (strlen($g_resource->vtype) && $g_resource->vtype != "NULL") {
             $virtualization = new virtualization();
             $virtualization->get_instance_by_id($g_resource->vtype);
             if (strstr($virtualization->type, "-vm")) {
                 // virtual
                 $resources_virtual++;
                 $restype = 1;
             } else {
                 // physical
                 $resources_physical++;
                 $restype = 0;
             }
         } else {
             // we treat unknown system types as physical
             $resources_physical++;
             $restype = 0;
         }
         // resource load
         // is idle or active ?
         if ("{$g_resource->imageid}" == "1" && "{$g_resource->state}" == "active") {
             // idle
             $resources_available++;
             // virtual or physical ?
             if ($restype == 0) {
                 $resources_available_physical++;
             } else {
                 $resources_available_virtual++;
             }
         } else {
             if ("{$g_resource->state}" == "active") {
                 // active
                 $resources_active++;
                 $dc_load_overall = $dc_load_overall + $g_resource->load;
                 // is storage ?
                 if (in_array($g_resource->id, $storage_resources_array)) {
                     $storage_active++;
                     $storage_load_overall = $storage_load_overall + $g_resource->load;
                     // is peak ?
                     if ($g_resource->load > $storage_load_peak) {
                         $storage_load_peak = $g_resource->load;
                     }
                 }
                 // is appliance ?
                 if (in_array($g_resource->id, $appliance_resources_array)) {
                     $appliance_active++;
                     $appliance_load_overall = $appliance_load_overall + $g_resource->load;
                     // is peak ?
                     if ($g_resource->load > $appliance_load_peak) {
                         $appliance_load_peak = $g_resource->load;
                     }
                 }
             } else {
                 if ("{$g_resource->state}" == "error") {
                     // error
                     $resources_error++;
                     // is storage ?
                     if (in_array($g_resource->id, $storage_resources_array)) {
                         $storage_error++;
                     }
                     // is appliance ?
                     if (in_array($g_resource->id, $appliance_resources_array)) {
                         $appliance_error++;
                     }
                 }
             }
         }
     }
     // end of gathering
     // divide with number of active resources, appliances + storages
     if ($resources_active != 0) {
         $dc_load_overall = $dc_load_overall / $resources_active;
         $dc_load_overall = number_format($dc_load_overall, 2, '.', '');
     }
     if ($appliance_active != 0) {
         $appliance_load_overall = $appliance_load_overall / $appliance_active;
         $appliance_load_overall = number_format($appliance_load_overall, 2, '.', '');
     }
     if ($storage_active != 0) {
         $storage_load_overall = $storage_load_overall / $storage_active;
         $storage_load_overall = number_format($storage_load_overall, 2, '.', '');
     }
     echo "{$dc_load_overall},{$storage_load_overall},{$storage_load_peak},{$appliance_load_overall},{$appliance_load_peak},{$resources_all},{$resources_physical},{$resources_virtual},{$resources_available},{$resources_available_physical},{$resources_available_virtual},{$resources_error},{$appliance_error},{$storage_error}";
     exit(0);
 }
 function __free_cloud_ips($cr_id, $noop)
 {
     $error = '';
     $cc_conf = new cloudconfig();
     $show_ip_mgmt = $cc_conf->get_value(26);
     // ip-mgmt enabled ?
     if ($cr_id == 0) {
         // check and clean all ips
         if (!strcmp($show_ip_mgmt, "true")) {
             if (file_exists($this->webdir . "/plugins/ip-mgmt/.running")) {
                 require_once $this->webdir . "/plugins/ip-mgmt/class/ip-mgmt.class.php";
                 $ip_mgmt = new ip_mgmt();
                 $ip_mgmt_id_array = $ip_mgmt->get_ids();
                 foreach ($ip_mgmt_id_array as $ip_id) {
                     $ip = $ip_mgmt->get_instance('id', $ip_id['ip_mgmt_id']);
                     $state = $ip['ip_mgmt_state'];
                     $token = $ip['ip_mgmt_token'];
                     $found_token = false;
                     // check if the appliance still exists
                     if (strlen($ip['ip_mgmt_appliance_id'])) {
                         $ip_appliance = new appliance();
                         if ($ip_appliance->is_id_free($ip['ip_mgmt_appliance_id'])) {
                             // error
                             $error .= "found left over ip " . $ip['ip_mgmt_address'] . "<br>";
                         }
                     }
                     // check if we have a token set, if yes check all appliances for that token
                     if (strlen($token) && $token != '0') {
                         $ip_appliance = new appliance();
                         $ip_appliance_id_array = $ip_appliance->get_all_ids();
                         foreach ($ip_appliance_id_array as $ip_app_id_arr) {
                             $ip_app_id = $ip_app_id_arr['appliance_id'];
                             $ip_appliance->get_instance_by_id($ip_app_id);
                             if (strlen($ip_appliance->capabilities)) {
                                 $pos = strpos($ip_appliance->capabilities, $token);
                                 if ($pos !== false) {
                                     $found_token = true;
                                 }
                             }
                         }
                         if (!$found_token) {
                             // error
                             $error .= "found left over ip with token set / appliance missing " . $ip[ip_mgmt_address] . "<br>";
                         }
                     } else {
                         if ($state == 1) {
                             // error
                             $error .= "found left over ip with no token set " . $ip[ip_mgmt_address] . "<br>";
                         }
                     }
                 }
             }
         }
     } else {
         // clean ips for a specific cr
         $cr_ip = new cloudrequest();
         $cr_ip->get_instance_by_id($cr_id);
         if (!strcmp($show_ip_mgmt, "true")) {
             if (file_exists($this->webdir . "/plugins/ip-mgmt/.running")) {
                 require_once $this->webdir . "/plugins/ip-mgmt/class/ip-mgmt.class.php";
                 $ip_mgmt_array = explode(",", $cr_ip->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();
                     if ($noop) {
                         $ip_mgmt_assign->update_ip($ip_mgmt_id, $ip_mgmt_fields);
                     } else {
                         // error
                         $error .= "freeing up ip " . $ip_mgmt_id . "<br>";
                     }
                 }
             }
         }
     }
     return $error;
 }
 function find_host_to_start_from_off($virtualization_type_id, $resource_pools_enabled, $cu_id, $timeout)
 {
     global $OPENQRM_SERVER_BASE_DIR;
     global $OPENQRM_SERVER_IP_ADDRESS;
     global $OPENQRM_EXEC_PORT;
     global $RESOURCE_INFO_TABLE;
     global $vmware_mac_address_space;
     global $RootDir;
     $this->init($timeout);
     global $event;
     // find out the host virtualization type via the plugin name
     $vhost_type = new virtualization();
     $vhost_type->get_instance_by_id($virtualization_type_id);
     $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 5, "cloudhoststartfromoff.class.php", "Trying to find a powered-off virtualization host from type {$vhost_type->type} {$vhost_type->name}", "", "", 0, 0, 0);
     // for all in appliance list, find virtualization host appliances
     $appliance_tmp = new appliance();
     $appliance_id_list = $appliance_tmp->get_all_ids();
     $in_active_appliance_list = array();
     foreach ($appliance_id_list as $id_arr) {
         foreach ($id_arr as $id) {
             $appliance = new appliance();
             $appliance->get_instance_by_id($id);
             $sfo_resource = new resource();
             $sfo_resource->get_instance_by_id($appliance->resources);
             // state off ?
             if (!strcmp($appliance->state, "stopped") && !strcmp($sfo_resource->state, "off")) {
                 if ($appliance->virtualization == $virtualization_type_id) {
                     // we have found an active appliance from the right virtualization type
                     //
                     // 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("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "Hostlimit max_vm is reached for resource {$appliance->resources}", "", "", 0, 0, $appliance->resources);
                                 continue;
                             }
                         }
                     }
                     // resource pooling enabled ?
                     if (strcmp($resource_pools_enabled, "true")) {
                         // disabled, add any appliance from the right virtualization type
                         // $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 5, "cloudhoststartfromoff.class.php", "resource pooling is disabled", "", "", 0, 0, 0);
                         // check if the resource can start-from-off
                         $can_start_from_off = $sfo_resource->get_resource_capabilities('SFO');
                         if ($can_start_from_off == 1) {
                             $in_active_appliance_list[] .= $id;
                             $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 5, "cloudhoststartfromoff.class.php", "Resource pooling is disabled, adding appliance {$id}", "", "", 0, 0, 0);
                         } else {
                             $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "Resource pooling is disabled, resource of appliance {$id} cannot start-from-off", "", "", 0, 0, 0);
                         }
                     } else {
                         // $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.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("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "resource pool id $private_resource->id ", "", "", 0, 0, 0);
                         if (strlen($private_resource->id)) {
                             // $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "resource $appliance->resources is in a resource pool", "", "", 0, 0, 0);
                             // is it hidden ?
                             if ($private_resource->cg_id >= 0) {
                                 // $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.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("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.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
                                     // check if the resource can start-from-off
                                     $sfo_resource = new resource();
                                     $sfo_resource->get_instance_by_id($appliance->resources);
                                     $can_start_from_off = $sfo_resource->get_resource_capabilities('SFO');
                                     if ($can_start_from_off == 1) {
                                         $in_active_appliance_list[] .= $id;
                                         $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 5, "cloudhoststartfromoff.class.php", "Adding appliance {$id}", "", "", 0, 0, 0);
                                     } else {
                                         $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "Resource of appliance {$id} cannot start-from-off", "", "", 0, 0, 0);
                                     }
                                 } else {
                                     $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "Appliance {$id} (resource {$appliance->resources}) is NOT in dedicated for the users group", "", "", 0, 0, 0);
                                 }
                             } else {
                                 $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "Appliance {$id} (resource {$appliance->resources}) is marked as hidden", "", "", 0, 0, 0);
                             }
                         } else {
                             $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "Appliance {$id} (resource {$appliance->resources}) is NOT member of any resource pools", "", "", 0, 0, 0);
                         }
                     }
                 }
             }
         }
     }
     // did we found any active host ?
     if (count($in_active_appliance_list) < 1) {
         $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "Warning ! There is no virtualization host type {$vhost_type->name} available to start-from-off", "", "", 0, 0, 0);
         $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "Warning : Giving up to start a virtualization host type {$vhost_type->name} from power-off state .....", "", "", 0, 0, 0);
         return 0;
     }
     // simply take the first one
     foreach ($in_active_appliance_list as $in_active_id) {
         $in_active_appliance = new appliance();
         $in_active_appliance->get_instance_by_id($in_active_id);
         break;
     }
     // simply start the appliance, the rest will be done by the appliance start hook sending power-on
     // monitor until it is fully up or timeout
     $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 5, "cloudhoststartfromoff.class.php", "Notice: Starting host appliance {$in_active_id}, waiting until it is fully active ...", "", "", 0, 0, 0);
     $in_active_appliance->start();
     // check until it is full up
     $in_active_resource = new resource();
     $sec_loops = 0;
     while (0 == 0) {
         echo " ";
         flush();
         sleep(2);
         $sec_loops++;
         $sec_loops++;
         // check if the resource is active
         $in_active_resource->get_instance_by_id($in_active_appliance->resources);
         if (!strcmp($in_active_resource->state, "active")) {
             // the host is up :) return the appliance id of the host
             $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 5, "cloudhoststartfromoff.class.php", "Notice: Host resource id {$in_active_resource->id} successfully started from power-off", "", "", 0, 0, 0);
             return $in_active_id;
         }
         if ($this->timeout <= $sec_loops) {
             $event->log("find_host_to_start_from_off", $_SERVER['REQUEST_TIME'], 2, "cloudhoststartfromoff.class.php", "Error:Timeout while waiting for resource id {$in_active_resource->id} to start-from-off", "", "", 0, 0, 0);
             return 0;
         }
     }
 }