public function SetScripts(array $scripts, array $params = array())
{
if (count($params) > 0) {
foreach ($params as $param) {
if (isset($param['hash']) && count($param['params']) > 0) {
$roleId = $this->RoleID;
$roleParams = $this->DB->GetOne("SELECT params FROM role_scripts WHERE role_id = ? AND `hash` = ? LIMIT 1", array($roleId, $param['hash']));
$newParams = serialize($param['params']);
if ($newParams != $roleParams) {
//UNIQUE KEY `uniq` (`farm_role_id`,`hash`,`farm_role_script_id`),
$this->DB->Execute("\n INSERT INTO farm_role_scripting_params\n SET farm_role_id = ?,\n `hash` = ?,\n farm_role_script_id = ?,\n role_script_id = ?,\n params = ?\n ON DUPLICATE KEY UPDATE\n role_script_id = ?,\n params = ?\n ", array($this->ID, $param['hash'], 0, 0, $newParams, 0, $newParams));
}
}
}
}
$this->DB->Execute("DELETE FROM farm_role_scripts WHERE farm_roleid=?", array($this->ID));
if (count($scripts) > 0) {
foreach ($scripts as $script) {
$timeout = empty($script['timeout']) ? 0 : intval($script['timeout']);
if (!$timeout) {
$timeout = \Scalr::config('scalr.script.timeout.sync');
}
$event_name = isset($script['event']) ? $script['event'] : null;
if ($event_name == 'AllEvents') {
$event_name = '*';
}
if ($event_name && (!empty($script['script_id']) && $script['script_type'] == Scalr_Scripting_Manager::ORCHESTRATION_SCRIPT_TYPE_SCALR || !empty($script['script_path']) && $script['script_type'] == Scalr_Scripting_Manager::ORCHESTRATION_SCRIPT_TYPE_LOCAL || !empty($script['params']) && $script['script_type'] == Scalr_Scripting_Manager::ORCHESTRATION_SCRIPT_TYPE_CHEF)) {
$this->DB->Execute("INSERT INTO farm_role_scripts SET\n scriptid = ?,\n farmid = ?,\n farm_roleid = ?,\n params = ?,\n event_name = ?,\n target = ?,\n version = ?,\n timeout = ?,\n issync = ?,\n order_index = ?,\n issystem = '1',\n script_path = ?,\n run_as = ?,\n script_type = ?\n ", array($script['script_id'], $this->FarmID, $this->ID, serialize($script['params']), $event_name, $script['target'], $script['version'], $timeout, $script['isSync'], (int) $script['order_index'], $script['script_path'], $script['run_as'], $script['script_type']));
$farmRoleScriptId = $this->DB->Insert_ID();
if ($script['target'] == Script::TARGET_ROLES || $script['target'] == Script::TARGET_BEHAVIORS || $script['target'] == Script::TARGET_FARMROLES) {
switch ($script['target']) {
case $script['target'] == Script::TARGET_ROLES:
$targetType = OrchestrationRule::TARGET_ROLES;
$varName = 'target_roles';
break;
case $script['target'] == Script::TARGET_FARMROLES:
$targetType = OrchestrationRule::TARGET_ROLES;
$varName = 'target_farmroles';
break;
case $script['target'] == Script::TARGET_BEHAVIORS:
$targetType = OrchestrationRule::TARGET_BEHAVIOR;
$varName = 'target_behaviors';
break;
}
if (is_array($script[$varName])) {
foreach ($script[$varName] as $t) {
// Workaround to be able to specify self role when it's not yet saved in farm.
if ($t == '*self*') {
$t = $this->ID;
}
$this->DB->Execute("INSERT INTO farm_role_scripting_targets SET\n `farm_role_script_id` = ?,\n `target_type` = ?,\n `target` =?\n ", array($farmRoleScriptId, $targetType, $t));
}
}
}
}
}
}
}