private static function prepareScript($scriptSettings, DBServer $targetServer, Event $event = null) { $db = Core::GetDBInstance(); //$scriptSettings['version'] = (int)$scriptSettings['version']; if ($scriptSettings['version'] == 'latest' || (int) $scriptSettings['version'] == -1) { $version = (int) $db->GetOne("SELECT MAX(revision) FROM script_revisions WHERE scriptid=?", array($scriptSettings['scriptid'])); } else { $version = (int) $scriptSettings['version']; } $template = $db->GetRow("SELECT name,id FROM scripts WHERE id=?", array($scriptSettings['scriptid'])); $template['timeout'] = $scriptSettings['timeout']; $template['issync'] = $scriptSettings['issync']; $template['body'] = $db->GetOne("SELECT script FROM script_revisions WHERE scriptid=? AND revision=?", array($template['id'], $version)); if (!$template['body']) { return false; } $params = array_merge($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}; } } // Prepare keys array and array with values for replacement in script $keys = array_keys($params); $f = create_function('$item', 'return "%".$item."%";'); $keys = array_map($f, $keys); $values = array_values($params); // Generate script contents $script_contents = str_replace($keys, $values, $template['body']); $template['body'] = str_replace('\\%', "%", $script_contents); $template['name'] = preg_replace("/[^A-Za-z0-9]+/", "_", $template['name']); return $template; }
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 static function listServerGlobalVariables(DBServer $dbServer, $includeSystem = false, AbstractServerEvent $event = null) { $retval = array(); if ($includeSystem) { $variables = $dbServer->GetScriptingVars(); if ($event) { if ($event->DBServer) { foreach ($event->DBServer->GetScriptingVars() as $k => $v) { $variables["event_{$k}"] = $v; } } foreach ($event->GetScriptingVars() as $k => $v) { $variables[$k] = $event->{$v}; } if (isset($event->params) && is_array($event->params)) { foreach ($event->params as $k => $v) { $variables[$k] = $v; } } $variables['event_name'] = $event->GetName(); } $formats = \Scalr::config("scalr.system.global_variables.format"); foreach ($variables as $name => $value) { $name = "SCALR_" . strtoupper($name); $value = trim($value); if (isset($formats[$name])) { $value = @sprintf($formats[$name], $value); } $private = strpos($name, 'SCALR_EVENT_') === 0 ? 1 : 0; $retval[] = (object) array('name' => $name, 'value' => $value, 'private' => $private, 'system' => 1); } } try { $globalVariables = new Scalr_Scripting_GlobalVariables($dbServer->GetEnvironmentObject()->clientId, $dbServer->envId, ScopeInterface::SCOPE_SERVER); $vars = $globalVariables->listVariables($dbServer->GetFarmRoleObject()->RoleID, $dbServer->farmId, $dbServer->farmRoleId, $dbServer->serverId); foreach ($vars as $v) { $retval[] = (object) $v; } } catch (Exception $e) { } return $retval; }
public function getConfiguration(DBServer $dbServer) { $configuration = new stdClass(); $dbFarmRole = $dbServer->GetFarmRoleObject(); if (!$dbFarmRole->GetSetting(self::ROLE_CHEF_BOOTSTRAP)) { return $configuration; } $jsonAttributes = $dbFarmRole->GetSetting(self::ROLE_CHEF_ATTRIBUTES); $chefCookbookUrl = $dbFarmRole->GetSetting(self::ROLE_CHEF_COOKBOOK_URL); if ($chefCookbookUrl) { $configuration->cookbookUrl = $chefCookbookUrl; $configuration->runList = $dbFarmRole->GetSetting(self::ROLE_CHEF_RUNLIST); $configuration->cookbookUrlType = $dbFarmRole->GetSetting(self::ROLE_CHEF_COOKBOOK_URL_TYPE); $configuration->sshPrivateKey = $dbFarmRole->GetSetting(self::ROLE_CHEF_SSH_PRIVATE_KEY); $configuration->relativePath = $dbFarmRole->GetSetting(self::ROLE_CHEF_RELATIVE_PATH); } else { // Get chef server info $chefServerId = $dbFarmRole->GetSetting(self::ROLE_CHEF_SERVER_ID); $chefServerInfo = $this->db->GetRow("SELECT * FROM services_chef_servers WHERE id=?", array($chefServerId)); $chefServerInfo['v_auth_key'] = trim($this->getCrypto()->decrypt($chefServerInfo['v_auth_key'], $this->cryptoKey)); // Prepare node name $configuration->nodeName = $dbServer->GetProperty(self::SERVER_CHEF_NODENAME); if (!$configuration->nodeName) { $nodeNameTpl = $dbFarmRole->GetSetting(self::ROLE_CHEF_NODENAME_TPL); if ($nodeNameTpl) { $params = $dbServer->GetScriptingVars(); $keys = array_keys($params); $f = create_function('$item', 'return "%".$item."%";'); $keys = array_map($f, $keys); $values = array_values($params); $configuration->nodeName = str_replace($keys, $values, $nodeNameTpl); //TODO: Add support for Global variables } } $configuration->serverUrl = $chefServerInfo['url']; $configuration->validatorName = $chefServerInfo['v_username']; $configuration->validatorKey = $chefServerInfo['v_auth_key']; if ($dbFarmRole->GetSetting(self::ROLE_CHEF_ROLE_NAME)) { $configuration->role = $dbFarmRole->GetSetting(self::ROLE_CHEF_ROLE_NAME); } else { $configuration->runList = $dbFarmRole->GetSetting(self::ROLE_CHEF_RUNLIST); } $configuration->environment = $dbFarmRole->GetSetting(self::ROLE_CHEF_ENVIRONMENT); $configuration->daemonize = $dbFarmRole->GetSetting(self::ROLE_CHEF_DAEMONIZE); } if ($jsonAttributes) { $params = $dbServer->GetScriptingVars(); // Prepare keys array and array with values for replacement in script $keys = array_keys($params); $f = create_function('$item', 'return "%".$item."%";'); $keys = array_map($f, $keys); $values = array_values($params); $contents = str_replace($keys, $values, $jsonAttributes); $configuration->jsonAttributes = str_replace('\\%', "%", $contents); //TODO: Add support for Global variables } 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) { array_push($records, array("name" => "@", "value" => $DBServer->remoteIp, "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 || $ext_record_alias) { $params = $DBServer->GetScriptingVars(); $keys = array_keys($params); $f = create_function('$item', 'return "%".$item."%";'); $keys = array_map($f, $keys); $values = array_values($params); } if ($int_record_alias) { $int_record = str_replace($keys, $values, $int_record_alias); } if ($ext_record_alias) { $ext_record = str_replace($keys, $values, $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 static function prepareScript($scriptSettings, DBServer $targetServer, Event $event = null) { $db = \Scalr::getDb(); $template = array('timeout' => $scriptSettings['timeout'], 'issync' => $scriptSettings['issync'], 'run_as' => $scriptSettings['run_as'], 'execution_id' => Scalr::GenerateUID()); if ($scriptSettings['scriptid']) { if ($scriptSettings['version'] == 'latest' || (int) $scriptSettings['version'] == -1) { $version = (int) $db->GetOne("SELECT MAX(revision) FROM script_revisions WHERE scriptid=?", array($scriptSettings['scriptid'])); } else { $version = (int) $scriptSettings['version']; } $info = $db->GetRow("SELECT name,id FROM scripts WHERE id=? LIMIT 1", array($scriptSettings['scriptid'])); $template['name'] = $info['name']; $template['id'] = $info['id']; $revisionInfo = $db->GetRow("SELECT script, variables FROM script_revisions WHERE scriptid=? AND revision=? LIMIT 1", array($template['id'], $version)); $template['body'] = $revisionInfo['script']; if (!$template['body']) { return false; } $scriptParams = (array) unserialize($revisionInfo['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) { if (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); $f = create_function('$item', 'return "%".$item."%";'); $keys = array_map($f, $keys); $values = array_values($params); $script_contents = str_replace($keys, $values, $template['body']); $template['body'] = str_replace('\\%', "%", $script_contents); // Parse and set variables from data bag //TODO: @param_name@ // Generate script contents $template['name'] = preg_replace("/[^A-Za-z0-9]+/", "_", $template['name']); } else { $template['path'] = $scriptSettings['script_path']; } return $template; }