本文整理汇总了PHP中Scalr::LaunchServer方法的典型用法代码示例。如果您正苦于以下问题:PHP Scalr::LaunchServer方法的具体用法?PHP Scalr::LaunchServer怎么用?PHP Scalr::LaunchServer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Scalr
的用法示例。
在下文中一共展示了Scalr::LaunchServer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: startForking
/**
* {@inheritdoc}
* @see Scalr_System_Cronjob_MultiProcess_DefaultWorker::startForking()
*/
function startForking($workQueue)
{
// Reopen DB connection after daemonizing
$this->db = $this->getContainer()->adodb;
// Check that time has come to cleanup dead servers
$doCleanup = false;
sem_acquire($this->cleanupSem);
try {
if (time() - (int) $this->lastCleanup->get(0) >= $this->cleanupInterval) {
$doCleanup = true;
$this->lastCleanup->put(0, time());
}
} catch (Exception $e) {
sem_release($this->cleanupSem);
}
sem_release($this->cleanupSem);
if ($doCleanup) {
$this->logger->info("Cleanup dead servers");
try {
$importing_servers = $this->db->GetAll("\n SELECT server_id FROM servers\n WHERE status IN(?, ?)\n AND `dtadded` < NOW() - INTERVAL 1 DAY\n ", array(SERVER_STATUS::IMPORTING, SERVER_STATUS::TEMPORARY));
foreach ($importing_servers as $ts) {
$dbServer = DBServer::LoadByID($ts['server_id']);
if ($dbServer->status == SERVER_STATUS::TEMPORARY) {
try {
$dbServer->terminate(DBServer::TERMINATE_REASON_TEMPORARY_SERVER_ROLE_BUILDER);
} catch (Exception $e) {
}
} else {
if ($dbServer->status == SERVER_STATUS::IMPORTING) {
$dbServer->Remove();
}
}
}
$pending_launch_servers = $this->db->GetAll("SELECT server_id FROM servers WHERE status=?", array(SERVER_STATUS::PENDING_LAUNCH));
try {
foreach ($pending_launch_servers as $ts) {
$DBServer = DBServer::LoadByID($ts['server_id']);
if ($DBServer->status == SERVER_STATUS::PENDING_LAUNCH) {
$account = Scalr_Account::init()->loadById($DBServer->clientId);
if ($account->status == Scalr_Account::STATUS_ACTIVE) {
Scalr::LaunchServer(null, $DBServer);
}
}
}
} catch (Exception $e) {
Logger::getLogger(LOG_CATEGORY::FARM)->error(sprintf("Can't load server with ID #'%s'", $ts['server_id'], $e->getMessage()));
}
} catch (Exception $e) {
$this->logger->fatal("Poller::cleanup failed: {$e->getMessage()}");
}
}
}
示例2: xLaunchNewServerAction
public function xLaunchNewServerAction()
{
$dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId'));
$dbFarm = $dbFarmRole->GetFarmObject();
$this->user->getPermissions()->validate($dbFarmRole);
if ($dbFarm->Status != FARM_STATUS::RUNNING) {
throw new Exception("You can launch servers only on running farms");
}
$dbRole = $dbFarmRole->GetRoleObject();
if ($dbRole->hasBehavior(ROLE_BEHAVIORS::VPC_ROUTER)) {
throw new Exception("Manual launch of VPC Router insatnces is not allowed");
}
$pendingInstancesCount = $dbFarmRole->GetPendingInstancesCount();
$maxInstances = $dbFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES);
$minInstances = $dbFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES);
if ($maxInstances < $minInstances + 1) {
$dbFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES, $maxInstances + 1, DBFarmRole::TYPE_CFG);
$warnMsg = sprintf(_("Server count has been increased. Scalr will now request a new server from your cloud. Since the server count was already at the maximum set for this role, we increased the maximum by one."), $dbRole->name, $dbRole->name);
}
$runningInstancesCount = $dbFarmRole->GetRunningInstancesCount();
if ($runningInstancesCount + $pendingInstancesCount >= $minInstances) {
$dbFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES, $minInstances + 1, DBFarmRole::TYPE_CFG);
}
$serverCreateInfo = new ServerCreateInfo($dbFarmRole->Platform, $dbFarmRole);
Scalr::LaunchServer($serverCreateInfo, null, false, DBServer::LAUNCH_REASON_MANUALLY, $this->user);
if ($warnMsg) {
$this->response->warning($warnMsg);
} else {
$this->response->success('Server successfully launched');
}
}
示例3: xLaunchNewServerAction
/**
* Launches new server
* @param int $farmRoleId
* @param bool $increaseMinInstances
* @param bool $needConfirmation
* @throws Scalr_Exception_Core
*/
public function xLaunchNewServerAction($farmRoleId, $increaseMinInstances = false, $needConfirmation = true)
{
$dbFarmRole = DBFarmRole::LoadByID($farmRoleId);
$dbFarm = $dbFarmRole->GetFarmObject();
$this->user->getPermissions()->validate($dbFarmRole);
$this->request->restrictFarmAccess($dbFarm, Acl::PERM_FARMS_SERVERS);
if ($dbFarm->Status != FARM_STATUS::RUNNING) {
throw new Scalr_Exception_Core('You can launch servers only on running farms');
}
$dbRole = $dbFarmRole->GetRoleObject();
if ($dbRole->hasBehavior(ROLE_BEHAVIORS::VPC_ROUTER)) {
throw new Scalr_Exception_Core('Manual launch of VPC Router insatnces is not allowed');
}
if ($dbFarmRole->GetSetting(Entity\FarmRoleSetting::SCALING_ENABLED) == 1) {
$scalingManager = new Scalr_Scaling_Manager($dbFarmRole);
$scalingMetrics = $scalingManager->getFarmRoleMetrics();
$hasScalingMetrics = count($scalingMetrics) > 0;
$curInstances = $dbFarmRole->GetPendingInstancesCount() + $dbFarmRole->GetRunningInstancesCount();
$maxInstances = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::SCALING_MAX_INSTANCES);
$minInstances = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::SCALING_MIN_INSTANCES);
if ($needConfirmation) {
$res = ['showConfirmation' => true];
if ($maxInstances < $curInstances + 1) {
$res['showIncreaseMaxInstancesWarning'] = true;
$res['maxInstances'] = $maxInstances + 1;
}
if ($hasScalingMetrics && $curInstances >= $minInstances) {
$res['showIncreaseMinInstancesConfirm'] = true;
}
$this->response->data($res);
return;
} else {
if ($maxInstances < $curInstances + 1) {
$dbFarmRole->SetSetting(Entity\FarmRoleSetting::SCALING_MAX_INSTANCES, $maxInstances + 1, Entity\FarmRoleSetting::TYPE_CFG);
}
if ($increaseMinInstances && $hasScalingMetrics && $curInstances >= $minInstances) {
$dbFarmRole->SetSetting(Entity\FarmRoleSetting::SCALING_MIN_INSTANCES, $minInstances + 1, Entity\FarmRoleSetting::TYPE_CFG);
}
}
}
$serverCreateInfo = new ServerCreateInfo($dbFarmRole->Platform, $dbFarmRole);
Scalr::LaunchServer($serverCreateInfo, null, false, DBServer::LAUNCH_REASON_MANUALLY, $this->user);
$this->response->success('Server successfully launched');
}
示例4: StartThread
//.........这里部分代码省略.........
} else {
$servers = $db->GetAll("SELECT server_id FROM servers WHERE farm_roleid = ? AND role_id=? AND status NOT IN (?,?)", array($DBFarmRole->ID, $DBFarmRole->RoleID, SERVER_STATUS::TERMINATED, SERVER_STATUS::PENDING_TERMINATE));
$BundleTask->Log(sprintf("Found %s servers that need to be replaced with new ones. Role '%s' (ID: %s), farm '%s' (ID: %s)", count($servers), $DBFarmRole->GetRoleObject()->name, $DBFarmRole->ID, $DBFarmRole->GetFarmObject()->Name, $DBFarmRole->FarmID));
if (count($servers) == 0) {
$DBFarmRole->RoleID = $DBFarmRole->NewRoleID;
$DBFarmRole->NewRoleID = null;
$DBFarmRole->Save();
$update_farm_dns_zones[$DBFarmRole->FarmID] = 1;
$completed_roles++;
} else {
$metaData = $BundleTask->getSnapshotDetails();
foreach ($servers as $server) {
try {
$DBServer = DBServer::LoadByID($server['server_id']);
} catch (Exception $e) {
//TODO:
continue;
}
if ($DBServer->serverId == $BundleTask->serverId || $metaData['noServersReplace']) {
$DBServer->roleId = $BundleTask->roleId;
$DBServer->Save();
if ($metaData['noServersReplace']) {
$BundleTask->Log(sprintf("'Do not replace servers' option was checked. Server '%s' won't be replaced to new image.", $DBServer->serverId));
} else {
$BundleTask->Log(sprintf("Server '%s', on which snapshot has been taken, already has all modifications. No need to replace it.", $DBServer->serverId));
}
if ($DBServer->GetFarmObject()->Status == FARM_STATUS::SYNCHRONIZING) {
PlatformFactory::NewPlatform($DBServer->platform)->TerminateServer($DBServer);
$db->Execute("UPDATE servers_history SET\r\n\t\t\t\t\t\t\t\t\t\t\t\tdtterminated\t= NOW(),\r\n\t\t\t\t\t\t\t\t\t\t\t\tterminate_reason\t= ?\r\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE server_id = ?\r\n\t\t\t\t\t\t\t\t\t\t\t", array(sprintf("Farm was in 'Synchronizing' state. Server terminated when bundling was completed. Bundle task #%s", $BundleTask->id), $DBServer->serverId));
}
} else {
if (!$db->GetOne("SELECT server_id FROM servers WHERE replace_server_id=? AND status NOT IN (?,?)", array($DBServer->serverId, SERVER_STATUS::TERMINATED, SERVER_STATUS::PENDING_TERMINATE))) {
$ServerCreateInfo = new ServerCreateInfo($DBFarmRole->Platform, $DBFarmRole, $DBServer->index, $DBFarmRole->NewRoleID);
$nDBServer = Scalr::LaunchServer($ServerCreateInfo);
$nDBServer->replaceServerID = $DBServer->serverId;
$nDBServer->Save();
$BundleTask->Log(sprintf(_("Started new server %s to replace server %s"), $nDBServer->serverId, $DBServer->serverId));
}
}
// if serverid != bundletask->serverID
}
// foreach server
}
// count($servers)
}
}
if ($completed_roles == count($r_farm_roles)) {
$BundleTask->Log(sprintf(_("No servers with old role. Replacement complete. Bundle task complete."), SERVER_REPLACEMENT_TYPE::NO_REPLACE, SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS));
$BundleTask->setDate('finished');
$BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
$BundleTask->Save();
}
try {
if (count($update_farm_dns_zones) != 0) {
foreach ($update_farm_dns_zones as $farm_id => $v) {
$dnsZones = DBDNSZone::loadByFarmId($farm_id);
foreach ($dnsZones as $dnsZone) {
if ($dnsZone->status != DNS_ZONE_STATUS::INACTIVE && $dnsZone->status != DNS_ZONE_STATUS::PENDING_DELETE) {
$dnsZone->updateSystemRecords();
$dnsZone->save();
}
}
}
}
} catch (Exception $e) {
$this->Logger->fatal("DNS ZONE: {$e->getMessage()}");
示例5: ServerLaunch
public function ServerLaunch($FarmRoleID, $IncreaseMaxInstances = false)
{
try {
$DBFarmRole = DBFarmRole::LoadByID($FarmRoleID);
$DBFarm = DBFarm::LoadByID($DBFarmRole->FarmID);
} catch (Exception $e) {
throw new Exception(sprintf("Farm Role ID #%s not found", $FarmRoleID));
}
if ($DBFarm->EnvID != $this->Environment->id) {
throw new Exception(sprintf("Farm Role ID #%s not found", $FarmRoleID));
}
if ($DBFarm->Status != FARM_STATUS::RUNNING) {
throw new Exception(sprintf("Farm ID #%s is not running", $DBFarm->ID));
}
$this->user->getPermissions()->validate($DBFarm);
$this->restrictFarmAccess($DBFarm, Acl::PERM_FARMS_SERVERS);
$isSzr = true;
$n = $DBFarmRole->GetPendingInstancesCount();
if ($n >= 5 && !$isSzr) {
throw new Exception("There are {$n} pending instances. You cannot launch new instances while you have 5 pending ones.");
}
$response = $this->CreateInitialResponse();
$max_instances = $DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES);
$min_instances = $DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES);
if ($IncreaseMaxInstances) {
if ($max_instances < $min_instances + 1) {
$DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES, $max_instances + 1, DBFarmRole::TYPE_CFG);
}
}
if ($DBFarmRole->GetRunningInstancesCount() + $DBFarmRole->GetPendingInstancesCount() >= $max_instances) {
if ($IncreaseMaxInstances) {
$DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES, $max_instances + 1, DBFarmRole::TYPE_CFG);
} else {
throw new Exception("Max instances limit reached. Use 'IncreaseMaxInstances' parameter or increase max isntances settings in UI");
}
}
if ($DBFarmRole->GetRunningInstancesCount() + $DBFarmRole->GetPendingInstancesCount() == $min_instances) {
$DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES, $min_instances + 1, DBFarmRole::TYPE_CFG);
}
$ServerCreateInfo = new ServerCreateInfo($DBFarmRole->Platform, $DBFarmRole);
try {
$DBServer = Scalr::LaunchServer($ServerCreateInfo, null, false, DBServer::LAUNCH_REASON_MANUALLY_API, $this->user);
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Starting new instance (API). ServerID = %s.", $DBServer->serverId), $DBServer->serverId));
} catch (Exception $e) {
Logger::getLogger(LOG_CATEGORY::API)->error($e->getMessage());
}
$response->ServerID = $DBServer->serverId;
$response->CloudServerID = $DBServer->GetCloudServerID();
return $response;
}
示例6: xLaunchNewServerAction
public function xLaunchNewServerAction()
{
$dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId'));
$dbFarm = $dbFarmRole->GetFarmObject();
$this->user->getPermissions()->validate($dbFarmRole);
if ($dbFarm->Status != FARM_STATUS::RUNNING) {
throw new Exception("You can launch servers only on running farms");
}
$dbRole = $dbFarmRole->GetRoleObject();
$isSzr = $dbFarmRole->GetRoleObject()->isSupported("0.5");
$pendingInstancesCount = $dbFarmRole->GetPendingInstancesCount();
if ($pendingInstancesCount >= 5 && !$isSzr) {
throw new Exception("There are {$pendingInstancesCount} pending instances. You cannot launch new instances while you have 5 pending ones.");
}
$maxInstances = $dbFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES);
$minInstances = $dbFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES);
if ($maxInstances < $minInstances + 1) {
$dbFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES, $maxInstances + 1);
$warnMsg = sprintf(_("Server successfully launched. The number of running %s instances is equal to maximum instances setting for this role. Maximum Instances setting for role %s has been increased automatically"), $dbRole->name, $dbRole->name);
}
$runningInstancesCount = $dbFarmRole->GetRunningInstancesCount();
if ($runningInstancesCount + $pendingInstancesCount >= $minInstances) {
$dbFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES, $minInstances + 1);
}
$serverCreateInfo = new ServerCreateInfo($dbFarmRole->Platform, $dbFarmRole);
Scalr::LaunchServer($serverCreateInfo);
if ($warnMsg) {
$this->response->warning($warnMsg);
} else {
$this->response->success('Server successfully launched');
}
}
示例7: handleWork
//.........这里部分代码省略.........
* I.e. LA may skyrocket during sync and drop dramatically after sync.
*/
if ($DBServer->dateLastSync != 0) {
$chk_sync_time = $this->db->GetOne("SELECT server_id FROM servers \n\t \t\tWHERE dtlastsync > {$DBServer->dateLastSync} \n\t\t \tAND farm_roleid='{$DBServer->farmRoleId}' AND status != '" . SERVER_STATUS::TERMINATED . "'");
if ($chk_sync_time) {
$got_valid_instance = true;
}
} else {
$got_valid_instance = true;
}
}
}
if ($DBServer && $got_valid_instance) {
$this->logger->info(sprintf("Server '%s' selected for termination...", $DBServer->serverId));
$allow_terminate = false;
if ($DBServer->platform == SERVER_PLATFORMS::EC2) {
$AmazonEC2Client = Scalr_Service_Cloud_Aws::newEc2($DBServer->GetProperty(EC2_SERVER_PROPERTIES::REGION), $DBServer->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::PRIVATE_KEY), $DBServer->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::CERTIFICATE));
// Shutdown an instance just before a full hour running
$response = $AmazonEC2Client->DescribeInstances($DBServer->GetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID));
if ($response && $response->reservationSet->item) {
$launch_time = strtotime($response->reservationSet->item->instancesSet->item->launchTime);
$time = 3600 - (time() - $launch_time) % 3600;
// Terminate instance in < 10 minutes for full hour.
if ($time <= 600) {
$allow_terminate = true;
} else {
$timeout = round(($time - 600) / 60, 1);
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Farm %s, role %s scaling down. Server '%s' will be terminated in %s minutes. Launch time: %s", $DBFarm->Name, $DBServer->GetFarmRoleObject()->GetRoleObject()->name, $DBServer->serverId, $timeout, $response->reservationSet->item->instancesSet->item->launchTime)));
}
}
//
} else {
$allow_terminate = true;
}
if ($allow_terminate) {
//Check safe shutdown
if ($DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_SCALING_SAFE_SHUTDOWN) == 1) {
$snmpClient = new Scalr_Net_Snmp_Client();
$port = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_SNMP_PORT);
$snmpClient->connect($DBServer->remoteIp, $port ? $port : 161, $DBFarm->Hash, null, null, false);
$res = $snmpClient->get('1.3.6.1.4.1.36632.6.1');
if ($res != '1') {
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Safe shutdown enabled. Server '%s'. Script return '%s', server won't be terminated while return value not '1'", $DBServer->serverId, $res)));
}
}
try {
Scalr::FireEvent($DBFarm->ID, new BeforeHostTerminateEvent($DBServer, false));
$DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_DOWNSCALE_DATETIME, time());
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Farm %s, role %s scaling down. Server '%s' marked as 'Pending terminate' and will be fully terminated in 3 minutes.", $DBFarm->Name, $DBServer->GetFarmRoleObject()->GetRoleObject()->name, $DBServer->serverId)));
} catch (Exception $e) {
$this->logger->fatal(sprintf("Cannot terminate %s: %s", $DBFarm->ID, $DBServer->serverId, $e->getMessage()));
}
}
} else {
$this->logger->warn(sprintf("[FarmID: {$DBFarm->ID}] Scalr unable to determine what instance it should terminate (FarmRoleID: {$DBFarmRole->ID}). Skipping..."));
}
break;
} elseif ($scalingDecision == Scalr_Scaling_Decision::UPSCALE) {
/*
Timeout instance's count increase. Increases instance's count after
scaling resolution �need more instances� for selected timeout interval
from scaling EditOptions
*/
if ($DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_UPSCALE_TIMEOUT_ENABLED)) {
// if the farm timeout is exceeded
// checking timeout interval.
$last_up_scale_data_time = $DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_UPSCALE_DATETIME);
$timeout_interval = $DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_UPSCALE_TIMEOUT);
// check the time interval to continue scaling or cancel it...
if (time() - $last_up_scale_data_time < $timeout_interval * 60) {
// if the launch time is too small to terminate smth in this role -> go to the next role in foreach()
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Waiting for upscaling timeout on farm %s, role %s", $DBFarm->Name, $DBFarmRole->GetRoleObject()->name)));
continue 2;
}
}
// end Timeout instance's count increase
if ($DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_ONE_BY_ONE) == 1) {
$pendingInstances = $DBFarmRole->GetPendingInstancesCount();
if ($pendingInstances > 0) {
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("There are %s pending intances of %s role on % farm. Waiting...", $pendingInstances, $DBFarmRole->GetRoleObject()->name, $DBFarm->Name)));
continue 2;
}
}
$fstatus = $this->db->GetOne("SELECT status FROM farms WHERE id=?", array($DBFarm->ID));
if ($fstatus != FARM_STATUS::RUNNING) {
$this->logger->warn("[FarmID: {$DBFarm->ID}] Farm terminated. There is no need to scale it.");
return;
}
$ServerCreateInfo = new ServerCreateInfo($DBFarmRole->Platform, $DBFarmRole);
try {
$DBServer = Scalr::LaunchServer($ServerCreateInfo);
$DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_UPSCALE_DATETIME, time());
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Farm %s, role %s scaling up. Starting new instance. ServerID = %s.", $DBFarm->Name, $DBServer->GetFarmRoleObject()->GetRoleObject()->name, $DBServer->serverId)));
} catch (Exception $e) {
Logger::getLogger(LOG_CATEGORY::SCALING)->error($e->getMessage());
}
}
}
}
}
示例8: worker
/**
* {@inheritdoc}
* @see \Scalr\System\Zmq\Cron\TaskInterface::worker()
*/
public function worker($request)
{
//It must be done for any worker in daemon mode. (Policy engine, PM, etc.. settings shouldn't be cached)
\Scalr::getContainer()->warmup();
try {
$dbServer = DBServer::LoadByID($request->serverId);
if ($dbServer->status == SERVER_STATUS::TEMPORARY) {
try {
$dbServer->terminate(DBServer::TERMINATE_REASON_TEMPORARY_SERVER_ROLE_BUILDER);
} catch (Exception $e) {
}
} else {
if ($dbServer->status == SERVER_STATUS::IMPORTING) {
$dbServer->Remove();
} else {
if ($dbServer->status == SERVER_STATUS::PENDING_LAUNCH) {
$account = Scalr_Account::init()->loadById($dbServer->clientId);
if ($account->status == Scalr_Account::STATUS_ACTIVE) {
\Scalr::LaunchServer(null, $dbServer);
}
}
}
}
} catch (Exception $e) {
$this->getLogger()->error("Server: %s, manager failed with exception: %s", $request->serverId, $e->getMessage());
}
return $request;
}
示例9: OnFarmLaunched
/**
* Farm launched
*
* @param FarmLaunchedEvent $event
*/
public function OnFarmLaunched(FarmLaunchedEvent $event)
{
$DBFarm = DBFarm::LoadByID($this->FarmID);
// TODO: Refactoting -> Move to DBFarm class
$this->DB->Execute("UPDATE farms SET status=?, dtlaunched=NOW() WHERE id=?", array(FARM_STATUS::RUNNING, $this->FarmID));
$governance = new Scalr_Governance($DBFarm->EnvID);
if ($governance->isEnabled(Scalr_Governance::GENERAL_LEASE) && $DBFarm->GetSetting(DBFarm::SETTING_LEASE_STATUS)) {
$dt = new DateTime();
$dt->add(new DateInterval('P' . intval($governance->getValue(Scalr_Governance::GENERAL_LEASE, 'defaultLifePeriod')) . 'D'));
$DBFarm->SetSetting(DBFarm::SETTING_LEASE_EXTEND_CNT, 0);
$DBFarm->SetSetting(DBFarm::SETTING_LEASE_TERMINATE_DATE, $dt->format('Y-m-d H:i:s'));
$DBFarm->SetSetting(DBFarm::SETTING_LEASE_NOTIFICATION_SEND, '');
}
$roles = $DBFarm->GetFarmRoles();
foreach ($roles as $dbFarmRole) {
if ($dbFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_ENABLED) && !$DBFarm->GetSetting(DBFarm::SETTING_EC2_VPC_ID)) {
$scalingManager = new Scalr_Scaling_Manager($dbFarmRole);
$scalingDecision = $scalingManager->makeScalingDecition();
if ($scalingDecision == Scalr_Scaling_Decision::UPSCALE) {
$ServerCreateInfo = new ServerCreateInfo($dbFarmRole->Platform, $dbFarmRole);
try {
$DBServer = Scalr::LaunchServer($ServerCreateInfo, null, true, "Farm launched", isset($event->userId) ? $event->userId : null);
$dbFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_UPSCALE_DATETIME, time(), DBFarmRole::TYPE_LCL);
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Farm %s, role %s scaling up. Starting new instance. ServerID = %s.", $DBFarm->Name, $dbFarmRole->GetRoleObject()->name, $DBServer->serverId)));
} catch (Exception $e) {
Logger::getLogger(LOG_CATEGORY::SCALING)->error($e->getMessage());
}
}
}
}
}
示例10: handleWork
//.........这里部分代码省略.........
}
} else {
$allow_terminate = true;
}
//Releases memory
$DBServer->GetEnvironmentObject()->getContainer()->release('aws');
unset($aws);
} else {
$allow_terminate = true;
}
if ($allow_terminate) {
//Check safe shutdown
if ($DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_SCALING_SAFE_SHUTDOWN) == 1) {
if ($DBServer->IsSupported('0.11.3')) {
try {
$port = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_API_PORT);
if (!$port) {
$port = 8010;
}
$szrClient = Scalr_Net_Scalarizr_Client::getClient($DBServer, Scalr_Net_Scalarizr_Client::NAMESPACE_SYSTEM, $port);
$res = $szrClient->callAuthShutdownHook();
} catch (Exception $e) {
$res = $e->getMessage();
}
} else {
Logger::getLogger(LOG_CATEGORY::FARM)->error(new FarmLogMessage($DBFarm->ID, sprintf("Safe shutdown enabled, but not supported by scalarizr installed on server '%s'. Ignoring.", $DBServer->serverId)));
}
if ($res != '1') {
Logger::getLogger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($DBFarm->ID, sprintf("Safe shutdown enabled. Server '%s'. Script returned '%s', server won't be terminated while return value not '1'", $DBServer->serverId, $res)));
break;
}
}
try {
$DBServer->terminate('SCALING_DOWN', false);
$DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_DOWNSCALE_DATETIME, time(), DBFarmRole::TYPE_LCL);
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Farm %s, role %s scaling down. Server '%s' marked as 'Pending terminate' and will be fully terminated in 3 minutes.", $DBFarm->Name, $DBServer->GetFarmRoleObject()->GetRoleObject()->name, $DBServer->serverId)));
} catch (Exception $e) {
$this->logger->fatal(sprintf("Cannot terminate %s: %s", $DBFarm->ID, $DBServer->serverId, $e->getMessage()));
}
}
} else {
$this->logger->warn(sprintf("[FarmID: %s] Scalr unable to determine what instance it should terminate (FarmRoleID: %s). Skipping...", $DBFarm->ID, $DBFarmRole->ID));
}
break;
} elseif ($scalingDecision == Scalr_Scaling_Decision::UPSCALE) {
/*
Timeout instance's count increase. Increases instance's count after
scaling resolution �need more instances� for selected timeout interval
from scaling EditOptions
*/
if ($DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_UPSCALE_TIMEOUT_ENABLED)) {
// if the farm timeout is exceeded
// checking timeout interval.
$last_up_scale_data_time = $DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_UPSCALE_DATETIME);
$timeout_interval = $DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_UPSCALE_TIMEOUT);
// check the time interval to continue scaling or cancel it...
if (time() - $last_up_scale_data_time < $timeout_interval * 60) {
// if the launch time is too small to terminate smth in this role -> go to the next role in foreach()
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Waiting for upscaling timeout on farm %s, role %s", $DBFarm->Name, $DBFarmRole->GetRoleObject()->name)));
continue 2;
}
}
// end Timeout instance's count increase
//Check DBMsr. Do not start slave during slave2master process
$isDbMsr = $DBFarmRole->GetRoleObject()->getDbMsrBehavior();
if ($isDbMsr) {
if ($DBFarmRole->GetSetting(Scalr_Db_Msr::SLAVE_TO_MASTER)) {
$runningServers = $DBFarmRole->GetRunningInstancesCount();
if ($runningServers > 0) {
Logger::getLogger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($DBFarm->ID, sprintf("Role is in slave2master promotion process. Do not launch new slaves while there is no active slaves")));
continue 2;
} else {
$DBFarmRole->SetSetting(Scalr_Db_Msr::SLAVE_TO_MASTER, 0, DBFarmRole::TYPE_LCL);
}
}
}
if ($DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_ONE_BY_ONE) == 1) {
$pendingInstances = $DBFarmRole->GetPendingInstancesCount();
if ($pendingInstances > 0) {
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("There are %s pending intances of %s role on % farm. Waiting...", $pendingInstances, $DBFarmRole->GetRoleObject()->name, $DBFarm->Name)));
continue 2;
}
}
$fstatus = $this->db->GetOne("SELECT status FROM farms WHERE id=? LIMIT 1", array($DBFarm->ID));
if ($fstatus != FARM_STATUS::RUNNING) {
$this->logger->warn("[FarmID: {$DBFarm->ID}] Farm terminated. There is no need to scale it.");
return;
}
$ServerCreateInfo = new ServerCreateInfo($DBFarmRole->Platform, $DBFarmRole);
try {
$DBServer = Scalr::LaunchServer($ServerCreateInfo, null, false, "Scaling up");
$DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_UPSCALE_DATETIME, time(), DBFarmRole::TYPE_LCL);
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Farm %s, role %s scaling up. Starting new instance. ServerID = %s.", $DBFarm->Name, $DBServer->GetFarmRoleObject()->GetRoleObject()->name, $DBServer->serverId)));
} catch (Exception $e) {
Logger::getLogger(LOG_CATEGORY::SCALING)->error($e->getMessage());
}
}
}
}
}
示例11: OnFarmLaunched
/**
* Farm launched
*
* @param FarmLaunchedEvent $event
*/
public function OnFarmLaunched(FarmLaunchedEvent $event)
{
$DBFarm = DBFarm::LoadByID($this->FarmID);
// TODO: Refactoting -> Move to DBFarm class
$this->DB->Execute("UPDATE farms SET status=?, dtlaunched=NOW() WHERE id=?", array(FARM_STATUS::RUNNING, $this->FarmID));
$roles = $DBFarm->GetFarmRoles();
foreach ($roles as $dbFarmRole) {
$scalingManager = new Scalr_Scaling_Manager($dbFarmRole);
$scalingDecision = $scalingManager->makeScalingDecition();
if ($scalingDecision == Scalr_Scaling_Decision::UPSCALE) {
$ServerCreateInfo = new ServerCreateInfo($dbFarmRole->Platform, $dbFarmRole);
try {
$DBServer = Scalr::LaunchServer($ServerCreateInfo, null, true);
$dbFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_UPSCALE_DATETIME, time());
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Farm %s, role %s scaling up. Starting new instance. ServerID = %s.", $DBFarm->Name, $dbFarmRole->GetRoleObject()->name, $DBServer->serverId)));
} catch (Exception $e) {
Logger::getLogger(LOG_CATEGORY::SCALING)->error($e->getMessage());
}
}
}
}
示例12: StartThread
//.........这里部分代码省略.........
foreach ($r_farm_roles as $DBFarmRole) {
if ($DBFarmRole->CloudLocation != $BundleTask->cloudLocation) {
$BundleTask->Log(sprintf("Role '%s' (ID: %s), farm '%s' (ID: %s) using the same role " . "but in abother cloud location. Skiping it.", $DBFarmRole->GetRoleObject()->name, $DBFarmRole->ID, $DBFarmRole->GetFarmObject()->Name, $DBFarmRole->FarmID));
$completed_roles++;
} else {
$servers = $db->GetAll("SELECT server_id FROM servers WHERE farm_roleid = ? AND role_id=? AND status NOT IN (?,?)", array($DBFarmRole->ID, $DBFarmRole->RoleID, SERVER_STATUS::TERMINATED, SERVER_STATUS::PENDING_TERMINATE));
$BundleTask->Log(sprintf("Found %s servers that need to be replaced with new ones. " . "Role '%s' (ID: %s), farm '%s' (ID: %s)", count($servers), $DBFarmRole->GetRoleObject()->name, $DBFarmRole->ID, $DBFarmRole->GetFarmObject()->Name, $DBFarmRole->FarmID));
if (count($servers) == 0) {
$DBFarmRole->RoleID = $DBFarmRole->NewRoleID;
$DBFarmRole->NewRoleID = null;
$DBFarmRole->Save();
$update_farm_dns_zones[$DBFarmRole->FarmID] = 1;
$completed_roles++;
} else {
$metaData = $BundleTask->getSnapshotDetails();
foreach ($servers as $server) {
try {
$DBServer = DBServer::LoadByID($server['server_id']);
} catch (Exception $e) {
//TODO:
continue;
}
if ($DBServer->serverId == $BundleTask->serverId || $metaData['noServersReplace']) {
$DBServer->roleId = $BundleTask->roleId;
$DBServer->Save();
if ($metaData['noServersReplace']) {
$BundleTask->Log(sprintf("'Do not replace servers' option was checked. " . "Server '%s' won't be replaced to new image.", $DBServer->serverId));
} else {
$BundleTask->Log(sprintf("Server '%s', on which snapshot has been taken, " . "already has all modifications. No need to replace it.", $DBServer->serverId));
}
} else {
if (!$db->GetOne("SELECT server_id FROM servers WHERE replace_server_id=? AND status NOT IN (?,?,?) LIMIT 1", array($DBServer->serverId, SERVER_STATUS::TERMINATED, SERVER_STATUS::PENDING_TERMINATE, SERVER_STATUS::TROUBLESHOOTING, SERVER_STATUS::SUSPENDED))) {
$ServerCreateInfo = new ServerCreateInfo($DBFarmRole->Platform, $DBFarmRole, $DBServer->index, $DBFarmRole->NewRoleID);
$nDBServer = Scalr::LaunchServer($ServerCreateInfo, null, false, DBServer::LAUNCH_REASON_REPLACE_SERVER_FROM_SNAPSHOT, !empty($BundleTask->createdById) ? $BundleTask->createdById : null);
$nDBServer->replaceServerID = $DBServer->serverId;
$nDBServer->Save();
$BundleTask->Log(sprintf(_("Started new server %s to replace server %s"), $nDBServer->serverId, $DBServer->serverId));
}
}
}
}
}
}
if ($completed_roles == count($r_farm_roles)) {
$BundleTask->Log(sprintf(_("No servers with old role. Replacement complete. Bundle task complete."), SERVER_REPLACEMENT_TYPE::NO_REPLACE, SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS));
try {
if ($DBServer->status == SERVER_STATUS::IMPORTING) {
$DBServer->Remove();
} elseif ($DBServer->status == SERVER_STATUS::TEMPORARY) {
$BundleTask->Log("Terminating temporary server");
$DBServer->terminate(DBServer::TERMINATE_REASON_TEMPORARY_SERVER_ROLE_BUILDER);
$BundleTask->Log("Termination request has been sent");
}
} catch (Exception $e) {
$BundleTask->Log("Warning: {$e->getMessage()}");
}
$BundleTask->setDate('finished');
$BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
$BundleTask->Save();
$BundleTask->createImageEntity();
}
try {
if (count($update_farm_dns_zones) != 0) {
foreach ($update_farm_dns_zones as $farm_id => $v) {
$dnsZones = DBDNSZone::loadByFarmId($farm_id);
foreach ($dnsZones as $dnsZone) {
示例13: ServerLaunch
public function ServerLaunch($FarmRoleID, $IncreaseMaxInstances = false)
{
try {
$DBFarmRole = DBFarmRole::LoadByID($FarmRoleID);
$DBFarm = DBFarm::LoadByID($DBFarmRole->FarmID);
} catch (Exception $e) {
throw new Exception(sprintf("Farm Role ID #%s not found", $FarmRoleID));
}
if ($DBFarm->EnvID != $this->Environment->id) {
throw new Exception(sprintf("Farm Role ID #%s not found", $FarmRoleID));
}
//TODO: Remove this limitation
/*
$isSzr = $dbFarmRole->GetRoleObject()->isSupported("0.5");
if ($retval == Scalr_Scaling_Decision::UPSCALE && ($dbFarmRole->GetPendingInstancesCount() > 5 || !$isSzr))
*/
$isSzr = $DBFarmRole->GetRoleObject()->isSupported("0.5");
$n = $DBFarmRole->GetPendingInstancesCount();
if ($n >= 5 && !$isSzr) {
throw new Exception("There are {$n} pending instances. You cannot launch new instances while you have 5 pending ones.");
}
$response = $this->CreateInitialResponse();
$max_instances = $DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES);
$min_instances = $DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES);
if ($IncreaseMaxInstances) {
if ($max_instances < $min_instances + 1) {
$DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES, $max_instances + 1);
}
}
if ($DBFarmRole->GetRunningInstancesCount() + $DBFarmRole->GetPendingInstancesCount() >= $max_instances) {
if ($IncreaseMaxInstances) {
$DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES, $max_instances + 1);
} else {
throw new Exception("Max instances limit reached. Use 'IncreaseMaxInstances' parameter or increase max isntances settings in UI");
}
}
if ($DBFarmRole->GetRunningInstancesCount() < $min_instances || $DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES) < $min_instances) {
$DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES, $min_instances + 1);
}
$ServerCreateInfo = new ServerCreateInfo($DBFarmRole->Platform, $DBFarmRole);
try {
$DBServer = Scalr::LaunchServer($ServerCreateInfo);
Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Starting new instance (API). ServerID = %s.", $DBServer->serverId)));
} catch (Exception $e) {
Logger::getLogger(LOG_CATEGORY::API)->error($e->getMessage());
}
$response->ServerID = $DBServer->serverId;
return $response;
}
示例14: worker
/**
* {@inheritdoc}
* @see \Scalr\System\Zmq\Cron\TaskInterface::worker()
*/
public function worker($request)
{
try {
$dbServer = DBServer::LoadByID($request->serverId);
if ($dbServer->status == SERVER_STATUS::TEMPORARY) {
try {
$dbServer->terminate(DBServer::TERMINATE_REASON_TEMPORARY_SERVER_ROLE_BUILDER);
} catch (Exception $e) {
}
} else {
if ($dbServer->status == SERVER_STATUS::IMPORTING) {
$dbServer->Remove();
} else {
if ($dbServer->status == SERVER_STATUS::PENDING_LAUNCH) {
$account = Scalr_Account::init()->loadById($dbServer->clientId);
if ($account->status == Scalr_Account::STATUS_ACTIVE) {
\Scalr::LaunchServer(null, $dbServer);
}
}
}
}
} catch (Exception $e) {
$this->getLogger()->error("Server: %s, manager failed with exception: %s", $request->serverId, $e->getMessage());
}
return $request;
}
示例15: cleanup
private function cleanup()
{
// Check that time has come to cleanup dead servers
$doCleanup = false;
sem_acquire($this->cleanupSem);
try {
if (time() - (int) $this->lastCleanup->get(0) >= $this->cleanupInterval) {
$doCleanup = true;
$this->lastCleanup->put(0, time());
}
} catch (Exception $e) {
sem_release($this->cleanupSem);
}
sem_release($this->cleanupSem);
if ($doCleanup) {
$this->logger->info("Cleanup dead servers");
try {
$terminated_servers = $this->db->GetAll("SELECT server_id FROM servers WHERE status=? AND (UNIX_TIMESTAMP(dtshutdownscheduled)+3600 < UNIX_TIMESTAMP(NOW()) OR dtshutdownscheduled IS NULL)", array(SERVER_STATUS::TERMINATED));
foreach ($terminated_servers as $ts) {
DBServer::LoadByID($ts['server_id'])->Remove();
}
$p_terminated_servers = $this->db->GetAll("SELECT server_id FROM servers WHERE status=? AND role_id='0' AND farm_id IS NULL", array(SERVER_STATUS::PENDING_TERMINATE));
foreach ($p_terminated_servers as $ts) {
DBServer::LoadByID($ts['server_id'])->Remove();
}
$importing_servers = $this->db->GetAll("SELECT server_id FROM servers WHERE status IN(?,?) AND UNIX_TIMESTAMP(dtadded)+86400 < UNIX_TIMESTAMP(NOW())", array(SERVER_STATUS::IMPORTING, SERVER_STATUS::TEMPORARY));
foreach ($importing_servers as $ts) {
DBServer::LoadByID($ts['server_id'])->Remove();
}
$pending_launch_servers = $this->db->GetAll("SELECT server_id FROM servers WHERE status=?", array(SERVER_STATUS::PENDING_LAUNCH));
try {
foreach ($pending_launch_servers as $ts) {
$DBServer = DBServer::LoadByID($ts['server_id']);
$account = Scalr_Account::init()->loadById($DBServer->clientId);
if ($account->status == Scalr_Account::STATUS_ACTIVE) {
Scalr::LaunchServer(null, $DBServer);
}
}
} catch (Exception $e) {
Logger::getLogger(LOG_CATEGORY::FARM)->error(sprintf("Can't load server with ID #'%s'", $ts['server_id'], $e->getMessage()));
}
} catch (Exception $e) {
$this->logger->fatal("Poller::cleanup failed: {$e->getMessage()}");
}
}
}