getListForFarmRole() 공개 정적인 메소드

..
public static getListForFarmRole ( DBFarmRole $farmRole ) : Scalr_Role_Behavior[]
$farmRole DBFarmRole
리턴 Scalr_Role_Behavior[]
예제 #1
0
 public function OnHostDown(\HostDownEvent $event)
 {
     $dbServer = $event->DBServer;
     if ($dbServer->farmRoleId != 0) {
         try {
             $dbFarmRole = $dbServer->GetFarmRoleObject();
         } catch (\Exception $e) {
             return false;
         }
         foreach (\Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $bObj) {
             $bObj->onHostDown($dbServer, $event);
         }
         //Storage
         if (!$event->isSuspended) {
             try {
                 $storage = new FarmRoleStorage($dbFarmRole);
                 $storageConfigs = $storage->getConfigs();
                 if (empty($storageConfigs)) {
                     return true;
                 }
                 foreach ($storageConfigs as $config) {
                     //Check for existing volume
                     $dbVolume = FarmRoleStorageDevice::getByConfigIdAndIndex($config->id, $dbServer->index);
                     if ($dbVolume && !$config->reUse) {
                         $dbVolume->status = FarmRoleStorageDevice::STATUS_ZOMBY;
                         $dbVolume->save();
                     }
                 }
             } catch (\Exception $e) {
                 $this->logger->error(new \FarmLogMessage($dbServer, "Marking storage for disposal failed: {$e->getMessage()}"));
             }
         }
     }
 }
 public function OnFarmTerminated(FarmTerminatedEvent $event)
 {
     $dbFarm = DBFarm::LoadByID($this->FarmID);
     foreach ($dbFarm->GetFarmRoles() as $dbFarmRole) {
         foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $bObj) {
             $bObj->onFarmTerminated($dbFarmRole);
         }
     }
 }
예제 #3
0
 public function OnHostDown(HostDownEvent $event)
 {
     $dbServer = $event->DBServer;
     if ($dbServer->farmRoleId != 0) {
         try {
             $dbFarmRole = $dbServer->GetFarmRoleObject();
         } catch (Exception $e) {
             return false;
         }
         foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $bObj) {
             $bObj->onHostDown($dbServer);
         }
     }
 }
예제 #4
0
 public function xTroubleshootAction()
 {
     $this->request->defineParams(array('serverId'));
     $dbServer = DBServer::LoadByID($this->getParam('serverId'));
     $this->user->getPermissions()->validate($dbServer);
     $dbServer->status = SERVER_STATUS::TROUBLESHOOTING;
     $dbServer->Save();
     // Send before host terminate to the server to detach all used volumes.
     $msg = new Scalr_Messaging_Msg_BeforeHostTerminate($dbServer);
     if ($dbServer->farmRoleId != 0) {
         foreach (Scalr_Role_Behavior::getListForFarmRole($dbServer->GetFarmRoleObject()) as $behavior) {
             $msg = $behavior->extendMessage($msg, $dbServer);
         }
     }
     $dbServer->SendMessage($msg);
     Scalr::FireEvent($dbServer->farmId, new HostDownEvent($dbServer));
     $this->response->success();
 }
예제 #5
0
 /**
  *
  * @return Scalr_Scaling_Decision
  */
 function makeScalingDecition()
 {
     /*
             Base Scaling
     */
     foreach (Scalr_Role_Behavior::getListForFarmRole($this->dbFarmRole) as $behavior) {
         $result = $behavior->makeUpscaleDecision($this->dbFarmRole);
         if ($result === false) {
             continue;
         } else {
             return $result;
         }
     }
     $farm_pending_instances = $this->db->GetOne("SELECT COUNT(*) FROM servers WHERE farm_id=? AND status IN (?,?,?)", array($this->dbFarmRole->FarmID, SERVER_STATUS::PENDING, SERVER_STATUS::INIT, SERVER_STATUS::PENDING_LAUNCH));
     if ($this->dbFarmRole->GetFarmObject()->RolesLaunchOrder == 1 && $farm_pending_instances > 0) {
         if ($this->dbFarmRole->GetRunningInstancesCount() == 0) {
             $this->logger->info("{$farm_pending_instances} instances in pending state. Launch roles one-by-one. Waiting...");
             return Scalr_Scaling_Decision::STOP_SCALING;
         }
     }
     /*
      Metrics scaling
     */
     foreach ($this->getFarmRoleMetrics() as $farmRoleMetric) {
         $scalingMetricDecision = $farmRoleMetric->getScalingDecision();
         $this->logger->info(sprintf(_("Metric: %s. Decision: %s. Last value: %s"), $farmRoleMetric->getMetric()->name, $scalingMetricDecision, $farmRoleMetric->lastValue));
         $scalingMetricName = $farmRoleMetric->getMetric()->name;
         $scalingMetricInstancesCount = null;
         if ($scalingMetricName == 'DateAndTime') {
             $scalingMetricInstancesCount = $farmRoleMetric->instancesNumber;
         }
         $this->decisonInfo = '1';
         if ($scalingMetricDecision == Scalr_Scaling_Decision::NOOP) {
             continue;
         }
         Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($this->dbFarmRole->FarmID, sprintf("%s: Role '%s' on farm '%s'. Metric name: %s. Last metric value: %s.", $scalingMetricDecision, $this->dbFarmRole->Alias ? $this->dbFarmRole->Alias : $this->dbFarmRole->GetRoleObject()->name, $this->dbFarmRole->GetFarmObject()->Name, $farmRoleMetric->getMetric()->name, $farmRoleMetric->lastValue)));
         if ($scalingMetricDecision != Scalr_Scaling_Decision::NOOP) {
             break;
         }
     }
     $isDbMsr = $this->dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MYSQL) || $this->dbFarmRole->GetRoleObject()->getDbMsrBehavior();
     $needOneByOneLaunch = $this->dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::RABBITMQ) || $this->dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MONGODB);
     // Check do we need upscale to min instances count
     $roleTotalInstances = $this->dbFarmRole->GetRunningInstancesCount() + $this->dbFarmRole->GetPendingInstancesCount();
     $maxInstances = $this->dbFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MAX_INSTANCES);
     $minInstances = $this->dbFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES);
     if ($roleTotalInstances < $minInstances) {
         if ($needOneByOneLaunch) {
             $pendingTerminateInstances = count($this->dbFarmRole->GetServersByFilter(array('status' => SERVER_STATUS::PENDING_TERMINATE)));
             // If we launching DbMSR instances. Master should be running.
             if ($this->dbFarmRole->GetPendingInstancesCount() == 0 && !$pendingTerminateInstances) {
                 $this->logger->info(_("Increasing number of running instances to fit min instances setting"));
                 $this->decisonInfo = '2';
                 return Scalr_Scaling_Decision::UPSCALE;
             } else {
                 $this->logger->info(_("Found servers in Pending or PendingTerminate state. Waiting..."));
                 return Scalr_Scaling_Decision::NOOP;
             }
         } elseif ($isDbMsr) {
             // If we launching DbMSR instances. Master should be running.
             if ($this->dbFarmRole->GetRunningInstancesCount() > 0 || $this->dbFarmRole->GetPendingInstancesCount() == 0) {
                 $this->logger->info(_("Increasing number of running instances to fit min instances setting"));
                 $this->decisonInfo = '3';
                 return Scalr_Scaling_Decision::UPSCALE;
             } else {
                 $this->logger->info(_("Waiting for running master"));
                 return Scalr_Scaling_Decision::NOOP;
             }
         } else {
             $this->logger->info(_("Increasing number of running instances to fit min instances setting"));
             $this->decisonInfo = '4';
             return Scalr_Scaling_Decision::UPSCALE;
         }
     } elseif ($maxInstances && $this->dbFarmRole->GetRunningInstancesCount() > $maxInstances) {
         // Need to check Date&Time based scaling. Otherwise Scalr downscale role every time.
         if ($scalingMetricInstancesCount) {
             if ($this->dbFarmRole->GetRunningInstancesCount() > $scalingMetricInstancesCount) {
                 $this->logger->info(_("Decreasing number of running instances to fit DateAndTime scaling settings ({$scalingMetricInstancesCount})"));
                 $this->decisonInfo = '5';
                 return Scalr_Scaling_Decision::DOWNSCALE;
             }
         } else {
             $this->logger->info(_("Decreasing number of running instances to fit max instances setting ({$scalingMetricInstancesCount})"));
             $this->decisonInfo = '6';
             return Scalr_Scaling_Decision::DOWNSCALE;
         }
     }
     return $scalingMetricDecision ? $scalingMetricDecision : Scalr_Scaling_Decision::NOOP;
 }
예제 #6
0
 /**
  * @param Scalr_Messaging_Msg $message
  * @param DBServer $dbserver
  */
 private function onHostUp($message, $dbserver, $skipStatusCheck = false)
 {
     if ($dbserver->status == SERVER_STATUS::INIT || $skipStatusCheck) {
         $event = new HostUpEvent($dbserver, "");
         $dbFarmRole = $dbserver->GetFarmRoleObject();
         foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $behavior) {
             $behavior->handleMessage($message, $dbserver);
         }
         //TODO: Move MySQL to MSR
         /****** MOVE TO MSR ******/
         //TODO: Legacy MySQL code
         if ($dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MYSQL)) {
             if (!$message->mysql) {
                 $this->logger->error(sprintf("Strange situation. HostUp message from MySQL behavior doesn't contains `mysql` property. Server %s (%s)", $dbserver->serverId, $dbserver->remoteIp));
                 return;
             }
             $mysqlData = $message->mysql;
             if ($dbserver->GetProperty(SERVER_PROPERTIES::DB_MYSQL_MASTER)) {
                 if ($mysqlData->rootPassword) {
                     $dbFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_REPL_PASSWORD, $mysqlData->replPassword);
                     $dbFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_ROOT_PASSWORD, $mysqlData->rootPassword);
                     $dbFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_STAT_PASSWORD, $mysqlData->statPassword);
                 }
                 $dbFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_LOG_FILE, $mysqlData->logFile);
                 $dbFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_LOG_POS, $mysqlData->logPos);
                 if ($dbserver->IsSupported("0.7")) {
                     //$dbFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_SNAPSHOT_ID, $mysqlData->snapshotConfig);
                     //$dbFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_SNAPSHOT_ID, $mysqlData->volumeConfig);
                     if ($mysqlData->volumeConfig) {
                         try {
                             $storageVolume = Scalr_Storage_Volume::init();
                             try {
                                 $storageVolume->loadById($mysqlData->volumeConfig->id);
                                 $storageVolume->setConfig($mysqlData->volumeConfig);
                                 $storageVolume->save();
                             } catch (Exception $e) {
                                 if (strpos($e->getMessage(), 'not found')) {
                                     $storageVolume->loadBy(array('id' => $mysqlData->volumeConfig->id, 'client_id' => $dbserver->clientId, 'env_id' => $dbserver->envId, 'name' => "MySQL data volume", 'type' => $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_DATA_STORAGE_ENGINE), 'platform' => $dbserver->platform, 'size' => $mysqlData->volumeConfig->size, 'fstype' => $mysqlData->volumeConfig->fstype, 'purpose' => ROLE_BEHAVIORS::MYSQL, 'farm_roleid' => $dbserver->farmRoleId, 'server_index' => $dbserver->index));
                                     $storageVolume->setConfig($mysqlData->volumeConfig);
                                     $storageVolume->save(true);
                                 } else {
                                     throw $e;
                                 }
                             }
                             $dbFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_SCALR_VOLUME_ID, $storageVolume->id);
                         } catch (Exception $e) {
                             $this->logger->error(new FarmLogMessage($event->DBServer->farmId, "Cannot save storage volume: {$e->getMessage()}"));
                         }
                     }
                     if ($mysqlData->snapshotConfig) {
                         try {
                             $storageSnapshot = Scalr_Storage_Snapshot::init();
                             try {
                                 $storageSnapshot->loadById($mysqlData->snapshotConfig->id);
                                 $storageSnapshot->setConfig($mysqlData->snapshotConfig);
                                 $storageSnapshot->save();
                             } catch (Exception $e) {
                                 if (strpos($e->getMessage(), 'not found')) {
                                     $storageSnapshot->loadBy(array('id' => $mysqlData->snapshotConfig->id, 'client_id' => $dbserver->clientId, 'farm_id' => $dbserver->farmId, 'farm_roleid' => $dbserver->farmRoleId, 'env_id' => $dbserver->envId, 'name' => sprintf(_("MySQL data bundle #%s"), $mysqlData->snapshotConfig->id), 'type' => $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_DATA_STORAGE_ENGINE), 'platform' => $dbserver->platform, 'description' => sprintf(_("MySQL data bundle created on Farm '%s' -> Role '%s'"), $dbFarmRole->GetFarmObject()->Name, $dbFarmRole->GetRoleObject()->name), 'ismysql' => true, 'service' => ROLE_BEHAVIORS::MYSQL));
                                     $storageSnapshot->setConfig($mysqlData->snapshotConfig);
                                     $storageSnapshot->save(true);
                                 } else {
                                     throw $e;
                                 }
                             }
                             $dbFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_SCALR_SNAPSHOT_ID, $storageSnapshot->id);
                         } catch (Exception $e) {
                             $this->logger->error(new FarmLogMessage($event->DBServer->farmId, "Cannot save storage snapshot: {$e->getMessage()}"));
                         }
                     }
                 } else {
                     /**
                      * @deprecated
                      */
                     $dbFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_SNAPSHOT_ID, $mysqlData->snapshotId);
                 }
             }
         }
         return $event;
     } else {
         $this->logger->error("Strange situation. Received HostUp message" . " from server '{$dbserver->serverId}' ('{$message->remoteIp})" . " with state {$dbserver->status}!");
     }
 }
예제 #7
0
 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;
 }
