Difference between revisions of "Advanced Cloud Hosting/Cloud API"

From XMission Wiki
Jump to: navigation, search
(Accessing a Resource)
Line 21: Line 21:
  
  
Below is a PHP script that you can add to a cron job - this one we will run every five minutes. The timing is up to completely up to you and you can change it any time you want. A shorter time makes the server more sensitive to traffic and load, however it also puts additional load on the server itself.  
+
==Server Management==
 +
Using this API can come in handy for various types of resource information for example you can use the API to list your current server, configure your server, even stop and start lets take a look at a few basic commands.  
  
 +
Use this request to obtain the list of servers owned by the current user
 +
<code><pre>https://api.cloud.xmission.com:4464/paci/v1.0/ve</pre></code>
 +
 +
A sample out put would list your current servers.
 
<code><pre>
 
<code><pre>
#!/usr/bin/php -q
+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?php
+
<ve-list>
// Script for resizing server memory using the XMission Cloud Infrastructure API
+
    <ve-info description="XMission minecraft server" state="STARTED" name="minecraft" subscription-id="1003209"/>
 +
</ve-list>
 +
</pre></code>
  
// Configuration
+
If you want to be more specific and obtain information about a specific server you can add the server name like this:
 +
<code><pre>https://api.cloud.xmission.com:4464/paci/v1.0/ve/server1</pre></code>
  
// Memory tolerances
+
A sample out put will look like this:
define("LOWER_RAM_LIMIT", 256); # The lowest amount of memory for the server
+
<code><pre>
define("UPPER_RAM_LIMIT", 1024); # The highest amount of memory for the server
+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
define("LOWER_TOLERANCE", 0.25); # Decrease the memory if ratio is below this
+
<ve>
define("UPPER_TOLERANCE", 0.8); # Increase the memory if ratio is above this
+
     <id>296</id>
define("MEMORY_INCREMENT", 256); # Increment the memory by this amount
+
     <uuid>3c1c50b1.13d794f9065._7ff0</uuid>
 
+
     <hnId>14</hnId>
// The RESTful API
+
     <customer-id>1000001</customer-id>
define("API_URL", "https://api.cloud.xmission.com:4464/paci/v1.0/");
+
     <name>server1</name>
 
+
     <description>Testing Server</description>
// Your username and password for the API (same as your login to https://cp.xmission.com)
+
     <subscription-id>100001</subscription-id>
define("USER", '<YOUR_USER_NAME>');
+
     <cpu number="4" power="1500"/>
define("PASS", '<YOUR_PASSWORD>');
+
     <ram-size>6400</ram-size>
 
+
    <bandwidth>10000</bandwidth>
// The name of the server - should match Control Panel's name of the server
+
     <ve-disk created="true" id="0" local="true" size="25"/>
define("VPS_HOST_NAME", 'webserver01');
+
     <platform>
// Check RAM usage
+
        <template-info name="ubuntu-12.04-x86_64"/>
error_log("resize_check: Checking server RAM usage.");
+
        <os-info type="ubuntu-12.04-x64" technology="CT"/>
 
+
    </platform>
// Get the raw memory output
+
     <network private-ip="10.0.0.0/8">
$raw_output = shell_exec("free -o -m");
+
        <public-ip chunk-ref="1" id="269" address="123.123.123.123/24" gateway="123.123.123.1"/>
$memory_lines = explode("\n", $raw_output);
+
     </network>
$current_memory = preg_split("/[\s:]+/",$memory_lines[1]);
+
     <state>STARTED</state>
 
+
     <primary-disk-id>0</primary-disk-id>
// $current_memory[1] is total, $current_memory[2] is used
+
     <template-id>12</template-id>
$current_memory_load = $current_memory[2] / $current_memory[1];
+
     <admin login="root" password="[hidden]"/>
error_log("resize_check: Current memory use is: " . $current_memory_load) ;
+
     <steady-state>STARTED</steady-state>
 
+
</ve>
// Is the current memory load in tolerance?
 
