Пример #1
0
 function CloudApplianceResize($method_parameters)
 {
     global $event;
     $parameter_array = explode(',', $method_parameters);
     $mode = $parameter_array[0];
     $username = $parameter_array[1];
     $password = $parameter_array[2];
     $ca_id = $parameter_array[3];
     $ca_new_disk_size = $parameter_array[4];
     // check all user input
     for ($i = 0; $i <= 4; $i++) {
         if (!$this->check_param($parameter_array[$i])) {
             $event->log("cloudsoap->CloudApplianceResize", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Not allowing user-intput with special-characters : {$parameter_array[$i]}", "", "", 0, 0, 0);
             return 1;
         }
     }
     // check parameter count
     $parameter_count = count($parameter_array);
     if ($parameter_count != 5) {
         $event->log("cloudsoap->CloudApplianceResize", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Wrong parameter count {$parameter_count} ! Exiting.", "", "", 0, 0, 0);
         return 1;
     }
     // check authentication
     if (!$this->check_user($mode, $username, $password)) {
         $event->log("cloudsoap->CloudApplianceResize", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "User authentication failed (mode {$mode})", "", "", 0, 0, 0);
         return 1;
     }
     // disk-resize enabled ?
     $cd_config = new cloudconfig();
     $show_disk_resize = $cd_config->get_value(20);
     // show_disk_resize
     if (!strcmp($show_disk_resize, "false")) {
         $event->log("cloudsoap->CloudApplianceResize", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Disk resize is disabled! Not resizing", "", "", 0, 0, 0);
         return 1;
     }
     $cr_appliance = new cloudappliance();
     $cr_appliance->get_instance_by_id($ca_id);
     // get the request to check for the user
     $cr = new cloudrequest();
     $cr->get_instance_by_id($cr_appliance->cr_id);
     $cl_user = new clouduser();
     $cl_user->get_instance_by_id($cr->cu_id);
     switch ($mode) {
         case 'user':
             if (strcmp($username, $cl_user->name)) {
                 $event->log("cloudsoap->CloudApplianceResize", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$username} is trying to execute a Cloud-command on behalf of Cloud User {$cl_user->name}!", "", "", 0, 0, 0);
                 return 1;
             }
             break;
     }
     // check resize
     $appliance = new appliance();
     $appliance->get_instance_by_id($cr_appliance->appliance_id);
     $image = new image();
     $image->get_instance_by_id($appliance->imageid);
     $cloud_image = new cloudimage();
     $cloud_image->get_instance_by_image_id($image->id);
     $cloud_image_current_disk_size = $cloud_image->disk_size;
     if ($cloud_image_current_disk_size == $ca_new_disk_size) {
         $event->log("cloudsoap->CloudApplianceResize", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "New Disk size Cloud appliance {$cr_appliance->id} is equal current Disk size. Not resizing", "", "", 0, 0, 0);
         return 1;
     }
     if ($cloud_image_current_disk_size > $ca_new_disk_size) {
         $event->log("cloudsoap->CloudApplianceResize", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "New Disk size Cloud appliance {$cr_appliance->id} needs to be greater current Disk size. Not resizing", "", "", 0, 0, 0);
         return 1;
     }
     // check if no other command is currently running
     if ($cr_appliance->cmd != 0) {
         $event->log("cloudsoap->CloudApplianceResize", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Another command is already registerd for Cloud appliance {$cr_appliance->id}", "", "", 0, 0, 0);
         return 1;
     }
     // check that state is active
     if ($cr_appliance->state != 1) {
         $event->log("cloudsoap->CloudApplianceResize", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Can only resize Cloud appliance {$cr_appliance->id} if it is in active state", "", "", 0, 0, 0);
         return 1;
     }
     $additional_disk_space = $ca_new_disk_size - $cloud_image_current_disk_size;
     // put the new size in the cloud_image
     $cloudi_request = array('ci_disk_rsize' => "{$ca_new_disk_size}");
     $cloud_image->update($cloud_image->id, $cloudi_request);
     // create a new cloud-image resize-life-cycle / using cloudappliance id
     $cloudirlc = new cloudirlc();
     $cirlc_fields['cd_id'] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
     $cirlc_fields['cd_appliance_id'] = $cr_appliance->id;
     $cirlc_fields['cd_state'] = '1';
     $cloudirlc->add($cirlc_fields);
     $event->log("cloudsoap->CloudApplianceResize", $_SERVER['REQUEST_TIME'], 5, "cloud-soap-server.php", "Updating comment of Cloud appliance {$ca_id}", "", "", 0, 0, 0);
     return 0;
 }
 function update()
 {
     $this->ca_id = $this->response->html->request()->get($this->identifier_name);
     $this->response->add($this->identifier_name, $this->ca_id);
     $this->cloudappliance->get_instance_by_id($this->ca_id);
     $this->appliance->get_instance_by_id($this->cloudappliance->appliance_id);
     $this->cloudrequest->get_instance_by_id($this->cloudappliance->cr_id);
     // check appliance belongs to user
     if ($this->cloudrequest->cu_id != $this->clouduser->id) {
         $response = $this->response;
         $response->msg = sprintf($this->lang['appliances']['error_access_denied'], $this->ca_id);
     } else {
         $response = $this->get_response();
         $form = $response->form;
         if (!$form->get_errors() && $response->submit()) {
             $comment = $form->get_request('comment');
             $cpu = $form->get_request('cpu');
             $memory = $form->get_request('memory');
             $disk = $form->get_request('disk');
             // update appliance comment
             if (isset($comment)) {
                 $name = $this->appliance->name;
                 $appliance_fields['appliance_comment'] = $comment;
                 $this->appliance->update($this->appliance->id, $appliance_fields);
             }
             // update cpu_req in cr
             if (isset($cpu) && $cpu !== '' && $this->cloudappliance->state == 0 && $this->cloudrequest->cpu_req != $cpu) {
                 $cr_cpu_fields['cr_cpu_req'] = $cpu;
                 $this->cloudrequest->update($this->cloudrequest->id, $cr_cpu_fields);
             }
             // update ram_req in cr
             if (isset($memory) && $memory !== '' && $this->cloudappliance->state == 0 && $this->cloudrequest->ram_req != $memory) {
                 $cr_mem_fields['cr_ram_req'] = $memory;
                 $this->cloudrequest->update($this->cloudrequest->id, $cr_mem_fields);
             }
             // check resize
             if (isset($disk) && $disk !== '') {
                 $error = false;
                 $image = $this->openqrm->image();
                 $image->get_instance_by_id($this->appliance->imageid);
                 $this->cloudimage->get_instance_by_image_id($image->id);
                 $cloud_image_current_disk_size = $this->cloudimage->disk_size;
                 // check disk is bigger
                 if ($disk < $cloud_image_current_disk_size) {
                     $response->error = $this->lang['appliances']['error_disk_size'];
                     $error = true;
                 }
                 // check if no other command is currently running
                 if ($this->cloudappliance->cmd != 0) {
                     $response->error = $this->lang['appliances']['error_command_running'];
                     $error = true;
                 }
                 // check that state is active
                 if ($this->cloudappliance->state != 1) {
                     $response->error = $this->lang['appliances']['error_appliance_not_active'];
                     $error = true;
                 }
                 if (!$error) {
                     require_once $this->rootdir . '/plugins/cloud/class/cloudirlc.class.php';
                     $cloudirlc = new cloudirlc();
                     // put the disk in the cr
                     $cr_disk_fields['cr_disk_req'] = $disk;
                     $this->cloudrequest->update($this->cloudrequest->id, $cr_disk_fields);
                     // put the new size in the cloud_image
                     $cloudi_request = array('ci_disk_rsize' => "{$disk}");
                     $this->cloudimage->update($this->cloudimage->id, $cloudi_request);
                     // create a new cloud-image resize-life-cycle / using cloudappliance id
                     $cirlc_fields['cd_id'] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
                     $cirlc_fields['cd_appliance_id'] = $this->cloudappliance->id;
                     $cirlc_fields['cd_state'] = '1';
                     $cloudirlc->add($cirlc_fields);
                 }
             }
             $response->msg = sprintf($this->lang['appliances']['msg_updated_appliance'], $this->appliance->name);
         }
         $response->appliance = $this->appliance->name;
     }
     return $response;
 }