jgillman's Liquid Web Update Unofficial tips, tricks, and happenings from a Liquid Web sales engineer

30Apr/120

Interactive Storm API Testing Script

What happens when I get bored? I code apparently.

Here's a piece of PHP code that runs purely command line. What it does is allow you to interactively work with the StormOnDemand API.

Although the output is just a straight print_r() of the returned data set, it does provide a nice easy way if you want to see the returns from various methods (or while feeding the same method different parameter/value pairs) in an expedient manner - all without having to write/replace code in a static script.

Enjoy!

 PHP |  copy code |? 
01
02
<?php
03
	/*
04
	 * Author: Jason Gillman Jr.
05
	 * Description: My attempt at writing a simple interactive CLI script for dumping raw data from Storm API returns.
06
	 * 				All you are going to get is print_r() of the returned array.
07
	 * 				Hope it's useful!
08
	 */
09
 
10
	require_once('StormAPI.class.php');
11
 
12
	// Initial information
13
	echo "\nAPI Username: "; $api_user = trim(fgets(STDIN));
14
	echo "Password: "; $api_pass = trim(fgets(STDIN));
15
	echo "Initial Method: "; $api_method = trim(fgets(STDIN));
16
 
17
	$storm = new StormAPI($api_user, $api_pass, $api_method);
18
 
19
	// Menu
20
	while(!isset($stop))
21
	{
22
		echo "\n\nPick your poison... \n";
23
		echo "1. Change method (will clear params) \n";
24
		echo "2. Add parameter \n";
25
		echo "3. Clear parameters \n";
26
		echo "4. Execute request and display \n";
27
		echo "5. Get me out of here \n";
28
		echo "Enter a number: "; fscanf(STDIN, "%d\n", $choice); // Get the choice
29
 
30
		switch($choice)
31
		{
32
			case 1:
33
				echo "\nEnter your new method: "; $api_method = trim(fgets(STDIN));
34
				$storm->new_method($api_method);
35
				break;
36
			case 2:
37
				echo "\nEnter the parameter: "; $parameter = trim(fgets(STDIN));
38
				echo "\nEnter the value: "; $value = trim(fgets(STDIN));
39
				$storm->add_param($parameter, $value);
40
				unset($parameter, $value);
41
				break;
42
			case 3:
43
				$storm->clear_params();
44
				break;
45
			case 4:
46
				print_r($storm->request());
47
				break;
48
			case 5:
49
				echo "\n\n";
50
				$stop = TRUE;
51
				break;
52
			default:
53
				echo "Really? How about you enter a valid value?";
54
				break;
55
		}
56
	}
57
?>
58

29Jul/110

Copying databases from one cPanel user to another

I was going through some of my tomboy notes and saw some PHP code that I cobbled together for a customer who needed to copy MySQL databases from one cPanel user to another.

I thought that it may be useful to others as well

 PHP |  copy code |? 
01
<?php
02
	// cPanel user you are copying data from
03
	$from_user = "fromuser";
04
	// cPanel user you are copying data to
05
	$to_user = "touser";
06
	// Some place to temporarilly story the SQL dump files
07
	$dumpdir = "/root/tempdbdump/";	
08
 
09
	// Add all the databases you need copied over to the array
10
	$dumpdb[] = "db1";
11
	$dumpdb[] = "db2";
12
	$dumpdb[] = "db3";
13
	$dumpdb[] = "db4";
14
	$dumpdb[] = "db5";
15
	$dumpdb[] = "db6";
16
 
17
	foreach($dumpdb as $database)
18
	{
19
		$dumpline = "mysqldump " .  $from_user . "_" . $database . " " . $dumpdir . $database .".sql";
20
		shell_exec($dumpline);
21
 
22
		$restoreline = "mysql " $to_user . "_" . $databasename " < " . $dumpdir . $database . ".sql";
23
		shell_exec($restoreline);
24
	}
25
?>

One thing to note is that I got lazy and didn't include code to delete the dump file after use, but that's a relatively simple task.

Tagged as: , , No Comments
18Jul/114

Utilizing the Storm API with PHP

One of most powerful features of StormOnDemand is the API.

With the API, you can do anything with your Storm account programmaticaly that you can in the regular web based interface. It's a powerful tool that would allow for things such as automatic instance resizing based on load, emailed load reports, etc. However, I think that the API isn't utilized as much because people might not know how to work with it. What follows is a primer for utilizing PHP to interface with the API (I know PHP much, much, much more than Perl).

Although the documentation might seem a little terse in regards to making calls to the API, it essentially boils down to HTTP POST methods utilizing one of the supported encodings, which are currently JSON, YAML, and XML. Making these calls procedurally can quickly turn your code into something that resembles Italian cuisine, which is why I cobbled together a class to take a more object oriented approach.

The class utilizes the YAML encoding method, as well as the HTTP_REQUEST2 PEAR package. Again, this is just something that I put together, and there are probably much better ways to approach this. But it works. Here is the source:

 PHP |  copy code |? 
01
<?php
02
	/*
03
	 * Author: Jason Gillman Jr.
04
	 * Description: This is my attempt at writing a PHP wrapper that will ease Storm API calls with PHP
05
	 * 				It will be designed to use the YAML format for talking with the API server.
06
	 * 				$api_method is as described in docs (Case matters)
07
	 * 				request() method returns an array generated from the API return
08
	 */
09
	class StormAPI
10
	{
11
		// Let's define attributes
12
		private $api_user, $api_pass, $base_url, $api_format, $api_full_uri, $api_request;
13
		private $api_request_body, $api_method, $api_params, $api_return; 
14
 
15
		function __construct($api_user, $api_pass, $api_method)
16
		{
17
			// Requires PEAR Package HTTP_Request2
18
			require_once('HTTP/Request2.php');
19
 
20
			$this->api_user = $api_user;
21
			$this->api_pass = $api_pass;
22
			$this->api_method = $api_method;
23
			$this->base_url = 'https://api.stormondemand.com/';
24
			$this->api_format = 'yaml';
25
 
26
			$this->api_full_uri = $this->base_url . $this->api_method . "." .$this->api_format;
27
		}
28
 
29
		function add_param($parameter, $value)
30
		{
31
			$this->api_request_body['params'][$parameter] = $value;
32
		}
33
 
34
		function request()
35
		{
36
			// HTTP Request goodness
37
			if(isset($this->api_request_body)) // We have params
38
			{
39
				$api_request = new HTTP_Request2($this->api_full_uri, HTTP_Request2::METHOD_POST); // Instantiate with POST method since we'll be feeding params
40
				$api_request->setHeader('Content-type: application/yaml'); // Specify what you're feeding it
41
				$api_request->setBody(yaml_emit($this->api_request_body));
42
			}
43
			else // No params
44
			{
45
				$api_request = new HTTP_Request2($this->api_full_uri, HTTP_Request2::METHOD_GET); // Instantiate with GET method since no params
46
			}
47
			$api_request->setAuth($this->api_user, $this->api_pass, HTTP_Request2::AUTH_BASIC); // Auth stuff
48
			$api_request->setConfig('ssl_verify_peer', FALSE); // This is needed, otherwise HTTP_Request errors last I checked
49
 
50
			// Now send the request and get the return on investment
51
			try
52
			{
53
				$api_return = $api_request->send();
54
				return yaml_parse($api_return->getBody()); // We now have a nice array of all the returned data
55
			}
56
			catch (HTTP_Request2_Exception $e)
57
			{
58
				echo 'Error: ' . $e->getMessage();
59
			}
60
		}
61
	}
62
?>

One thing that I should mention before continuing is that you need to create an API user. This is done in the interface through Account >> Users

Essentially, you instantiate the class by passing the API username, password, and the API method you are going to be using. The API method comes in the format "First/Second/third" or "First/second" depending on the method you want to use. You don't include the leading slash, and the methods as far as I know are case sensitive.

Once you instantiate the object, you would use the add_param() method in the format of (parameter, value).

Once you add all the parameters (if any), just run the request() method, which will return an array containing the data.

So let's look at a real simple example scenario where you wanted to get the Five Minute load average for a particular server with a uniq_id of '123ABC':

 PHP |  copy code |? 
01
<?php
02
 
03
require_once(StormAPI.class.php); // Assuming we put the class in this file
04
 
05
$api_user = 'user';
06
$api_pass = 'pass';
07
$api_method = 'Monitoring/Load/stats';
08
 
09
$get_load = new StormAPI($api_user, $api_pass, $api_method);
10
$get_load->add_param('uniq_id', '123ABC'); // Where we pass our uniq_id
11
$results = $get_load->request(); // Initial array grab
12
 
13
// Now to get the five minute load
14
echo "Five Minute Load Average is: " . $results['loadavg']['five'];
15
 
16
//Done
17
?>

That's it. Relatively simple if you have a nice class to use. In order to get the uniq_id for a server, you would use the Server/list method.

Of course you can do this either via a web based application, or a CLI ran PHP script, for example, that might belong in a cron job (for checking load every so often for example).

Hopefully this gives a good starting point for working with the API. If you have questions, just leave a comment!

Updated as of April 2012: The class has been updated to utilize cURL and JSON

   
FireStats icon Powered by FireStats
Optimization WordPress Plugins & Solutions by W3 EDGE