protected function ListScripts()
{
$ResponseDOMDocument = $this->CreateResponse();
$ScriptsDOMNode = $ResponseDOMDocument->createElement("scripts");
/************/
// Get and Validate Event name
$instance_events = array("hostinit" => "HostInit", "hostup" => "HostUp", "rebootfinish" => "RebootComplete", "newmysqlmaster" => "NewMysqlMasterUp", "ebsvolumeattached" => "EBSVolumeAttached", "blockdeviceattached" => "EBSVolumeAttached", "blockdevicemounted" => "EBSVolumeMounted");
$Reflect = new ReflectionClass("EVENT_TYPE");
$scalr_events = $Reflect->getConstants();
if (!$this->DBServer->IsSupported(0.5)) {
throw new Exception("ami-scripts roles cannot execute scripts anymore. Please upgrade your roles to scalarizr: http://scalr.net/blog/announcements/ami-scripts/");
}
if (!in_array($this->GetArg("event"), $scalr_events)) {
$event_name = $instance_events[strtolower($this->GetArg("event"))];
} else {
$event_name = $this->GetArg("event");
}
if (!$event_name && preg_match("/^(Custom|API)Event-[0-9]+-[0-9]+\$/si", $this->GetArg("event"))) {
$custom_event_name = $this->GetArg("event");
}
try {
if ($this->GetArg("event_id")) {
if (preg_match("/^FRSID-([0-9]+)\$/", $this->GetArg("event_id"), $matches)) {
$scripts = $this->DB->GetAll("SELECT * FROM farm_role_scripts WHERE farmid=? AND id=? ORDER BY order_index ASC", array($this->DBServer->farmId, $matches[1]));
} else {
$event_info = $this->DB->GetRow("SELECT * FROM events WHERE event_id=?", array($this->GetArg("event_id")));
if ($event_info) {
$Event = unserialize($event_info['event_object']);
if ($Event->DBServer) {
if ($Event->DBServer->serverId == $this->DBServer->serverId) {
$is_target = '1';
} else {
$is_target = '0';
}
$scripts = $this->DB->GetAll("SELECT * FROM farm_role_scripts WHERE farmid=? \r\n\t\t\t\t \t\tAND event_name=? AND (target = ? OR (target = ? AND 1 = {$is_target} AND farm_roleid=?) OR (target = ? AND farm_roleid=?)) ORDER BY order_index ASC", array($Event->GetFarmID(), $Event->GetName(), SCRIPTING_TARGET::FARM, SCRIPTING_TARGET::INSTANCE, $Event->DBServer->farmRoleId, SCRIPTING_TARGET::ROLE, $Event->DBServer->farmRoleId));
$TargetDBFarmRole = $Event->DBServer->GetFarmRoleObject();
$target_instance_id = $Event->DBServer->serverId;
} else {
$scripts = $this->DB->GetAll("SELECT * FROM farm_role_scripts WHERE farmid=? \r\n\t\t\t\t \t\tAND event_name=? AND target = ? ORDER BY order_index ASC", array($Event->GetFarmID(), $Event->GetName(), SCRIPTING_TARGET::FARM));
}
}
}
}
} catch (Exception $e) {
return $ResponseDOMDocument;
}
/************/
/***********************************************************/
/** Instance from which request has come **/
try {
$DBFarmRole = $this->DBServer->GetFarmRoleObject();
} catch (Exception $e) {
return $ResponseDOMDocument;
}
$DBFarm = $this->DBServer->GetFarmObject();
if (!$scripts) {
// Check context and get list of scripts
if (!$this->GetArg("target_ip")) {
//
// Build a list of scripts to be executed on that particular instance
//
if ($event_name == EVENT_TYPE::HOST_INIT) {
$this->DBServer->remoteIp = $_SERVER['REMOTE_ADDR'];
$this->DBServer->localIp = $this->GetArg('local_ip');
}
$scripts = $this->DB->GetAll("SELECT * FROM farm_role_scripts WHERE farmid=? AND farm_roleid=? \r\n\t \t\tAND event_name=? ORDER BY order_index ASC", array($this->DBServer->farmId, $DBFarmRole->ID, $event_name));
} else {
if ($event_name == EVENT_TYPE::HOST_INIT && $this->GetArg("target_ip") == $this->GetArg('local_ip')) {
$this->DBServer->remoteIp = $_SERVER['REMOTE_ADDR'];
$this->DBServer->localIp = $this->GetArg('local_ip');
//
// Build a list of scripts to be executed upon event from another instance.
//
$targetDBServer = $this->DBServer;
} else {
//
// Build a list of scripts to be executed upon event from another instance.
//
try {
$targetDBServer = DBServer::LoadByLocalIp($this->GetArg("target_ip"));
} catch (Exception $e) {
return $ResponseDOMDocument;
}
}
if ($this->GetArg("target_ip") == $this->GetArg('local_ip')) {
$is_target = '1';
} else {
$is_target = '0';
}
if (!$targetDBServer) {
exit;
}
try {
$TargetDBFarmRole = $targetDBServer->GetFarmRoleObject();
} catch (Exception $e) {
return $ResponseDOMDocument;
}
$scripts = $this->DB->GetAll("SELECT * FROM farm_role_scripts WHERE farmid=? \r\n\t \t\tAND event_name=? AND (target = ? OR (target = ? AND 1 = {$is_target} AND farm_roleid=?) OR (target = ? AND farm_roleid=?)) ORDER BY order_index ASC", array($this->DBServer->farmId, $event_name, SCRIPTING_TARGET::FARM, SCRIPTING_TARGET::INSTANCE, $TargetDBFarmRole->ID, SCRIPTING_TARGET::ROLE, $TargetDBFarmRole->ID));
}
}
/***********************************************************/
// Build XML list of scripts
if (count($scripts) > 0) {
foreach ($scripts as $script) {
if ($script['target'] == SCRIPTING_TARGET::INSTANCE && $targetDBServer) {
if ($targetDBServer->serverId != $this->DBServer->serverId) {
continue;
}
}
if ($script['target'] == SCRIPTING_TARGET::ROLE && $TargetDBFarmRole) {
if ($TargetDBFarmRole->ID != $DBFarmRole->ID) {
continue;
}
}
if ($script['version'] == 'latest') {
$version = (int) $this->DB->GetOne("SELECT MAX(revision) FROM script_revisions WHERE scriptid=?", array($script['scriptid']));
} else {
$version = (int) $script['version'];
}
$template = $this->DB->GetRow("SELECT * FROM scripts WHERE id=?", array($script['scriptid']));
$template['script'] = $this->DB->GetOne("SELECT script FROM script_revisions WHERE scriptid=? AND revision=?", array($template['id'], $version));
if (!$template['script']) {
throw new Exception("Script {$template['name']}:{$version} doesn't exist or inactive. Make sure that is does exist and is approved.");
}
if ($template) {
$params = array_merge($this->DBServer->GetScriptingVars(), (array) unserialize($script['params']));
if ($Event) {
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['script']);
$script_contents = str_replace('\\%', "%", $script_contents);
$name = preg_replace("/[^A-Za-z0-9]+/", "_", $template['name']);
$ScriptDOMNode = $ResponseDOMDocument->createElement("script");
$ScriptDOMNode->setAttribute("asynchronous", $script['issync'] == 1 ? '0' : '1');
$ScriptDOMNode->setAttribute("exec-timeout", $script['timeout']);
$ScriptDOMNode->setAttribute("name", $name);
$BodyDOMNode = $ResponseDOMDocument->createElement("body");
$BodyDOMNode->appendChild($ResponseDOMDocument->createCDATASection($script_contents));
$ScriptDOMNode->appendChild($BodyDOMNode);
} else {
throw new Exception(sprintf(_("Script template ID: %s not found."), $script['scriptid']));
}
$ScriptsDOMNode->appendChild($ScriptDOMNode);
}
}
$ResponseDOMDocument->documentElement->appendChild($ScriptsDOMNode);
return $ResponseDOMDocument;
}