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)); }