Checks wheter specified cloud is CloudStack based
public static isCloudstack ( string $platform ) : boolean | ||
$platform | string | A platform name |
리턴 | boolean | Returns true if specified cloud is CloudStack based or false otherwise |
/** * Constructor * * @param string $platform Platform * @param DBFarmRole $DBFarmRole optional Farm Role object * @param int $index optional Server index within the Farm Role scope * @param string $role_id optional Identifier of the Role */ 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::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; } } } $this->SetProperties(array(SERVER_PROPERTIES::SZR_VESION => '0.20.0')); }
/** * Gets a new Instance of the adapter * * @param string|CloudCredentials|object $name The name of the adapter, or CloudCredentials entity, or cloud credentials data * * @return ApiEntityAdapter Returns the instance of cloud credentials adapter * * @throws ApiErrorException */ public function adapter($name, array $transform = null) { if (is_object($name)) { // $property = $name instanceof $this->entityClass ? static::$entityDescriminator : static::$objectDiscriminator; $value = empty($transform) ? $name->{$property} : $transform[$name->{$property}]; switch (true) { case PlatformFactory::isOpenstack($value, true): $value = SERVER_PLATFORMS::OPENSTACK; break; case PlatformFactory::isCloudstack($value): $value = SERVER_PLATFORMS::CLOUDSTACK; break; case PlatformFactory::isRackspace($value): $value = SERVER_PLATFORMS::RACKSPACE; break; } if (!isset(static::$inheritanceMap[$value])) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Unknown cloud '{$value}'"); } $class = empty(static::$inheritanceMap) ? $value : static::$inheritanceMap[$value]; $name = empty(static::$inheritedNamespace) ? $class : static::$inheritedNamespace . "\\{$class}"; } return parent::adapter($name); }
/** * Decode params from href depending on type of OS * * Possible formats (branch is optioanl): * for windows: /{repo}/[{branch}/]install_scalarizr.ps1 * for linux: /{repo}/{platform}/[{branch}]/install_scalarizr.sh * * @param array $params * @param bool $isLinux * @return array */ protected function parseInstallScriptArgs($params, $isLinux = true) { $result = ['repo' => '', 'platform' => '', 'repoUrls' => []]; $repo = array_shift($params); $platform = $isLinux ? array_shift($params) : ''; array_pop($params); // pop script name from the end of href $branch = implode('/', $params); $repos = $this->getContainer()->config('scalr.scalarizr_update.' . ($branch ? 'devel_repos' : 'repos')); if (in_array($repo, array_keys($repos))) { if ($branch) { // strip illegal chars $branch = preg_replace('/[^A-Za-z\\/0-9_.-]/', '', $branch); $branch = str_replace(array(".", '/'), array('', '-'), $branch); } $repoUrls = $repos[$repo]; if ($branch) { foreach ($repoUrls as $key => &$url) { $url = sprintf($url, $branch); } } if ($isLinux) { if (in_array($platform, $this->getContainer()->config('scalr.allowed_clouds'))) { if (PlatformFactory::isOpenstack($platform)) { $platform = SERVER_PLATFORMS::OPENSTACK; } else { if (PlatformFactory::isCloudstack($platform)) { $platform = SERVER_PLATFORMS::CLOUDSTACK; } } $result['platform'] = $platform; $result['repo'] = $repo; $result['repoUrls'] = $repoUrls; } } else { $result['repo'] = $repo; $result['repoUrls'] = $repoUrls; } } return $result; }
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(GoogleCEPlatformModule::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_NAME, $server->name); 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); do { 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; } } while (false !== ($r = $r->getNextPage())); } elseif (PlatformFactory::isCloudstack($this->getParam('platform'))) { $client = $this->environment->cloudstack($this->getParam('platform')); $platform = PlatformFactory::NewPlatform($this->getParam('platform')); $r = $client->instance->describe(array('zoneid' => $this->getParam('cloudLocation'))); if (count($r) > 0) { 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; $nextToken = null; do { if (isset($r)) { $nextToken = $r->getNextToken(); } $r = $client->instance->describe(null, null, $nextToken); 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; } } } } while ($r->getNextToken()); } elseif ($this->getParam('platform') == SERVER_PLATFORMS::EUCALYPTUS) { $client = $this->environment->eucalyptus($this->getParam('cloudLocation'))->ec2; $r = $client->instance->describe(null, null, $nextToken); 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(EUCA_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 run1($stage) { if ($this->hasTable('images')) { $this->db->Execute('DROP TABLE images'); // drop old table if existed } $this->db->Execute("CREATE TABLE `images` (\n `hash` binary(16) NOT NULL,\n `id` varchar(128) NOT NULL DEFAULT '',\n `env_id` int(11) NULL DEFAULT NULL,\n `bundle_task_id` int(11) NULL DEFAULT NULL,\n `platform` varchar(25) NOT NULL DEFAULT '',\n `cloud_location` varchar(255) NOT NULL DEFAULT '',\n `os_family` varchar(25) NULL DEFAULT NULL,\n `os_version` varchar(10) NULL DEFAULT NULL,\n `os_name` varchar(255) NULL DEFAULT NULL,\n `created_by_id` int(11) NULL DEFAULT NULL,\n `created_by_email` varchar(100) NULL DEFAULT NULL,\n `architecture` enum('i386','x86_64') NOT NULL DEFAULT 'x86_64',\n `is_deprecated` tinyint(1) NOT NULL DEFAULT '0',\n `source` enum('BundleTask','Manual') NOT NULL DEFAULT 'Manual',\n `type` varchar(20) NULL DEFAULT NULL,\n `status` varchar(20) NOT NULL,\n `status_error` varchar(255) NULL DEFAULT NULL,\n `agent_version` varchar(20) NULL DEFAULT NULL,\n PRIMARY KEY (`hash`),\n UNIQUE KEY `idx_id` (`env_id`, `id`, `platform`, `cloud_location`),\n CONSTRAINT `fk_images_client_environmnets_id` FOREIGN KEY (`env_id`) REFERENCES `client_environments` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION\n ) ENGINE=InnoDB DEFAULT CHARSET=latin1;\n "); $allRecords = 0; $excludedCL = 0; $excludedMissing = 0; // convert $tasks = []; foreach ($this->db->GetAll('SELECT id as bundle_task_id, client_id as account_id, env_id, platform, snapshot_id as id, cloud_location, os_family, os_name, os_version, created_by_id, created_by_email, bundle_type as type FROM bundle_tasks WHERE status = ?', [\SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS]) as $t) { if (!is_array($tasks[$t['env_id']])) { $tasks[$t['env_id']] = []; } $allRecords++; $tasks[$t['env_id']][] = $t; } foreach ($this->db->GetAll('SELECT r.client_id as account_id, r.env_id, ri.platform, ri.image_id as id, ri.cloud_location, ri.os_family, ri.os_name, ri.os_version, r.added_by_userid as created_by_id, r.added_by_email as created_by_email, ri.agent_version FROM role_images ri JOIN roles r ON r.id = ri.role_id') as $t) { if (!is_array($tasks[$t['env_id']])) { $tasks[$t['env_id']] = []; } $allRecords++; $tasks[$t['env_id']][] = $t; } foreach ($tasks as $id => $e) { if ($id == 0) { continue; } try { $env = (new \Scalr_Environment())->loadById($id); } catch (\Exception $e) { $this->console->warning('Invalid environment %d: %s', $id, $e->getMessage()); continue; } foreach ($e as $t) { // check if snapshot exists $add = false; if ($this->db->GetOne('SELECT id FROM images WHERE id = ? AND env_id = ? AND platform = ? AND cloud_location = ? LIMIT 1', [$t['id'], $t['env_id'], $t['platform'], $t['cloud_location']])) { continue; } if ($t['platform'] != \SERVER_PLATFORMS::GCE && !$t['cloud_location']) { $excludedCL++; continue; } try { switch ($t['platform']) { case \SERVER_PLATFORMS::EC2: $snap = $env->aws($t['cloud_location'])->ec2->image->describe($t['id']); if (count($snap)) { $add = true; $t['architecture'] = $snap->toArray()[0]['architecture']; } break; case \SERVER_PLATFORMS::RACKSPACE: $platform = PlatformFactory::NewPlatform(\SERVER_PLATFORMS::RACKSPACE); /* @var $platform RackspacePlatformModule */ $client = \Scalr_Service_Cloud_Rackspace::newRackspaceCS($env->getPlatformConfigValue(RackspacePlatformModule::USERNAME, true, $t['cloud_location']), $env->getPlatformConfigValue(RackspacePlatformModule::API_KEY, true, $t['cloud_location']), $t['cloud_location']); $snap = $client->getImageDetails($t['id']); if ($snap) { $add = true; } else { $excludedMissing++; } break; case \SERVER_PLATFORMS::GCE: $platform = PlatformFactory::NewPlatform(\SERVER_PLATFORMS::GCE); /* @var $platform GoogleCEPlatformModule */ $client = $platform->getClient($env); /* @var $client \Google_Service_Compute */ $projectId = $env->getPlatformConfigValue(GoogleCEPlatformModule::PROJECT_ID); $snap = $client->images->get($projectId, str_replace($projectId . '/images/', '', $t['id'])); if ($snap) { $add = true; $t['architecture'] = 'x86_64'; } else { $excludedMissing++; } break; case \SERVER_PLATFORMS::EUCALYPTUS: $snap = $env->eucalyptus($t['cloud_location'])->ec2->image->describe($t['id']); if (count($snap)) { $add = true; $t['architecture'] = $snap->toArray()[0]['architecture']; } break; default: if (PlatformFactory::isOpenstack($t['platform'])) { $snap = $env->openstack($t['platform'], $t['cloud_location'])->servers->getImage($t['id']); if ($snap) { $add = true; $t['architecture'] = $snap->metadata->arch == 'x84-64' ? 'x84_64' : 'i386'; } else { $excludedMissing++; } } else { if (PlatformFactory::isCloudstack($t['platform'])) { $snap = $env->cloudstack($t['platform'])->template->describe(['templatefilter' => 'executable', 'id' => $t['id'], 'zoneid' => $t['cloud_location']]); if ($snap) { if (isset($snap[0])) { $add = true; } } else { $excludedMissing++; } } else { $this->console->warning('Unknown platform: %s', $t['platform']); } } } if ($add) { $image = new Image(); $image->id = $t['id']; $image->envId = $t['env_id']; $image->bundleTaskId = $t['bundle_task_id']; $image->platform = $t['platform']; $image->cloudLocation = $t['cloud_location']; $image->createdById = $t['created_by_id']; $image->createdByEmail = $t['created_by_email']; $image->architecture = $t['architecture'] ? $t['architecture'] : 'x86_64'; $image->isDeprecated = 0; $image->source = $t['bundle_task_id'] ? 'BundleTask' : 'Manual'; $image->type = $t['type']; $image->status = Image::STATUS_ACTIVE; $image->agentVersion = $t['agent_version']; $image->save(); } else { $excludedMissing++; } } catch (\Exception $e) { if (strpos($e->getMessage(), 'The resource could not be found') !== FALSE) { $excludedMissing++; } else { if (strpos($e->getMessage(), 'The requested URL / was not found on this server.') !== FALSE) { $excludedMissing++; } else { if (strpos($e->getMessage(), 'Not Found') !== FALSE) { $excludedMissing++; } else { if (strpos($e->getMessage(), 'was not found') !== FALSE) { $excludedMissing++; } else { if (strpos($e->getMessage(), 'Bad username or password') !== FALSE) { $excludedMissing++; } else { if (strpos($e->getMessage(), 'unable to verify user credentials and/or request signature') !== FALSE) { $excludedMissing++; } else { if (strpos($e->getMessage(), 'OpenStack error. Image not found.') !== FALSE) { $excludedMissing++; } else { if (strpos($e->getMessage(), 'Neither api key nor password was provided for the OpenStack config.') !== FALSE) { $excludedMissing++; } else { $this->console->warning('SnapshotId: %s, envId: %d, error: %s', $t['id'], $t['env_id'], $e->getMessage()); } } } } } } } } } } } $this->console->notice('Found %d records', $allRecords); $this->console->notice('Excluded %d images because of null cloud_location', $excludedCL); $this->console->notice('Excluded %d missed images', $excludedMissing); }
public function dashboardAction() { $this->request->defineParams(array('farmId' => array('type' => 'int'), 'farmRoleId' => array('type' => 'int'), 'type')); $dbFarm = DBFarm::LoadByID($this->getParam('farmId')); $this->user->getPermissions()->validate($dbFarm); if ($this->getParam('farmRoleId')) { $dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId')); if ($dbFarmRole->FarmID != $dbFarm->ID) { throw new Exception("Role not found"); } } elseif ($this->getParam('type')) { foreach ($dbFarm->GetFarmRoles() as $sDbFarmRole) { if ($sDbFarmRole->GetRoleObject()->hasBehavior($this->getParam('type'))) { $dbFarmRole = $sDbFarmRole; break; } } if (!$dbFarmRole) { throw new Exception("Role not found"); } } else { throw new Scalr_UI_Exception_NotFound(); } $data = array('farmRoleId' => $dbFarmRole->ID, 'farmId' => $dbFarmRole->FarmID); $data['dbType'] = $dbFarmRole->GetRoleObject()->getDbMsrBehavior(); if (!$data['dbType']) { $this->response->failure("Unknown db type"); return; } switch ($data['dbType']) { case ROLE_BEHAVIORS::MYSQL2: case ROLE_BEHAVIORS::PERCONA: case ROLE_BEHAVIORS::MARIADB: $szrApiNamespace = Scalr_Net_Scalarizr_Client::NAMESPACE_MYSQL; break; case ROLE_BEHAVIORS::REDIS: $szrApiNamespace = Scalr_Net_Scalarizr_Client::NAMESPACE_REDIS; $data['extras'] = array(array('name' => 'Processes', 'value' => $dbFarmRole->GetSetting(Scalr_Db_Msr_Redis::NUM_PROCESSES)), array('name' => 'Persistence type', 'value' => $dbFarmRole->GetSetting(Scalr_Db_Msr_Redis::PERSISTENCE_TYPE))); break; case ROLE_BEHAVIORS::POSTGRESQL: $szrApiNamespace = Scalr_Net_Scalarizr_Client::NAMESPACE_POSTGRESQL; break; } // Get PMA details for MySQL / Percona if (in_array($data['dbType'], array(ROLE_BEHAVIORS::MYSQL, ROLE_BEHAVIORS::MYSQL2, ROLE_BEHAVIORS::PERCONA, ROLE_BEHAVIORS::MARIADB))) { $data['pma'] = $this->getPmaDetails($dbFarmRole); } $behavior = Scalr_Role_Behavior::loadByName($data['dbType']); $masterServer = $behavior->getMasterServer($dbFarmRole); if ($masterServer) { // Get Storage details $data['storage'] = $this->getDbStorageStatus($masterServer, $data['dbType']); } // Get Access details and DNS endpoints $data['accessDetails'] = $this->getDbAccessDetails($dbFarmRole); $data['name'] = ROLE_BEHAVIORS::GetName($data['dbType']); // Get data bundle info $bundlesEnabled = $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BUNDLE_ENABLED); $lastActionTime = $dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_BUNDLE_LAST_TS")); $data['bundles'] = array('history' => $this->db->GetAll("SELECT *, UNIX_TIMESTAMP(date) as date FROM services_db_backups_history WHERE `farm_role_id` = ? AND `operation` = ? ORDER BY id ASC", array($dbFarmRole->ID, 'bundle')), 'inProgress' => array('status' => (int) $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BUNDLE_IS_RUNNING), 'serverId' => $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BUNDLE_SERVER_ID)), 'last' => $lastActionTime ? Scalr_Util_DateTime::convertTz((int) $lastActionTime, 'd M Y \\a\\t H:i:s') : 'Never'); foreach ($data['bundles']['history'] as &$h) { $h['date'] = Scalr_Util_DateTime::convertTz((int) $h['date'], 'd M Y \\a\\t H:i:s'); } if ($bundlesEnabled) { $period = $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BUNDLE_EVERY); if ($lastActionTime) { $nextTime = $lastActionTime + $period * 3600; } $data['bundles']['next'] = !$nextTime || $nextTime < time() ? "Within 30 minutes" : Scalr_Util_DateTime::convertTz((int) $nextTime, 'd M Y \\a\\t H:i:s'); $data['bundles']['schedule'] = "Every {$period} hours"; } else { $data['bundles']['next'] = " - "; $data['bundles']['schedule'] = "Auto-snapshotting disabled"; } // Get backups info $lastActionTime = $dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_BACKUP_LAST_TS")); $nextTime = false; $backupsEnabled = $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BACKUP_ENABLED); $data['backups'] = array('history' => $this->db->GetAll("SELECT *, UNIX_TIMESTAMP(date) as date FROM services_db_backups_history WHERE `farm_role_id` = ? AND `operation` = ? ORDER BY id ASC", array($dbFarmRole->ID, 'backup')), 'inProgress' => array('status' => (int) $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BACKUP_IS_RUNNING), 'serverId' => $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BACKUP_SERVER_ID)), 'last' => $lastActionTime ? Scalr_Util_DateTime::convertTz((int) $lastActionTime, 'd M Y \\a\\t H:i:s') : 'Never', 'supported' => !PlatformFactory::isCloudstack($dbFarmRole->Platform) && (!PlatformFactory::isOpenstack($dbFarmRole->Platform) || PlatformFactory::NewPlatform($dbFarmRole->Platform)->getConfigVariable(OpenstackPlatformModule::EXT_SWIFT_ENABLED, $this->getEnvironment(), false))); foreach ($data['backups']['history'] as &$h) { $h['date'] = Scalr_Util_DateTime::convertTz((int) $h['date'], 'd M Y \\a\\t H:i:s'); } if ($backupsEnabled) { $period = $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BACKUP_EVERY); if ($lastActionTime) { $nextTime = $lastActionTime + $period * 3600; } $data['backups']['next'] = !$nextTime || $nextTime < time() ? "Within 30 minutes" : Scalr_Util_DateTime::convertTz((int) $nextTime, 'd M Y \\a\\t H:i:s'); $data['backups']['schedule'] = "Every {$period} hours"; } else { $data['backups']['next'] = " - "; $data['backups']['schedule'] = "Auto-backups disabled"; } /* if ($dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_STORAGE_ENGINE) == 'lvm') { $data['noDataBundleForSlaves'] = ($dbFarmRole->GetSetting(Scalr_Role_DbMsrBehavior::ROLE_NO_DATA_BUNDLE_FOR_SLAVES)) ? true : false; } */ $conf = $this->getContainer()->config->get('scalr.load_statistics.connections.plotter'); foreach ($dbFarmRole->GetServersByFilter(array('status' => array(SERVER_STATUS::INIT, SERVER_STATUS::RUNNING, SERVER_STATUS::PENDING))) as $dbServer) { $isMaster = $dbServer->GetProperty(Scalr_Db_Msr::REPLICATION_MASTER) == 1; $serverRole = $isMaster ? 'master' : 'slave'; $serverInfo = array('status' => $dbServer->status, 'remoteIp' => $dbServer->remoteIp, 'localIp' => $dbServer->localIp, 'serverId' => $dbServer->serverId, 'cloudServerId' => $dbServer->GetCloudServerID(), 'cloudLocation' => $dbServer->GetCloudLocation(), 'serverRole' => $serverRole, 'index' => $dbServer->index); $serverInfo['monitoring'] = array('farmId' => $dbFarmRole->FarmID, 'farmRoleId' => $dbFarmRole->ID, 'index' => $dbServer->index, 'hash' => $dbFarm->Hash, 'hostUrl' => "{$conf['scheme']}://{$conf['host']}:{$conf['port']}"); if ($dbServer->platform == SERVER_PLATFORMS::EC2) { $serverInfo['cloudLocation'] = $dbServer->GetProperty(EC2_SERVER_PROPERTIES::AVAIL_ZONE); } if ($dbServer->status == SERVER_STATUS::RUNNING) { try { $rStatus = $dbServer->scalarizr->{$szrApiNamespace}->replicationStatus(); if ($data['dbType'] != ROLE_BEHAVIORS::REDIS) { $rStatus = (array) $rStatus->{$serverRole}; $replication = $rStatus; } else { if ($isMaster) { $rStatus = (array) $rStatus->masters; foreach ($rStatus as $port => $status) { $rStatus['status'] = $status; if ($status != 'up') { break; } } } else { $rStatus = (array) $rStatus->slaves; foreach ($rStatus as $port => $status) { $rStatus['status'] = $status->status; if ($status->status != 'up') { break; } } } $replication = $rStatus; } if (in_array($data['dbType'], array(ROLE_BEHAVIORS::MYSQL2, ROLE_BEHAVIORS::PERCONA, ROLE_BEHAVIORS::MARIADB))) { if ($rStatus['status'] == 'up' && $replication['seconds_behind_master'] > 0) { $status = 'lagging'; } else { $status = $rStatus['status']; } } elseif ($data['dbType'] == ROLE_BEHAVIORS::REDIS) { $status = $rStatus['status']; } elseif ($data['dbType'] == ROLE_BEHAVIORS::POSTGRESQL) { if ($rStatus['status'] == 'up' && $replication['Xlog_delay'] > 1000) { $status = 'lagging'; } else { $status = $rStatus['status']; } } $serverInfo['replication'] = array('status' => $status, $data['dbType'] => $replication); } catch (Exception $e) { $serverInfo['replication'] = array('status' => 'error', 'message' => $e->getMessage()); } } $data['servers'][] = $serverInfo; } $this->response->page('ui/db/manager/dashboard.js', $data, array('ui/monitoring/window.js'), array('ui/db/manager/dashboard.css')); }
/** * Check if image exists * * @param bool $update on true update name, size, status from cloud information (you should call save manually) * @return bool */ public function checkImage($update = true) { if (!$this->envId) { return true; } $env = Scalr_Environment::init()->loadById($this->envId); switch ($this->platform) { case SERVER_PLATFORMS::EC2: try { $snap = $env->aws($this->cloudLocation)->ec2->image->describe($this->id); if ($snap->count() == 0) { return false; } if ($update) { $sn = $snap->get(0)->toArray(); $this->name = $sn['name']; $this->architecture = $sn['architecture']; if ($sn['rootDeviceType'] == 'ebs') { $this->type = 'ebs'; } else { if ($sn['rootDeviceType'] == 'instance-store') { $this->type = 'instance-store'; } } if ($sn['virtualizationType'] == 'hvm') { $this->type = $this->type . '-hvm'; } foreach ($sn['blockDeviceMapping'] as $b) { if ($b['deviceName'] == $sn['rootDeviceName'] && $b['ebs']) { $this->size = $b['ebs']['volumeSize']; } } } } catch (Exception $e) { return false; } break; case SERVER_PLATFORMS::GCE: try { $platform = PlatformFactory::NewPlatform(SERVER_PLATFORMS::GCE); /* @var $platform GoogleCEPlatformModule */ $client = $platform->getClient($env); /* @var $client \Google_Service_Compute */ // for global images we use another projectId $ind = strpos($this->id, '/global/'); if ($ind !== FALSE) { $projectId = substr($this->id, 0, $ind); $id = str_replace("{$projectId}/global/images/", '', $this->id); } else { $ind = strpos($this->id, '/images/'); if ($ind !== false) { $projectId = substr($this->id, 0, $ind); } else { $projectId = $env->getPlatformConfigValue(GoogleCEPlatformModule::PROJECT_ID); } $id = str_replace("{$projectId}/images/", '', $this->id); } $snap = $client->images->get($projectId, $id); if ($update) { $this->name = $snap->name; $this->size = $snap->diskSizeGb; $this->architecture = 'x86_64'; } } catch (Exception $e) { return false; } break; case SERVER_PLATFORMS::RACKSPACE: try { $client = \Scalr_Service_Cloud_Rackspace::newRackspaceCS($env->getPlatformConfigValue(RackspacePlatformModule::USERNAME, true, $this->cloudLocation), $env->getPlatformConfigValue(RackspacePlatformModule::API_KEY, true, $this->cloudLocation), $this->cloudLocation); $snap = $client->getImageDetails($this->id); if ($snap) { if ($update) { $this->name = $snap->image->name; } } else { return false; } } catch (\Exception $e) { return false; } break; default: if (PlatformFactory::isOpenstack($this->platform)) { try { $snap = $env->openstack($this->platform, $this->cloudLocation)->servers->getImage($this->id); if ($snap) { if ($update) { $this->name = $snap->name; $this->size = $snap->metadata->instance_type_root_gb; } } else { return false; } } catch (\Exception $e) { return false; } } else { if (PlatformFactory::isCloudstack($this->platform)) { try { $snap = $env->cloudstack($this->platform)->template->describe(['templatefilter' => 'executable', 'id' => $this->id, 'zoneid' => $this->cloudLocation]); if ($snap && isset($snap[0])) { if ($update) { $this->name = $snap[0]->name; $this->size = ceil($snap[0]->size / (1024 * 1024 * 1024)); } } else { return false; } } catch (\Exception $e) { return false; } } else { return false; } } } return true; }
/** * Gets a normalized url for an each platform * * @param string $platform Cloud platform * @return string Returns url */ public function getUrl($platform) { if (!isset($this->aUrl[$platform])) { if ($platform == \SERVER_PLATFORMS::EC2 || $platform == \SERVER_PLATFORMS::GCE || $platform == \SERVER_PLATFORMS::AZURE) { $value = ''; } else { if (PlatformFactory::isOpenstack($platform)) { $value = CloudLocation::normalizeUrl($this->env->keychain($platform)->properties[CloudCredentialsProperty::OPENSTACK_KEYSTONE_URL]); } else { if (PlatformFactory::isCloudstack($platform)) { $value = CloudLocation::normalizeUrl($this->env->keychain($platform)->properties[CloudCredentialsProperty::CLOUDSTACK_API_URL]); } } } $this->aUrl[$platform] = $value; } return $this->aUrl[$platform]; }
/** * @param string $platform * @param string $cloudLocation * @throws Exception */ public function xGetCloudServersListAction($platform, $cloudLocation) { if (!$this->environment->isPlatformEnabled($platform)) { throw new Exception(sprintf('Cloud %s is not enabled for current environment', $platform)); } $results = []; $platformObj = PlatformFactory::NewPlatform($platform); if ($platform == SERVER_PLATFORMS::GCE) { $gce = $platformObj->getClient($this->environment); $result = $gce->instances->listInstances($this->environment->keychain(SERVER_PLATFORMS::GCE)->properties[CloudCredentialsProperty::GCE_PROJECT_ID], $cloudLocation, []); if (is_array($result->items)) { foreach ($result->items as $server) { if ($server->status != 'RUNNING') { continue; } $ips = $platformObj->determineServerIps($gce, $server); $itm = ['id' => $server->name, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $cloudLocation, 'isImporting' => false, 'isManaged' => false]; //Check is instance already importing try { $dbServer = DBServer::LoadByPropertyValue(GCE_SERVER_PROPERTIES::SERVER_NAME, $server->name); 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; } } } else { if ($platform == SERVER_PLATFORMS::AZURE) { // cloudLocation is resourceGroup $t = $this->getEnvironment()->azure()->compute->virtualMachine->getList($this->getEnvironment()->keychain(SERVER_PLATFORMS::AZURE)->properties[CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID], $cloudLocation); foreach ($t as $server) { $itm = ['id' => $server->name, 'isImporting' => false, 'isManaged' => false]; $nicInfo = $server->properties->networkProfile->networkInterfaces[0]->id; // get id and call if (!empty($nicInfo->properties->ipConfigurations)) { foreach ($nicInfo->properties->ipConfigurations as $ipConfig) { $privateIp = $ipConfig->properties->privateIPAddress; if ($ipConfig->properties->publicIPAddress) { $publicIp = $ipConfig->properties->publicIPAddress->properties->ipAddress; if ($publicIp) { break; } } } } $itm['localIp'] = $privateIp; $itm['publicIp'] = $publicIp; $itm['zone'] = $server->location; $results[] = $itm; } } elseif (PlatformFactory::isOpenstack($platform)) { $client = $this->environment->openstack($platform, $cloudLocation); $r = $client->servers->list(true); do { foreach ($r as $server) { if ($server->status != 'ACTIVE') { continue; } $ips = $platformObj->determineServerIps($client, $server); $itm = array('id' => $server->id, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $cloudLocation, 'isImporting' => false, 'isManaged' => false, 'fullInfo' => $server); //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; } } while (false !== ($r = $r->getNextPage())); } elseif (PlatformFactory::isCloudstack($platform)) { $client = $this->environment->cloudstack($platform); $platformObj = PlatformFactory::NewPlatform($platform); $r = $client->instance->describe(array('zoneid' => $cloudLocation)); if (count($r) > 0) { foreach ($r as $server) { $ips = $platformObj->determineServerIps($client, $server); $itm = array('id' => $server->id, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $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 ($platform == SERVER_PLATFORMS::EC2) { $client = $this->environment->aws($cloudLocation)->ec2; $nextToken = null; do { if (isset($r)) { $nextToken = $r->getNextToken(); } $r = $client->instance->describe(null, null, $nextToken); 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; } } } } while ($r->getNextToken()); } } $this->response->data(array('data' => $results)); }
/** * xGetPlatformInstanceTypesAction * * @param string $platform The name of the cloud platform * @param string $cloudLocation The cloud location * @param string $envId optional The identifier of the environment * @param string $effectiveDate optional The date on which prices should be applied YYYY-MM-DD * @throws \Exception */ public function xGetPlatformInstanceTypesAction($platform, $cloudLocation, $envId = null, $effectiveDate = null) { list($curDate, $effectiveDate) = $this->handleEffectiveDate($effectiveDate); $pm = PlatformFactory::NewPlatform($platform); $env = null; $url = ''; try { if (!empty($envId)) { $env = Scalr_Environment::init()->loadById($envId); if (PlatformFactory::isOpenstack($platform)) { $key = $platform . '.' . OpenstackPlatformModule::KEYSTONE_URL; } else { if (PlatformFactory::isCloudstack($platform)) { $key = $platform . '.' . CloudstackPlatformModule::API_URL; } else { if ($platform == SERVER_PLATFORMS::EUCALYPTUS) { $key = EucalyptusPlatformModule::EC2_URL; $url = $this->getContainer()->analytics->prices->normalizeUrl($env->getPlatformConfigValue($key, false, $cloudLocation)); } else { throw new Exception('This action is not yet supported for the specified cloud platform.'); } } } if (empty($url)) { $url = $this->getContainer()->analytics->prices->normalizeUrl($env->getPlatformConfigValue($key)); } } else { if ($platform == SERVER_PLATFORMS::EC2 || $platform == SERVER_PLATFORMS::GCE) { $gcenvid = $this->getPlatformEnvId($platform); $env = Scalr_Environment::init()->loadById($gcenvid); } } } catch (Exception $e) { if (stristr($e->getMessage(), 'not found')) { //Tries to find url from the cloud_locations table if (empty($url) && (PlatformFactory::isOpenstack($platform) || PlatformFactory::isCloudstack($platform))) { $clEntity = CloudLocation::findOne([['platform' => $platform], ['cloudLocation' => $cloudLocation]], ['updated' => false]); if ($clEntity instanceof CloudLocation) { $url = $clEntity->url; } } } else { throw $e; } } $result = $this->getTypesWithPrices($cloudLocation, $url, $pm, $platform, $effectiveDate, $env); $this->response->data(['data' => $result]); }
/** * Terminates instance * * @param string $serverId UUID of the server * @return ResultEnvelope * @throws ApiErrorException */ public function terminateAction($serverId) { $server = $this->getServer($serverId); $this->checkPermissions($server, Acl::PERM_FARMS_SERVERS); if (in_array($server->status, [Server::STATUS_IMPORTING, Server::STATUS_TEMPORARY])) { throw new ApiErrorException(409, ErrorMessage::ERR_UNACCEPTABLE_STATE, sprintf("The Server can't be terminated in %s state.", $server->status)); } if ($server->platform == SERVER_PLATFORMS::EC2 && !empty($server->properties[EC2_SERVER_PROPERTIES::IS_LOCKED])) { throw new ApiErrorException(409, ErrorMessage::ERR_LOCKED, "Server has disableAPITermination flag and can not be terminated."); } $object = $this->request->getJsonBody(); $force = isset($object->force) ? ServerAdapter::convertInputValue('boolean', $object->force) : false; if ((PlatformFactory::isOpenstack($server->platform) || PlatformFactory::isCloudstack($server->platform)) && $force) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf("Force termination is not available for platform %s.", $server->platform)); } if (!$server->terminate([Server::TERMINATE_REASON_MANUALLY_API, $this->getUser()->fullName], $force, $this->getUser())) { throw new ApiErrorException(409, ErrorMessage::ERR_UNACCEPTABLE_STATE, sprintf("Server with id %s has already been terminated.", $serverId)); } $this->response->setStatus(200); return $this->result($this->adapter('server')->toData($server)); }
/** * {@inheritdoc} * @see \Scalr\System\Zmq\Cron\TaskInterface::worker() */ public function worker($request) { $serverId = $request->serverId; $logger = \Scalr::getContainer()->logger(__CLASS__); $this->log("INFO", "Processing messages for %s server", $serverId); try { $dbserver = DBServer::LoadByID($serverId); if ($dbserver->farmId) { if ($dbserver->GetFarmObject()->Status == FARM_STATUS::TERMINATED) { throw new ServerNotFoundException("Farm related to this server has been terminated."); } } } catch (ServerNotFoundException $e) { //By some reason server does not exist $this->db->Execute("\n DELETE m FROM messages m\n WHERE m.server_id = ? AND m.`type` = ? AND m.`status` = ?\n ", [$serverId, "in", MESSAGE_STATUS::PENDING]); return false; } //Warming up static DI cache \Scalr::getContainer()->warmup(); // Reconfigure observers \Scalr::ReconfigureObservers(); $rs = $this->db->Execute("\n SELECT m.* FROM messages m\n WHERE m.server_id = ? AND m.type = ? AND m.status = ?\n ORDER BY m.dtadded ASC\n ", [$serverId, "in", MESSAGE_STATUS::PENDING]); while ($row = $rs->FetchRow()) { try { if ($row["message_format"] == 'xml') { $message = $this->serializer->unserialize($row["message"]); } else { $message = $this->jsonSerializer->unserialize($row["message"]); $dbserver->SetProperty(SERVER_PROPERTIES::SZR_MESSAGE_FORMAT, 'json'); } $message->messageIpAddress = $row['ipaddress']; $event = null; $startTime = microtime(true); // Update scalarizr package version if ($message->meta[Scalr_Messaging_MsgMeta::SZR_VERSION]) { $dbserver->setScalarizrVersion($message->meta[Scalr_Messaging_MsgMeta::SZR_VERSION]); } if ($message->meta[Scalr_Messaging_MsgMeta::SZR_UPD_CLIENT_VERSION]) { $dbserver->SetProperty(SERVER_PROPERTIES::SZR_UPD_CLIENT_VERSION, $message->meta[Scalr_Messaging_MsgMeta::SZR_UPD_CLIENT_VERSION]); } if ($dbserver->GetProperty(SERVER_PROPERTIES::SYSTEM_IGNORE_INBOUND_MESSAGES)) { continue; } if ($message instanceof \Scalr_Messaging_Msg) { $this->log('INFO', "Handling '%s' for '%s' server", $message->getName(), $serverId); } try { if ($message instanceof Scalr_Messaging_Msg_OperationResult) { if ($message->status == 'ok' || $message->status == 'completed') { if ($message->name == 'Grow MySQL/Percona data volume' || $message->name == 'mysql.grow-volume') { $volumeConfig = $message->data ? $message->data : $message->result; $oldVolumeId = $dbserver->GetFarmRoleObject()->GetSetting(Scalr_Db_Msr::VOLUME_ID); $engine = $dbserver->GetFarmRoleObject()->GetSetting(Scalr_Db_Msr::DATA_STORAGE_ENGINE); try { // clear information about last request $dbserver->GetFarmRoleObject()->SetSetting(Entity\FarmRoleSetting::STORAGE_GROW_OPERATION_ID, null); $dbserver->GetFarmRoleObject()->SetSetting(Entity\FarmRoleSetting::STORAGE_GROW_SERVER_ID, null); $dbserver->GetFarmRoleObject()->SetSetting(Entity\FarmRoleSetting::STORAGE_GROW_LAST_ERROR, null); $storageVolume = Scalr_Storage_Volume::init(); try { $storageVolume->loadById($volumeConfig->id); $storageVolume->setConfig($volumeConfig); $storageVolume->save(); } catch (Exception $e) { if (strpos($e->getMessage(), 'not found')) { $storageVolume->loadBy(array('id' => $volumeConfig->id, 'client_id' => $dbserver->clientId, 'env_id' => $dbserver->envId, 'name' => "'{$volumeConfig->tags->service}' data volume", 'type' => $engine, 'platform' => $dbserver->platform, 'size' => $volumeConfig->size, 'fstype' => $volumeConfig->fstype, 'purpose' => $volumeConfig->tags->service, 'farm_roleid' => $dbserver->farmRoleId, 'server_index' => $dbserver->index)); $storageVolume->setConfig($volumeConfig); $storageVolume->save(true); } else { throw $e; } } $dbserver->GetFarmRoleObject()->SetSetting(Scalr_Db_Msr::VOLUME_ID, $volumeConfig->id, Entity\FarmRoleSetting::TYPE_LCL); if ($engine == MYSQL_STORAGE_ENGINE::EBS) { $dbserver->GetFarmRoleObject()->SetSetting(Scalr_Db_Msr::DATA_STORAGE_EBS_SIZE, $volumeConfig->size, Entity\FarmRoleSetting::TYPE_CFG); $dbserver->GetFarmRoleObject()->SetSetting(Scalr_Db_Msr::DATA_STORAGE_EBS_TYPE, $volumeConfig->volumeType, Entity\FarmRoleSetting::TYPE_CFG); if ($volumeConfig->volumeType == 'io1') { $dbserver->GetFarmRoleObject()->SetSetting(Scalr_Db_Msr::DATA_STORAGE_EBS_IOPS, $volumeConfig->iops, Entity\FarmRoleSetting::TYPE_CFG); } } elseif ($engine == MYSQL_STORAGE_ENGINE::RAID_EBS) { $dbserver->GetFarmRoleObject()->SetSetting(Scalr_Db_Msr::DATA_STORAGE_RAID_DISK_SIZE, $volumeConfig->size, Entity\FarmRoleSetting::TYPE_CFG); $dbserver->GetFarmRoleObject()->SetSetting(Scalr_Db_Msr::DATA_STORAGE_RAID_EBS_DISK_TYPE, $volumeConfig->volumeType, Entity\FarmRoleSetting::TYPE_CFG); if ($volumeConfig->volumeType == 'io1') { $dbserver->GetFarmRoleObject()->SetSetting(Scalr_Db_Msr::DATA_STORAGE_RAID_EBS_DISK_IOPS, $volumeConfig->iops, Entity\FarmRoleSetting::TYPE_CFG); } } // Remove old $storageVolume->delete($oldVolumeId); } catch (Exception $e) { \Scalr::getContainer()->logger(__CLASS__)->error(new FarmLogMessage($dbserver->farmId, "Cannot save storage volume: {$e->getMessage()}", !empty($dbserver->serverId) ? $dbserver->serverId : null)); } } } elseif ($message->status == 'error' || $message->status == 'failed') { if ($message->name == 'Initialization' || $message->name == 'system.init') { $dbserver->SetProperty(SERVER_PROPERTIES::SZR_IS_INIT_FAILED, 1); if (is_object($message->error)) { $errorText = $message->error->message; } elseif ($message->error) { $errorText = $message->error; } $dbserver->SetProperty(SERVER_PROPERTIES::SZR_IS_INIT_ERROR_MSG, $errorText); $event = new HostInitFailedEvent($dbserver, $errorText); } else { if ($message->name == 'Grow MySQL/Percona data volume' || $message->name == 'mysql.grow-volume') { $dbserver->GetFarmRoleObject()->SetSetting(Entity\FarmRoleSetting::STORAGE_GROW_LAST_ERROR, is_object($message->error) ? $message->error->message : $message->error); $dbserver->GetFarmRoleObject()->SetSetting(Entity\FarmRoleSetting::STORAGE_GROW_OPERATION_ID, null); $dbserver->GetFarmRoleObject()->SetSetting(Entity\FarmRoleSetting::STORAGE_GROW_SERVER_ID, null); } } } } elseif ($message instanceof Scalr_Messaging_Msg_InitFailed) { $errorText = $message->reason; $dbserver->SetProperty(SERVER_PROPERTIES::SZR_IS_INIT_ERROR_MSG, $errorText); $event = new HostInitFailedEvent($dbserver, $errorText); } elseif ($message instanceof \Scalr_Messaging_Msg_RuntimeError) { $logger->fatal(new FarmLogMessage($dbserver->farmId, "Scalarizr failed to launch on server '{$dbserver->getNameByConvention()}' with runtime error: {$message->message}", $dbserver->serverId)); } elseif ($message instanceof Scalr_Messaging_Msg_UpdateControlPorts) { $apiPort = $message->api; $ctrlPort = $message->messaging; // Check API port; $currentApiPort = $dbserver->GetProperty(SERVER_PROPERTIES::SZR_API_PORT); if (!$currentApiPort) { $currentApiPort = 8010; } if ($apiPort && $apiPort != $currentApiPort) { $logger->warn(new FarmLogMessage($dbserver->farmId, "Scalarizr API port was changed from {$currentApiPort} to {$apiPort}", $dbserver->serverId)); $dbserver->SetProperty(SERVER_PROPERTIES::SZR_API_PORT, $apiPort); } // Check Control port $currentCtrlPort = $dbserver->GetProperty(SERVER_PROPERTIES::SZR_CTRL_PORT); if (!$currentCtrlPort) { $currentCtrlPort = 8013; } if ($ctrlPort && $ctrlPort != $currentCtrlPort) { $logger->warn(new FarmLogMessage($dbserver->farmId, "Scalarizr Control port was changed from {$currentCtrlPort} to {$ctrlPort}", $dbserver->serverId)); $dbserver->SetProperty(SERVER_PROPERTIES::SZR_CTRL_PORT, $ctrlPort); } } elseif ($message instanceof Scalr_Messaging_Msg_Win_HostDown) { $event = $this->onHostDown($message, $dbserver); if ($event === false) { $doNotProcessMessage = true; } } elseif ($message instanceof Scalr_Messaging_Msg_Win_PrepareBundleResult) { try { $bundleTask = BundleTask::LoadById($message->bundleTaskId); } catch (Exception $e) { } if ($bundleTask) { if ($bundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::PREPARING) { if ($message->status == 'ok') { $metaData = array('szr_version' => $message->meta[Scalr_Messaging_MsgMeta::SZR_VERSION], 'os' => $message->os, 'software' => $message->software); $bundleTask->setMetaData($metaData); $bundleTask->Save(); PlatformFactory::NewPlatform($bundleTask->platform)->CreateServerSnapshot($bundleTask); } else { $bundleTask->SnapshotCreationFailed("PrepareBundle procedure failed: {$message->lastError}"); } } } } elseif ($message instanceof Scalr_Messaging_Msg_DeployResult) { try { $deploymentTask = Scalr_Model::init(Scalr_Model::DM_DEPLOYMENT_TASK)->loadById($message->deployTaskId); } catch (Exception $e) { } if ($deploymentTask) { if ($message->status == 'error') { $deploymentTask->status = Scalr_Dm_DeploymentTask::STATUS_FAILED; $deploymentTask->lastError = $message->lastError; } else { $deploymentTask->status = Scalr_Dm_DeploymentTask::STATUS_DEPLOYED; $deploymentTask->dtDeployed = date("Y-m-d H:i:s"); } $deploymentTask->save(); } } elseif ($message instanceof Scalr_Messaging_Msg_Hello) { $event = $this->onHello($message, $dbserver); } elseif ($message instanceof Scalr_Messaging_Msg_FireEvent) { //Validate event $isEventExist = $this->db->GetOne("\n SELECT id FROM event_definitions\n WHERE name = ? AND ((env_id = ? AND account_id = ?) OR (env_id IS NULL AND account_id = ?) OR (env_id IS NULL AND account_id IS NULL))\n LIMIT 1\n ", array($message->eventName, $dbserver->envId, $dbserver->clientId, $dbserver->clientId)); if ($isEventExist) { $event = new CustomEvent($dbserver, $message->eventName, (array) $message->params); } } elseif ($message instanceof Scalr_Messaging_Msg_HostUpdate) { try { $dbFarmRole = $dbserver->GetFarmRoleObject(); } catch (Exception $e) { } if ($dbFarmRole instanceof DBFarmRole) { foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $behavior) { $behavior->handleMessage($message, $dbserver); } } } elseif ($message instanceof Scalr_Messaging_Msg_MongoDb) { /********* MONGODB *********/ try { $dbFarmRole = $dbserver->GetFarmRoleObject(); } catch (Exception $e) { } if ($dbFarmRole instanceof DBFarmRole) { foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $behavior) { $behavior->handleMessage($message, $dbserver); } } } elseif ($message instanceof Scalr_Messaging_Msg_DbMsr) { /********* DBMSR *********/ try { $dbFarmRole = $dbserver->GetFarmRoleObject(); } catch (Exception $e) { } if ($dbFarmRole instanceof DBFarmRole) { foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $behavior) { $behavior->handleMessage($message, $dbserver); } } } elseif ($message instanceof Scalr_Messaging_Msg_HostInit) { $event = $this->onHostInit($message, $dbserver); try { $dbserver->updateTimelog('ts_hi', $message->secondsSinceBoot, $message->secondsSinceStart); } catch (Exception $e) { } if (!$event) { continue; } } elseif ($message instanceof Scalr_Messaging_Msg_HostUp) { $event = $this->onHostUp($message, $dbserver); try { $dbserver->updateTimelog('ts_hu'); } catch (Exception $e) { } } elseif ($message instanceof Scalr_Messaging_Msg_HostDown) { $event = $this->onHostDown($message, $dbserver); if ($event == false) { $doNotProcessMessage = true; } } elseif ($message instanceof Scalr_Messaging_Msg_RebootStart) { $event = new RebootBeginEvent($dbserver); } elseif ($message instanceof Scalr_Messaging_Msg_RebootFinish) { if ($dbserver->status == \SERVER_STATUS::RESUMING) { try { // UPDATE IPs $p = PlatformFactory::NewPlatform($dbserver->platform); $ipaddresses = $p->GetServerIPAddresses($dbserver); if ($ipaddresses['remoteIp'] && !$dbserver->remoteIp || $ipaddresses['localIp'] && !$dbserver->localIp) { $dbserver->remoteIp = $update['remoteIp'] = $ipaddresses['remoteIp']; if (!$dbserver->localIp) { $update['localIp'] = $ipaddresses['localIp'] ? $ipaddresses['localIp'] : $message->localIp; } } // Update type after resume on EC2 if ($dbserver->platform == \SERVER_PLATFORMS::EC2) { $cacheKey = sprintf('%s:%s', $dbserver->envId, $dbserver->cloudLocation); $type = $p->instancesListCache[$cacheKey][$dbserver->GetCloudServerID()]['type']; if ($type != $dbserver->getType()) { $dbserver->setType($type); } } } catch (Exception $e) { if (stristr($e->getMessage(), "AWS Error. Request DescribeInstances failed. Cannot establish connection to AWS server")) { $doNotProcessMessage = true; } else { throw $e; } } // Set cloudstack Static IP if needed if (PlatformFactory::isCloudstack($dbserver->platform) && !$dbserver->remoteIp) { $remoteIp = CloudstackHelper::getSharedIP($dbserver); if ($remoteIp) { $dbserver->remoteIp = $update['remoteIp'] = $remoteIp; } } if (!$doNotProcessMessage) { if (!empty($update)) { $dbserver->update($update); unset($update); } $event = new \ResumeCompleteEvent($dbserver); } } elseif ($dbserver->status == \SERVER_STATUS::SUSPENDED) { //We need to wait for Poller to update status to RESUMING before processing this message $doNotProcessMessage = true; } elseif ($dbserver->status == \SERVER_STATUS::RUNNING) { if (!$dbserver->localIp && $message->localIp) { $dbserver->update(['localIp' => $message->localIp]); } $event = new RebootCompleteEvent($dbserver); } } elseif ($message instanceof Scalr_Messaging_Msg_BeforeHostUp) { $event = new BeforeHostUpEvent($dbserver); try { $dbserver->updateTimelog('ts_bhu'); } catch (Exception $e) { } } elseif ($message instanceof Scalr_Messaging_Msg_BlockDeviceAttached) { if ($dbserver->platform == SERVER_PLATFORMS::EC2) { $aws = $dbserver->GetEnvironmentObject()->aws($dbserver->GetProperty(EC2_SERVER_PROPERTIES::REGION)); $instanceId = $dbserver->GetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID); //The main goal of using filters there is to considerably decrease the size of the response. $volumes = $aws->ec2->volume->describe(null, array(array('name' => VolumeFilterNameType::attachmentInstanceId(), 'value' => (string) $instanceId), array('name' => VolumeFilterNameType::attachmentDevice(), 'value' => (string) $message->deviceName), array('name' => VolumeFilterNameType::status(), 'value' => AMAZON_EBS_STATE::IN_USE))); foreach ($volumes as $volume) { /* @var $volume \Scalr\Service\Aws\Ec2\DataType\VolumeData */ if ($volume->status == AMAZON_EBS_STATE::IN_USE && count($volume->attachmentSet) && $volume->attachmentSet[0]->instanceId == $instanceId && $volume->attachmentSet[0]->device == $message->deviceName) { $message->volumeId = $volume->volumeId; } } //Releases memory unset($volumes); $dbserver->GetEnvironmentObject()->getContainer()->release('aws'); unset($aws); } $event = new EBSVolumeAttachedEvent($dbserver, $message->deviceName, $message->volumeId); } elseif ($message instanceof Scalr_Messaging_Msg_BlockDeviceMounted) { // Single volume $ebsinfo = $this->db->GetRow("\n SELECT * FROM ec2_ebs WHERE volume_id=? LIMIT 1\n ", array($message->volumeId)); if ($ebsinfo) { $this->db->Execute("\n UPDATE ec2_ebs\n SET mount_status=?, isfsexist='1'\n WHERE id=?\n ", array(EC2_EBS_MOUNT_STATUS::MOUNTED, $ebsinfo['id'])); } $event = new EBSVolumeMountedEvent($dbserver, $message->mountpoint, $message->volumeId, $message->deviceName); } elseif ($message instanceof Scalr_Messaging_Msg_RebundleResult) { if ($message->status == Scalr_Messaging_Msg_RebundleResult::STATUS_OK) { $metaData = array('szr_version' => $message->meta[Scalr_Messaging_MsgMeta::SZR_VERSION], 'dist' => $message->dist, 'os' => $message->os, 'software' => $message->software); if ($dbserver->platform == SERVER_PLATFORMS::EC2) { if ($message->aws) { if ($message->aws->rootDeviceType == 'ebs') { $tags[] = ROLE_TAGS::EC2_EBS; } if ($message->aws->virtualizationType == 'hvm') { $tags[] = ROLE_TAGS::EC2_HVM; } } else { $aws = $dbserver->GetEnvironmentObject()->aws($dbserver); try { $info = $aws->ec2->image->describe($dbserver->GetProperty(EC2_SERVER_PROPERTIES::AMIID))->get(0); if ($info->rootDeviceType == 'ebs') { $tags[] = ROLE_TAGS::EC2_EBS; } else { try { $bundleTask = BundleTask::LoadById($message->bundleTaskId); if ($bundleTask->bundleType == SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS) { $tags[] = ROLE_TAGS::EC2_EBS; } } catch (Exception $e) { } } if ($info->virtualizationType == 'hvm') { $tags[] = ROLE_TAGS::EC2_HVM; } unset($info); } catch (Exception $e) { $metaData['tagsError'] = $e->getMessage(); try { $bundleTask = BundleTask::LoadById($message->bundleTaskId); if ($bundleTask->bundleType == SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS) { $tags[] = ROLE_TAGS::EC2_EBS; } } catch (Exception $e) { } } //Releases memory $dbserver->GetEnvironmentObject()->getContainer()->release('aws'); unset($aws); } } $metaData['tags'] = $tags; $event = new RebundleCompleteEvent($dbserver, $message->snapshotId, $message->bundleTaskId, $metaData); } else { if ($message->status == Scalr_Messaging_Msg_RebundleResult::STATUS_FAILED) { $event = new RebundleFailedEvent($dbserver, $message->bundleTaskId, $message->lastError); } } } elseif ($message instanceof Scalr_Messaging_Msg_Mysql_CreateDataBundleResult) { if ($message->status == "ok") { $event = new MysqlBackupCompleteEvent($dbserver, MYSQL_BACKUP_TYPE::BUNDLE, array('snapshotConfig' => $message->snapshotConfig, 'logFile' => $message->logFile, 'logPos' => $message->logPos, 'dataBundleSize' => $message->dataBundleSize, 'snapshotId' => $message->snapshotId)); } else { $event = new MysqlBackupFailEvent($dbserver, MYSQL_BACKUP_TYPE::BUNDLE); $event->lastError = $message->lastError; } } elseif ($message instanceof Scalr_Messaging_Msg_Mysql_CreateBackupResult) { if ($message->status == "ok") { $event = new MysqlBackupCompleteEvent($dbserver, MYSQL_BACKUP_TYPE::DUMP, array()); $event->backupParts = $message->backupParts; } else { $event = new MysqlBackupFailEvent($dbserver, MYSQL_BACKUP_TYPE::DUMP); $event->lastError = $message->lastError; } } elseif ($message instanceof Scalr_Messaging_Msg_Mysql_PromoteToMasterResult) { $event = $this->onMysql_PromoteToMasterResult($message, $dbserver); } elseif ($message instanceof Scalr_Messaging_Msg_Mysql_CreatePmaUserResult) { $farmRole = DBFarmRole::LoadByID($message->farmRoleId); if ($message->status == "ok") { $farmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_PMA_USER, $message->pmaUser, Entity\FarmRoleSetting::TYPE_LCL); $farmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_PMA_PASS, $message->pmaPassword, Entity\FarmRoleSetting::TYPE_LCL); } else { $farmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_PMA_REQUEST_TIME, "", Entity\FarmRoleSetting::TYPE_LCL); $farmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_PMA_REQUEST_ERROR, $message->lastError, Entity\FarmRoleSetting::TYPE_LCL); } } elseif ($message instanceof Scalr_Messaging_Msg_RabbitMq_SetupControlPanelResult) { $farmRole = $dbserver->GetFarmRoleObject(); if ($message->status == "ok") { $mgmtHost = $dbserver->getSzrHost(); if ($message->port) { $mgmtURL = "http://{$mgmtHost}:{$message->port}/mgmt"; } elseif ($message->cpanelUrl) { $info = parse_url($message->cpanelUrl); $mgmtURL = "http://{$mgmtHost}:{$info['port']}/mgmt"; } $farmRole->SetSetting(Scalr_Role_Behavior_RabbitMQ::ROLE_CP_SERVER_ID, $dbserver->serverId, Entity\FarmRoleSetting::TYPE_LCL); $farmRole->SetSetting(Scalr_Role_Behavior_RabbitMQ::ROLE_CP_URL, $mgmtURL, Entity\FarmRoleSetting::TYPE_LCL); $farmRole->SetSetting(Scalr_Role_Behavior_RabbitMQ::ROLE_CP_REQUEST_TIME, "", Entity\FarmRoleSetting::TYPE_LCL); } else { $farmRole->SetSetting(Scalr_Role_Behavior_RabbitMQ::ROLE_CP_SERVER_ID, "", Entity\FarmRoleSetting::TYPE_LCL); $farmRole->SetSetting(Scalr_Role_Behavior_RabbitMQ::ROLE_CP_REQUEST_TIME, "", Entity\FarmRoleSetting::TYPE_LCL); $farmRole->SetSetting(Scalr_Role_Behavior_RabbitMQ::ROLE_CP_ERROR_MSG, $message->lastError, Entity\FarmRoleSetting::TYPE_LCL); } } elseif ($message instanceof Scalr_Messaging_Msg_AmiScriptsMigrationResult) { try { //Open security group: if ($dbserver->platform == SERVER_PLATFORMS::EC2) { $info = PlatformFactory::NewPlatform($dbserver->platform)->GetServerExtendedInformation($dbserver); $sg = empty($info['Security groups']) ? [] : explode(", ", $info['Security groups']); foreach ($sg as $sgroup) { if ($sgroup != 'default') { // For Scalarizr $group_rules = array(array('rule' => 'tcp:8013:8013:0.0.0.0/0'), array('rule' => 'udp:8014:8014:0.0.0.0/0')); $aws = $dbserver->GetEnvironmentObject()->aws($dbserver); $ipPermissions = new \Scalr\Service\Aws\Ec2\DataType\IpPermissionList(); foreach ($group_rules as $rule) { $group_rule = explode(":", $rule["rule"]); $ipPermissions->append(new \Scalr\Service\Aws\Ec2\DataType\IpPermissionData($group_rule[0], $group_rule[1], $group_rule[2], new \Scalr\Service\Aws\Ec2\DataType\IpRangeData($group_rule[3]))); } $aws->ec2->securityGroup->authorizeIngress($ipPermissions, null, $sgroup); $dbserver->GetEnvironmentObject()->getContainer()->release('aws'); unset($aws); unset($ipPermissions); break; } } } } catch (Exception $e) { $logger->fatal($e->getMessage()); } $dbserver->SetProperty(SERVER_PROPERTIES::SZR_SNMP_PORT, 8014); $dbserver->SetProperty(SERVER_PROPERTIES::SZR_VESION, "0.7.217"); if ($message->mysql) { $event = $this->onHostUp($message, $dbserver, true); } } $handle_status = MESSAGE_STATUS::HANDLED; } catch (Exception $e) { $handle_status = MESSAGE_STATUS::FAILED; $logger->error(sprintf("Cannot handle message '%s' (message_id: %s) " . "from server '%s' (server_id: %s). %s", $message->getName(), $message->messageId, $dbserver->remoteIp ? $dbserver->remoteIp : '*no-ip*', $dbserver->serverId, $e->getMessage() . "({$e->getFile()}:{$e->getLine()})")); } if (!$doNotProcessMessage) { $totalTime = microtime(true) - $startTime; $this->db->Execute("\n UPDATE messages\n SET status = ?, processing_time = ?, dtlasthandleattempt = NOW()\n WHERE messageid = ?\n ", array($handle_status, $totalTime, $message->messageId)); } else { $logger->info(sprintf("Handle message '%s' (message_id: %s) " . "from server '%s' (server_id: %s) is postponed due to status transition", $message->getName(), $message->messageId, $dbserver->remoteIp ? $dbserver->remoteIp : '*no-ip*', $dbserver->serverId)); } if ($event instanceof \AbstractServerEvent) { \Scalr::FireEvent($dbserver->farmId, $event); } } catch (Exception $e) { $logger->error($e->getMessage()); } } return $request; }
/** * Returns instance type id * * @return mixed */ public function getInstanceType() { switch ($this->Platform) { case SERVER_PLATFORMS::EC2: $name = Entity\FarmRoleSetting::AWS_INSTANCE_TYPE; break; case SERVER_PLATFORMS::GCE: $name = Entity\FarmRoleSetting::GCE_MACHINE_TYPE; break; case SERVER_PLATFORMS::RACKSPACE: $name = Entity\FarmRoleSetting::RS_FLAVOR_ID; break; case SERVER_PLATFORMS::AZURE: $name = Entity\FarmRoleSetting::SETTING_AZURE_VM_SIZE; break; default: if (PlatformFactory::isOpenstack($this->Platform)) { $name = Entity\FarmRoleSetting::OPENSTACK_FLAVOR_ID; } else { if (PlatformFactory::isCloudstack($this->Platform)) { $name = Entity\FarmRoleSetting::CLOUDSTACK_SERVICE_OFFERING_ID; } } } return $this->GetSetting($name); }
/** * xGetPlatformInstanceTypesAction * * @param string $platform The name of the cloud platform * @param string $cloudLocation The cloud location * @param string $envId optional The identifier of the environment * @param string $effectiveDate optional The date on which prices should be applied YYYY-MM-DD * @throws \Exception */ public function xGetPlatformInstanceTypesAction($platform, $cloudLocation, $envId = null, $effectiveDate = null) { list($curDate, $effectiveDate) = $this->handleEffectiveDate($effectiveDate); $pm = PlatformFactory::NewPlatform($platform); $env = null; $url = ''; if (!empty($envId)) { $env = Scalr_Environment::init()->loadById($envId); //TODO the key should be retrieved from the method which is provisioned by interface if (PlatformFactory::isOpenstack($platform)) { $key = $platform . '.' . OpenstackPlatformModule::KEYSTONE_URL; } else { if (PlatformFactory::isCloudstack($platform)) { $key = $platform . '.' . CloudstackPlatformModule::API_URL; } else { if ($platform == SERVER_PLATFORMS::EUCALYPTUS) { $key = EucalyptusPlatformModule::EC2_URL; $url = $this->getContainer()->analytics->prices->normalizeUrl($env->getPlatformConfigValue($key, false, $cloudLocation)); } else { throw new Exception('This action is not yet supported for the specified cloud platform.'); } } } if (empty($url)) { $url = $this->getContainer()->analytics->prices->normalizeUrl($env->getPlatformConfigValue($key)); } } $result = $this->getTypesWithPrices($cloudLocation, $url, $pm, $platform, $effectiveDate, $env); $this->response->data(['data' => $result]); }
public function getBaseConfiguration(DBServer $dbServer, $isHostInit = false, $onlyBase = false) { $configuration = new stdClass(); $dbFarmRole = $dbServer->GetFarmRoleObject(); //Storage if (!$onlyBase) { try { if ($dbFarmRole) { $storage = new FarmRoleStorage($dbFarmRole); $volumes = $storage->getVolumesConfigs($dbServer, $isHostInit); if (!empty($volumes)) { $configuration->volumes = $volumes; } } } catch (Exception $e) { $this->logger->error(new FarmLogMessage($dbServer->farmId, "Cannot init storage: {$e->getMessage()}")); } } // Base try { if ($dbFarmRole) { $scriptingLogTimeout = $dbFarmRole->GetSetting(self::ROLE_BASE_KEEP_SCRIPTING_LOGS_TIME); if (!$scriptingLogTimeout) { $scriptingLogTimeout = 3600; } $configuration->base = new stdClass(); $configuration->base->keepScriptingLogsTime = $scriptingLogTimeout; $configuration->base->abortInitOnScriptFail = (int) $dbFarmRole->GetSetting(self::ROLE_BASE_ABORT_INIT_ON_SCRIPT_FAIL); $configuration->base->disableFirewallManagement = (int) $dbFarmRole->GetSetting(self::ROLE_BASE_DISABLE_FIREWALL_MANAGEMENT); $configuration->base->rebootAfterHostinitPhase = (int) $dbFarmRole->GetSetting(self::ROLE_BASE_REBOOT_AFTER_HOSTINIT_PHASE); $configuration->base->resumeStrategy = PlatformFactory::NewPlatform($dbFarmRole->Platform)->getResumeStrategy(); //Dev falgs for our if (Scalr::isHostedScalr() && $dbServer->envId == 3414) { $configuration->base->unionScriptExecutor = 1; } $governance = new Scalr_Governance($dbFarmRole->GetFarmObject()->EnvID); if ($governance->isEnabled(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_HOSTNAME_FORMAT)) { $hostNameFormat = $governance->getValue(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_HOSTNAME_FORMAT); } else { $hostNameFormat = $dbFarmRole->GetSetting(self::ROLE_BASE_HOSTNAME_FORMAT); } $configuration->base->hostname = !empty($hostNameFormat) ? $dbServer->applyGlobalVarsToValue($hostNameFormat) : ''; if ($configuration->base->hostname != '') { $dbServer->SetProperty(self::SERVER_BASE_HOSTNAME, $configuration->base->hostname); } $apiPort = null; $messagingPort = null; if (!PlatformFactory::isCloudstack($dbFarmRole->Platform)) { $apiPort = $dbFarmRole->GetSetting(self::ROLE_BASE_API_PORT); $messagingPort = $dbFarmRole->GetSetting(self::ROLE_BASE_MESSAGING_PORT); } $configuration->base->apiPort = $apiPort ? $apiPort : 8010; $configuration->base->messagingPort = $messagingPort ? $messagingPort : 8013; } //Update settings $updateSettings = $dbServer->getScalarizrRepository(); $configuration->base->update = new stdClass(); foreach ($updateSettings as $k => $v) { $configuration->base->update->{$k} = $v; } } catch (Exception $e) { } return $configuration; }
public function xSaveCloudParamsAction() { $platform = $this->getParam('platform'); if (PlatformFactory::isCloudstack($platform)) { $method = SERVER_PLATFORMS::CLOUDSTACK; } elseif (PlatformFactory::isOpenstack($platform)) { $method = SERVER_PLATFORMS::OPENSTACK; } else { $method = $platform; } $method = 'save' . ucfirst($method); if (method_exists($this, $method)) { $this->{$method}(); $suspensionInfo = new CloudPlatformSuspensionInfo($this->env->id, $platform); $suspensionInfo->resume(); $this->response->data(array('params' => $this->getCloudParams($platform))); } else { $this->response->failure('Under construction ...'); } }
public function _provider($from, $to, $action) { switch ($action) { case static::ACT_CONVERT_TO_OBJECT: /* @var $from Entity\CloudCredentials */ if (PlatformFactory::isOpenstack($from->cloud, true) || PlatformFactory::isCloudstack($from->cloud)) { $to->provider = $from->cloud; } break; case static::ACT_CONVERT_TO_ENTITY: /* @var $to Entity\CloudCredentials */ break; case static::ACT_GET_FILTER_CRITERIA: if (!(PlatformFactory::isOpenstack($from->provider, true) || PlatformFactory::isCloudstack($from->provider))) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Unknown cloud provider"); } return [['cloud' => $from->provider]]; } }
public function isCloudstack() { return PlatformFactory::isCloudstack($this->platform); }
private function getPlatformService($platform, $cloudLocation) { if ($platform == SERVER_PLATFORMS::EC2 || $platform == SERVER_PLATFORMS::EUCALYPTUS) { $method = $platform == SERVER_PLATFORMS::EC2 ? 'aws' : 'eucalyptus'; return $this->getEnvironment()->{$method}($cloudLocation)->ec2->securityGroup; } elseif (PlatformFactory::isOpenstack($platform)) { $openstack = $this->getEnvironment()->openstack($platform, $cloudLocation); return $openstack; } elseif (PlatformFactory::isCloudstack($platform)) { return $this->getEnvironment()->cloudstack($platform)->securityGroup; } else { throw new Exception('Platform is not supported'); } }
/** * xGetPlatformInstanceTypesAction * * @param string $platform The name of the cloud platform * @param string $cloudLocation The cloud location * @param string $envId optional The identifier of the environment * @param string $effectiveDate optional The date on which prices should be applied YYYY-MM-DD * @throws \Exception */ public function xGetPlatformInstanceTypesAction($platform, $cloudLocation, $envId = null, $effectiveDate = null) { list($curDate, $effectiveDate) = $this->handleEffectiveDate($effectiveDate); $pm = PlatformFactory::NewPlatform($platform); $env = null; $url = ''; try { if (!empty($envId)) { $env = Scalr_Environment::init()->loadById($envId); if (PlatformFactory::isOpenstack($platform)) { $key = Entity\CloudCredentialsProperty::OPENSTACK_KEYSTONE_URL; } else { if (PlatformFactory::isCloudstack($platform)) { $key = Entity\CloudCredentialsProperty::CLOUDSTACK_API_URL; } else { throw new Exception('This action is not yet supported for the specified cloud platform.'); } } if (empty($url)) { $url = $env->keychain($platform)->properties[$key]; } } else { if ($platform == SERVER_PLATFORMS::EC2 || $platform == SERVER_PLATFORMS::GCE) { $gcenvid = $this->getEnvIdByPlatform($platform); $env = Scalr_Environment::init()->loadById($gcenvid); } } } catch (Exception $e) { if (stristr($e->getMessage(), 'not found')) { //Tries to find url from the cloud_locations table if (empty($url) && (PlatformFactory::isOpenstack($platform) || PlatformFactory::isCloudstack($platform))) { $clEntity = CloudLocation::findOne([['platform' => $platform], ['cloudLocation' => $cloudLocation]], null, ['updated' => false]); if ($clEntity instanceof CloudLocation) { $url = $clEntity->url; } } } else { throw $e; } } $result = $this->getTypesWithPrices($cloudLocation, $url, $pm, $platform, $effectiveDate, $env); $this->response->data(['data' => $result]); }
public function FarmAddRole($Alias, $FarmID, $RoleID, $Platform, $CloudLocation, array $Configuration = array()) { try { $dbFarm = DBFarm::LoadByID($FarmID); if ($dbFarm->EnvID != $this->Environment->id) { throw new Exception("N"); } } catch (Exception $e) { throw new Exception(sprintf("Farm #%s not found", $FarmID)); } $this->user->getPermissions()->validate($dbFarm); $this->restrictFarmAccess($dbFarm, Acl::PERM_FARMS_MANAGE); $dbFarm->isLocked(true); $governance = new Scalr_Governance($this->Environment->id); $dbRole = DBRole::loadById($RoleID); if ($dbRole->envId != 0) { $this->user->getPermissions()->validate($dbRole); } foreach ($dbRole->getBehaviors() as $behavior) { if ($behavior != ROLE_BEHAVIORS::BASE && $behavior != ROLE_BEHAVIORS::CHEF) { throw new Exception("Only base roles supported to be added to farm via API"); } } $config = array('scaling.enabled' => 0, 'scaling.min_instances' => 1, 'scaling.max_instances' => 1, 'scaling.polling_interval' => 2, 'system.timeouts.launch' => 9600, 'system.timeouts.reboot' => 9600); if (PlatformFactory::isOpenstack($Platform)) { //TODO: } if ($Platform == SERVER_PLATFORMS::EC2) { $config['aws.security_groups.list'] = json_encode(array('default', \Scalr::config('scalr.aws.security_group_name'))); $vpcId = $dbFarm->GetSetting(DBFarm::SETTING_EC2_VPC_ID); if ($vpcId) { if (!$Configuration['aws.vpc_subnet_id']) { throw new Exception("Farm configured to run inside VPC. 'aws.vpc_subnet_id' is required"); } $vpcRegion = $dbFarm->GetSetting(DBFarm::SETTING_EC2_VPC_REGION); if ($CloudLocation != $vpcRegion) { throw new Exception(sprintf("Farm configured to run inside VPC in %s region. Only roles in this region are allowed.", $vpcRegion)); } $vpcGovernance = $governance->getValue('ec2', 'aws.vpc'); $vpcGovernanceIds = $governance->getValue('ec2', 'aws.vpc', 'ids'); $subnets = json_decode($Configuration['aws.vpc_subnet_id'], true); if (count($subnets) == 0) { throw new Exception("Subnets list is empty or json is incorrect"); } $type = false; foreach ($subnets as $subnetId) { $platform = PlatformFactory::NewPlatform(SERVER_PLATFORMS::EC2); $info = $platform->listSubnets($this->Environment, $CloudLocation, $vpcId, true, $subnetId); if (substr($info['availability_zone'], 0, -1) != $vpcRegion) { throw new Exception(sprintf("Only subnets from %s region are allowed according to VPC settings", $vpcRegion)); } if ($vpcGovernance == 1) { // Check valid subnets if ($vpcGovernanceIds[$vpcId] && is_array($vpcGovernanceIds[$vpcId]) && !in_array($subnetId, $vpcGovernanceIds[$vpcId])) { throw new Exception(sprintf("Only %s subnet(s) allowed by governance settings", implode(', ', $vpcGovernanceIds[$vpcId]))); } // Check if subnets types if ($vpcGovernanceIds[$vpcId] == "outbound-only") { if ($info['type'] != 'private') { throw new Exception("Only private subnets allowed by governance settings"); } } if ($vpcGovernanceIds[$vpcId] == "full") { if ($info['type'] != 'public') { throw new Exception("Only public subnets allowed by governance settings"); } } } if (!$type) { $type = $info['type']; } else { if ($type != $info['type']) { throw new Exception("Mix of public and private subnets are not allowed. Please specify only public or only private subnets."); } } } } } if (PlatformFactory::isCloudstack($Platform)) { $config['cloudstack.security_groups.list'] = json_encode(array('default', \Scalr::config('scalr.aws.security_group_name'))); } if ($Platform == SERVER_PLATFORMS::GCE) { $config['gce.network'] = 'default'; $config['gce.on-host-maintenance'] = 'MIGRATE'; } if ($Configuration[Scalr_Role_Behavior_Chef::ROLE_CHEF_BOOTSTRAP] == 1 && !$Configuration[Scalr_Role_Behavior_Chef::ROLE_CHEF_ENVIRONMENT]) { $config[Scalr_Role_Behavior_Chef::ROLE_CHEF_ENVIRONMENT] = '_default'; } $config = array_merge($config, $Configuration); $this->validateFarmRoleConfiguration($config); if ($Platform == SERVER_PLATFORMS::GCE) { $config['gce.cloud-location'] = $CloudLocation; $config['gce.region'] = substr($CloudLocation, 0, -1); } $Alias = $this->stripValue($Alias); if (strlen($Alias) < 4) { throw new Exception("Role Alias should be longer than 4 characters"); } if (!preg_match("/^[A-Za-z0-9]+[A-Za-z0-9-]*[A-Za-z0-9]+\$/si", $Alias)) { throw new Exception("Alias should start and end with letter or number and contain only letters, numbers and dashes."); } if (!$this->Environment->isPlatformEnabled($Platform)) { throw new Exception("'{$Platform}' cloud is not configured in your environment"); } $images = $dbRole->__getNewRoleObject()->fetchImagesArray(); $locations = isset($images[$Platform]) ? array_keys($images[$Platform]) : []; if (!in_array($CloudLocation, $locations) && $Platform != SERVER_PLATFORMS::GCE) { throw new Exception(sprintf("Role '%s' doesn't have an image configured for cloud location '%s'", $dbRole->name, $CloudLocation)); } if ($Alias) { foreach ($dbFarm->GetFarmRoles() as $farmRole) { if ($farmRole->Alias == $Alias) { throw new Exception("Selected alias is already used by another role in selected farm"); } } } $dbFarmRole = $dbFarm->AddRole($dbRole, $Platform, $CloudLocation, 1); $dbFarmRole->Alias = $Alias ? $Alias : $dbRole->name; foreach ($config as $k => $v) { $dbFarmRole->SetSetting($k, trim($v), DBFarmRole::TYPE_CFG); } foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $behavior) { $behavior->onFarmSave($dbFarm, $dbFarmRole); } $dbFarmRole->Save(); $response = $this->CreateInitialResponse(); $response->FarmRoleID = $dbFarmRole->ID; return $response; }
/** * Gets a normalized url for an each platform * * @param string $platform Cloud platform * @return string Returns url */ public function getUrl($platform) { if (!isset($this->aUrl[$platform])) { if ($platform == \SERVER_PLATFORMS::EC2) { $value = ''; } else { if (PlatformFactory::isOpenstack($platform)) { $value = CloudLocation::normalizeUrl($this->env->getPlatformConfigValue($platform . '.' . OpenstackPlatformModule::KEYSTONE_URL)); } else { if (PlatformFactory::isCloudstack($platform)) { $value = CloudLocation::normalizeUrl($this->env->getPlatformConfigValue($platform . '.' . CloudstackPlatformModule::API_URL)); } else { if ($platform == \SERVER_PLATFORMS::GCE) { $value = ''; } } } } $this->aUrl[$platform] = $value; } return $this->aUrl[$platform]; }
public function getInstanceType() { switch ($this->Platform) { case SERVER_PLATFORMS::EC2: $name = self::SETTING_AWS_INSTANCE_TYPE; break; case SERVER_PLATFORMS::EUCALYPTUS: $name = self::SETTING_EUCA_INSTANCE_TYPE; break; case SERVER_PLATFORMS::GCE: $name = self::SETTING_GCE_MACHINE_TYPE; break; case SERVER_PLATFORMS::RACKSPACE: $name = self::SETTING_RS_FLAVOR_ID; break; default: if (PlatformFactory::isOpenstack($this->Platform)) { $name = self::SETTING_OPENSTACK_FLAVOR_ID; } else { if (PlatformFactory::isCloudstack($this->Platform)) { $name = self::SETTING_CLOUDSTACK_SERVICE_OFFERING_ID; } } } return $this->GetSetting($name); }