예제 #8
0
 private function getBehaviorsRecords(DBServer $dbServer)
 {
     $records = array();
     if ($dbServer->farmRoleId != 0) {
         foreach (Scalr_Role_Behavior::getListForFarmRole($dbServer->GetFarmRoleObject()) as $behavior) {
             $records = array_merge($records, (array) $behavior->getDnsRecords($dbServer));
         }
     }
     return $records;
 }
예제 #9
0
 public function FarmAddRole($Alias, $FarmID, $RoleID, $Platform, $CloudLocation, array $Configuration = array())
 {
     $this->restrictAccess(Acl::RESOURCE_FARMS, Acl::PERM_FARMS_MANAGE);
     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);
     $dbFarm->isLocked(true);
     $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')));
     }
     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);
     $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_-]+\$/", $Alias)) {
         throw new Exception("Role Alias should has only 'A-Za-z0-9-_' characters");
     }
     if (!PlatformFactory::isOpenstack($Platform) && $Platform != SERVER_PLATFORMS::EC2) {
         throw new Exception("Only EC2 and Openstack roles are supported by this method");
     }
     if (!$this->Environment->isPlatformEnabled($Platform)) {
         throw new Exception("'{$Platform}' cloud is not configured in your environment");
     }
     $locations = $dbRole->getCloudLocations($Platform);
     if (!in_array($CloudLocation, $locations)) {
         throw new Exception(sprintf("Role '%s' doesn't have 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, $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;
 }
예제 #10
0
 public function OnBeforeHostTerminate(\BeforeHostTerminateEvent $event)
 {
     try {
         $dbFarm = \DBFarm::LoadByID($this->FarmID);
     } catch (\Exception $e) {
     }
     if ($dbFarm) {
         $servers = $this->DB->Execute("SELECT * FROM servers WHERE farm_id = ? AND status IN (?,?,?,?)", array($dbFarm->ID, \SERVER_STATUS::INIT, \SERVER_STATUS::RUNNING, \SERVER_STATUS::PENDING_TERMINATE, \SERVER_STATUS::PENDING_SUSPEND));
         $event->messageLongestInsert = 0;
         while ($server = $servers->FetchRow()) {
             $DBServer = \DBServer::load($server);
             // We don't need to send beforeHostTerminate event to all "Pending terminate" servers,
             // only tu eventServer.
             if ($DBServer->status == \SERVER_STATUS::PENDING_TERMINATE) {
                 if ($DBServer->serverId != $event->DBServer->serverId) {
                     continue;
                 }
             }
             $msg = new \Scalr_Messaging_Msg_BeforeHostTerminate();
             $msg->setServerMetaData($event->DBServer);
             $msg->suspend = $event->suspend;
             $msg = \Scalr_Scripting_Manager::extendMessage($msg, $event, $event->DBServer, $DBServer);
             if ($event->DBServer->farmRoleId != 0) {
                 foreach (\Scalr_Role_Behavior::getListForFarmRole($event->DBServer->GetFarmRoleObject()) as $behavior) {
                     $msg = $behavior->extendMessage($msg, $event->DBServer);
                 }
             }
             $mt = microtime(true);
             $DBServer->SendMessage($msg, false, true);
             $mtResult = microtime(true) - $mt;
             if ($event->messageLongestInsert < $mtResult) {
                 $event->messageLongestInsert = $mtResult;
             }
         }
         try {
             if ($event->DBServer->GetFarmRoleObject()->GetSetting(\Scalr_Db_Msr::SLAVE_TO_MASTER) != 1) {
                 if ($event->DBServer->GetFarmRoleObject()->GetRoleObject()->getDbMsrBehavior()) {
                     $this->sendPromoteToMasterMessage($event);
                 }
             }
         } catch (\Exception $e) {
         }
     }
 }
예제 #11
0
 public function xBuildAction()
 {
     $this->request->defineParams(array('farmId' => array('type' => 'int'), 'roles' => array('type' => 'json'), 'farm' => array('type' => 'json'), 'roleUpdate' => array('type' => 'int'), 'launch' => array('type' => 'bool')));
     if (!$this->isFarmConfigurationValid($this->getParam('farmId'), $this->getParam('farm'), (array) $this->getParam('roles'))) {
         if ($this->errors['error_count'] != 0) {
             $this->response->failure();
             $this->response->data(array('errors' => $this->errors));
             return;
         }
     }
     $farm = $this->getParam('farm');
     $client = Client::Load($this->user->getAccountId());
     if ($this->getParam('farmId')) {
         $dbFarm = DBFarm::LoadByID($this->getParam('farmId'));
         $this->user->getPermissions()->validate($dbFarm);
         $this->request->restrictFarmAccess($dbFarm, Acl::PERM_FARMS_MANAGE);
         $dbFarm->isLocked();
         if ($this->getParam('changed') && $dbFarm->changedTime && $this->getParam('changed') != $dbFarm->changedTime) {
             $userName = '******';
             $changed = explode(' ', $this->getParam('changed'));
             $changedTime = intval($changed[1]);
             try {
                 $user = new Scalr_Account_User();
                 $user->loadById($dbFarm->changedByUserId);
                 $userName = $user->getEmail();
             } catch (Exception $e) {
             }
             $this->response->failure();
             $this->response->data(array('changedFailure' => sprintf('%s changed this farm at %s', $userName, Scalr_Util_DateTime::convertTz($changedTime))));
             return;
         }
         $dbFarm->changedByUserId = $this->user->getId();
         $dbFarm->changedTime = microtime();
         $bNew = false;
     } else {
         $this->request->restrictFarmAccess(null, Acl::PERM_FARMS_MANAGE);
         $this->user->getAccount()->validateLimit(Scalr_Limits::ACCOUNT_FARMS, 1);
         $dbFarm = new DBFarm();
         $dbFarm->ClientID = $this->user->getAccountId();
         $dbFarm->EnvID = $this->getEnvironmentId();
         $dbFarm->Status = FARM_STATUS::TERMINATED;
         $dbFarm->createdByUserId = $this->user->getId();
         $dbFarm->createdByUserEmail = $this->user->getEmail();
         $dbFarm->changedByUserId = $this->user->getId();
         $dbFarm->changedTime = microtime();
         $bNew = true;
     }
     if ($this->getParam('farm')) {
         $dbFarm->Name = $this->request->stripValue($farm['name']);
         $dbFarm->RolesLaunchOrder = $farm['rolesLaunchOrder'];
         $dbFarm->Comments = $this->request->stripValue($farm['description']);
     }
     if (empty($dbFarm->Name)) {
         throw new Exception(_("Farm name required"));
     }
     if ($bNew) {
         $dbFarm->teamId = is_numeric($farm['teamOwner']) && $farm['teamOwner'] > 0 ? $farm['teamOwner'] : NULL;
     } else {
         if ($dbFarm->createdByUserId == $this->user->getId() || $this->user->isAccountOwner() || $this->request->isFarmAllowed($dbFarm, Acl::PERM_FARMS_CHANGE_OWNERSHIP)) {
             if (is_numeric($farm['owner']) && $farm['owner'] != $dbFarm->createdByUserId) {
                 $user = (new Scalr_Account_User())->loadById($farm['owner']);
                 $dbFarm->createdByUserId = $user->getId();
                 $dbFarm->createdByUserEmail = $user->getEmail();
                 // TODO: move to subclass \Farm\Setting\OwnerHistory
                 $history = unserialize($dbFarm->GetSetting(DBFarm::SETTING_OWNER_HISTORY));
                 if (!is_array($history)) {
                     $history = [];
                 }
                 $history[] = ['newId' => $user->getId(), 'newEmail' => $user->getEmail(), 'changedById' => $this->user->getId(), 'changedByEmail' => $this->user->getEmail(), 'dt' => date('Y-m-d H:i:s')];
                 $dbFarm->SetSetting(DBFarm::SETTING_OWNER_HISTORY, serialize($history));
             }
             $dbFarm->teamId = is_numeric($farm['teamOwner']) && $farm['teamOwner'] > 0 ? $farm['teamOwner'] : NULL;
         }
     }
     $dbFarm->save();
     $governance = new Scalr_Governance($this->getEnvironmentId());
     if (!$this->getParam('farmId') && $governance->isEnabled(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_LEASE)) {
         $dbFarm->SetSetting(DBFarm::SETTING_LEASE_STATUS, 'Active');
         // for created farm
     }
     if (isset($farm['variables'])) {
         $variables = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(), Scalr_Scripting_GlobalVariables::SCOPE_FARM);
         $variables->setValues(is_array($farm['variables']) ? $farm['variables'] : [], 0, $dbFarm->ID, 0, '', false, true);
     }
     if (!$farm['timezone']) {
         $farm['timezone'] = date_default_timezone_get();
     }
     $dbFarm->SetSetting(DBFarm::SETTING_TIMEZONE, $farm['timezone']);
     $dbFarm->SetSetting(DBFarm::SETTING_EC2_VPC_ID, $farm['vpc_id']);
     $dbFarm->SetSetting(DBFarm::SETTING_EC2_VPC_REGION, $farm['vpc_region']);
     $dbFarm->SetSetting(DBFarm::SETTING_SZR_UPD_REPOSITORY, $farm[DBFarm::SETTING_SZR_UPD_REPOSITORY]);
     $dbFarm->SetSetting(DBFarm::SETTING_SZR_UPD_SCHEDULE, $farm[DBFarm::SETTING_SZR_UPD_SCHEDULE]);
     if (!$dbFarm->GetSetting(DBFarm::SETTING_CRYPTO_KEY)) {
         $dbFarm->SetSetting(DBFarm::SETTING_CRYPTO_KEY, Scalr::GenerateRandomKey(40));
     }
     if ($this->getContainer()->analytics->enabled) {
         //Cost analytics project must be set for the Farm object
         $dbFarm->setProject(!empty($farm['projectId']) ? $farm['projectId'] : null);
     }
     $virtualFarmRoles = array();
     $roles = $this->getParam('roles');
     if (!empty($roles)) {
         foreach ($roles as $role) {
             if (strpos($role['farm_role_id'], "virtual_") !== false) {
                 $dbRole = DBRole::loadById($role['role_id']);
                 $dbFarmRole = $dbFarm->AddRole($dbRole, $role['platform'], $role['cloud_location'], (int) $role['launch_index'], $role['alias']);
                 $virtualFarmRoles[$role['farm_role_id']] = $dbFarmRole->ID;
             }
         }
     }
     $usedPlatforms = array();
     $dbFarmRolesList = array();
     $newFarmRolesList = array();
     $farmRoleVariables = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(), Scalr_Scripting_GlobalVariables::SCOPE_FARMROLE);
     if (!empty($roles)) {
         foreach ($roles as $role) {
             if ($role['farm_role_id']) {
                 if ($virtualFarmRoles[$role['farm_role_id']]) {
                     $role['farm_role_id'] = $virtualFarmRoles[$role['farm_role_id']];
                 }
                 $update = true;
                 $dbFarmRole = DBFarmRole::LoadByID($role['farm_role_id']);
                 $dbRole = DBRole::loadById($dbFarmRole->RoleID);
                 $role['role_id'] = $dbFarmRole->RoleID;
                 if ($dbFarmRole->Platform == SERVER_PLATFORMS::GCE) {
                     $dbFarmRole->CloudLocation = $role['cloud_location'];
                 }
             } else {
                 $update = false;
                 $dbRole = DBRole::loadById($role['role_id']);
                 $dbFarmRole = $dbFarm->AddRole($dbRole, $role['platform'], $role['cloud_location'], (int) $role['launch_index']);
             }
             if ($dbRole->hasBehavior(ROLE_BEHAVIORS::RABBITMQ)) {
                 $role['settings'][DBFarmRole::SETTING_SCALING_MAX_INSTANCES] = $role['settings'][DBFarmRole::SETTING_SCALING_MIN_INSTANCES];
             }
             if ($dbFarmRole->NewRoleID) {
                 continue;
             }
             if ($update) {
                 $dbFarmRole->LaunchIndex = (int) $role['launch_index'];
                 $dbFarmRole->Alias = $role['alias'];
                 $dbFarmRole->Save();
             }
             $usedPlatforms[$role['platform']] = 1;
             $oldRoleSettings = $dbFarmRole->GetAllSettings();
             // Update virtual farm_role_id with actual value
             $scripts = (array) $role['scripting'];
             if (count($virtualFarmRoles) > 0) {
                 array_walk_recursive($scripts, function (&$v, $k) use($virtualFarmRoles) {
                     if (is_string($v)) {
                         $v = str_replace(array_keys($virtualFarmRoles), array_values($virtualFarmRoles), $v);
                     }
                 });
                 array_walk_recursive($role['settings'], function (&$v, $k) use($virtualFarmRoles) {
                     if (is_string($v)) {
                         $v = str_replace(array_keys($virtualFarmRoles), array_values($virtualFarmRoles), $v);
                     }
                 });
             }
             $dbFarmRole->ClearSettings("chef.");
             if (!empty($role['scaling_settings']) && is_array($role['scaling_settings'])) {
                 foreach ($role['scaling_settings'] as $k => $v) {
                     $dbFarmRole->SetSetting($k, $v, DBFarmRole::TYPE_CFG);
                 }
             }
             foreach ($role['settings'] as $k => $v) {
                 $dbFarmRole->SetSetting($k, $v, DBFarmRole::TYPE_CFG);
             }
             /****** Scaling settings ******/
             $scalingManager = new Scalr_Scaling_Manager($dbFarmRole);
             $scalingManager->setFarmRoleMetrics(is_array($role['scaling']) ? $role['scaling'] : array());
             //TODO: optimize this code...
             $this->db->Execute("DELETE FROM farm_role_scaling_times WHERE farm_roleid=?", array($dbFarmRole->ID));
             // 5 = Time based scaling -> move to constants
             if ($role['scaling'][5]) {
                 foreach ($role['scaling'][5] as $scal_period) {
                     $chunks = explode(":", $scal_period['id']);
                     $this->db->Execute("INSERT INTO farm_role_scaling_times SET\n                            farm_roleid\t\t= ?,\n                            start_time\t\t= ?,\n                            end_time\t\t= ?,\n                            days_of_week\t= ?,\n                            instances_count\t= ?\n                        ", array($dbFarmRole->ID, $chunks[0], $chunks[1], $chunks[2], $chunks[3]));
                 }
             }
             /*****************/
             /* Update role params */
             $dbFarmRole->SetParameters((array) $role['params']);
             /* End of role params management */
             /* Add script options to databse */
             $dbFarmRole->SetScripts($scripts, (array) $role['scripting_params']);
             /* End of scripting section */
             /* Add services configuration */
             $dbFarmRole->SetServiceConfigPresets((array) $role['config_presets']);
             /* End of scripting section */
             /* Add storage configuration */
             if (isset($role['storages'])) {
                 if (isset($role['storages']['configs'])) {
                     $dbFarmRole->getStorage()->setConfigs($role['storages']['configs']);
                 }
             }
             $farmRoleVariables->setValues(is_array($role['variables']) ? $role['variables'] : [], $dbFarmRole->GetRoleID(), $dbFarm->ID, $dbFarmRole->ID, '', false, true);
             foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $behavior) {
                 $behavior->onFarmSave($dbFarm, $dbFarmRole);
             }
             /**
              * Platform specified updates
              */
             if ($dbFarmRole->Platform == SERVER_PLATFORMS::EC2) {
                 \Scalr\Modules\Platforms\Ec2\Helpers\EbsHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
                 \Scalr\Modules\Platforms\Ec2\Helpers\EipHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
                 \Scalr\Modules\Platforms\Ec2\Helpers\ElbHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
             }
             if (in_array($dbFarmRole->Platform, array(SERVER_PLATFORMS::IDCF, SERVER_PLATFORMS::CLOUDSTACK))) {
                 Scalr\Modules\Platforms\Cloudstack\Helpers\CloudstackHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
             }
             $dbFarmRolesList[] = $dbFarmRole;
             $newFarmRolesList[] = $dbFarmRole->ID;
         }
     }
     if (!$this->getParam('roleUpdate')) {
         foreach ($dbFarm->GetFarmRoles() as $dbFarmRole) {
             if (!$dbFarmRole->NewRoleID && !in_array($dbFarmRole->ID, $newFarmRolesList)) {
                 $dbFarmRole->Delete();
             }
         }
     }
     $dbFarm->save();
     if (!$client->GetSettingValue(CLIENT_SETTINGS::DATE_FARM_CREATED)) {
         $client->SetSettingValue(CLIENT_SETTINGS::DATE_FARM_CREATED, time());
     }
     if ($this->request->isFarmAllowed($dbFarm, Acl::PERM_FARMS_LAUNCH_TERMINATE) && $this->getParam('launch')) {
         $this->user->getPermissions()->validate($dbFarm);
         $dbFarm->isLocked();
         Scalr::FireEvent($dbFarm->ID, new FarmLaunchedEvent(true, $this->user->id));
         $this->response->success('Farm successfully saved and launched');
     } else {
         $this->response->success('Farm successfully saved');
     }
     $this->response->data(array('farmId' => $dbFarm->ID, 'isNewFarm' => $bNew));
 }