if (LOWER_TOLERANCE > $current_memory_load) {
 
     error_log("resize_check: Current memory usage of $current_memory_load below tolerance. Resizing.");
 
 
 
     $current_memory_setting = get_mem();
 
    error_log("resize_check: Current memory setting is: " . $current_memory_setting) ;
 
 
 
    if ($current_memory_setting > LOWER_RAM_LIMIT) {
 
        decrease_memory($current_memory_setting, MEMORY_INCREMENT);
 
        error_log("resize_check: Decreased memory by " . MEMORY_INCREMENT ) ;
 
    } else {
 
        error_log("resize_check: Could not decrease memory more, already at lowest limit.") ;
 
     }
 
 
 
     exit(0);
 
} else if ($current_memory_load > UPPER_TOLERANCE) {
 
     error_log("resize_check: Current memory usage of $current_memory_load above tolerance. Resizing.");
 
 
 
     $current_memory_setting = get_mem();
 
    error_log("resize_check: Current memory setting is: " . $current_memory_setting) ;
 
 
 
    if ($current_memory_setting < UPPER_RAM_LIMIT) {
 
        increase_memory($current_memory_setting, MEMORY_INCREMENT);
 
        error_log("resize_check: Increased memory by " . MEMORY_INCREMENT ) ;
 
    } else {
 
        error_log("resize_check: Could not increase memory more, already at highest limit.") ;
 
    }
 
 
 
    exit(0);
 
} else {
 
    error_log("resize_check: Current memory usage of $current_memory_load in tolerance. Exiting.");
 
    exit(0);
 
}
 
// Helper Functions
 
function get_mem() {
 
     $r_xml = req_rest('GET', VPS_HOST_NAME);
 
    return ((string) $r_xml->{'ram-size'}[0]);
 
}
 
 
 
function increase_memory($current, $increase_by) {
 
    $size_to_increase_to = $current + $increase_by;
 
     error_log("resize_check: setting memory to: " . $size_to_increase_to);
 
    $xml = '<?xml version="1.0" encoding="UTF-8"?>' .
 
     "<reconfigure-ve><ram-size>$size_to_increase_to</ram-size></reconfigure-ve>";
 
 
 
     $result = req_rest("PUT", VPS_HOST_NAME, $xml);
 
    return $result;
 
}
 
 
 
function decrease_memory($current, $decrease_by) {
 
    $size_to_decrease_to = $current - $decrease_by;
 
    error_log("resize_check: setting memory to: " . $size_to_decrease_to);
 
    $xml = '<?xml version="1.0" encoding="UTF-8"?>' .
 
     "<reconfigure-ve><ram-size>$size_to_decrease_to</ram-size></reconfigure-ve>";
 
 
 
     $result = req_rest("PUT", VPS_HOST_NAME, $xml);
 
    return $result;
 
}
 
 
 
function req_rest($method, $get_params = "", $parameters = "", $additional_headers = "") {
 
    $process = curl_init();
 
     curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additional_headers));
 
     //curl_setopt($process, CURLOPT_VERBOSE, 1);
 
     curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);
 
    curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 0);
 
     //curl_setopt($process, CURLOPT_HEADER, 1);
 
     curl_setopt($process, CURLOPT_USERPWD, USER. ":" . PASS);
 
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
 
 
 
    switch($method) {
 
        case 'POST':
 
            curl_setopt($process, CURLOPT_POST, 1);
 
            curl_setopt($process, CURLOPT_POSTFIELDS, array($parameters));
 
            break;
 
        case 'PUT':
 
            curl_setopt($process, CURLOPT_CUSTOMREQUEST, "PUT");
 
            curl_setopt($process, CURLOPT_POSTFIELDS, $parameters);
 
        case 'GET':
 
            default:
 
            break;
 
     }
 
 
 
    curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
 
    curl_setopt($process, CURLOPT_URL, API_URL . "ve/" . $get_params);
 
    $return = curl_exec($process);
 
    curl_close($process);
 
 
 
    if (strpos($return, '<') !== false) {
 
        $xml = new SimpleXmlElement($return);
 
        return $xml;
 
    } else {
 
        return $return;
 
    }
 
}
 
 
</pre></code>
 
</pre></code>
  
 +
Here you can see it shows you all relative information about the server including the number of CPUs, CPU Power, Ram, IP address and much more.
  
