Ejemplo n.º 1
0
 function CloudProvision($method_parameters)
 {
     global $CLOUD_REQUEST_TABLE;
     global $event;
     $parameter_array = explode(',', $method_parameters);
     $mode = $parameter_array[0];
     $username = $parameter_array[1];
     $password = $parameter_array[2];
     $cloud_username = $parameter_array[3];
     $start = $parameter_array[4];
     $stop = $parameter_array[5];
     $kernel_id = $parameter_array[6];
     $image_name = $parameter_array[7];
     $ram_req = $parameter_array[8];
     $cpu_req = $parameter_array[9];
     $disk_req = $parameter_array[10];
     $network_req = $parameter_array[11];
     $resource_quantity = $parameter_array[12];
     $virtualization_id = $parameter_array[13];
     $ha_req = $parameter_array[14];
     $application_groups = $parameter_array[15];
     $ip_mgmt = $parameter_array[16];
     $app_hostname = $parameter_array[17];
     $app_hostname = preg_replace('/\\s\\s+/', ' ', trim($app_hostname));
     // check all user input
     for ($i = 0; $i <= 17; $i++) {
         if (!$this->check_param($parameter_array[$i])) {
             $event->log("cloudsoap->CloudProvision", $_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 != 18) {
         $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Wrong parameter count {$parameter_count} ! Exiting.", "", "", 0, 0, 0);
         return -2;
     }
     // check authentication
     if (!$this->check_user($mode, $username, $password)) {
         $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "User authentication failed (mode {$mode})", "", "", 0, 0, 0);
         return -3;
     }
     $cc_conf = new cloudconfig();
     $cl_user = new clouduser();
     // check that the user exists in the Cloud
     if ($cl_user->is_name_free($cloud_username)) {
         $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User name {$cloud_username} does not exists in the Cloud. Not adding the request !", "", "", 0, 0, 0);
         return -4;
     }
     // check that in user mode the username is the same as the cloud_username
     switch ($mode) {
         case 'user':
             if (strcmp($username, $cloud_username)) {
                 $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$username} is trying to submit a Provsion request as Cloud User {$cloud_username}  !", "", "", 0, 0, 0);
                 return -5;
             }
             break;
     }
     // check if billing is enabled
     $cl_user->get_instance_by_name($cloud_username);
     $cloud_billing_enabled = $cc_conf->get_value(16);
     // 16 is cloud_billing_enabled
     if ($cloud_billing_enabled == 'true') {
         if ($cl_user->ccunits < 1) {
             $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud for user {$cloud_username} does not have any CCUs! Not adding the request.", "", "", 0, 0, 0);
             return -6;
         }
     }
     // check valid hostname
     if (!$this->check_hostname($app_hostname)) {
         $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Not allowing user hostname intput with special-characters : " . $app_hostname, "", "", 0, 0, 0);
         return -1;
     }
     // check global limits
     // max disk size
     $max_disk_size = $cc_conf->get_value(8);
     // 8 is max_disk_size config
     if ($disk_req > $max_disk_size) {
         $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username} Disk must be <= {$max_disk_size}.", "", "", 0, 0, 0);
         return -7;
     }
     // max network interfaces
     $max_network_infterfaces = $cc_conf->get_value(9);
     // 9 is max_network_interfaces
     if ($network_req > $max_network_infterfaces) {
         $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username} Network must be <= {$max_network_infterfaces}.", "", "", 0, 0, 0);
         return -8;
     }
     // max resource per cr
     $max_res_per_cr = $cc_conf->get_value(6);
     // 6 is max_resources_per_cr
     if ($resource_quantity > $max_res_per_cr) {
         $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username} Resource quantity must be <= {$max_res_per_cr}.", "", "", 0, 0, 0);
         return -9;
     }
     // check user limits
     $cloud_user_limit = new clouduserlimits();
     $cloud_user_limit->get_instance_by_cu_id($cl_user->id);
     if (!$cloud_user_limit->check_limits($resource_quantity, $ram_req, $disk_req, $cpu_req, $network_req)) {
         $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username} exceeds its Cloud-Limits ! Not adding the request.", "", "", 0, 0, 0);
         return -10;
     }
     // check cloudselector
     // ####### start of cloudselector case #######
     // if cloudselector is enabled check if products exist
     $cloud_selector_enabled = $cc_conf->get_value(22);
     // cloudselector
     if (!strcmp($cloud_selector_enabled, "true")) {
         $cloudselector = new cloudselector();
         // cpu
         if (!$cloudselector->product_exists_enabled("cpu", $cpu_req)) {
             $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username}: Cloud CPU Product ({$cpu_req}) is not existing", "", "", 0, 0, 0);
             return -11;
         }
         // disk
         if (!$cloudselector->product_exists_enabled("disk", $disk_req)) {
             $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username}: Cloud Disk Product ({$disk_req}) is not existing", "", "", 0, 0, 0);
             return -12;
         }
         // kernel
         $cs_kernel = new kernel();
         $cs_kernel->get_instance_by_id($kernel_id);
         if (!$cloudselector->product_exists_enabled("kernel", $cs_kernel->id)) {
             $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username}: Cloud Kernel Product ({$cs_kernel->id}) is not existing", "", "", 0, 0, 0);
             return -13;
         }
         // memory
         if (!$cloudselector->product_exists_enabled("memory", $ram_req)) {
             $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username}: Cloud Memory Product ({$ram_req}) is not existing", "", "", 0, 0, 0);
             return -14;
         }
         // network
         if (!$cloudselector->product_exists_enabled("network", $network_req)) {
             $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username}: Cloud Network Product ({$network_req}) is not existing", "", "", 0, 0, 0);
             return -15;
         }
         // application
         if (strlen($application_groups)) {
             $application_groups_array = explode(":", $application_groups);
             if (is_array($application_groups_array)) {
                 foreach ($application_groups_array as $application_group) {
                     if (!$cloudselector->product_exists_enabled("application", $application_group)) {
                         $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username}: Cloud Application Product ({$application_group}) is not existing", "", "", 0, 0, 0);
                         return -16;
                     }
                 }
             }
             // reformat with , instead of :
             $application_groups = str_replace(":", ",", $application_groups);
         }
         // quantity
         if (!$cloudselector->product_exists_enabled("quantity", $resource_quantity)) {
             $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username}: Cloud Quantity Product ({$resource_quantity}) is not existing", "", "", 0, 0, 0);
             return -17;
         }
         // resource type
         $cs_resource = new virtualization();
         $cs_resource->get_instance_by_id($virtualization_id);
         if (!$cloudselector->product_exists_enabled("resource", $cs_resource->id)) {
             $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Cloud User {$cloud_username}: Virtualization Product ({$cs_resource->id}) is not existing", "", "", 0, 0, 0);
             return -18;
         }
         // ####### end of cloudselector case #######
     }
     // reformat the ip-mgmt string
     // separators are :
     //  / for :
     //  _ for ,
     $ip_mgmt_str_reformated = str_replace('/', ':', $ip_mgmt);
     $ip_mgmt_str_reformated = str_replace('_', ',', $ip_mgmt_str_reformated);
     $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 5, "cloud-soap-server.php", "Provisioning appliance in the openQRM Cloud for user {$cloud_username}", "", "", 0, 0, 0);
     // fill the array
     $request_fields['cr_cu_id'] = $cl_user->id;
     $request_fields['cr_start'] = $this->date_to_timestamp($start);
     $request_fields['cr_stop'] = $this->date_to_timestamp($stop);
     $request_fields['cr_lastbill'] = '';
     $request_fields['cr_resource_quantity'] = $resource_quantity;
     $request_fields['cr_resource_type_req'] = $resource_type_req;
     $request_fields['cr_ha_req'] = $ha_req;
     $request_fields['cr_network_req'] = $network_req;
     $request_fields['cr_ram_req'] = $ram_req;
     $request_fields['cr_cpu_req'] = $cpu_req;
     $request_fields['cr_disk_req'] = $disk_req;
     $request_fields['cr_puppet_groups'] = $application_groups;
     $request_fields['cr_ip_mgmt'] = $ip_mgmt_str_reformated;
     $request_fields['cr_appliance_hostname'] = $app_hostname;
     // translate kernel- and image-name to their ids
     $kernel = new kernel();
     $kernel->get_instance_by_id($kernel_id);
     $kernel_id = $kernel->id;
     $image = new image();
     $image->get_instance_by_name($image_name);
     $image_id = $image->id;
     $request_fields['cr_kernel_id'] = $kernel_id;
     $request_fields['cr_image_id'] = $image_id;
     // translate the virtualization type
     $virtualization = new virtualization();
     $virtualization->get_instance_by_id($virtualization_id);
     $virtualization_id = $virtualization->id;
     $request_fields['cr_resource_type_req'] = $virtualization_id;
     // private image ? if yes do not clone it
     $cc_soap_conf = new cloudconfig();
     $show_private_image = $cc_soap_conf->get_value(21);
     // show_private_image
     if (!strcmp($show_private_image, "true")) {
         $private_cu_image = new cloudprivateimage();
         $private_cu_image->get_instance_by_image_id($image_id);
         if (strlen($private_cu_image->cu_id)) {
             if ($private_cu_image->cu_id > 0) {
                 if ($private_cu_image->cu_id == $cl_user->id) {
                     // check if the private image is not attached to a resource
                     // because we don't want to use the same private image on two appliances
                     $cloudimage_state = new cloudimage();
                     $cloudimage_state->get_instance_by_image_id($image->id);
                     if (!$cloudimage_state->id) {
                         // set to shared!
                         $request_fields['cr_shared_req'] = 1;
                     } else {
                         $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Private Cloud image {$image->name} for Cloud User {$cloud_username} is already in use! Not adding the request.", "", "", 0, 0, 0);
                         return -19;
                     }
                 } else {
                     $event->log("cloudsoap->CloudProvision", $_SERVER['REQUEST_TIME'], 2, "cloud-soap-server.php", "Unauthorized request of private Cloud image from Cloud User {$cloud_username} ! Not adding the request.", "", "", 0, 0, 0);
                     return -20;
                 }
             } else {
                 $request_fields['cr_shared_req'] = 1;
             }
         } else {
             $request_fields['cr_shared_req'] = 1;
         }
     } else {
         $request_fields['cr_shared_req'] = 1;
     }
     // get next free id
     // $request_fields['cr_id'] = (int)str_replace(".", "", str_pad(microtime(true), 15, "0"));
     $request_fields['cr_id'] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
     // add request
     $cr_request = new cloudrequest();
     $cr_request->add($request_fields);
     return $request_fields['cr_id'];
 }