예제 #12
0
 public function xBuildAction()
 {
     $this->request->defineParams(array('farmId' => array('type' => 'int'), 'roles' => array('type' => 'json'), 'farm' => array('type' => 'json'), 'roleUpdate' => array('type' => 'int')));
     $Validator = new Validator();
     $cloudFoundryStack = array();
     $nginxFound = 0;
     foreach ($this->getParam('roles') as $role) {
         $dbRole = DBRole::loadById($role['role_id']);
         if (!$dbRole->getImageId($role['platform'], $role['cloud_location'])) {
             throw new Exception(sprintf(_("Role '%s' is not available in %s on %s"), $dbRole->name, $role['platform'], $role['cloud_location']));
         }
         // Validate deployments
         $appId = $role[Scalr_Role_Behavior::ROLE_DM_APPLICATION_ID];
         if ($appId) {
             $application = Scalr_Dm_Application::init()->loadById($appId);
             $this->user->getPermissions()->validate($application);
             if (!$role[Scalr_Role_Behavior::ROLE_DM_REMOTE_PATH]) {
                 throw new Exception(sprintf("Remote path reuired for deployment on role '%s'", $dbRole->name));
             }
         }
         //-- CloudFoundryStuff
         if ($dbRole->hasBehavior(ROLE_BEHAVIORS::CF_CLOUD_CONTROLLER)) {
             $cloudFoundryStack[ROLE_BEHAVIORS::CF_CLOUD_CONTROLLER] = true;
         }
         if ($dbRole->hasBehavior(ROLE_BEHAVIORS::CF_DEA)) {
             $cloudFoundryStack[ROLE_BEHAVIORS::CF_DEA] = true;
         }
         if ($dbRole->hasBehavior(ROLE_BEHAVIORS::CF_HEALTH_MANAGER)) {
             $cloudFoundryStack[ROLE_BEHAVIORS::CF_HEALTH_MANAGER] = true;
         }
         if ($dbRole->hasBehavior(ROLE_BEHAVIORS::CF_ROUTER)) {
             $cloudFoundryStack[ROLE_BEHAVIORS::CF_ROUTER] = true;
         }
         if ($dbRole->hasBehavior(ROLE_BEHAVIORS::CF_SERVICE)) {
             $cloudFoundryStack[ROLE_BEHAVIORS::CF_SERVICE] = true;
         }
         if ($dbRole->hasBehavior(ROLE_BEHAVIORS::NGINX)) {
             $nginxFound++;
         }
         //-- End CloudFoundry stuff
         if ($dbRole->hasBehavior(ROLE_BEHAVIORS::RABBITMQ)) {
             $role['settings'][DBFarmRole::SETTING_SCALING_MAX_INSTANCES] = $role['settings'][DBFarmRole::SETTING_SCALING_MIN_INSTANCES];
             $role['settings'][Scalr_Role_Behavior_RabbitMQ::ROLE_NODES_RATIO] = (int) $role['settings'][Scalr_Role_Behavior_RabbitMQ::ROLE_NODES_RATIO];
             if ($role['settings'][Scalr_Role_Behavior_RabbitMQ::ROLE_NODES_RATIO] < 1 || $role['settings'][Scalr_Role_Behavior_RabbitMQ::ROLE_NODES_RATIO] > 100) {
                 throw new Exception(sprintf("Nodes ratio for RabbitMq role '%s' should be between 1 and 100", $dbRole->name));
             }
         }
         if ($dbRole->hasBehavior(ROLE_BEHAVIORS::MONGODB)) {
             if ($role['settings'][Scalr_Role_Behavior_MongoDB::ROLE_DATA_STORAGE_ENGINE] == 'ebs') {
                 if ($role['settings'][Scalr_Role_Behavior_MongoDB::ROLE_DATA_STORAGE_EBS_SIZE] < 10 || $role['settings'][Scalr_Role_Behavior_MongoDB::ROLE_DATA_STORAGE_EBS_SIZE] > 1000) {
                     throw new Exception(sprintf("EBS size for mongoDB role should be between 10 and 1000 GB", $dbRole->name));
                 }
             }
         }
         /* Validate scaling */
         $minCount = (int) $role['settings'][DBFarmRole::SETTING_SCALING_MIN_INSTANCES];
         if (!$minCount && $minCount != 0) {
             $minCount = 1;
         }
         if ($minCount < 0 || $minCount > 400) {
             throw new Exception(sprintf(_("Min instances for '%s' must be a number between 1 and 400"), $dbRole->name));
         }
         $maxCount = (int) $role['settings'][DBFarmRole::SETTING_SCALING_MAX_INSTANCES];
         if (!$maxCount) {
             $maxCount = 1;
         }
         if ($maxCount < 1 || $maxCount > 400) {
             throw new Exception(sprintf(_("Max instances for '%s' must be a number between 1 and 400"), $dbRole->name));
         }
         if ($maxCount < $minCount) {
             throw new Exception(sprintf(_("Max instances should be greater or equal than Min instances for role '%s'"), $dbRole->name));
         }
         if (isset($role['settings'][DBFarmRole::SETTING_SCALING_POLLING_INTERVAL]) && $role['settings'][DBFarmRole::SETTING_SCALING_POLLING_INTERVAL] > 0) {
             $polling_interval = (int) $role['settings'][DBFarmRole::SETTING_SCALING_POLLING_INTERVAL];
         } else {
             $polling_interval = 2;
         }
         if ($polling_interval < 1 || $polling_interval > 50) {
             throw new Exception(sprintf(_("Polling interval for role '%s' must be a number between 1 and 50"), $dbRole->name));
         }
         /** Validate platform specified settings **/
         switch ($role['platform']) {
             case SERVER_PLATFORMS::EC2:
                 Modules_Platforms_Ec2_Helpers_Ebs::farmValidateRoleSettings($role['settings'], $dbRole->name);
                 Modules_Platforms_Ec2_Helpers_Eip::farmValidateRoleSettings($role['settings'], $dbRole->name);
                 Modules_Platforms_Ec2_Helpers_Elb::farmValidateRoleSettings($role['settings'], $dbRole->name);
                 if ($dbRole->hasBehavior(ROLE_BEHAVIORS::MYSQL)) {
                     if ($role['settings'][DBFarmRole::SETTING_MYSQL_DATA_STORAGE_ENGINE] == MYSQL_STORAGE_ENGINE::EBS) {
                         if ($dbRole->generation != 2) {
                             if ($role['settings'][DBFarmRole::SETTING_AWS_AVAIL_ZONE] == "" || $role['settings'][DBFarmRole::SETTING_AWS_AVAIL_ZONE] == "x-scalr-diff" || stristr($role['settings'][DBFarmRole::SETTING_AWS_AVAIL_ZONE], 'x-scalr-custom')) {
                                 throw new Exception(sprintf(_("Requirement for EBS MySQL data storage is specific 'Placement' parameter for role '%s'"), $dbRole->name));
                             }
                         }
                     }
                 }
                 if ($dbRole->getDbMsrBehavior()) {
                     if ($role['settings'][Scalr_Db_Msr::DATA_STORAGE_ENGINE] == MYSQL_STORAGE_ENGINE::EPH) {
                         if (!$role['settings'][Scalr_Db_Msr::DATA_STORAGE_EPH_DISK]) {
                             throw new Exception(sprintf(_("Ephemeral disk settings is required for role '%s'"), $dbRole->name));
                         }
                     }
                 }
                 if ($role['settings'][DBFarmRole::SETTING_AWS_AVAIL_ZONE] == 'x-scalr-custom=') {
                     throw new Exception(sprintf(_("Availability zone for role \"%s\" should be selected"), $dbRole->name));
                 }
                 break;
             case SERVER_PLATFORMS::RDS:
                 Modules_Platforms_Rds_Helpers_Rds::farmValidateRoleSettings($role['settings'], $dbRole->name);
                 break;
             case SERVER_PLATFORMS::EUCALYPTUS:
                 Modules_Platforms_Eucalyptus_Helpers_Eucalyptus::farmValidateRoleSettings($role['settings'], $dbRole->name);
                 break;
             case SERVER_PLATFORMS::CLOUDSTACK:
                 Modules_Platforms_Cloudstack_Helpers_Cloudstack::farmValidateRoleSettings($role['settings'], $dbRole->name);
                 break;
             case SERVER_PLATFORMS::RACKSPACE:
                 Modules_Platforms_Rackspace_Helpers_Rackspace::farmValidateRoleSettings($role['settings'], $dbRole->name);
                 break;
         }
         Scalr_Helpers_Dns::farmValidateRoleSettings($role['settings'], $dbRole->name);
     }
     //Validate ClouFoundry stuff
     if (!empty($cloudFoundryStack)) {
         if (!$cloudFoundryStack[ROLE_BEHAVIORS::CF_CLOUD_CONTROLLER]) {
             throw new Exception("CF CloudContoller role required for CloudFoundry stack. Please add All-in-one CF or separate CCHM role to farm");
         }
         if (!$cloudFoundryStack[ROLE_BEHAVIORS::CF_HEALTH_MANAGER]) {
             throw new Exception("CF HealthManager role required for CloudFoundry stack. Please add All-in-one CF or separate CCHM role to farm");
         }
         if (!$cloudFoundryStack[ROLE_BEHAVIORS::CF_ROUTER]) {
             throw new Exception("CF Router role required for CloudFoundry stack. Please add All-in-one CF or separate CF Router role to farm");
         }
         if (!$cloudFoundryStack[ROLE_BEHAVIORS::CF_DEA]) {
             throw new Exception("CF DEA role required for CloudFoundry stack. Please add All-in-one CF or separate CF DEA role to farm");
         }
         if (!$nginxFound) {
             throw new Exception("Nginx load balancer role required for CloudFoundry stack. Please add it to the farm");
         }
         if ($cloudFoundryStack[ROLE_BEHAVIORS::CF_CLOUD_CONTROLLER] > 1) {
             throw new Exception("CloudFoundry stack can work only with ONE CF CloudController role. Please leave only one CloudController role in farm");
         }
         if ($cloudFoundryStack[ROLE_BEHAVIORS::CF_HEALTH_MANAGER] > 1) {
             throw new Exception("CloudFoundry stack can work only with ONE CF HealthManager role. Please leave only one HealthManager role in farm");
         }
         if ($nginxFound > 1) {
             throw new Exception("CloudFoundry stack can work only with ONE nginx role. Please leave only one nginx role in farm");
         }
     }
     $client = Client::Load($this->user->getAccountId());
     if ($this->getParam('farmId')) {
         $dbFarm = DBFarm::LoadByID($this->getParam('farmId'));
         $this->user->getPermissions()->validate($dbFarm);
     } else {
         $this->user->getAccount()->validateLimit(Scalr_Limits::ACCOUNT_FARMS, 1);
         $dbFarm = new DBFarm();
         $dbFarm->Status = FARM_STATUS::TERMINATED;
     }
     if ($this->getParam('farm')) {
         $farm = $this->getParam('farm');
         $dbFarm->Name = strip_tags($farm['name']);
         $dbFarm->RolesLaunchOrder = $farm['roles_launch_order'];
         $dbFarm->Comments = trim(strip_tags($farm['description']));
     }
     if (!$Validator->IsNotEmpty($dbFarm->Name)) {
         throw new Exception(_("Farm name required"));
     }
     $dbFarm->save();
     if (!$dbFarm->GetSetting(DBFarm::SETTING_CRYPTO_KEY)) {
         $dbFarm->SetSetting(DBFarm::SETTING_CRYPTO_KEY, Scalr::GenerateRandomKey(40));
     }
     $usedPlatforms = array();
     $dbFarmRolesList = array();
     $newFarmRolesList = array();
     foreach ($this->getParam('roles') as $role) {
         if ($role['farm_role_id']) {
             $update = true;
             $dbFarmRole = DBFarmRole::LoadByID($role['farm_role_id']);
             $dbRole = DBRole::loadById($dbFarmRole->RoleID);
             $role['role_id'] = $dbFarmRole->RoleID;
         } else {
             $update = false;
             $dbRole = DBRole::loadById($role['role_id']);
             $dbFarmRole = $dbFarm->AddRole($dbRole, $role['platform'], $role['cloud_location'], (int) $role['launch_index']);
         }
         if ($dbRole->hasBehavior(ROLE_BEHAVIORS::RABBITMQ)) {
             $role['settings'][DBFarmRole::SETTING_SCALING_MAX_INSTANCES] = $role['settings'][DBFarmRole::SETTING_SCALING_MIN_INSTANCES];
         }
         if ($dbFarmRole->NewRoleID) {
             continue;
         }
         if ($update) {
             $dbFarmRole->LaunchIndex = (int) $role['launch_index'];
             $dbFarmRole->Save();
         }
         $usedPlatforms[$role['platform']] = 1;
         $oldRoleSettings = $dbFarmRole->GetAllSettings();
         foreach ($role['scaling_settings'] as $k => $v) {
             $dbFarmRole->SetSetting($k, $v);
         }
         foreach ($role['settings'] as $k => $v) {
             $dbFarmRole->SetSetting($k, $v);
         }
         /****** Scaling settings ******/
         $scalingManager = new Scalr_Scaling_Manager($dbFarmRole);
         $scalingManager->setFarmRoleMetrics($role['scaling']);
         //TODO: optimize this code...
         $this->db->Execute("DELETE FROM farm_role_scaling_times WHERE farm_roleid=?", array($dbFarmRole->ID));
         // 5 = Time based scaling -> move to constants
         if ($role['scaling'][5]) {
             foreach ($role['scaling'][5] as $scal_period) {
                 $chunks = explode(":", $scal_period['id']);
                 $this->db->Execute("INSERT INTO farm_role_scaling_times SET\n\t\t\t\t\t\tfarm_roleid\t\t= ?,\n\t\t\t\t\t\tstart_time\t\t= ?,\n\t\t\t\t\t\tend_time\t\t= ?,\n\t\t\t\t\t\tdays_of_week\t= ?,\n\t\t\t\t\t\tinstances_count\t= ?\n\t\t\t\t\t", array($dbFarmRole->ID, $chunks[0], $chunks[1], $chunks[2], $chunks[3]));
             }
         }
         /*****************/
         /* Update role params */
         $dbFarmRole->SetParameters($role['params']);
         /* End of role params management */
         /* Add script options to databse */
         $dbFarmRole->SetScripts($role['scripting']);
         /* End of scripting section */
         /* Add services configuration */
         $dbFarmRole->SetServiceConfigPresets($role['config_presets']);
         /* End of scripting section */
         Scalr_Helpers_Dns::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
         foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $behavior) {
             $behavior->onFarmSave($dbFarm, $dbFarmRole);
         }
         /**
          * Platfrom specified updates
          */
         if ($dbFarmRole->Platform == SERVER_PLATFORMS::EC2) {
             Modules_Platforms_Ec2_Helpers_Ebs::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
             Modules_Platforms_Ec2_Helpers_Eip::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
             Modules_Platforms_Ec2_Helpers_Elb::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
         }
         $dbFarmRolesList[] = $dbFarmRole;
         $newFarmRolesList[] = $dbFarmRole->ID;
     }
     if (!$this->getParam('roleUpdate')) {
         foreach ($dbFarm->GetFarmRoles() as $dbFarmRole) {
             if (!$dbFarmRole->NewRoleID && !in_array($dbFarmRole->ID, $newFarmRolesList)) {
                 $dbFarmRole->Delete();
             }
         }
     }
     if ($usedPlatforms[SERVER_PLATFORMS::CLOUDSTACK]) {
         Modules_Platforms_Cloudstack_Helpers_Cloudstack::farmSave($dbFarm, $dbFarmRolesList);
     }
     if ($usedPlatforms[SERVER_PLATFORMS::EC2]) {
         Modules_Platforms_Ec2_Helpers_Ec2::farmSave($dbFarm, $dbFarmRolesList);
     }
     if ($usedPlatforms[SERVER_PLATFORMS::EUCALYPTUS]) {
         Modules_Platforms_Eucalyptus_Helpers_Eucalyptus::farmSave($dbFarm, $dbFarmRolesList);
     }
     $dbFarm->save();
     if (!$client->GetSettingValue(CLIENT_SETTINGS::DATE_FARM_CREATED)) {
         $client->SetSettingValue(CLIENT_SETTINGS::DATE_FARM_CREATED, time());
     }
     $this->response->success('Farm successfully saved');
     $this->response->data(array('farmId' => $dbFarm->ID));
 }
