public function deleteAction() { $this->request->defineParams(array('sshKeyId' => array('type' => 'int'))); $sshKey = Scalr_SshKey::init()->loadById($this->getParam('sshKeyId')); $this->user->getPermissions()->validate($sshKey); if ($sshKey->type == Scalr_SshKey::TYPE_GLOBAL) { if ($sshKey->platform == SERVER_PLATFORMS::EC2) { $aws = $this->getEnvironment()->aws($sshKey->cloudLocation); $aws->ec2->keyPair->delete($sshKey->cloudKeyName); $sshKey->delete(); $this->response->success(); } elseif (PlatformFactory::isOpenstack($sshKey->platform)) { $os = $this->getEnvironment()->openstack($sshKey->platform, $sshKey->cloudLocation); try { $os->servers->keypairs->delete($sshKey->cloudKeyName); } catch (Exception $e) { } $sshKey->delete(); $this->response->success(); } else { $sshKey->delete(); } } else { //TODO: } $this->response->success("SSH key successfully removed"); }
/** * * @param string $platform (From SERVER_PLATFORMS class) * @param integer $farmid * @param integer $farm_roleid * @param integer $index * @return void */ public function __construct($platform, DBFarmRole $DBFarmRole = null, $index = null, $role_id = null) { $this->platform = $platform; $this->dbFarmRole = $DBFarmRole; $this->index = $index; $this->roleId = $role_id === null ? $this->dbFarmRole->RoleID : $role_id; if ($DBFarmRole) { $this->envId = $DBFarmRole->GetFarmObject()->EnvID; } //Refletcion $Reflect = new ReflectionClass(DBServer::$platformPropsClasses[$this->platform]); foreach ($Reflect->getConstants() as $k => $v) { $this->platformProps[] = $v; } if ($DBFarmRole) { if (PlatformFactory::isOpenstack($this->platform)) { $this->SetProperties(array(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => $DBFarmRole->CloudLocation)); } elseif (PlatformFactory::isCloudstack($this->platform)) { $this->SetProperties(array(CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => $DBFarmRole->CloudLocation)); } else { switch ($this->platform) { case SERVER_PLATFORMS::NIMBULA: break; case SERVER_PLATFORMS::EUCALYPTUS: $this->SetProperties(array(EUCA_SERVER_PROPERTIES::REGION => $DBFarmRole->CloudLocation)); break; case SERVER_PLATFORMS::RACKSPACE: $this->SetProperties(array(RACKSPACE_SERVER_PROPERTIES::DATACENTER => $DBFarmRole->CloudLocation)); break; case SERVER_PLATFORMS::GCE: $this->SetProperties(array(GCE_SERVER_PROPERTIES::CLOUD_LOCATION => $DBFarmRole->CloudLocation)); break; case SERVER_PLATFORMS::EC2: $this->SetProperties(array(EC2_SERVER_PROPERTIES::REGION => $DBFarmRole->CloudLocation)); break; } } //TODO: $this->SetProperties(array(SERVER_PROPERTIES::SZR_VESION => '0.20.0')); } else { $this->SetProperties(array(SERVER_PROPERTIES::SZR_VESION => '0.20.0')); } }
public static function onCreateBackupResult(Scalr_Messaging_Msg $message, DBServer $dbServer) { $dbFarmRole = $dbServer->GetFarmRoleObject(); $dbFarmRole->SetSetting(Scalr_Db_Msr::DATA_BACKUP_LAST_TS, time(), DBFarmRole::TYPE_LCL); $dbFarmRole->SetSetting(Scalr_Db_Msr::DATA_BACKUP_IS_RUNNING, 0, DBFarmRole::TYPE_LCL); //$dbFarmRole->SetSetting(Scalr_Db_Msr::DATA_BACKUP_SERVER_ID, ""); if (PlatformFactory::isOpenstack($dbServer->platform)) { $provider = 'cf'; } else { switch ($dbServer->platform) { case SERVER_PLATFORMS::EC2: $provider = 's3'; break; case SERVER_PLATFORMS::GCE: $provider = 'gcs'; break; default: $provider = 'unknown'; break; } } $backup = Scalr_Db_Backup::init(); $backup->service = $message->dbType; $backup->platform = $dbServer->platform; $backup->provider = $provider; $backup->envId = $dbServer->envId; $backup->farmId = $dbServer->farmId; $backup->cloudLocation = $dbServer->GetCloudLocation(); $backup->status = Scalr_Db_Backup::STATUS_AVAILABLE; $total = 0; if (isset($message->backupParts) && is_array($message->backupParts)) { foreach ($message->backupParts as $item) { if (is_object($item) && $item->size) { $backup->addPart(str_replace(array("s3://", "cf://", "gcs://", "swift://"), array("", "", "", ""), $item->path), $item->size); $total = $total + (int) $item->size; } else { $backup->addPart(str_replace(array("s3://", "cf://", "gcs://", "swift://"), array("", "", "", ""), $item), 0); } } } $backup->size = $total; $backup->save(); }
public function xGetWindowsPasswordAction() { $this->request->restrictAccess(Acl::RESOURCE_SECURITY_RETRIEVE_WINDOWS_PASSWORDS); $this->request->defineParams(array('serverId')); $dbServer = DBServer::LoadByID($this->getParam('serverId')); $this->user->getPermissions()->validate($dbServer); if ($dbServer->platform == SERVER_PLATFORMS::EC2) { $env = Scalr_Environment::init()->loadById($dbServer->envId); $ec2 = $env->aws($dbServer->GetCloudLocation())->ec2; $encPassword = $ec2->instance->getPasswordData($dbServer->GetCloudServerID()); $privateKey = Scalr_SshKey::init()->loadGlobalByFarmId($dbServer->farmId, $dbServer->GetCloudLocation(), $dbServer->platform); $password = Scalr_Util_CryptoTool::opensslDecrypt(base64_decode($encPassword->passwordData), $privateKey->getPrivate()); } elseif (PlatformFactory::isOpenstack($dbServer->platform)) { $env = Scalr_Environment::init()->loadById($dbServer->envId); $os = $env->openstack($dbServer->platform, $dbServer->GetCloudLocation()); //TODO: } else { throw new Exception("Requested operation supported only by EC2"); } $this->response->data(array('password' => $password)); }
public function xGetCloudServersListAction() { $this->request->defineParams(array('platform', 'cloudLocation')); if (!$this->environment->isPlatformEnabled($this->getParam('platform'))) { throw new Exception(sprintf('Cloud %s is not enabled for current environment', $this->getParam('platform'))); } $results = array(); $platform = PlatformFactory::NewPlatform($this->getParam('platform')); //TODO: Added support for GCE if ($this->getParam('platform') == SERVER_PLATFORMS::GCE) { $gce = $platform->getClient($this->environment, $this->getParam('cloudLocation')); $result = $gce->instances->listInstances($this->environment->getPlatformConfigValue(Modules_Platforms_GoogleCE::PROJECT_ID), $this->getParam('cloudLocation'), array()); if (is_array($result->items)) { foreach ($result->items as $server) { if ($server->status != 'RUNNING') { continue; } $ips = $platform->determineServerIps($gce, $server); $itm = array('id' => $server->name, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $this->getParam('cloudLocation'), 'isImporting' => false, 'isManaged' => false); //Check is instance already importing try { $dbServer = DBServer::LoadByPropertyValue(GCE_SERVER_PROPERTIES::SERVER_ID, $server->id); if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) { if ($dbServer->status == SERVER_STATUS::IMPORTING) { $itm['isImporting'] = true; } else { $itm['isManaged'] = true; } $itm['serverId'] = $dbServer->serverId; } } catch (Exception $e) { } $results[] = $itm; } } } elseif (PlatformFactory::isOpenstack($this->getParam('platform'))) { $client = $this->environment->openstack($this->getParam('platform'), $this->getParam('cloudLocation')); $r = $client->servers->list(true); foreach ($r as $server) { if ($server->status != 'ACTIVE') { continue; } $ips = $platform->determineServerIps($client, $server); $itm = array('id' => $server->id, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $this->getParam('cloudLocation'), 'isImporting' => false, 'isManaged' => false); //Check is instance already importing try { $dbServer = DBServer::LoadByPropertyValue(OPENSTACK_SERVER_PROPERTIES::SERVER_ID, $server->id); if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) { if ($dbServer->status == SERVER_STATUS::IMPORTING) { $itm['isImporting'] = true; } else { $itm['isManaged'] = true; } $itm['serverId'] = $dbServer->serverId; } } catch (Exception $e) { } $results[] = $itm; } } elseif (PlatformFactory::isCloudstack($this->getParam('platform'))) { $client = Scalr_Service_Cloud_Cloudstack::newCloudstack($platform->getConfigVariable(Modules_Platforms_Cloudstack::API_URL, $this->environment), $platform->getConfigVariable(Modules_Platforms_Cloudstack::API_KEY, $this->environment), $platform->getConfigVariable(Modules_Platforms_Cloudstack::SECRET_KEY, $this->environment), $this->getParam('platform')); $platform = PlatformFactory::NewPlatform($this->getParam('platform')); $r = $client->listVirtualMachines(null, $this->getParam('cloudLocation')); foreach ($r as $server) { $ips = $platform->determineServerIps($client, $server); $itm = array('id' => $server->id, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $this->getParam('cloudLocation'), 'isImporting' => false, 'isManaged' => false); //Check is instance already importing try { $dbServer = DBServer::LoadByPropertyValue(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID, $server->id); if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) { if ($dbServer->status == SERVER_STATUS::IMPORTING) { $itm['isImporting'] = true; } else { $itm['isManaged'] = true; } $itm['serverId'] = $dbServer->serverId; } } catch (Exception $e) { } $results[] = $itm; } } elseif ($this->getParam('platform') == SERVER_PLATFORMS::EC2) { $client = $this->environment->aws($this->getParam('cloudLocation'))->ec2; $r = $client->instance->describe(); if (count($r)) { foreach ($r as $reservation) { /* @var $reservation Scalr\Service\Aws\Ec2\DataType\ReservationData */ foreach ($reservation->instancesSet as $instance) { /* @var $instance Scalr\Service\Aws\Ec2\DataType\InstanceData */ if ($instance->instanceState->name != 'running') { continue; } $itm = array('id' => $instance->instanceId, 'localIp' => $instance->privateIpAddress, 'publicIp' => $instance->ipAddress, 'zone' => $instance->placement->availabilityZone, 'isImporting' => false, 'isManaged' => false); //Check is instance already importing try { $dbServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $instance->instanceId); if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) { if ($dbServer->status == SERVER_STATUS::IMPORTING) { $itm['isImporting'] = true; } else { $itm['isManaged'] = true; } $itm['serverId'] = $dbServer->serverId; } } catch (Exception $e) { } $results[] = $itm; } } } } $this->response->data(array('data' => $results)); }
public function GetCloudUserData() { $dbFarmRole = $this->GetFarmRoleObject(); $baseurl = \Scalr::config('scalr.endpoint.scheme') . "://" . \Scalr::config('scalr.endpoint.host'); $retval = array("farmid" => $this->farmId, "role" => implode(",", $dbFarmRole->GetRoleObject()->getBehaviors()), "httpproto" => \Scalr::config('scalr.endpoint.scheme'), "region" => $this->GetCloudLocation(), "hash" => $this->GetFarmObject()->Hash, "realrolename" => $dbFarmRole->GetRoleObject()->name, "szr_key" => $this->GetKey(), "serverid" => $this->serverId, 'p2p_producer_endpoint' => $baseurl . "/messaging", 'queryenv_url' => $baseurl . "/query-env", 'behaviors' => implode(",", $dbFarmRole->GetRoleObject()->getBehaviors()), 'farm_roleid' => $dbFarmRole->ID, 'roleid' => $dbFarmRole->RoleID, 'env_id' => $dbFarmRole->GetFarmObject()->EnvID, 'platform' => $dbFarmRole->Platform, 'server_index' => $this->index, 'cloud_server_id' => $this->GetCloudServerID(), "owner_email" => $dbFarmRole->GetFarmObject()->createdByUserEmail); //FIXME remove environment //if ($this->envId == 3414) { $retval['message_format'] = 'json'; //} if (PlatformFactory::isOpenstack($this->platform)) { $retval["cloud_storage_path"] = "swift://"; } else { switch ($this->platform) { case SERVER_PLATFORMS::EC2: $retval["s3bucket"] = $dbFarmRole->GetSetting(DBFarmRole::SETTING_AWS_S3_BUCKET); $retval["cloud_storage_path"] = "s3://" . $dbFarmRole->GetSetting(DBFarmRole::SETTING_AWS_S3_BUCKET); break; case SERVER_PLATFORMS::RACKSPACE: $retval["cloud_storage_path"] = "cf://scalr-{$this->GetFarmObject()->Hash}"; break; case SERVER_PLATFORMS::GCE: $retval["cloud_storage_path"] = "gcs://"; break; } } // Custom settings foreach ($dbFarmRole->GetSettingsByFilter("user-data") as $k => $v) { $retval[str_replace("user-data", "custom", $k)] = $v; } return $retval; }
public function isOpenstack() { return PlatformFactory::isOpenstack($this->Platform); }