forked from tylerhall/php-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.ec2.php
168 lines (145 loc) · 4.87 KB
/
class.ec2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?PHP
class EC2
{
var $_key = "";
var $_secret = "";
var $_server = "http://ec2.amazonaws.com";
var $_pathToCurl = "";
var $_date = null;
var $_error = null;
function EC2($key = null, $secret = null)
{
if($key && $secret)
{
$this->_key = $key;
$this->_secret = $secret;
}
// If the path to curl isn't set, try and auto-detect it
if($this->_pathToCurl == "")
{
$path = trim(shell_exec("which curl"), "\n ");
if(is_executable($path))
$this->_pathToCurl = $path;
else
{
$this->_error = "Couldn't auto-detect path to curl";
return false;
}
}
return true;
}
function getImages($ownerId = null)
{
$params = array("Action" => "DescribeImages");
if(isset($ownerId)) $params['Owner.1'] = $ownerId;
$xmlstr = $this->sendRequest($params);
$xml = new SimpleXMLElement($xmlstr);
$images = array();
foreach($xml->imagesSet->item as $item)
$images[(string) $item->imageId] =
array("location" => (string) $item->imageLocation,
"state" => (string) $item->imageState,
"owner" => (string) $item->imageOwnerId,
"public" => (string) $item->isPublic);
return $images;
}
function getInstances()
{
$params = array("Action" => "DescribeInstances");
$xmlstr = $this->sendRequest($params);
$xml = new SimpleXMLElement($xmlstr);
$instances = array();
foreach($xml->reservationSet->item as $item)
$instances[(string) $item->instancesSet->item->instanceId] =
array("imageId" => (string) $item->instancesSet->item->imageId,
"state" => (string) $item->instancesSet->item->instanceState->name,
"dns" => (string) $item->instancesSet->item->dnsName);
return $instances;
}
function runInstances($imageId, $min = 1, $max = 1, $keyName = "gsg-keypair")
{
$params = array("Action" => "RunInstances",
"ImageId" => $imageId,
"MinCount" => $min,
"MaxCount" => $max,
"KeyName" => $keyName);
$xmlstr = $this->sendRequest($params);
$xml = new SimpleXMLElement($xmlstr);
$instances = array();
foreach($xml->instancesSet->item as $item)
$instances[(string) $item->instanceId] =
array("imageId" => (string) $item->imageId,
"state" => (string) $item->instanceState->name,
"dns" => (string) $item->dnsName);
return $instances;
}
function getKeys()
{
$params = array("Action" => "DescribeKeyPairs");
$xmlstr = $this->sendRequest($params);
$xml = new SimpleXMLElement($xmlstr);
$keys = array();
foreach($xml->keySet->item as $item)
$keys[] = array("name" => (string) $item->keyName, "fingerprint" => (string) $item->keyFingerprint);
return $keys;
}
function terminateInstances($toKill)
{
$params = array("Action" => "TerminateInstances");
$toKill = explode(",", $toKill);
$i = 0;
foreach($toKill as $id)
$params['InstanceId.' . ++$i] = $id;
$xmlstr = $this->sendRequest($params);
$xml = new SimpleXMLElement($xmlstr);
$instances = array();
foreach($xml->instancesSet->item as $item)
$instances[(string) $item->instanceId] =
array("shutdownState" => (string) $item->shutdownState,
"previousState" => (string) $item->previousState);
return $instances;
}
function sendRequest($params)
{
$params['AWSAccessKeyId'] = $this->_key;
$params['SignatureVersion'] = 1;
$params['Timestamp'] = gmdate("Y-m-d\TH:i:s\Z");
$params['Version'] = "2006-10-01";
uksort($params, "strnatcasecmp");
$toSign = "";
foreach($params as $key => $val)
$toSign .= $key . $val;
$sha1 = $this->hasher($toSign);
$sig = $this->base64($sha1);
$params['Signature'] = $sig;
$uri = $this->_server . '/?';
foreach($params as $key => $val)
$uri .= "$key=" . urlencode($val) . "&";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function hasher($data)
{
// Algorithm adapted (stolen) from http://pear.php.net/package/Crypt_HMAC/)
$key = $this->_secret;
if(strlen($key) > 64)
$key = pack("H40", sha1($key));
if(strlen($key) < 64)
$key = str_pad($key, 64, chr(0));
$ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
$opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
return sha1($opad . pack("H40", sha1($ipad . $data)));
}
function base64($str)
{
$ret = "";
for($i = 0; $i < strlen($str); $i += 2)
$ret .= chr(hexdec(substr($str, $i, 2)));
return base64_encode($ret);
}
}
?>