예제 #13
0
파일: Builder.php 프로젝트: recipe/scalr
 public function xBuildAction()
 {
     $this->request->defineParams(array('farmId' => array('type' => 'int'), 'roles' => array('type' => 'json'), 'farm' => array('type' => 'json'), 'roleUpdate' => array('type' => 'int')));
     $this->request->restrictAccess(Acl::RESOURCE_FARMS, Acl::PERM_FARMS_MANAGE);
     if (!$this->isFarmConfigurationValid($this->getParam('farmId'), $this->getParam('farm'), (array) $this->getParam('roles'))) {
         if ($this->errors['error_count'] != 0) {
             $this->response->failure();
             $this->response->data(array('errors' => $this->errors));
             return;
         }
     }
     $farm = $this->getParam('farm');
     $client = Client::Load($this->user->getAccountId());
     if ($this->getParam('farmId')) {
         $dbFarm = DBFarm::LoadByID($this->getParam('farmId'));
         $this->user->getPermissions()->validate($dbFarm);
         $dbFarm->isLocked();
         if ($this->getParam('changed') && $dbFarm->changedTime && $this->getParam('changed') != $dbFarm->changedTime) {
             $userName = '******';
             $changed = explode(' ', $this->getParam('changed'));
             $changedTime = intval($changed[1]);
             try {
                 $user = new Scalr_Account_User();
                 $user->loadById($dbFarm->changedByUserId);
                 $userName = $user->getEmail();
             } catch (Exception $e) {
             }
             $this->response->failure();
             $this->response->data(array('changedFailure' => sprintf('%s changed this farm at %s', $userName, Scalr_Util_DateTime::convertTz($changedTime))));
             return;
         }
         $dbFarm->changedByUserId = $this->user->getId();
         $dbFarm->changedTime = microtime();
     } else {
         $this->user->getAccount()->validateLimit(Scalr_Limits::ACCOUNT_FARMS, 1);
         $dbFarm = new DBFarm();
         $dbFarm->Status = FARM_STATUS::TERMINATED;
         $dbFarm->createdByUserId = $this->user->getId();
         $dbFarm->createdByUserEmail = $this->user->getEmail();
         $dbFarm->changedByUserId = $this->user->getId();
         $dbFarm->changedTime = microtime();
     }
     if ($this->getParam('farm')) {
         $dbFarm->Name = strip_tags($farm['name']);
         $dbFarm->RolesLaunchOrder = $farm['rolesLaunchOrder'];
         $dbFarm->Comments = trim(strip_tags($farm['description']));
     }
     if (empty($dbFarm->Name)) {
         throw new Exception(_("Farm name required"));
     }
     $dbFarm->save();
     $governance = new Scalr_Governance($this->getEnvironmentId());
     if ($governance->isEnabled(Scalr_Governance::GENERAL_LEASE)) {
         $dbFarm->SetSetting(DBFarm::SETTING_LEASE_STATUS, 'Active');
     }
     if (isset($farm['variables'])) {
         $variables = new Scalr_Scripting_GlobalVariables($this->getEnvironmentId(), Scalr_Scripting_GlobalVariables::SCOPE_FARM);
         $variables->setValues($farm['variables'], 0, $dbFarm->ID, 0, '', false);
     }
     if (!$farm['timezone']) {
         $farm['timezone'] = date_default_timezone_get();
     }
     $dbFarm->SetSetting(DBFarm::SETTING_TIMEZONE, $farm['timezone']);
     $dbFarm->SetSetting(DBFarm::SETTING_EC2_VPC_ID, $farm['vpc_id']);
     $dbFarm->SetSetting(DBFarm::SETTING_EC2_VPC_REGION, $farm['vpc_region']);
     if (!$dbFarm->GetSetting(DBFarm::SETTING_CRYPTO_KEY)) {
         $dbFarm->SetSetting(DBFarm::SETTING_CRYPTO_KEY, Scalr::GenerateRandomKey(40));
     }
     $virtualFarmRoles = array();
     $roles = $this->getParam('roles');
     if (!empty($roles)) {
         foreach ($roles as $role) {
             if (strpos($role['farm_role_id'], "virtual_") !== false) {
                 $dbRole = DBRole::loadById($role['role_id']);
                 $dbFarmRole = $dbFarm->AddRole($dbRole, $role['platform'], $role['cloud_location'], (int) $role['launch_index'], $role['alias']);
                 $virtualFarmRoles[$role['farm_role_id']] = $dbFarmRole->ID;
             }
         }
     }
     $usedPlatforms = array();
     $dbFarmRolesList = array();
     $newFarmRolesList = array();
     $farmRoleVariables = new Scalr_Scripting_GlobalVariables($this->getEnvironmentId(), Scalr_Scripting_GlobalVariables::SCOPE_FARMROLE);
     if (!empty($roles)) {
         foreach ($roles as $role) {
             if ($role['farm_role_id']) {
                 if ($virtualFarmRoles[$role['farm_role_id']]) {
                     $role['farm_role_id'] = $virtualFarmRoles[$role['farm_role_id']];
                 }
                 $update = true;
                 $dbFarmRole = DBFarmRole::LoadByID($role['farm_role_id']);
                 $dbRole = DBRole::loadById($dbFarmRole->RoleID);
                 $role['role_id'] = $dbFarmRole->RoleID;
                 if ($dbFarmRole->Platform == SERVER_PLATFORMS::GCE) {
                     $dbFarmRole->CloudLocation = $role['cloud_location'];
                 }
             } else {
                 $update = false;
                 $dbRole = DBRole::loadById($role['role_id']);
                 $dbFarmRole = $dbFarm->AddRole($dbRole, $role['platform'], $role['cloud_location'], (int) $role['launch_index']);
             }
             if ($dbRole->hasBehavior(ROLE_BEHAVIORS::RABBITMQ)) {
                 $role['settings'][DBFarmRole::SETTING_SCALING_MAX_INSTANCES] = $role['settings'][DBFarmRole::SETTING_SCALING_MIN_INSTANCES];
             }
             if ($dbFarmRole->NewRoleID) {
                 continue;
             }
             if ($update) {
                 $dbFarmRole->LaunchIndex = (int) $role['launch_index'];
                 $dbFarmRole->Alias = $role['alias'];
                 $dbFarmRole->Save();
             }
             $usedPlatforms[$role['platform']] = 1;
             $oldRoleSettings = $dbFarmRole->GetAllSettings();
             // Update virtual farm_role_id with actual value
             $scripts = (array) $role['scripting'];
             if (count($virtualFarmRoles) > 0) {
                 array_walk_recursive($scripts, function (&$v, $k) use($virtualFarmRoles) {
                     if (is_string($v)) {
                         $v = str_replace(array_keys($virtualFarmRoles), array_values($virtualFarmRoles), $v);
                     }
                 });
                 array_walk_recursive($role['settings'], function (&$v, $k) use($virtualFarmRoles) {
                     if (is_string($v)) {
                         $v = str_replace(array_keys($virtualFarmRoles), array_values($virtualFarmRoles), $v);
                     }
                 });
             }
             //Audit log start
             //!TODO Enable Audit log for Farm Builder
             //             $auditLog = $this->getEnvironment()->auditLog;
             //             $docRoleSettingsBefore = new FarmRoleSettingsDocument($oldRoleSettings);
             //             $docRoleSettingsBefore['farmroleid'] = $dbFarmRole->ID;
             //             $docRoleSettings = new FarmRoleSettingsDocument(array_merge((array)$role['scaling_settings'], (array)$role['settings']));
             //             $docRoleSettings['farmroleid'] = $dbFarmRole->ID;
             $dbFarmRole->ClearSettings("chef.");
             if (!empty($role['scaling_settings']) && is_array($role['scaling_settings'])) {
                 foreach ($role['scaling_settings'] as $k => $v) {
                     $dbFarmRole->SetSetting($k, $v, DBFarmRole::TYPE_CFG);
                 }
             }
             foreach ($role['settings'] as $k => $v) {
                 $dbFarmRole->SetSetting($k, $v, DBFarmRole::TYPE_CFG);
             }
             //             $auditLog->log('Farm has been saved', array(AuditLogTags::TAG_UPDATE), $docRoleSettings, $docRoleSettingsBefore);
             //             unset($docRoleSettings);
             //             unset($docRoleSettingsBefore);
             //Audit log finish
             /****** Scaling settings ******/
             $scalingManager = new Scalr_Scaling_Manager($dbFarmRole);
             $scalingManager->setFarmRoleMetrics(is_array($role['scaling']) ? $role['scaling'] : array());
             //TODO: optimize this code...
             $this->db->Execute("DELETE FROM farm_role_scaling_times WHERE farm_roleid=?", array($dbFarmRole->ID));
             // 5 = Time based scaling -> move to constants
             if ($role['scaling'][5]) {
                 foreach ($role['scaling'][5] as $scal_period) {
                     $chunks = explode(":", $scal_period['id']);
                     $this->db->Execute("INSERT INTO farm_role_scaling_times SET\n                            farm_roleid\t\t= ?,\n                            start_time\t\t= ?,\n                            end_time\t\t= ?,\n                            days_of_week\t= ?,\n                            instances_count\t= ?\n                        ", array($dbFarmRole->ID, $chunks[0], $chunks[1], $chunks[2], $chunks[3]));
                 }
             }
             /*****************/
             /* Update role params */
             $dbFarmRole->SetParameters((array) $role['params']);
             /* End of role params management */
             /* Add script options to databse */
             $dbFarmRole->SetScripts($scripts, (array) $role['scripting_params']);
             /* End of scripting section */
             /* Add services configuration */
             $dbFarmRole->SetServiceConfigPresets((array) $role['config_presets']);
             /* End of scripting section */
             /* Add storage configuration */
             //try {
             $dbFarmRole->getStorage()->setConfigs((array) $role['storages']['configs']);
             //} catch (FarmRoleStorageException $e) {
             //    $errors[] = array('farm_role_id' => 1, 'tab' => 'storage', 'error' => $e->getMessage());
             //}
             $farmRoleVariables->setValues($role['variables'], $dbFarmRole->GetRoleID(), $dbFarm->ID, $dbFarmRole->ID, '', false);
             Scalr_Helpers_Dns::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
             foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $behavior) {
                 $behavior->onFarmSave($dbFarm, $dbFarmRole);
             }
             /**
              * Platfrom specified updates
              */
             if ($dbFarmRole->Platform == SERVER_PLATFORMS::EC2) {
                 Modules_Platforms_Ec2_Helpers_Ebs::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
                 Modules_Platforms_Ec2_Helpers_Eip::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
                 Modules_Platforms_Ec2_Helpers_Elb::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
             }
             if (in_array($dbFarmRole->Platform, array(SERVER_PLATFORMS::IDCF, SERVER_PLATFORMS::CLOUDSTACK))) {
                 Modules_Platforms_Cloudstack_Helpers_Cloudstack::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
             }
             $dbFarmRolesList[] = $dbFarmRole;
             $newFarmRolesList[] = $dbFarmRole->ID;
         }
     }
     if (!$this->getParam('roleUpdate')) {
         foreach ($dbFarm->GetFarmRoles() as $dbFarmRole) {
             if (!$dbFarmRole->NewRoleID && !in_array($dbFarmRole->ID, $newFarmRolesList)) {
                 $dbFarmRole->Delete();
             }
         }
     }
     if ($usedPlatforms[SERVER_PLATFORMS::CLOUDSTACK]) {
         Modules_Platforms_Cloudstack_Helpers_Cloudstack::farmSave($dbFarm, $dbFarmRolesList);
     }
     if ($usedPlatforms[SERVER_PLATFORMS::EC2]) {
         Modules_Platforms_Ec2_Helpers_Ec2::farmSave($dbFarm, $dbFarmRolesList);
     }
     if ($usedPlatforms[SERVER_PLATFORMS::EUCALYPTUS]) {
         Modules_Platforms_Eucalyptus_Helpers_Eucalyptus::farmSave($dbFarm, $dbFarmRolesList);
     }
     $dbFarm->save();
     if (!$client->GetSettingValue(CLIENT_SETTINGS::DATE_FARM_CREATED)) {
         $client->SetSettingValue(CLIENT_SETTINGS::DATE_FARM_CREATED, time());
     }
     $this->response->success('Farm successfully saved');
     $this->response->data(array('farmId' => $dbFarm->ID));
 }
 public function OnBeforeHostTerminate(BeforeHostTerminateEvent $event)
 {
     $servers = DBFarm::LoadByID($this->FarmID)->GetServersByFilter(array('status' => array(SERVER_STATUS::INIT, SERVER_STATUS::RUNNING, SERVER_STATUS::PENDING_TERMINATE)));
     foreach ($servers as $DBServer) {
         $msg = new Scalr_Messaging_Msg_BeforeHostTerminate($event->DBServer->GetFarmRoleObject()->GetRoleObject()->getBehaviors(), $event->DBServer->GetFarmRoleObject()->GetRoleObject()->name, $event->DBServer->localIp, $event->DBServer->remoteIp);
         $msg->farmRoleId = $event->DBServer->farmRoleId;
         $msg->serverId = $event->DBServer->serverId;
         $msg->serverIndex = $event->DBServer->serverIndex;
         $msg->scripts = $this->getScripts($event, $event->DBServer, $DBServer);
         if ($event->DBServer->farmRoleId != 0) {
             foreach (Scalr_Role_Behavior::getListForFarmRole($event->DBServer->GetFarmRoleObject()) as $behavior) {
                 $msg = $behavior->extendMessage($msg, $event->DBServer);
             }
         }
         $DBServer->SendMessage($msg, false, true);
     }
     try {
         if ($event->DBServer->GetFarmRoleObject()->GetRoleObject()->getDbMsrBehavior()) {
             $this->sendPromoteToMasterMessage($event);
         }
     } catch (Exception $e) {
     }
 }
