Created
April 14, 2012 14:18
-
-
Save stoermerjp/2384698 to your computer and use it in GitHub Desktop.
DroneMapper.com: Amazon EC2 PHP Integration (start/stop/execute remote commands on instance)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/* | |
* DroneMapper.com | |
* Quick EC2 Integration Script | |
* 2012-04-14 | |
* | |
* This script works from the command line and allows you to spin up an AWS EC2 | |
* instance that you have already created via the AWS console. | |
* | |
* You'll need to download the AWS PHP SDK and add your AWS credentials. API/KEY | |
* vim: set ts=4 | |
* | |
*/ | |
if (empty($argv[1])) { | |
print ("DroneMapper.com EC2 Integration.\n\n"); | |
print ("Usage: $argv[0] <arg1> <arg2> ...\n"); | |
print ("./EC2 [start/stop] [arg2] [arg3] [arg4]\n"); | |
print ("Example: ./EC2 start foo1 foo2 foo3\n"); | |
print ("\n"); | |
exit(); | |
} | |
require_once 'sdk-1.5.3/sdk.class.php'; | |
error_reporting(1); | |
/* | |
* Required to allow the instance to spin up before executing commands. | |
*/ | |
$sleeptime = '120'; | |
/* | |
* Change this to an instance you have already created and setup via AWS console or | |
* AWS command line tools. | |
*/ | |
$InstanceID = 'myinstance-id'; | |
print "Instance: ".$InstanceID."\n"; | |
print "Command: ".$argv[1]."\n"; | |
/* | |
* Example arguments from command line, used below for ssh/etc to new instance. | |
*/ | |
$myarg2 = $argv[2]; | |
print "My ARG2: ".$myarg2."\n"; | |
$myarg3 = $argv[3]; | |
print "My ARG3: ".$myarg3."\n"; | |
$myarg4 = $argv[4]; | |
print "My ARG4: ".$myarg4."\n"; | |
header("Content-type: text/html; charset=utf-8"); | |
$ec2 = new AmazonEC2(); | |
/* | |
* If "start" is found on command line args, fire off the instance. | |
*/ | |
if($argv[1] == "start") { | |
$response = $ec2->start_instances($InstanceID); | |
if ($response->isOK()) { | |
$start_time = date("Y-m-d H:i:s"); | |
print "Start Time: ".$start_time."\n"; | |
print ("Success: Starting EC2 Instance: ".$InstanceID."\n"); | |
sleep($sleeptime); | |
/* | |
* Get $response, find new instance hostname. | |
*/ | |
$response = $ec2->describe_instances(array( | |
'Filter' => array( | |
array('Name' => 'instance-id', 'Value' => "$InstanceID"), | |
) | |
)); | |
if (!($response->isOK())) { | |
print "Error: Getting ".$InstanceID." Hostname!"; | |
return; | |
} | |
$hostname = $response->body->reservationSet->item->instancesSet->item->dnsName; | |
print "EC2 Instance Hostname: ".$hostname."\n"; | |
/* | |
* Do stuff. | |
*/ | |
print "----------- Example Commands -------------\n"; | |
shell_exec("/bin/mount -t nfs ".$hostname.":/mymount /mylocalmount"); | |
$ssh_cmd = shell_exec("/usr/bin/ssh myuser@".$hostname." -o TCPKeepAlive=yes -o ServerAliveInterval=60 -o StrictHostKeyChecking=no -i /myuser/myuser_key.pem '/usr/bin/php /myuser/myscript.php ".$myarg2." ".$myarg3." ".$myarg4."' "); | |
print $ssh_cmd2; | |
print "------------------------------------------\n"; | |
$testfile = "/mylocalmount/OrthoN-DroneMapper_20.tif"; | |
sleep(20); | |
if(file_exists($testfile)) { | |
$response = $ec2->stop_instances($InstanceID); | |
$print ("Success: Local file ".$testfile." is here. Stopped Instance.\n"); | |
} else { | |
$print ("Error: Local file ".$testfile." isn't here. Instance still running.\n"); | |
} | |
} else { | |
print ("Error: Starting EC2 Instance: ".$InstanceID."\n"); | |
var_dump($response); | |
exit(); | |
} | |
} | |
/* | |
* Process 'stop' from command line. | |
*/ | |
if($argv[1] == "stop") { | |
$response = $ec2->stop_instances($InstanceID); | |
if ($response->isOK()) { | |
print ("Success: Stopping EC2 Instance: ".$InstanceID."\n"); | |
} else { | |
print ("Error: Stopping EC2 Instance: ".$InstanceID."\n"); | |
var_dump($response); | |
exit(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment