/**
* Creates clone for the farm
*
* @param string $name The name of the farm
* @param Scalr_Account_User $user The user object
* @param int $envId The identifier of the environment
* @return DBFarm Returns clone
*/
public function cloneFarm($name = false, Scalr_Account_User $user, $envId)
{
$account = $user->getAccount();
$account->validateLimit(Scalr_Limits::ACCOUNT_FARMS, 1);
$definition = $this->getDefinition();
if (!$name) {
$template = "";
if (!preg_match('/^(.*?)\\(clone \\#([0-9]*)\\)$/si', $definition->name)) {
$name = $definition->name;
} else {
preg_match('/^(.*?)\\(clone \\#([0-9]*)\\)$/si', $definition->name, $matches);
$name = trim($matches[1]);
}
$name = preg_replace('/[^A-Za-z0-9_\\. -]+/', '', $name);
$lastUsedIndex = $this->DB->GetOne('SELECT MAX(CAST((SUBSTR(@p:=SUBSTRING_INDEX(name, \'#\', -1), 1, LENGTH(@p) - 1)) AS UNSIGNED)) as lastUsedCloneNumber FROM farms WHERE name REGEXP \'' . str_replace('.', '\\.', $name) . ' \\\\(clone #[0-9]+\\\\)\' AND env_id = ?', array($envId));
$name = $name . ' (clone #' . ($lastUsedIndex + 1) . ')';
}
$dbFarm = self::create($name, $user, $envId);
$dbFarm->createdByUserId = $user->id;
$dbFarm->createdByUserEmail = $user->getEmail();
$dbFarm->RolesLaunchOrder = $definition->rolesLaunchOrder;
$dbFarm->teamId = $definition->teamId;
$dbFarm->SetSetting(Entity\FarmSetting::TIMEZONE, $definition->settings[Entity\FarmSetting::TIMEZONE]);
$dbFarm->SetSetting(Entity\FarmSetting::EC2_VPC_ID, $definition->settings[Entity\FarmSetting::EC2_VPC_ID]);
$dbFarm->SetSetting(Entity\FarmSetting::EC2_VPC_REGION, $definition->settings[Entity\FarmSetting::EC2_VPC_REGION]);
$dbFarm->SetSetting(Entity\FarmSetting::SZR_UPD_REPOSITORY, $definition->settings[Entity\FarmSetting::SZR_UPD_REPOSITORY]);
$dbFarm->SetSetting(Entity\FarmSetting::SZR_UPD_SCHEDULE, $definition->settings[Entity\FarmSetting::SZR_UPD_SCHEDULE]);
$dbFarm->SetSetting(Entity\FarmSetting::LEASE_STATUS, $definition->settings[Entity\FarmSetting::LEASE_STATUS]);
if ($definition->settings[Entity\FarmSetting::PROJECT_ID]) {
$dbFarm->SetSetting(Entity\FarmSetting::PROJECT_ID, $definition->settings[Entity\FarmSetting::PROJECT_ID]);
}
$variables = new Scalr_Scripting_GlobalVariables($dbFarm->ClientID, $envId, ScopeInterface::SCOPE_FARM);
$variables->setValues($definition->globalVariables, 0, $dbFarm->ID, 0);
foreach ($definition->roles as $index => $role) {
$dbFarmRole = $dbFarm->AddRole(DBRole::loadById($role->roleId), $role->platform, $role->cloudLocation, $role->launchIndex, $role->alias);
$oldRoleSettings = $dbFarmRole->GetAllSettings();
$dbFarmRole->applyDefinition($role, true);
$newSettings = $dbFarmRole->GetAllSettings();
// Platform specified updates
if ($dbFarmRole->Platform == SERVER_PLATFORMS::EC2) {
\Scalr\Modules\Platforms\Ec2\Helpers\EbsHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $newSettings);
\Scalr\Modules\Platforms\Ec2\Helpers\EipHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $newSettings);
\Scalr\Modules\Platforms\Ec2\Helpers\ElbHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $newSettings);
}
if (in_array($dbFarmRole->Platform, array(SERVER_PLATFORMS::IDCF, SERVER_PLATFORMS::CLOUDSTACK))) {
CloudstackHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $newSettings);
}
$dbFarmRolesList[] = $dbFarmRole;
$usedPlatforms[$role->platform] = 1;
}
$dbFarm->save();
return $dbFarm;
}