예제 #15
0
 private function GetServerSecurityGroupsList(DBServer $DBServer, $EC2Client)
 {
     // Add default security group
     $retval = array('default');
     if ($DBServer->farmRoleId) {
         $dbFarmRole = $DBServer->GetFarmRoleObject();
         $sgList = trim($dbFarmRole->GetSetting(DBFarmRole::SETTING_AWS_SG_LIST));
         if ($sgList) {
             $sgList = explode(",", $sgList);
             foreach ($sgList as $sg) {
                 if ($sg != '') {
                     array_push($retval, trim($sg));
                 }
             }
         }
     }
     try {
         $aws_sgroups_list_t = $EC2Client->DescribeSecurityGroups();
         $aws_sgroups_list_t = $aws_sgroups_list_t->securityGroupInfo->item;
         if ($aws_sgroups_list_t instanceof stdClass) {
             $aws_sgroups_list_t = array($aws_sgroups_list_t);
         }
         $aws_sgroups = array();
         foreach ($aws_sgroups_list_t as $sg) {
             $aws_sgroups[strtolower($sg->groupName)] = $sg;
         }
         unset($aws_sgroups_list_t);
     } catch (Exception $e) {
         throw new Exception("GetServerSecurityGroupsList failed: {$e->getMessage()}");
     }
     /**** Security group for role builder ****/
     if ($DBServer->status == SERVER_STATUS::TEMPORARY) {
         if (!$aws_sgroups['scalr-rb-system']) {
             try {
                 $EC2Client->CreateSecurityGroup('scalr-rb-system', _("Security group for Roles Builder"));
             } catch (Exception $e) {
                 throw new Exception("GetServerSecurityGroupsList failed: {$e->getMessage()}");
             }
             $IpPermissionSet = new IpPermissionSetType();
             $group_rules = array(array('rule' => 'tcp:22:22:0.0.0.0/0'), array('rule' => 'tcp:8013:8013:0.0.0.0/0'), array('rule' => 'udp:8014:8014:0.0.0.0/0'), array('rule' => 'icmp:-1:-1:0.0.0.0/0'));
             foreach ($group_rules as $rule) {
                 $group_rule = explode(":", $rule["rule"]);
                 $IpPermissionSet->AddItem($group_rule[0], $group_rule[1], $group_rule[2], null, array($group_rule[3]));
             }
             // Create security group
             $EC2Client->AuthorizeSecurityGroupIngress($DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::ACCOUNT_ID), 'scalr-rb-system', $IpPermissionSet);
         }
         array_push($retval, 'scalr-rb-system');
         return $retval;
     }
     /**********************************/
     // Add Role security group
     $role_sec_group = CONFIG::$SECGROUP_PREFIX . $DBServer->GetFarmRoleObject()->GetRoleObject()->name;
     $partent_sec_group = CONFIG::$SECGROUP_PREFIX . $DBServer->GetFarmRoleObject()->GetRoleObject()->getRoleHistory();
     $new_role_sec_group = "scalr-role." . $DBServer->GetFarmRoleObject()->ID;
     /****
      * SCALR IP POOL SECURITY GROUP
      */
     $scalrSecuritySettings = @parse_ini_file(APPPATH . '/etc/security.ini', true);
     if (!$aws_sgroups[$scalrSecuritySettings['ec2']['security_group_name']]) {
         try {
             $EC2Client->CreateSecurityGroup($scalrSecuritySettings['ec2']['security_group_name'], "Security rules needed by Scalr");
         } catch (Exception $e) {
             throw new Exception("GetServerSecurityGroupsList failed on scalr.ip-pool: {$e->getMessage()}");
         }
         $sRules = array(array('rule' => 'tcp:8008:8013:0.0.0.0/0'), array('rule' => 'udp:8014:8014:0.0.0.0/0'), array('rule' => 'tcp:3306:3306:0.0.0.0/0'));
         $IpPermissionSet = new IpPermissionSetType();
         foreach ($scalrSecuritySettings['ip-pool'] as $name => $ip) {
             foreach ($sRules as $rule) {
                 $group_rule = explode(":", $rule["rule"]);
                 $IpPermissionSet->AddItem($group_rule[0], $group_rule[1], $group_rule[2], null, array($ip));
             }
         }
         // Create security group
         $EC2Client->AuthorizeSecurityGroupIngress($DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::ACCOUNT_ID), $scalrSecuritySettings['ec2']['security_group_name'], $IpPermissionSet);
     }
     array_push($retval, $scalrSecuritySettings['ec2']['security_group_name']);
     /**********************************************/
     if ($aws_sgroups[strtolower($role_sec_group)]) {
         // OLD System. scalr.%ROLENAME% . Nothing to do
         array_push($retval, $role_sec_group);
     } else {
         if ($aws_sgroups[strtolower($new_role_sec_group)]) {
             // NEW System. scalr-role.%FARM_ROLE_ID% . Nothing to do
             array_push($retval, $new_role_sec_group);
         } else {
             try {
                 $EC2Client->CreateSecurityGroup($new_role_sec_group, sprintf("Security group for FarmRoleID #%s on FarmID #%s", $DBServer->GetFarmRoleObject()->ID, $DBServer->farmId));
             } catch (Exception $e) {
                 throw new Exception("GetServerSecurityGroupsList failed: {$e->getMessage()}");
             }
             $IpPermissionSet = new IpPermissionSetType();
             $group_rules = $DBServer->GetFarmRoleObject()->GetRoleObject()->getSecurityRules();
             //
             // Check parent security group
             //
             if (count($group_rules) == 0) {
                 $group_rules = array(array('rule' => 'tcp:22:22:0.0.0.0/0'), array('rule' => 'icmp:-1:-1:0.0.0.0/0'));
                 if ($DBServer->GetFarmRoleObject()->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MYSQL)) {
                     $group_rules[] = array('rule' => 'tcp:3306:3306:0.0.0.0/0');
                 }
                 if ($DBServer->GetFarmRoleObject()->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::CASSANDRA)) {
                     $group_rules[] = array('rule' => 'tcp:7000:7000:0.0.0.0/0');
                     $group_rules[] = array('rule' => 'tcp:9160:9160:0.0.0.0/0');
                 }
                 foreach (Scalr_Role_Behavior::getListForFarmRole($DBServer->GetFarmRoleObject()) as $bObj) {
                     $bRules = $bObj->getSecurityRules();
                     foreach ($bRules as $r) {
                         if ($r) {
                             $group_rules[] = array('rule' => $r);
                         }
                     }
                 }
             }
             foreach ($group_rules as $rule) {
                 $group_rule = explode(":", $rule["rule"]);
                 $IpPermissionSet->AddItem($group_rule[0], $group_rule[1], $group_rule[2], null, array($group_rule[3]));
             }
             //Create SG rule to enable communication between instances inside this role
             $IpPermissionSet->AddItem('tcp', 0, 65535, array('userId' => $DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::ACCOUNT_ID), 'groupName' => $new_role_sec_group));
             $IpPermissionSet->AddItem('udp', 0, 65535, array('userId' => $DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::ACCOUNT_ID), 'groupName' => $new_role_sec_group));
             // Create security group
             $EC2Client->AuthorizeSecurityGroupIngress($DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::ACCOUNT_ID), $new_role_sec_group, $IpPermissionSet);
             $DBServer->GetFarmRoleObject()->SetSetting(DBFarmRole::SETTING_AWS_SECURITY_GROUP, $new_role_sec_group);
             array_push($retval, $new_role_sec_group);
         }
     }
     return $retval;
 }
