forked from ssrini/scalr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cloudstack.php
404 lines (337 loc) · 13.6 KB
/
Cloudstack.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
class Modules_Platforms_Cloudstack implements IPlatformModule
{
private $db;
/** Properties **/
const API_KEY = 'cloudstack.api_key';
const SECRET_KEY = 'cloudstack.secret_key';
const API_URL = 'cloudstack.api_url';
const ACCOUNT_NAME = 'cloudstack.account_name';
const DOMAIN_NAME = 'cloudtsack.domain_name';
const DOMAIN_ID = 'cloudtsack.domain_id';
const SHARED_IP = 'cloudstack.shared_ip';
const SHARED_IP_ID = 'cloudstack.shared_ip_id';
const SHARED_IP_INFO = 'cloudstack.shared_ip_info';
const SZR_PORT_COUNTER = 'cloudstack.szr_port_counter';
/**
*
* @var AmazonRDS
*/
private $instancesListCache;
public function __construct()
{
$this->db = Core::GetDBInstance();
}
/**
*
* @param unknown_type $environment
* @param unknown_type $region
* @return Scalr_Service_Cloud_Nimbula_Client
*/
private function getCloudStackClient($environment, $cloudLoction=null)
{
return Scalr_Service_Cloud_Cloudstack::newCloudstack(
$environment->getPlatformConfigValue(self::API_URL),
$environment->getPlatformConfigValue(self::API_KEY),
$environment->getPlatformConfigValue(self::SECRET_KEY)
);
}
public function getRoleBuilderBaseImages() {}
public function getLocations() {
try {
$environment = Scalr_UI_Request::getInstance()->getEnvironment();
}
catch(Exception $e) {
return array();
}
if (!$environment || !$environment->isPlatformEnabled(SERVER_PLATFORMS::CLOUDSTACK))
return array();
try {
$cs = Scalr_Service_Cloud_Cloudstack::newCloudstack(
$environment->getPlatformConfigValue(self::API_URL),
$environment->getPlatformConfigValue(self::API_KEY),
$environment->getPlatformConfigValue(self::SECRET_KEY)
);
foreach ($cs->listZones() as $zone)
$retval[$zone->name] = "Cloudstack / {$zone->name}";
} catch (Exception $e) {
return array();
}
return $retval;
}
public function getPropsList()
{
return array(
self::API_URL => 'API URL',
self::API_KEY => 'API Key',
self::SECRET_KEY => 'Secret Key'
);
}
public function GetServerCloudLocation(DBServer $DBServer)
{
return $DBServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION);
}
public function GetServerID(DBServer $DBServer)
{
return $DBServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID);
}
public function GetServerFlavor(DBServer $DBServer)
{
return NULL;
}
public function IsServerExists(DBServer $DBServer)
{
return in_array(
$DBServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID),
array_keys($this->GetServersList($DBServer->GetEnvironmentObject(), $this->GetServerCloudLocation($DBServer)))
);
}
public function GetServerIPAddresses(DBServer $DBServer)
{
//
}
private function GetServersList(Scalr_Environment $environment, $region, $skipCache = false)
{
if (!$region)
return array();
if (!$this->instancesListCache[$environment->id][$region] || $skipCache) {
$cs = $this->getCloudStackClient($environment, $region);
try {
$results = $cs->listVirtualMachines(null, $region);
}
catch(Exception $e) {
throw new Exception(sprintf("Cannot get list of servers for platfrom ec2: %s", $e->getMessage()));
}
if (count($results) > 0) {
foreach ($results as $item)
$this->instancesListCache[$environment->id][$region][$item->id] = $item->state;
}
}
return $this->instancesListCache[$environment->id][$region];
}
public function GetServerRealStatus(DBServer $DBServer)
{
$region = $this->GetServerCloudLocation($DBServer);
$iid = $DBServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID);
if (!$iid || !$region) {
$status = 'not-found';
}
elseif (!$this->instancesListCache[$DBServer->GetEnvironmentObject()->id][$region][$iid]) {
$cs = $this->getCloudStackClient($DBServer->GetEnvironmentObject(), $region);
try {
$iinfo = $cs->listVirtualMachines($iid);
$iinfo = (is_array($iinfo)) ? $iinfo[0] : false;
if ($iinfo)
$status = $iinfo->state;
else
$status = 'not-found';
}
catch(Exception $e) {
if (stristr($e->getMessage(), "Not Found"))
$status = 'not-found';
}
}
else {
$status = $this->instancesListCache[$DBServer->GetEnvironmentObject()->id][$region][$DBServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID)];
}
return Modules_Platforms_Cloudstack_Adapters_Status::load($status);
}
public function TerminateServer(DBServer $DBServer)
{
$cs = $this->getCloudStackClient($DBServer->GetEnvironmentObject(), $this->GetServerCloudLocation($DBServer));
$cs->destroyVirtualMachine($DBServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID));
return true;
}
public function RebootServer(DBServer $DBServer)
{
$cs = $this->getCloudStackClient($DBServer->GetEnvironmentObject(), $this->GetServerCloudLocation($DBServer));
$cs->rebootVirtualMachine($DBServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID));
return true;
}
public function RemoveServerSnapshot(DBRole $DBRole)
{
//TODO:
}
public function CheckServerSnapshotStatus(BundleTask $BundleTask)
{
//TODO:
}
public function CreateServerSnapshot(BundleTask $BundleTask)
{
$DBServer = DBServer::LoadByID($BundleTask->serverId);
$BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS;
$BundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::CSTACK_DEF;
$msg = new Scalr_Messaging_Msg_Rebundle(
$BundleTask->id,
$BundleTask->roleName,
array()
);
if (!$DBServer->SendMessage($msg)) {
$BundleTask->SnapshotCreationFailed("Cannot send rebundle message to server. Please check event log for more details.");
return;
}
else {
$BundleTask->Log(sprintf(_("Snapshot creating initialized (MessageID: %s). Bundle task status changed to: %s"),
$msg->messageId, $BundleTask->status
));
}
$BundleTask->setDate('started');
$BundleTask->Save();
}
public function GetServerConsoleOutput(DBServer $DBServer)
{
//NOT SUPPORTED
}
public function GetServerExtendedInformation(DBServer $DBServer)
{
$cs = $this->getCloudStackClient($DBServer->GetEnvironmentObject(), $this->GetServerCloudLocation($DBServer));
try {
$iinfo = $cs->listVirtualMachines($DBServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID));
$iinfo = (is_array($iinfo)) ? $iinfo[0] : null;
} catch (Exception $e) {}
if ($iinfo->id && $iinfo->id == $DBServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID))
{
return array(
'ID' => $iinfo->id,
'Name' => $iinfo->name,
'State' => $iinfo->state,
'Group' => $iinfo->group,
'Zone' => $iinfo->zonename,
'Template name' => $iinfo->templatename,
'Offering name' => $iinfo->serviceofferingname,
'Root device type' => $iinfo->rootdevicetype,
'Internal IP' => $iinfo->nic[0]->ipaddress,
'Hypervisor' => $iinfo->hypervisor
);
}
return false;
}
public function LaunchServer(DBServer $DBServer, Scalr_Server_LaunchOptions $launchOptions = null)
{
$environment = $DBServer->GetEnvironmentObject();
$farmRole = $DBServer->GetFarmRoleObject();
if (!$launchOptions)
{
$launchOptions = new Scalr_Server_LaunchOptions();
$dbRole = DBRole::loadById($DBServer->roleId);
$launchOptions->imageId = $dbRole->getImageId(SERVER_PLATFORMS::CLOUDSTACK, $DBServer->GetFarmRoleObject()->CloudLocation);
$launchOptions->serverType = $DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_CLOUDSTACK_SERVICE_OFFERING_ID);
$launchOptions->cloudLocation = $DBServer->GetFarmRoleObject()->CloudLocation;
/*
* User Data
*/
foreach ($DBServer->GetCloudUserData() as $k=>$v)
$u_data .= "{$k}={$v};";
$launchOptions->userData = trim($u_data, ";");
$launchOptions->architecture = 'x86_64';
}
$cs = $this->getCloudStackClient(
$environment,
$launchOptions->cloudLocation
);
$networkType = $farmRole->GetSetting(DBFarmRole::SETTING_CLOUDSTACK_NETWORK_TYPE);
if ($networkType == 'Virtual') {
$sharedIpId = $environment->getPlatformConfigValue(self::SHARED_IP_ID.".{$launchOptions->cloudLocation}", false);
if (!$sharedIpId)
{
$ipResult = $cs->associateIpAddress($launchOptions->cloudLocation);
$ipId = $ipResult->id;
if ($ipId) {
while (true) {
$ipInfo = $cs->listPublicIpAddresses($ipId);
$ipInfo = $ipInfo->publicipaddress[0];
if (!$ipInfo)
throw new Exception("Cannot allocate IP address: listPublicIpAddresses -> failed");
if ($ipInfo->state == 'Allocated') {
$environment->setPlatformConfig(array(self::SHARED_IP_ID.".{$launchOptions->cloudLocation}" => $ipId), false);
$environment->setPlatformConfig(array(self::SHARED_IP.".{$launchOptions->cloudLocation}" => $ipInfo->ipaddress), false);
$environment->setPlatformConfig(array(self::SHARED_IP_INFO.".{$launchOptions->cloudLocation}" => serialize($ipInfo)), false);
$sharedIpId = $ipId;
break;
} else if ($ipInfo->state == 'Allocating') {
sleep(1);
} else {
throw new Exception("Cannot allocate IP address: ipAddress->state = {$ipInfo->state}");
}
}
}
else
throw new Exception("Cannot allocate IP address: associateIpAddress -> failed");
}
}
$sshKey = Scalr_SshKey::init();
try {
if (!$sshKey->loadGlobalByFarmId($DBFarm->ID, $launchOptions->cloudLocation))
{
$key_name = "FARM-{$DBServer->farmId}";
$result = $cs->createSSHKeyPair($key_name);
if ($result->keypair->privatekey)
{
$sshKey->farmId = $DBServer->farmId;
$sshKey->clientId = $DBServer->clientId;
$sshKey->envId = $DBServer->envId;
$sshKey->type = Scalr_SshKey::TYPE_GLOBAL;
$sshKey->cloudLocation = $launchOptions->cloudLocation;
$sshKey->cloudKeyName = $key_name;
$sshKey->platform = SERVER_PLATFORMS::CLOUDSTACK;
$sshKey->setPrivate($result->keypair->privatekey);
$sshKey->setPublic($sshKey->generatePublicKey());
$sshKey->save();
}
}
} catch (Exception $e) { }
$keyName = $sshKey->cloudKeyName;
$vResult = $cs->deployVirtualMachine(
$launchOptions->serverType,
$launchOptions->imageId,
$launchOptions->cloudLocation,
null, //account
null, // diskoffering
"", //displayName
null, //domainId
$farmRole->GetRoleObject()->name,
null, //hostId
null, //hypervisor
$keyName,
"", //name
$farmRole->GetSetting(DBFarmRole::SETTING_CLOUDSTACK_NETWORK_ID),
null, //securityGroupIds
null, //SecGroupNames
null, //size
base64_encode($launchOptions->userData)
);
if ($vResult->id) {
$DBServer->SetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID, $vResult->id);
$DBServer->SetProperty(CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION, $launchOptions->cloudLocation);
$DBServer->SetProperty(CLOUDSTACK_SERVER_PROPERTIES::LAUNCH_JOB_ID, $vResult->jobid);
return $DBServer;
} else
throw new Exception(sprintf("Cannot launch new instance: %s", $vResult->errortext));
}
public function PutAccessData(DBServer $DBServer, Scalr_Messaging_Msg $message)
{
$put = false;
$put |= $message instanceof Scalr_Messaging_Msg_Rebundle;
$put |= $message instanceof Scalr_Messaging_Msg_BeforeHostUp;
$put |= $message instanceof Scalr_Messaging_Msg_HostInitResponse;
$put |= $message instanceof Scalr_Messaging_Msg_Mysql_PromoteToMaster;
$put |= $message instanceof Scalr_Messaging_Msg_Mysql_NewMasterUp;
$put |= $message instanceof Scalr_Messaging_Msg_Mysql_CreateDataBundle;
$put |= $message instanceof Scalr_Messaging_Msg_Mysql_CreateBackup;
$put |= $message instanceof Scalr_Messaging_Msg_DbMsr_PromoteToMaster;
$put |= $message instanceof Scalr_Messaging_Msg_DbMsr_CreateDataBundle;
$put |= $message instanceof Scalr_Messaging_Msg_DbMsr_CreateBackup;
if ($put) {
$environment = $DBServer->GetEnvironmentObject();
$accessData = new stdClass();
$accessData->apiKey = $environment->getPlatformConfigValue(self::API_KEY);
$accessData->secretKey = $environment->getPlatformConfigValue(self::SECRET_KEY);
$accessData->apiUrl = $environment->getPlatformConfigValue(self::API_URL);
$message->platformAccessData = $accessData;
}
}
public function ClearCache()
{
$this->instancesListCache = array();
}
}