public function getConfiguration(DBServer $dbServer) { $config = new stdClass(); $config->virtualHosts = array(); $vhosts = $this->db->Execute("SELECT * FROM `apache_vhosts` WHERE farm_id = ? AND farm_roleid = ?", array($dbServer->farmId, $dbServer->farmRoleId)); while ($vhost = $vhosts->FetchRow()) { if ($vhost['is_ssl_enabled']) { $itm = new stdClass(); $itm->hostname = $vhost['name']; $itm->port = 80; $itm->template = $vhost['httpd_conf']; $itm->ssl = 0; $itm->sslCertificateId = null; $vars = unserialize($vhost['httpd_conf_vars']); $vars['host'] = $vhost['name']; $vKeys = array_keys($vars); $keys = array_map(function ($item) { return '{$' . $item . '}'; }, $vKeys); $vValues = array_values($vars); $itm->template = str_replace($keys, $vValues, $itm->template); $dbServer->applyGlobalVarsToValue($itm->template); array_push($config->virtualHosts, $itm); } $itm = new stdClass(); $itm->hostname = $vhost['name']; $itm->port = $vhost['is_ssl_enabled'] ? 443 : 80; $itm->template = $vhost['is_ssl_enabled'] ? $vhost['httpd_conf_ssl'] : $vhost['httpd_conf']; $itm->ssl = $vhost['is_ssl_enabled']; $itm->sslCertificateId = $vhost['ssl_cert_id']; $vars = unserialize($vhost['httpd_conf_vars']); $vars['host'] = $vhost['name']; $vKeys = array_keys($vars); $keys = array_map(function ($item) { return '{$' . $item . '}'; }, $vKeys); $vValues = array_values($vars); $itm->template = str_replace($keys, $vValues, $itm->template); $dbServer->applyGlobalVarsToValue($itm->template); array_push($config->virtualHosts, $itm); } return $config; }
public static function prepareScript($scriptSettings, DBServer $targetServer, AbstractServerEvent $event = null) { $template = ['type' => isset($scriptSettings['type']) ? $scriptSettings['type'] : null, 'timeout' => isset($scriptSettings['timeout']) ? $scriptSettings['timeout'] : null, 'issync' => isset($scriptSettings['issync']) ? $scriptSettings['issync'] : null, 'run_as' => isset($scriptSettings['run_as']) ? $scriptSettings['run_as'] : null, 'execution_id' => Scalr::GenerateUID()]; if ($scriptSettings['type'] == self::ORCHESTRATION_SCRIPT_TYPE_SCALR) { /* @var $script Script */ $script = Script::findPk($scriptSettings['scriptid']); if (!$script) { return false; } // TODO: validate permission to access script ? if ($script->os && $targetServer->osType && $script->os != $targetServer->osType) { return false; } if ($scriptSettings['version'] == 'latest' || (int) $scriptSettings['version'] == -1) { $version = $script->getLatestVersion(); } else { $version = $script->getVersion((int) $scriptSettings['version']); } if (empty($version)) { return false; } $template['name'] = $script->name; $template['id'] = $script->id; $template['body'] = $version->content; $template['scriptVersion'] = $version->version; // variables could be null $scriptParams = $script->allowScriptParameters ? (array) $version->variables : []; foreach ($scriptParams as &$val) { $val = ""; } $params = array_merge($scriptParams, $targetServer->GetScriptingVars(), (array) unserialize($scriptSettings['params'])); if ($event) { $eventServer = $event->DBServer; foreach ($eventServer->GetScriptingVars() as $k => $v) { $params["event_{$k}"] = $v; } foreach ($event->GetScriptingVars() as $k => $v) { $params[$k] = $event->{$v}; } if (isset($event->params) && is_array($event->params)) { foreach ($event->params as $k => $v) { $params[$k] = $v; } } $params['event_name'] = $event->GetName(); } if ($event instanceof CustomEvent && count($event->params) > 0) { $params = array_merge($params, $event->params); } // Prepare keys array and array with values for replacement in script $keys = array_keys($params); $keys = array_map(function ($item) { return '%' . $item . '%'; }, $keys); $values = array_values($params); $script_contents = str_replace($keys, $values, $template['body']); $template['body'] = str_replace('\\%', "%", $script_contents); // Generate script contents $template['name'] = preg_replace("/[^A-Za-z0-9]+/", "_", $template['name']); } elseif ($scriptSettings['type'] == self::ORCHESTRATION_SCRIPT_TYPE_LOCAL) { $template['path'] = $targetServer->applyGlobalVarsToValue($scriptSettings['script_path']); } elseif ($scriptSettings['type'] == self::ORCHESTRATION_SCRIPT_TYPE_CHEF) { $chef = new stdClass(); $chefSettings = (array) unserialize($scriptSettings['params']); if ($chefSettings['chef.cookbook_url']) { $chef->cookbookUrl = $chefSettings['chef.cookbook_url']; } if ($chefSettings['chef.cookbook_url_type']) { $chef->cookbookUrlType = $chefSettings['chef.cookbook_url_type']; } if ($chefSettings['chef.relative_path']) { $chef->relativePath = $chefSettings['chef.relative_path']; } if ($chefSettings['chef.ssh_private_key']) { $chef->sshPrivateKey = $chefSettings['chef.ssh_private_key']; } if ($chefSettings['chef.role_name']) { $chef->role = $chefSettings['chef.role_name']; } else { $chef->runList = $chefSettings['chef.runlist']; } $chef->jsonAttributes = $chefSettings['chef.attributes']; $template['chef'] = $chef; } return $template; }
public function getConfiguration(DBServer $dbServer) { $configuration = new stdClass(); $chefSettings = $dbServer->GetFarmRoleObject()->getChefSettings(); if (empty($chefSettings[self::ROLE_CHEF_BOOTSTRAP])) { return $configuration; } $jsonAttributes = $chefSettings[self::ROLE_CHEF_ATTRIBUTES]; if (!empty($chefSettings[self::ROLE_CHEF_COOKBOOK_URL])) { $configuration->cookbookUrl = $chefSettings[self::ROLE_CHEF_COOKBOOK_URL]; $configuration->runList = $chefSettings[self::ROLE_CHEF_RUNLIST]; $configuration->cookbookUrlType = $chefSettings[self::ROLE_CHEF_COOKBOOK_URL_TYPE]; $configuration->sshPrivateKey = isset($chefSettings[self::ROLE_CHEF_SSH_PRIVATE_KEY]) ? $chefSettings[self::ROLE_CHEF_SSH_PRIVATE_KEY] : false; $configuration->relativePath = isset($chefSettings[self::ROLE_CHEF_RELATIVE_PATH]) ? $chefSettings[self::ROLE_CHEF_RELATIVE_PATH] : false; } else { // Get chef server info $chefServerInfo = $this->db->GetRow("SELECT * FROM services_chef_servers WHERE id=?", array($chefSettings[self::ROLE_CHEF_SERVER_ID])); $chefServerInfo['v_auth_key'] = trim($this->getCrypto()->decrypt($chefServerInfo['v_auth_key'], $this->cryptoKey)); // Prepare node name $configuration->nodeName = $chefSettings[self::SERVER_CHEF_NODENAME]; if (!$configuration->nodeName) { $nodeNameTpl = $chefSettings[self::ROLE_CHEF_NODENAME_TPL]; if ($nodeNameTpl) { $configuration->nodeName = $dbServer->applyGlobalVarsToValue($nodeNameTpl); } } $configuration->serverUrl = $chefServerInfo['url']; $configuration->validatorName = $chefServerInfo['v_username']; $configuration->validatorKey = $chefServerInfo['v_auth_key']; if (!empty($chefSettings[self::ROLE_CHEF_ROLE_NAME])) { $configuration->role = $chefSettings[self::ROLE_CHEF_ROLE_NAME]; } else { $configuration->runList = $chefSettings[self::ROLE_CHEF_RUNLIST]; } $configuration->environment = $chefSettings[self::ROLE_CHEF_ENVIRONMENT]; $configuration->daemonize = $chefSettings[self::ROLE_CHEF_DAEMONIZE]; } $configuration->logLevel = !$chefSettings[self::ROLE_CHEF_LOG_LEVEL] ? 'auto' : $chefSettings[self::ROLE_CHEF_LOG_LEVEL]; if ($jsonAttributes) { $configuration->jsonAttributes = $dbServer->applyGlobalVarsToValue($jsonAttributes); } return $configuration; }
private function getServerDNSRecords(DBServer $DBServer) { $records = array(); if ($DBServer->status != SERVER_STATUS::RUNNING) { return $records; } if ($DBServer->GetProperty(SERVER_PROPERTIES::EXCLUDE_FROM_DNS)) { return $records; } $DBFarmRole = $DBServer->GetFarmRoleObject(); if ($DBFarmRole->GetSetting(DBFarmRole::SETTING_EXCLUDE_FROM_DNS)) { return $records; } if ($DBFarmRole->ID == $this->farmRoleId) { $ip = $this->privateRootRecords == 1 ? $DBServer->localIp : $DBServer->remoteIp; if ($ip) { array_push($records, array("name" => "@", "value" => $ip, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); } } if (!$DBFarmRole->GetSetting(DBFarmRole::SETTING_DNS_CREATE_RECORDS)) { return $records; } $int_record_alias = $DBFarmRole->GetSetting(DBFarmRole::SETTING_DNS_INT_RECORD_ALIAS); $int_record = "int-{$DBFarmRole->GetRoleObject()->name}"; $ext_record_alias = $DBFarmRole->GetSetting(DBFarmRole::SETTING_DNS_EXT_RECORD_ALIAS); $ext_record = "ext-{$DBFarmRole->GetRoleObject()->name}"; if ($int_record_alias) { $int_record = $DBServer->applyGlobalVarsToValue($int_record_alias); } if ($ext_record_alias) { $ext_record = $DBServer->applyGlobalVarsToValue($ext_record_alias); } array_push($records, array("name" => $int_record, "value" => $DBServer->localIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); array_push($records, array("name" => $ext_record, "value" => $DBServer->remoteIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); $records = array_merge($records, (array) $this->getDbRecords($DBServer)); $records = array_merge($records, (array) $this->getBehaviorsRecords($DBServer)); if ($DBFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MYSQL)) { array_push($records, array("name" => "int-mysql", "value" => $DBServer->localIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); array_push($records, array("name" => "ext-mysql", "value" => $DBServer->remoteIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); if ($DBServer->GetProperty(SERVER_PROPERTIES::DB_MYSQL_MASTER)) { array_push($records, array("name" => "int-mysql-master", "value" => $DBServer->localIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); array_push($records, array("name" => "ext-mysql-master", "value" => $DBServer->remoteIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); array_push($records, array("name" => "{$int_record}-master", "value" => $DBServer->localIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); array_push($records, array("name" => "{$ext_record}-master", "value" => $DBServer->remoteIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); } if ($DBFarmRole->GetRunningInstancesCount() == 1 || !$DBServer->GetProperty(SERVER_PROPERTIES::DB_MYSQL_MASTER)) { array_push($records, array("name" => "int-mysql-slave", "value" => $DBServer->localIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); array_push($records, array("name" => "ext-mysql-slave", "value" => $DBServer->remoteIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); array_push($records, array("name" => "{$int_record}-slave", "value" => $DBServer->localIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); array_push($records, array("name" => "{$ext_record}-slave", "value" => $DBServer->remoteIp, "type" => "A", "ttl" => 90, "server_id" => $DBServer->serverId, "issystem" => '1')); } } return $records; }
public function extendMessage(Scalr_Messaging_Msg $message, DBServer $dbServer) { if (in_array(ROLE_BEHAVIORS::BASE, $message->handlers)) { return $message; } if ($dbServer->farmRoleId) { $dbFarmRole = DBFarmRole::LoadByID($dbServer->farmRoleId); } switch (get_class($message)) { case "Scalr_Messaging_Msg_BeforeHostTerminate": //Storage try { if ($dbFarmRole) { $storage = new FarmRoleStorage($dbFarmRole); $volumes = $storage->getVolumesConfigs($dbServer->index); if (!empty($volumes)) { $message->volumes = $volumes; } } } catch (Exception $e) { $this->logger->error(new FarmLogMessage($dbServer->farmId, "Cannot init storage: {$e->getMessage()}")); } break; case "Scalr_Messaging_Msg_HostInitResponse": //Deployments try { if ($dbFarmRole) { $appId = $dbServer->GetFarmRoleObject()->GetSetting(self::ROLE_DM_APPLICATION_ID); if (!$message->deploy && $appId) { $application = Scalr_Dm_Application::init()->loadById($appId); $deploymentTask = Scalr_Dm_DeploymentTask::init(); $deploymentTask->create($dbServer->farmRoleId, $appId, $dbServer->serverId, Scalr_Dm_DeploymentTask::TYPE_AUTO, $dbFarmRole->GetSetting(self::ROLE_DM_REMOTE_PATH), $dbServer->envId, Scalr_Dm_DeploymentTask::STATUS_DEPLOYING); $message->deploy = $deploymentTask->getDeployMessageProperties(); } } } catch (Exception $e) { $this->logger->error(new FarmLogMessage($dbServer->farmId, "Cannot init deployment: {$e->getMessage()}")); } //Storage try { if ($dbFarmRole) { $storage = new FarmRoleStorage($dbFarmRole); $volumes = $storage->getVolumesConfigs($dbServer->index); if (!empty($volumes)) { $message->volumes = $volumes; } } } catch (Exception $e) { $this->logger->error(new FarmLogMessage($dbServer->farmId, "Cannot init storage: {$e->getMessage()}")); } // Base try { if ($dbFarmRole) { $scriptingLogTimeout = $dbFarmRole->GetSetting(self::ROLE_BASE_KEEP_SCRIPTING_LOGS_TIME); if (!$scriptingLogTimeout) { $scriptingLogTimeout = 3600; } $message->base = new stdClass(); $message->base->keepScriptingLogsTime = $scriptingLogTimeout; $hostNameFormat = $dbFarmRole->GetSetting(self::ROLE_BASE_HOSTNAME_FORMAT); $message->base->hostname = !empty($hostNameFormat) ? $dbServer->applyGlobalVarsToValue($hostNameFormat) : ''; } //keep_scripting_logs_time } catch (Exception $e) { } break; } $message->handlers[] = ROLE_BEHAVIORS::BASE; return $message; }
public function getBaseConfiguration(DBServer $dbServer, $isHostInit = false) { $configuration = new stdClass(); $dbFarmRole = $dbServer->GetFarmRoleObject(); //Storage try { if ($dbFarmRole) { $storage = new FarmRoleStorage($dbFarmRole); $volumes = $storage->getVolumesConfigs($dbServer->index, $isHostInit); if (!empty($volumes)) { $configuration->volumes = $volumes; } } } catch (Exception $e) { $this->logger->error(new FarmLogMessage($dbServer->farmId, "Cannot init storage: {$e->getMessage()}")); } // Base try { if ($dbFarmRole) { $scriptingLogTimeout = $dbFarmRole->GetSetting(self::ROLE_BASE_KEEP_SCRIPTING_LOGS_TIME); if (!$scriptingLogTimeout) { $scriptingLogTimeout = 3600; } $configuration->base = new stdClass(); $configuration->base->keepScriptingLogsTime = $scriptingLogTimeout; $configuration->base->resumeStrategy = PlatformFactory::NewPlatform($dbFarmRole->Platform)->getResumeStrategy(); $governance = new Scalr_Governance($dbFarmRole->GetFarmObject()->EnvID); if ($governance->isEnabled(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_HOSTNAME_FORMAT)) { $hostNameFormat = $governance->getValue(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_HOSTNAME_FORMAT); } else { $hostNameFormat = $dbFarmRole->GetSetting(self::ROLE_BASE_HOSTNAME_FORMAT); } $configuration->base->hostname = !empty($hostNameFormat) ? $dbServer->applyGlobalVarsToValue($hostNameFormat) : ''; $apiPort = $dbFarmRole->GetSetting(self::ROLE_BASE_API_PORT); $messagingPort = $dbFarmRole->GetSetting(self::ROLE_BASE_MESSAGING_PORT); $configuration->base->apiPort = $apiPort ? $apiPort : 8010; $configuration->base->messagingPort = $messagingPort ? $messagingPort : 8013; } //Update settings $updateSettings = $dbServer->getScalarizrRepository(); $configuration->base->update = new stdClass(); foreach ($updateSettings as $k => $v) { $configuration->base->update->{$k} = $v; } } catch (Exception $e) { } return $configuration; }