예제 #16
0
 /**
  * Gets the list of the security groups for the specified db server.
  *
  * If server does not have required security groups this method will create them.
  *
  * @param   DBServer               $DBServer    The DB Server instance
  * @param   \Scalr\Service\Aws\Ec2 $ec2         Ec2 Client instance
  * @param   string                 $vpcId       optional The ID of VPC
  * @param   \Scalr_Governance      $governance  Governance
  * @param   string                 $osFamily    optional OS family of the instance
  * @return  array  Returns array looks like array(groupid-1, groupid-2, ..., groupid-N)
  */
 private function GetServerSecurityGroupsList(DBServer $DBServer, \Scalr\Service\Aws\Ec2 $ec2, $vpcId = "", \Scalr_Governance $governance = null, $osFamily = null)
 {
     $retval = array();
     $checkGroups = array();
     $wildCardSgs = [];
     $sgGovernance = false;
     $allowAdditionalSgs = true;
     $roleBuilderSgName = \Scalr::config('scalr.aws.security_group_name') . "-rb";
     if ($governance && $DBServer->farmRoleId) {
         $sgs = $governance->getValue(SERVER_PLATFORMS::EC2, \Scalr_Governance::AWS_SECURITY_GROUPS);
         if ($osFamily == 'windows' && $governance->getValue(SERVER_PLATFORMS::EC2, \Scalr_Governance::AWS_SECURITY_GROUPS, 'windows')) {
             $sgs = $governance->getValue(SERVER_PLATFORMS::EC2, \Scalr_Governance::AWS_SECURITY_GROUPS, 'windows');
         }
         if ($sgs !== null) {
             $governanceSecurityGroups = @explode(",", $sgs);
             if (!empty($governanceSecurityGroups)) {
                 foreach ($governanceSecurityGroups as $sg) {
                     if ($sg != '') {
                         array_push($checkGroups, trim($sg));
                         if (strpos($sg, '*') !== false) {
                             array_push($wildCardSgs, trim($sg));
                         }
                     }
                 }
             }
             if (!empty($checkGroups)) {
                 $sgGovernance = true;
             }
             $allowAdditionalSgs = $governance->getValue(SERVER_PLATFORMS::EC2, \Scalr_Governance::AWS_SECURITY_GROUPS, 'allow_additional_sec_groups');
         }
     }
     if (!$sgGovernance || $allowAdditionalSgs) {
         if ($DBServer->farmRoleId != 0) {
             $dbFarmRole = $DBServer->GetFarmRoleObject();
             if ($dbFarmRole->GetSetting(Entity\FarmRoleSetting::AWS_SECURITY_GROUPS_LIST) !== null) {
                 // New SG management
                 $sgs = @json_decode($dbFarmRole->GetSetting(Entity\FarmRoleSetting::AWS_SECURITY_GROUPS_LIST));
                 if (!empty($sgs)) {
                     foreach ($sgs as $sg) {
                         if (stripos($sg, 'sg-') === 0) {
                             array_push($retval, $sg);
                         } else {
                             array_push($checkGroups, $sg);
                         }
                     }
                 }
             } else {
                 // Old SG management
                 array_push($checkGroups, 'default');
                 array_push($checkGroups, \Scalr::config('scalr.aws.security_group_name'));
                 if (!$vpcId) {
                     array_push($checkGroups, "scalr-farm.{$DBServer->farmId}");
                     array_push($checkGroups, "scalr-role.{$DBServer->farmRoleId}");
                 }
                 $additionalSgs = trim($dbFarmRole->GetSetting(Entity\FarmRoleSetting::AWS_SG_LIST));
                 if ($additionalSgs) {
                     $sgs = explode(",", $additionalSgs);
                     if (!empty($sgs)) {
                         foreach ($sgs as $sg) {
                             $sg = trim($sg);
                             if (stripos($sg, 'sg-') === 0) {
                                 array_push($retval, $sg);
                             } else {
                                 array_push($checkGroups, $sg);
                             }
                         }
                     }
                 }
             }
         } else {
             array_push($checkGroups, $roleBuilderSgName);
         }
     }
     // No name based security groups, return only SG ids.
     if (empty($checkGroups)) {
         return $retval;
     }
     // Filter groups
     $filter = array(array('name' => SecurityGroupFilterNameType::groupName(), 'value' => $checkGroups));
     // If instance run in VPC, add VPC filter
     if ($vpcId != '') {
         $filter[] = array('name' => SecurityGroupFilterNameType::vpcId(), 'value' => $vpcId);
     }
     // Get filtered list of SG required by scalr;
     try {
         $list = $ec2->securityGroup->describe(null, null, $filter);
         $sgList = array();
         foreach ($list as $sg) {
             /* @var $sg \Scalr\Service\Aws\Ec2\DataType\SecurityGroupData */
             if ($vpcId == '' && !$sg->vpcId || $vpcId && $sg->vpcId == $vpcId) {
                 $sgList[$sg->groupName] = $sg->groupId;
             }
         }
         unset($list);
     } catch (Exception $e) {
         throw new Exception("Cannot get list of security groups (1): {$e->getMessage()}");
     }
     foreach ($checkGroups as $groupName) {
         // Check default SG
         if ($groupName == 'default') {
             array_push($retval, $sgList[$groupName]);
             // Check Roles builder SG
         } elseif ($groupName == $roleBuilderSgName) {
             if (!isset($sgList[$groupName])) {
                 try {
                     $securityGroupId = $ec2->securityGroup->create($roleBuilderSgName, "Security group for Roles Builder", $vpcId);
                     $ipRangeList = new IpRangeList();
                     foreach (\Scalr::config('scalr.aws.ip_pool') as $ip) {
                         $ipRangeList->append(new IpRangeData($ip));
                     }
                     sleep(2);
                     $ec2->securityGroup->authorizeIngress(array(new IpPermissionData('tcp', 22, 22, $ipRangeList), new IpPermissionData('tcp', 8008, 8013, $ipRangeList)), $securityGroupId);
                     $sgList[$roleBuilderSgName] = $securityGroupId;
                 } catch (Exception $e) {
                     throw new Exception(sprintf(_("Cannot create security group '%s': %s"), $roleBuilderSgName, $e->getMessage()));
                 }
             }
             array_push($retval, $sgList[$groupName]);
             //Check scalr-farm.* security group
         } elseif (stripos($groupName, 'scalr-farm.') === 0) {
             if (!isset($sgList[$groupName])) {
                 try {
                     $securityGroupId = $ec2->securityGroup->create($groupName, sprintf("Security group for FarmID N%s", $DBServer->farmId), $vpcId);
                     sleep(2);
                     $userIdGroupPairList = new UserIdGroupPairList(new UserIdGroupPairData($DBServer->GetEnvironmentObject()->keychain(SERVER_PLATFORMS::EC2)->properties[Entity\CloudCredentialsProperty::AWS_ACCOUNT_ID], null, $groupName));
                     $ec2->securityGroup->authorizeIngress(array(new IpPermissionData('tcp', 0, 65535, null, $userIdGroupPairList), new IpPermissionData('udp', 0, 65535, null, $userIdGroupPairList)), $securityGroupId);
                     $sgList[$groupName] = $securityGroupId;
                 } catch (Exception $e) {
                     throw new Exception(sprintf(_("Cannot create security group '%s': %s"), $groupName, $e->getMessage()));
                 }
             }
             array_push($retval, $sgList[$groupName]);
             //Check scalr-role.* security group
         } elseif (stripos($groupName, 'scalr-role.') === 0) {
             if (!isset($sgList[$groupName])) {
                 try {
                     $securityGroupId = $ec2->securityGroup->create($groupName, sprintf("Security group for FarmRoleID N%s on FarmID N%s", $DBServer->GetFarmRoleObject()->ID, $DBServer->farmId), $vpcId);
                     sleep(2);
                     // DB rules
                     $dbRules = $DBServer->GetFarmRoleObject()->GetRoleObject()->getSecurityRules();
                     $groupRules = array();
                     foreach ($dbRules as $rule) {
                         $groupRules[CryptoTool::hash($rule['rule'])] = $rule;
                     }
                     // Behavior rules
                     foreach (\Scalr_Role_Behavior::getListForFarmRole($DBServer->GetFarmRoleObject()) as $bObj) {
                         $bRules = $bObj->getSecurityRules();
                         foreach ($bRules as $r) {
                             if ($r) {
                                 $groupRules[CryptoTool::hash($r)] = array('rule' => $r);
                             }
                         }
                     }
                     // Default rules
                     $userIdGroupPairList = new UserIdGroupPairList(new UserIdGroupPairData($DBServer->GetEnvironmentObject()->keychain(SERVER_PLATFORMS::EC2)->properties[Entity\CloudCredentialsProperty::AWS_ACCOUNT_ID], null, $groupName));
                     $rules = array(new IpPermissionData('tcp', 0, 65535, null, $userIdGroupPairList), new IpPermissionData('udp', 0, 65535, null, $userIdGroupPairList));
                     foreach ($groupRules as $rule) {
                         $group_rule = explode(":", $rule["rule"]);
                         $rules[] = new IpPermissionData($group_rule[0], $group_rule[1], $group_rule[2], new IpRangeData($group_rule[3]));
                     }
                     $ec2->securityGroup->authorizeIngress($rules, $securityGroupId);
                     $sgList[$groupName] = $securityGroupId;
                 } catch (Exception $e) {
                     throw new Exception(sprintf(_("Cannot create security group '%s': %s"), $groupName, $e->getMessage()));
                 }
             }
             array_push($retval, $sgList[$groupName]);
         } elseif ($groupName == \Scalr::config('scalr.aws.security_group_name')) {
             if (!isset($sgList[$groupName])) {
                 try {
                     $securityGroupId = $ec2->securityGroup->create($groupName, "Security rules needed by Scalr", $vpcId);
                     $ipRangeList = new IpRangeList();
                     foreach (\Scalr::config('scalr.aws.ip_pool') as $ip) {
                         $ipRangeList->append(new IpRangeData($ip));
                     }
                     // TODO: Open only FOR VPC ranges
                     $ipRangeList->append(new IpRangeData('10.0.0.0/8'));
                     sleep(2);
                     $ec2->securityGroup->authorizeIngress(array(new IpPermissionData('tcp', 3306, 3306, $ipRangeList), new IpPermissionData('tcp', 8008, 8013, $ipRangeList), new IpPermissionData('udp', 8014, 8014, $ipRangeList)), $securityGroupId);
                     $sgList[$groupName] = $securityGroupId;
                 } catch (Exception $e) {
                     throw new Exception(sprintf(_("Cannot create security group '%s': %s"), $groupName, $e->getMessage()));
                 }
             }
             array_push($retval, $sgList[$groupName]);
         } else {
             if (!isset($sgList[$groupName])) {
                 if (!in_array($groupName, $wildCardSgs)) {
                     throw new Exception(sprintf(_("Security group '%s' is not found"), $groupName));
                 } else {
                     $wildCardMatchedSgs = [];
                     $groupNamePattern = \Scalr_Governance::convertAsteriskPatternToRegexp($groupName);
                     foreach ($sgList as $sgGroupName => $sgGroupId) {
                         if (preg_match($groupNamePattern, $sgGroupName) === 1) {
                             array_push($wildCardMatchedSgs, $sgGroupId);
                         }
                     }
                     if (empty($wildCardMatchedSgs)) {
                         throw new Exception(sprintf(_("Security group matched to pattern '%s' is not found."), $groupName));
                     } else {
                         if (count($wildCardMatchedSgs) > 1) {
                             throw new Exception(sprintf(_("There are more than one Security group matched to pattern '%s' found."), $groupName));
                         } else {
                             array_push($retval, $wildCardMatchedSgs[0]);
                         }
                     }
                 }
             } else {
                 array_push($retval, $sgList[$groupName]);
             }
         }
     }
     return $retval;
 }