For an explanation of what is going on here visit our blog at:
+
Many more useful command can be used as well - more common would be to start or stop your server.  
:[http://transmission.xmission.com/2013/08/19/use-the-cloud-infrastructure-api-to-automatically-resize-your-server http://transmission.xmission.com/2013/08/19/use-the-cloud-infrastructure-api-to-automatically-resize-your-server]
 
  
  
You can save this script to ''usr/local/sbin/resize_check.php'', then you will add it to the crontab using '''crontab -e''' with the following entry this will run your script every five minutes:
 
<pre>
 
*/5 * * * * /usr/local/sbin/resize_check.php
 
</pre>
 
  
If you want to set up your cron schedule for other times then this document you can visit the following link on how to set up a cron job to meet your needs:
 
:[http://en.wikipedia.org/wiki/Cron http://en.wikipedia.org/wiki/Cron]
 
  
  
 
This documentation is to be used as a guide. Your server will require different settings to optimize your traffic and this code is not perfect. We encourage you to customize it to fit your needs, or write it in a language of your choice. Remember to refer to the documentation.
 
This documentation is to be used as a guide. Your server will require different settings to optimize your traffic and this code is not perfect. We encourage you to customize it to fit your needs, or write it in a language of your choice. Remember to refer to the documentation.

Revision as of 13:11, 9 October 2013

If you find yourself with traffic to your website and consistently trying to log in to your Control Panel to adjust your servers RAM to compensate for the heavy load. We have a solution that may help you get some sleep.

Cloud Infrastructure API can Automatically Resize your Server. The RESTful API is on of the most useful and more interesting features of our Cloud Infrastructure service. With the ability to expose nearly all the features of your control panel the API will definitely help you get some 'REST'.

The Cloud Infrastructure RESTful API is well documented and available to all Cloud Infrastructure and Cloud Server subscribers. You can read the full documentation here:

http://download.pa.parallels.com/poa/5.4/doc/pdf/POA%20RESTful%20API%20Guide/paci-restful-api-guide-5.4.pdf


Accessing a Resource

A PACI resource is accessed by sending an HTTPS request to a PACI server. When the server receives a request, it processes it accordingly (performs actions or retrieves data) and sends back a response that can contain the data that you requested, an operation status code, or an error messate. All PACI resources are accessed at the following URL (referred to as baseURL)

https://{ip_address | hostname}:port/paci/version

where:

  1. ip_address | hostname is the PACI server IP address or hostname.
  2. port is the port number on which the server is listening for REST requests
  3. paci must be typed exactly as shown
  4. version is the API version number

For our resource the baseURL will use the following:

https://api.cloud.xmission.com:4464/paci/v1.0/


Server Management

Using this API can come in handy for various types of resource information for example you can use the API to list your current server, configure your server, even stop and start lets take a look at a few basic commands.

Use this request to obtain the list of servers owned by the current user

https://api.cloud.xmission.com:4464/paci/v1.0/ve

A sample out put would list your current servers.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ve-list>
    <ve-info description="XMission minecraft server" state="STARTED" name="minecraft" subscription-id="1003209"/>
</ve-list>

If you want to be more specific and obtain information about a specific server you can add the server name like this:

https://api.cloud.xmission.com:4464/paci/v1.0/ve/server1

A sample out put will look like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ve>
    <id>296</id>
    <uuid>3c1c50b1.13d794f9065._7ff0</uuid>
    <hnId>14</hnId>
    <customer-id>1000001</customer-id>
    <name>server1</name>
    <description>Testing Server</description>
    <subscription-id>100001</subscription-id>
    <cpu number="4" power="1500"/>
    <ram-size>6400</ram-size>
    <bandwidth>10000</bandwidth>
    <ve-disk created="true" id="0" local="true" size="25"/>
    <platform>
        <template-info name="ubuntu-12.04-x86_64"/>
        <os-info type="ubuntu-12.04-x64" technology="CT"/>
    </platform>
    <network private-ip="10.0.0.0/8">
        <public-ip chunk-ref="1" id="269" address="123.123.123.123/24" gateway="123.123.123.1"/>
    </network>
    <state>STARTED</state>
    <primary-disk-id>0</primary-disk-id>
    <template-id>12</template-id>
    <admin login="root" password="[hidden]"/>
    <steady-state>STARTED</steady-state>
</ve>

Here you can see it shows you all relative information about the server including the number of CPUs, CPU Power, Ram, IP address and much more.

Many more useful command can be used as well - more common would be to start or stop your server.



This documentation is to be used as a guide. Your server will require different settings to optimize your traffic and this code is not perfect. We encourage you to customize it to fit your needs, or write it in a language of your choice. Remember to refer to the documentation.