예제 #17
0
 /**
  * Gets the list of the security groups for the specified db server.
  *
  * If server does not have required security groups this method will create them.
  *
  * @param   DBServer               $DBServer The DB Server instance
  * @param   \Scalr\Service\Aws\Ec2 $ec2      Ec2 Client instance
  * @param   string                 $vpcId    optional The ID of VPC
  * @return  array  Returns array looks like array(groupid-1, groupid-2, ..., groupid-N)
  */
 private function GetServerSecurityGroupsList(DBServer $DBServer, \Scalr\Service\Aws\Ec2 $ec2, $vpcId = "", \Scalr_Governance $governance = null)
 {
     $retval = array();
     $checkGroups = array();
     $sgGovernance = true;
     $allowAdditionalSgs = true;
     $vpcId = null;
     if ($governance) {
         $sgs = $governance->getValue(\SERVER_PLATFORMS::EUCALYPTUS, \Scalr_Governance::EUCALYPTUS_SECURITY_GROUPS);
         if ($sgs !== null) {
             $governanceSecurityGroups = @explode(",", $sgs);
             if (!empty($governanceSecurityGroups)) {
                 foreach ($governanceSecurityGroups as $sg) {
                     if ($sg != '') {
                         array_push($checkGroups, trim($sg));
                     }
                 }
             }
             $sgGovernance = false;
             $allowAdditionalSgs = $governance->getValue(\SERVER_PLATFORMS::EUCALYPTUS, \Scalr_Governance::EUCALYPTUS_SECURITY_GROUPS, 'allow_additional_sec_groups');
         }
     }
     if (!$sgGovernance || $allowAdditionalSgs) {
         if ($DBServer->farmRoleId != 0) {
             $dbFarmRole = $DBServer->GetFarmRoleObject();
             if ($dbFarmRole->GetSetting(DBFarmRole::SETTING_EUCA_SECURITY_GROUPS_LIST) !== null) {
                 // New SG management
                 $sgs = @json_decode($dbFarmRole->GetSetting(DBFarmRole::SETTING_EUCA_SECURITY_GROUPS_LIST));
                 if (!empty($sgs)) {
                     foreach ($sgs as $sg) {
                         if (stripos($sg, 'sg-') === 0) {
                             array_push($retval, $sg);
                         } else {
                             array_push($checkGroups, $sg);
                         }
                     }
                 }
             }
         } else {
             array_push($checkGroups, 'scalr-rb-system');
         }
     }
     // No name based security groups, return only SG ids.
     if (empty($checkGroups)) {
         return $retval;
     }
     // Filter groups
     $filter = array(array('name' => SecurityGroupFilterNameType::groupName(), 'value' => $checkGroups));
     // Get filtered list of SG required by scalr;
     try {
         $list = $ec2->securityGroup->describe(null, null, $filter);
         $sgList = array();
         foreach ($list as $sg) {
             /* @var $sg \Scalr\Service\Aws\Ec2\DataType\SecurityGroupData */
             if ($vpcId == '' && !$sg->vpcId || $vpcId && $sg->vpcId == $vpcId) {
                 $sgList[$sg->groupName] = $sg->groupId;
             }
         }
         unset($list);
     } catch (\Exception $e) {
         throw new \Exception("Cannot get list of security groups (1): {$e->getMessage()}");
     }
     foreach ($checkGroups as $groupName) {
         // Check default SG
         if ($groupName == 'default') {
             array_push($retval, $sgList[$groupName]);
             // Check Roles builder SG
         } elseif ($groupName == 'scalr-rb-system') {
             if (!isset($sgList[$groupName])) {
                 try {
                     $securityGroupId = $ec2->securityGroup->create('scalr-rb-system', "Security group for Roles Builder", $vpcId);
                     $ipRangeList = new IpRangeList();
                     foreach (\Scalr::config('scalr.aws.ip_pool') as $ip) {
                         $ipRangeList->append(new IpRangeData($ip));
                     }
                     sleep(2);
                     $ec2->securityGroup->authorizeIngress(array(new IpPermissionData('tcp', 22, 22, $ipRangeList), new IpPermissionData('tcp', 8008, 8013, $ipRangeList)), $securityGroupId);
                     $sgList['scalr-rb-system'] = $securityGroupId;
                 } catch (\Exception $e) {
                     throw new \Exception(sprintf(_("Cannot create security group '%s': %s"), 'scalr-rb-system', $e->getMessage()));
                 }
             }
             array_push($retval, $sgList[$groupName]);
             //Check scalr-farm.* security group
         } elseif (stripos($groupName, 'scalr-farm.') === 0) {
             if (!isset($sgList[$groupName])) {
                 try {
                     $securityGroupId = $ec2->securityGroup->create($groupName, sprintf("Security group for FarmID N%s", $DBServer->farmId), $vpcId);
                     sleep(2);
                     $userIdGroupPairList = new UserIdGroupPairList(new UserIdGroupPairData($DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::ACCOUNT_ID), null, $groupName));
                     $ec2->securityGroup->authorizeIngress(array(new IpPermissionData('tcp', 0, 65535, null, $userIdGroupPairList), new IpPermissionData('udp', 0, 65535, null, $userIdGroupPairList)), $securityGroupId);
                     $sgList[$groupName] = $securityGroupId;
                 } catch (\Exception $e) {
                     throw new \Exception(sprintf(_("Cannot create security group '%s': %s"), $groupName, $e->getMessage()));
                 }
             }
             array_push($retval, $sgList[$groupName]);
             //Check scalr-role.* security group
         } elseif (stripos($groupName, 'scalr-role.') === 0) {
             if (!isset($sgList[$groupName])) {
                 try {
                     $securityGroupId = $ec2->securityGroup->create($groupName, sprintf("Security group for FarmRoleID N%s on FarmID N%s", $DBServer->GetFarmRoleObject()->ID, $DBServer->farmId), $vpcId);
                     sleep(2);
                     // DB rules
                     $dbRules = $DBServer->GetFarmRoleObject()->GetRoleObject()->getSecurityRules();
                     $groupRules = array();
                     foreach ($dbRules as $rule) {
                         $groupRules[\Scalr_Util_CryptoTool::hash($rule['rule'])] = $rule;
                     }
                     // Behavior rules
                     foreach (\Scalr_Role_Behavior::getListForFarmRole($DBServer->GetFarmRoleObject()) as $bObj) {
                         $bRules = $bObj->getSecurityRules();
                         foreach ($bRules as $r) {
                             if ($r) {
                                 $groupRules[\Scalr_Util_CryptoTool::hash($r)] = array('rule' => $r);
                             }
                         }
                     }
                     // Default rules
                     $userIdGroupPairList = new UserIdGroupPairList(new UserIdGroupPairData($DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::ACCOUNT_ID), null, $groupName));
                     $rules = array(new IpPermissionData('tcp', 0, 65535, null, $userIdGroupPairList), new IpPermissionData('udp', 0, 65535, null, $userIdGroupPairList));
                     foreach ($groupRules as $rule) {
                         $group_rule = explode(":", $rule["rule"]);
                         $rules[] = new IpPermissionData($group_rule[0], $group_rule[1], $group_rule[2], new IpRangeData($group_rule[3]));
                     }
                     $ec2->securityGroup->authorizeIngress($rules, $securityGroupId);
                     $sgList[$groupName] = $securityGroupId;
                 } catch (\Exception $e) {
                     throw new \Exception(sprintf(_("Cannot create security group '%s': %s"), $groupName, $e->getMessage()));
                 }
             }
             array_push($retval, $sgList[$groupName]);
         } elseif ($groupName == \Scalr::config('scalr.aws.security_group_name')) {
             if (!isset($sgList[$groupName])) {
                 try {
                     $securityGroupId = $ec2->securityGroup->create($groupName, "Security rules needed by Scalr", $vpcId);
                     $ipRangeList = new IpRangeList();
                     foreach (\Scalr::config('scalr.aws.ip_pool') as $ip) {
                         $ipRangeList->append(new IpRangeData($ip));
                     }
                     // TODO: Open only FOR VPC ranges
                     $ipRangeList->append(new IpRangeData('10.0.0.0/8'));
                     sleep(2);
                     $ec2->securityGroup->authorizeIngress(array(new IpPermissionData('tcp', 3306, 3306, $ipRangeList), new IpPermissionData('tcp', 8008, 8013, $ipRangeList), new IpPermissionData('udp', 8014, 8014, $ipRangeList)), $securityGroupId);
                     $sgList[$groupName] = $securityGroupId;
                 } catch (\Exception $e) {
                     throw new \Exception(sprintf(_("Cannot create security group '%s': %s"), $groupName, $e->getMessage()));
                 }
             }
             array_push($retval, $sgList[$groupName]);
         } else {
             if (!isset($sgList[$groupName])) {
                 throw new \Exception(sprintf(_("Security group '%s' is not found"), $groupName));
             } else {
                 array_push($retval, $sgList[$groupName]);
             }
         }
     }
     return $retval;
 }
예제 #18
0
파일: Manager.php 프로젝트: mheydt/scalr
 /**
  * Makes a decision to scale farm
  *
  * @return Scalr_Scaling_Decision
  */
 function makeScalingDecision()
 {
     /**
      * Base Scaling
      */
     foreach (Scalr_Role_Behavior::getListForFarmRole($this->dbFarmRole) as $behavior) {
         $result = $behavior->makeUpscaleDecision($this->dbFarmRole);
         if ($result === false) {
             continue;
         } else {
             return $result;
         }
     }
     $farmPendingInstances = $this->db->GetOne("SELECT COUNT(*) FROM servers WHERE farm_id=? AND status IN (?,?,?)", array($this->dbFarmRole->FarmID, SERVER_STATUS::PENDING, SERVER_STATUS::INIT, SERVER_STATUS::PENDING_LAUNCH));
     if ($this->dbFarmRole->GetFarmObject()->RolesLaunchOrder == 1 && $farmPendingInstances > 0) {
         if ($this->dbFarmRole->GetRunningInstancesCount() == 0) {
             $this->logger->info("{$farmPendingInstances} instances in pending state. Launch roles one-by-one. Waiting...");
             return Scalr_Scaling_Decision::STOP_SCALING;
         }
     }
     $scalingMetricDecision = null;
     $scalingMetricInstancesCount = null;
     $this->logger->info(sprintf(_("%s scaling rules configured"), count($this->farmRoleMetrics)));
     if (isset($this->farmRoleMetrics[Entity\ScalingMetric::METRIC_DATE_AND_TIME_ID])) {
         $dateAndTimeMetric = $this->farmRoleMetrics[Entity\ScalingMetric::METRIC_DATE_AND_TIME_ID];
         try {
             $scalingMetricDecision = $dateAndTimeMetric->getScalingDecision();
         } catch (Exception $e) {
             $this->logger->error("Scaling error in deciding metric '{$dateAndTimeMetric->getMetric()->name}' for farm role '{$this->dbFarmRole->FarmID}:{$this->dbFarmRole->Alias}': {$e->getMessage()}");
         }
         $scalingMetricName = $dateAndTimeMetric->getMetric()->name;
         $scalingMetricInstancesCount = $dateAndTimeMetric->instancesNumber;
         $this->decisonInfo = '1';
         if ($scalingMetricDecision !== Scalr_Scaling_Decision::NOOP) {
             $this->logDecisionInfo($scalingMetricDecision, $scalingMetricName, $dateAndTimeMetric->lastValue);
             return $scalingMetricDecision;
         }
     }
     /**
      * Metrics scaling
      */
     $farmRoleMetrics = array_diff_key($this->farmRoleMetrics, [Entity\ScalingMetric::METRIC_DATE_AND_TIME_ID => null]);
     if (!empty($farmRoleMetrics)) {
         $checkAllMetrics = $this->dbFarmRole->GetSetting(Entity\FarmRoleSetting::SCALING_DOWN_ONLY_IF_ALL_METRICS_TRUE);
         $variousDecisions = false;
         /* @var $farmRoleMetric Scalr_Scaling_FarmRoleMetric */
         foreach ($farmRoleMetrics as $farmRoleMetric) {
             try {
                 $newDecision = $farmRoleMetric->getScalingDecision();
             } catch (Exception $e) {
                 $this->logger->error("Scaling error in deciding metric '{$farmRoleMetric->getMetric()->name}' for farm role '{$this->dbFarmRole->FarmID}:{$this->dbFarmRole->Alias}': {$e->getMessage()}");
                 continue;
             }
             if (isset($scalingMetricDecision)) {
                 if ($newDecision != $scalingMetricDecision) {
                     $variousDecisions = true;
                 }
             }
             $scalingMetricDecision = $newDecision;
             $scalingMetricName = $farmRoleMetric->getMetric()->name;
             $this->decisonInfo = '1';
             switch ($scalingMetricDecision) {
                 case Scalr_Scaling_Decision::NOOP:
                     continue;
                 case Scalr_Scaling_Decision::DOWNSCALE:
                     if (!$checkAllMetrics) {
                         break 2;
                     }
                     continue;
                 case Scalr_Scaling_Decision::UPSCALE:
                     break 2;
             }
         }
         if (isset($scalingMetricDecision) && !($scalingMetricDecision == Scalr_Scaling_Decision::DOWNSCALE && $checkAllMetrics && $variousDecisions)) {
             $this->logDecisionInfo($scalingMetricDecision, $scalingMetricName, isset($farmRoleMetric) ? $farmRoleMetric->lastValue : null);
         } else {
             $scalingMetricDecision = Scalr_Scaling_Decision::NOOP;
         }
     }
     return $this->getFinalDecision($scalingMetricDecision, $scalingMetricInstancesCount);
 }
 public function OnBeforeHostTerminate(BeforeHostTerminateEvent $event)
 {
     try {
         $dbFarm = DBFarm::LoadByID($this->FarmID);
     } catch (Exception $e) {
     }
     if ($dbFarm) {
         $servers = $dbFarm->GetServersByFilter(array('status' => array(SERVER_STATUS::INIT, SERVER_STATUS::RUNNING, SERVER_STATUS::PENDING_TERMINATE, SERVER_STATUS::PENDING_SUSPEND)));
         foreach ($servers as $DBServer) {
             // We don't need to send beforeHostTerminate event to all "Pending terminate" servers,
             // only tu eventServer.
             if ($DBServer->status == SERVER_STATUS::PENDING_TERMINATE) {
                 if ($DBServer->serverId != $event->DBServer->serverId) {
                     continue;
                 }
             }
             $msg = new Scalr_Messaging_Msg_BeforeHostTerminate();
             $msg->setServerMetaData($event->DBServer);
             $msg->suspend = $event->suspend;
             $msg = Scalr_Scripting_Manager::extendMessage($msg, $event, $event->DBServer, $DBServer);
             if ($event->DBServer->farmRoleId != 0) {
                 foreach (Scalr_Role_Behavior::getListForFarmRole($event->DBServer->GetFarmRoleObject()) as $behavior) {
                     $msg = $behavior->extendMessage($msg, $event->DBServer);
                 }
             }
             $DBServer->SendMessage($msg, false, true);
         }
         try {
             if ($event->DBServer->GetFarmRoleObject()->GetSetting(Scalr_Db_Msr::SLAVE_TO_MASTER) != 1) {
                 if ($event->DBServer->GetFarmRoleObject()->GetRoleObject()->getDbMsrBehavior()) {
                     $this->sendPromoteToMasterMessage($event);
                 }
             }
         } catch (Exception $e) {
         }
     }
 }
예제 #20
0
파일: Builder.php 프로젝트: scalr/scalr
 public function xBuildAction()
 {
     $this->request->defineParams(array('farmId' => array('type' => 'int'), 'roles' => array('type' => 'json'), 'rolesToRemove' => array('type' => 'json'), 'farm' => array('type' => 'json'), 'launch' => array('type' => 'bool')));
     if (!$this->isFarmConfigurationValid($this->getParam('farmId'), $this->getParam('farm'), (array) $this->getParam('roles'))) {
         if ($this->errors['error_count'] != 0) {
             $this->response->failure();
             $this->response->data(array('errors' => $this->errors));
             return;
         }
     }
     $farm = $this->getParam('farm');
     $client = Client::Load($this->user->getAccountId());
     if ($this->getParam('farmId')) {
         $dbFarm = DBFarm::LoadByID($this->getParam('farmId'));
         $this->user->getPermissions()->validate($dbFarm);
         $this->request->checkPermissions($dbFarm->__getNewFarmObject(), Acl::PERM_FARMS_UPDATE);
         $dbFarm->isLocked();
         if ($this->getParam('changed') && $dbFarm->changedTime && $this->getParam('changed') != $dbFarm->changedTime) {
             $userName = '******';
             $changed = explode(' ', $this->getParam('changed'));
             $changedTime = intval($changed[1]);
             try {
                 $user = new Scalr_Account_User();
                 $user->loadById($dbFarm->changedByUserId);
                 $userName = $user->getEmail();
             } catch (Exception $e) {
             }
             $this->response->failure();
             $this->response->data(array('changedFailure' => sprintf('%s changed this farm at %s', $userName, Scalr_Util_DateTime::convertTz($changedTime))));
             return;
         } else {
             if ($this->getParam('changed')) {
                 $this->checkFarmConfigurationIntegrity($this->getParam('farmId'), $this->getParam('farm'), (array) $this->getParam('roles'), (array) $this->getParam('rolesToRemove'));
             }
         }
         $dbFarm->changedByUserId = $this->user->getId();
         $dbFarm->changedTime = microtime();
         if ($this->getContainer()->analytics->enabled) {
             $projectId = $farm['projectId'];
             if (empty($projectId)) {
                 $ccId = $dbFarm->GetEnvironmentObject()->getPlatformConfigValue(Scalr_Environment::SETTING_CC_ID);
                 if (!empty($ccId)) {
                     //Assigns Project automatically only if it is the one withing the Cost Center
                     $projects = ProjectEntity::findByCcId($ccId);
                     if (count($projects) == 1) {
                         $projectId = $projects->getArrayCopy()[0]->projectId;
                     }
                 }
             }
             if (!empty($projectId) && $dbFarm->GetSetting(Entity\FarmSetting::PROJECT_ID) != $projectId) {
                 $this->request->checkPermissions($dbFarm->__getNewFarmObject(), Acl::PERM_FARMS_PROJECTS);
             }
         }
         $bNew = false;
     } else {
         $this->request->restrictAccess(Acl::RESOURCE_OWN_FARMS, Acl::PERM_FARMS_CREATE);
         $this->user->getAccount()->validateLimit(Scalr_Limits::ACCOUNT_FARMS, 1);
         $dbFarm = new DBFarm();
         $dbFarm->ClientID = $this->user->getAccountId();
         $dbFarm->EnvID = $this->getEnvironmentId();
         $dbFarm->Status = FARM_STATUS::TERMINATED;
         $dbFarm->ownerId = $this->user->getId();
         $dbFarm->changedByUserId = $this->user->getId();
         $dbFarm->changedTime = microtime();
         $bNew = true;
     }
     if ($this->getParam('farm')) {
         $dbFarm->Name = $this->request->stripValue($farm['name']);
         $dbFarm->RolesLaunchOrder = $farm['rolesLaunchOrder'];
         $dbFarm->Comments = $this->request->stripValue($farm['description']);
     }
     if (empty($dbFarm->Name)) {
         throw new Exception(_("Farm name required"));
     }
     $setFarmTeams = false;
     if ($bNew) {
         $setFarmTeams = true;
     } else {
         if ($dbFarm->ownerId == $this->user->getId() || $this->request->hasPermissions($dbFarm->__getNewFarmObject(), Acl::PERM_FARMS_CHANGE_OWNERSHIP)) {
             if (is_numeric($farm['owner']) && $farm['owner'] != $dbFarm->ownerId) {
                 $dbFarm->ownerId = $farm['owner'];
                 $f = Entity\Farm::findPk($dbFarm->ID);
                 Entity\FarmSetting::addOwnerHistory($f, User::findPk($farm['owner']), User::findPk($this->user->getId()));
                 $f->save();
             }
             $setFarmTeams = true;
         }
     }
     $dbFarm->save();
     if ($setFarmTeams && is_array($farm['teamOwner'])) {
         /* @var $f Entity\Farm */
         $f = Entity\Farm::findPk($dbFarm->ID);
         $f->setTeams(empty($farm['teamOwner']) ? [] : Entity\Account\Team::find([['name' => ['$in' => $farm['teamOwner']]], ['accountId' => $this->getUser()->accountId]]));
         $f->save();
     }
     if ($bNew) {
         $dbFarm->SetSetting(Entity\FarmSetting::CREATED_BY_ID, $this->user->getId());
         $dbFarm->SetSetting(Entity\FarmSetting::CREATED_BY_EMAIL, $this->user->getEmail());
     }
     $governance = new Scalr_Governance($this->getEnvironmentId());
     if (!$this->getParam('farmId') && $governance->isEnabled(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_LEASE)) {
         $dbFarm->SetSetting(Entity\FarmSetting::LEASE_STATUS, 'Active');
         // for created farm
     }
     if (isset($farm['variables'])) {
         $variables = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(), ScopeInterface::SCOPE_FARM);
         $variables->setValues(is_array($farm['variables']) ? $farm['variables'] : [], 0, $dbFarm->ID, 0, '', false, true);
     }
     if (!$farm['timezone']) {
         $farm['timezone'] = date_default_timezone_get();
     }
     $dbFarm->SetSetting(Entity\FarmSetting::TIMEZONE, $farm['timezone']);
     $dbFarm->SetSetting(Entity\FarmSetting::EC2_VPC_ID, isset($farm["vpc_id"]) ? $farm['vpc_id'] : null);
     $dbFarm->SetSetting(Entity\FarmSetting::EC2_VPC_REGION, isset($farm["vpc_id"]) ? $farm['vpc_region'] : null);
     $dbFarm->SetSetting(Entity\FarmSetting::SZR_UPD_REPOSITORY, $farm[Entity\FarmSetting::SZR_UPD_REPOSITORY]);
     $dbFarm->SetSetting(Entity\FarmSetting::SZR_UPD_SCHEDULE, $farm[Entity\FarmSetting::SZR_UPD_SCHEDULE]);
     if (!$dbFarm->GetSetting(Entity\FarmSetting::CRYPTO_KEY)) {
         $dbFarm->SetSetting(Entity\FarmSetting::CRYPTO_KEY, Scalr::GenerateRandomKey(40));
     }
     if ($this->getContainer()->analytics->enabled) {
         //Cost analytics project must be set for the Farm object
         $dbFarm->setProject(!empty($farm['projectId']) ? $farm['projectId'] : null);
     }
     $virtualFarmRoles = array();
     $roles = $this->getParam('roles');
     if (!empty($roles)) {
         foreach ($roles as $role) {
             if (strpos($role['farm_role_id'], "virtual_") !== false) {
                 $dbRole = DBRole::loadById($role['role_id']);
                 $dbFarmRole = $dbFarm->AddRole($dbRole, $role['platform'], $role['cloud_location'], (int) $role['launch_index'], $role['alias']);
                 $virtualFarmRoles[$role['farm_role_id']] = $dbFarmRole->ID;
             }
         }
     }
     $usedPlatforms = array();
     $farmRoleVariables = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(), ScopeInterface::SCOPE_FARMROLE);
     if (!empty($roles)) {
         foreach ($roles as $role) {
             if ($role['farm_role_id']) {
                 if (isset($virtualFarmRoles[$role['farm_role_id']])) {
                     $role['farm_role_id'] = $virtualFarmRoles[$role['farm_role_id']];
                 }
                 $update = true;
                 $dbFarmRole = DBFarmRole::LoadByID($role['farm_role_id']);
                 $dbRole = DBRole::loadById($dbFarmRole->RoleID);
                 $role['role_id'] = $dbFarmRole->RoleID;
                 if ($dbFarmRole->Platform == SERVER_PLATFORMS::GCE) {
                     $dbFarmRole->CloudLocation = $role['cloud_location'];
                 }
             } else {
                 /** TODO:  Remove because will be handled with virtual_ **/
                 $update = false;
                 $dbRole = DBRole::loadById($role['role_id']);
                 $dbFarmRole = $dbFarm->AddRole($dbRole, $role['platform'], $role['cloud_location'], (int) $role['launch_index']);
             }
             if ($dbRole->hasBehavior(ROLE_BEHAVIORS::RABBITMQ)) {
                 $role['settings'][Entity\FarmRoleSetting::SCALING_MAX_INSTANCES] = $role['settings'][Entity\FarmRoleSetting::SCALING_MIN_INSTANCES];
             }
             if ($update) {
                 $dbFarmRole->LaunchIndex = (int) $role['launch_index'];
                 $dbFarmRole->Alias = $role['alias'];
                 $dbFarmRole->Save();
             }
             $usedPlatforms[$role['platform']] = 1;
             $oldRoleSettings = $dbFarmRole->GetAllSettings();
             // Update virtual farm_role_id with actual value
             $scripts = (array) $role['scripting'];
             if (!empty($virtualFarmRoles)) {
                 array_walk_recursive($scripts, function (&$v, $k) use($virtualFarmRoles) {
                     if (is_string($v)) {
                         $v = str_replace(array_keys($virtualFarmRoles), array_values($virtualFarmRoles), $v);
                     }
                 });
                 array_walk_recursive($role['settings'], function (&$v, $k) use($virtualFarmRoles) {
                     if (is_string($v)) {
                         $v = str_replace(array_keys($virtualFarmRoles), array_values($virtualFarmRoles), $v);
                     }
                 });
             }
             $dbFarmRole->ClearSettings("chef.");
             if (!empty($role['scaling_settings']) && is_array($role['scaling_settings'])) {
                 foreach ($role['scaling_settings'] as $k => $v) {
                     $dbFarmRole->SetSetting($k, $v, Entity\FarmRoleSetting::TYPE_CFG);
                 }
             }
             foreach ($role['settings'] as $k => $v) {
                 $dbFarmRole->SetSetting($k, $v, Entity\FarmRoleSetting::TYPE_CFG);
             }
             /****** Scaling settings ******/
             $scalingManager = new Scalr_Scaling_Manager($dbFarmRole);
             $scalingManager->setFarmRoleMetrics(is_array($role['scaling']) ? $role['scaling'] : array());
             //TODO: optimize this code...
             $this->db->Execute("DELETE FROM farm_role_scaling_times WHERE farm_roleid=?", array($dbFarmRole->ID));
             // 5 = Time based scaling -> move to constants
             if (!empty($role['scaling'][Entity\ScalingMetric::METRIC_DATE_AND_TIME_ID])) {
                 foreach ($role['scaling'][Entity\ScalingMetric::METRIC_DATE_AND_TIME_ID] as $scal_period) {
                     $chunks = explode(":", $scal_period['id']);
                     $this->db->Execute("INSERT INTO farm_role_scaling_times SET\n                            farm_roleid\t\t= ?,\n                            start_time\t\t= ?,\n                            end_time\t\t= ?,\n                            days_of_week\t= ?,\n                            instances_count\t= ?\n                        ", array($dbFarmRole->ID, $chunks[0], $chunks[1], $chunks[2], $chunks[3]));
                 }
             }
             /*****************/
             /* Add script options to databse */
             $dbFarmRole->SetScripts($scripts, (array) $role['scripting_params']);
             /* End of scripting section */
             /* Add storage configuration */
             if (isset($role['storages']['configs'])) {
                 $dbFarmRole->getStorage()->setConfigs($role['storages']['configs'], false);
             }
             $farmRoleVariables->setValues(is_array($role['variables']) ? $role['variables'] : [], $dbFarmRole->GetRoleID(), $dbFarm->ID, $dbFarmRole->ID, '', false, true);
             foreach (Scalr_Role_Behavior::getListForFarmRole($dbFarmRole) as $behavior) {
                 $behavior->onFarmSave($dbFarm, $dbFarmRole);
             }
             /**
              * Platform specified updates
              */
             if ($dbFarmRole->Platform == SERVER_PLATFORMS::EC2) {
                 \Scalr\Modules\Platforms\Ec2\Helpers\EbsHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
                 \Scalr\Modules\Platforms\Ec2\Helpers\EipHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
                 if ($role['settings']['aws.elb.remove']) {
                     $this->request->restrictAccess(Acl::RESOURCE_AWS_ELB, Acl::PERM_AWS_ELB_MANAGE);
                 }
                 \Scalr\Modules\Platforms\Ec2\Helpers\ElbHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
             }
             if (in_array($dbFarmRole->Platform, array(SERVER_PLATFORMS::IDCF, SERVER_PLATFORMS::CLOUDSTACK))) {
                 Scalr\Modules\Platforms\Cloudstack\Helpers\CloudstackHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $role['settings']);
             }
         }
     }
     $rolesToRemove = $this->getParam('rolesToRemove');
     if (!empty($rolesToRemove)) {
         $currentFarmRoles = Entity\FarmRole::find([['farmId' => $dbFarm->ID], ['id' => ['$in' => $rolesToRemove]]]);
         /* @var $farmRole Entity\FarmRole */
         foreach ($currentFarmRoles as $farmRole) {
             $farmRole->delete();
         }
     }
     $dbFarm->save();
     if (!$client->GetSettingValue(CLIENT_SETTINGS::DATE_FARM_CREATED)) {
         $client->SetSettingValue(CLIENT_SETTINGS::DATE_FARM_CREATED, time());
     }
     if ($this->request->hasPermissions($dbFarm->__getNewFarmObject(), Acl::PERM_FARMS_LAUNCH_TERMINATE) && $this->getParam('launch')) {
         $this->user->getPermissions()->validate($dbFarm);
         $dbFarm->isLocked();
         Scalr::FireEvent($dbFarm->ID, new FarmLaunchedEvent(true, $this->user->id));
         $this->response->success('Farm successfully saved and launched');
     } else {
         $this->response->success('Farm successfully saved');
     }
     $this->response->data(array('farmId' => $dbFarm->ID, 'isNewFarm' => $bNew));
 }