PageRenderTime 66ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/scalr-2/tags/scalr-2.2.0/app/src/Scalr/UI/Controller/Scripts.php

http://scalr.googlecode.com/
PHP | 656 lines | 540 code | 111 blank | 5 comment | 52 complexity | 2699c1eea70313a13b3e2d5f7257d5f9 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. class Scalr_UI_Controller_Scripts extends Scalr_UI_Controller
  3. {
  4. const CALL_PARAM_NAME = 'scriptId';
  5. public function __construct() {
  6. parent::__construct();
  7. $this->filterSql = "(" .
  8. " origin='".SCRIPT_ORIGIN_TYPE::SHARED."'" .
  9. " OR (origin='".SCRIPT_ORIGIN_TYPE::CUSTOM."' AND clientid='".$this->session->getClientId()."')" .
  10. " OR (origin='".SCRIPT_ORIGIN_TYPE::USER_CONTRIBUTED."' AND (scripts.approval_state='".APPROVAL_STATE::APPROVED."' OR clientid='".$this->session->getClientId()."'))" .
  11. ")";
  12. }
  13. public function hasAccess()
  14. {
  15. return $this->session->getAuthToken()->hasAccess(Scalr_AuthToken::ACCOUNT_USER);
  16. }
  17. static public function getCustomVariables($template)
  18. {
  19. $text = preg_replace('/(\\\%)/si', '$$scalr$$', $template);
  20. preg_match_all("/\%([^\%\s]+)\%/si", $text, $matches);
  21. return $matches[1];
  22. }
  23. /** Actions **/
  24. public function defaultAction()
  25. {
  26. $this->viewAction();
  27. }
  28. public function viewAction()
  29. {
  30. $this->response->setJsonResponse(array(
  31. 'success' => true,
  32. 'module' => $this->response->template->fetchJs('scripts/view.js'),
  33. 'moduleParams' => array(
  34. 'isScalrAdmin' => $this->session->getAuthToken()->hasAccess(Scalr_AuthToken::SCALR_ADMIN),
  35. 'clientId' => $this->session->getClientId()
  36. )
  37. ));
  38. }
  39. public function xGetScriptContentAction()
  40. {
  41. $this->request->defineParams(array(
  42. 'scriptId' => array('type' => 'int'),
  43. 'version' => array('type' => 'int')
  44. ));
  45. try {
  46. $scriptInfo = $this->db->GetRow("SELECT * FROM scripts WHERE id=? AND {$this->filterSql}", array($this->getParam('scriptId')));
  47. if (!$scriptInfo)
  48. throw new Exception("Script not found");
  49. $content = $this->db->GetOne("SELECT script FROM script_revisions WHERE scriptid = ? AND revision =?", array(
  50. $this->getParam('scriptId'), $this->getParam('version')
  51. ));
  52. $this->response->setJsonResponse(array(
  53. 'success' => true,
  54. 'scriptContents' => $content
  55. ));
  56. }
  57. catch (Exception $e) {
  58. $this->response->setJsonResponse(array('success' => false, 'error' => $e->getMessage()));
  59. }
  60. }
  61. public function xRemoveAction()
  62. {
  63. $this->request->defineParams(array(
  64. 'scriptId' => array('type' => 'int')
  65. ));
  66. // Get template infor from database
  67. $script = $this->db->GetRow("SELECT * FROM scripts WHERE id=? AND {$this->filterSql}", array($this->getParam('scriptId')));
  68. if (!$script)
  69. throw new Exception(_("You don't have permissions to remove this script"));
  70. // Check template usage
  71. $roles_count = $this->db->GetOne("SELECT COUNT(*) FROM farm_role_scripts WHERE scriptid=? AND event_name NOT LIKE 'CustomEvent-%'",
  72. array($this->getParam('scriptId'))
  73. );
  74. // If script used redirect and show error
  75. if ($roles_count > 0)
  76. throw new Exception(_("This script being used and can't be deleted"));
  77. $this->db->BeginTrans();
  78. // Delete tempalte and all revisions
  79. $this->db->Execute("DELETE FROM farm_role_scripts WHERE scriptid=?", array($this->getParam('scriptId')));
  80. $this->db->Execute("DELETE FROM scripts WHERE id=?", array($this->getParam('scriptId')));
  81. $this->db->Execute("DELETE FROM script_revisions WHERE scriptid=?", array($this->getParam('scriptId')));
  82. $this->db->CommitTrans();
  83. $this->response->setJsonResponse(array('success' => true));
  84. }
  85. public function xSaveAction()
  86. {
  87. $this->request->defineParams(array(
  88. 'scriptId' => array('type' => 'int'),
  89. 'scriptName', 'scriptDescription', 'scriptContents',
  90. 'version' => array('type' => 'int'),
  91. 'saveCurrentRevision' => array('type' => 'int')
  92. ));
  93. try {
  94. if (!$this->getParam('scriptId')) {
  95. // Add new script
  96. $this->db->Execute("INSERT INTO scripts SET
  97. name = ?,
  98. description = ?,
  99. origin = ?,
  100. dtadded = NOW(),
  101. clientid = ?,
  102. approval_state = ?
  103. ", array(
  104. htmlspecialchars($this->getParam('scriptName')),
  105. htmlspecialchars($this->getParam('scriptDescription')),
  106. SCRIPT_ORIGIN_TYPE::CUSTOM,
  107. $this->session->getClientId(),
  108. APPROVAL_STATE::APPROVED
  109. ));
  110. $scriptId = $this->db->Insert_ID();
  111. } else {
  112. $scriptInfo = $this->db->GetRow("SELECT * FROM scripts WHERE id=? AND {$this->filterSql}", array($this->getParam('scriptId')));
  113. if (!$scriptInfo)
  114. throw new Exception("Script not found");
  115. $this->db->Execute("UPDATE scripts SET
  116. name = ?,
  117. description = ?
  118. WHERE id = ?
  119. ", array(
  120. htmlspecialchars($this->getParam('scriptName')),
  121. htmlspecialchars($this->getParam('scriptDescription')),
  122. $this->getParam('scriptId')
  123. ));
  124. $scriptId = $this->getParam('scriptId');
  125. }
  126. if (!$this->getParam('saveCurrentRevision')) {
  127. $revision = $this->db->GetOne("SELECT IF(MAX(revision), MAX(revision), 0) FROM script_revisions WHERE scriptid=?",
  128. array($scriptId)
  129. );
  130. $this->db->Execute("INSERT INTO script_revisions SET
  131. scriptid = ?,
  132. revision = ?,
  133. script = ?,
  134. dtcreated = NOW(),
  135. approval_state = ?
  136. ", array(
  137. $scriptId,
  138. $revision+1,
  139. str_replace("\r\n", "\n", $this->getParam('scriptContents')),
  140. APPROVAL_STATE::APPROVED
  141. ));
  142. } else {
  143. $this->db->Execute("UPDATE script_revisions SET
  144. script = ?
  145. WHERE scriptId = ? AND revision = ?
  146. ", array(
  147. str_replace("\r\n", "\n", $this->getParam('scriptContents')),
  148. $scriptId,
  149. $this->getParam('scriptVersion')
  150. ));
  151. }
  152. $this->response->setJsonResponse(array('success' => true));
  153. } catch (Exception $e) {
  154. $this->response->setJsonResponse(array('success' => false, 'error' => $e->getMessage()));
  155. }
  156. }
  157. public function xForkAction()
  158. {
  159. $this->request->defineParams(array(
  160. 'scriptId' => array('type' => 'int')
  161. ));
  162. try {
  163. $scriptInfo = $this->db->GetRow("SELECT * FROM scripts WHERE id=? AND origin != ?", array($this->getParam('scriptId'), SCRIPT_ORIGIN_TYPE::CUSTOM));
  164. if (!$scriptInfo)
  165. throw new Exception("Script not found");
  166. $this->db->Execute("INSERT INTO scripts SET
  167. name = ?,
  168. description = ?,
  169. origin = ?,
  170. dtadded = NOW(),
  171. clientid = ?,
  172. approval_state = ?
  173. ", array(
  174. 'Custom ' . $scriptInfo['name'],
  175. $scriptInfo['description'],
  176. SCRIPT_ORIGIN_TYPE::CUSTOM,
  177. $this->session->getClientId(),
  178. APPROVAL_STATE::APPROVED
  179. ));
  180. $content = $this->db->GetOne("SELECT script FROM script_revisions WHERE scriptid = ? ORDER BY id DESC LIMIT 0,1", array($this->getParam('scriptId')));
  181. $scriptId = $this->db->Insert_ID();
  182. $this->db->Execute("INSERT INTO script_revisions SET
  183. scriptid = ?,
  184. revision = ?,
  185. script = ?,
  186. dtcreated = NOW(),
  187. approval_state = ?
  188. ", array(
  189. $scriptId,
  190. 1,
  191. $content,
  192. APPROVAL_STATE::APPROVED
  193. ));
  194. $this->response->setJsonResponse(array('success' => true));
  195. } catch (Exception $e) {
  196. $this->response->setJsonResponse(array('success' => false, 'error' => $e->getMessage()));
  197. }
  198. }
  199. public function editAction()
  200. {
  201. $this->request->defineParams(array(
  202. 'scriptId' => array('type' => 'int'),
  203. 'version' => array('type' => 'int')
  204. ));
  205. $vars = CONFIG::getScriptingBuiltinVariables();
  206. try {
  207. $scriptInfo = $this->db->GetRow("SELECT * FROM scripts WHERE id=? AND {$this->filterSql}", array($this->getParam('scriptId')));
  208. if (!$scriptInfo)
  209. throw new Exception("Script not found");
  210. $latestRevision = $this->db->GetRow("SELECT MAX(revision) as rev FROM script_revisions WHERE scriptid=? GROUP BY scriptid", array($this->getParam('scriptId')));
  211. if ($this->getParam('version'))
  212. $rev = $this->db->GetRow("SELECT revision as rev, script FROM script_revisions WHERE scriptid=? AND revision=?", array($this->getParam('scriptId'), $this->getParam('version')));
  213. else
  214. $rev = $latestRevision;
  215. $this->response->setJsonResponse(array(
  216. 'success' => true,
  217. 'module' => $this->response->template->fetchJs('scripts/create.js'),
  218. 'moduleParams' => array(
  219. 'scriptName' => $scriptInfo['name'],
  220. 'scriptId' => $scriptInfo['id'],
  221. 'description' => $scriptInfo['description'],
  222. 'scriptContents'=> $this->db->GetOne("SELECT script FROM script_revisions WHERE scriptid=? AND revision=?", array($this->getParam('scriptId'), $rev['rev'])),
  223. 'latestVersion'=> $latestRevision['rev'],
  224. 'version' => $rev['rev'],
  225. 'versions' => range(1, $latestRevision['rev']),
  226. 'variables' => "%" . implode("%, %", array_keys($vars)) . "%"
  227. )
  228. ));
  229. } catch (Exception $e) {
  230. $this->response->setJsonResponse(array('success' => false, 'error' => $e->getMessage()));
  231. }
  232. }
  233. public function createAction()
  234. {
  235. $vars = CONFIG::getScriptingBuiltinVariables();
  236. $this->response->setJsonResponse(array(
  237. 'success' => true,
  238. 'module' => $this->response->template->fetchJs('scripts/create.js'),
  239. 'moduleParams' => array(
  240. 'scriptName' => '',
  241. 'scriptId' => 0,
  242. 'description' => '',
  243. 'scriptContents'=> '',
  244. 'version' => 1,
  245. 'versions' => array(1),
  246. 'variables' => "%" . implode("%, %", array_keys($vars)) . "%"
  247. )
  248. ));
  249. }
  250. public function xListViewScriptsAction()
  251. {
  252. try {
  253. $this->request->defineParams(array(
  254. 'scriptId', 'origin', 'approvalState',
  255. 'sort' => array('type' => 'string', 'default' => 'id'),
  256. 'dir' => array('type' => 'string', 'default' => 'DESC')
  257. ));
  258. if (!$this->session->getAuthToken()->hasAccess(Scalr_AuthToken::SCALR_ADMIN)) {
  259. $filterSql = $this->filterSql;
  260. }
  261. else {
  262. $filterSql = " (origin='".SCRIPT_ORIGIN_TYPE::SHARED."' OR origin='".SCRIPT_ORIGIN_TYPE::USER_CONTRIBUTED."')";
  263. }
  264. $sql = "SELECT
  265. scripts.id,
  266. scripts.name,
  267. scripts.description,
  268. scripts.origin,
  269. scripts.clientid,
  270. scripts.approval_state,
  271. MAX(script_revisions.dtcreated) as dtupdated, MAX(script_revisions.revision) AS version FROM scripts
  272. INNER JOIN script_revisions ON script_revisions.scriptid = scripts.id
  273. WHERE 1=1 AND {$filterSql}";
  274. if ($this->getParam('origin'))
  275. $sql .= " AND origin=".$this->db->qstr($this->getParam('origin'));
  276. if ($this->getParam('approvalState'))
  277. $sql .= " AND scripts.approval_state=".$this->db->qstr($this->getParam('approvalState'));
  278. $response = $this->buildResponseFromSql($sql, array("scripts.name", "scripts.description"), " GROUP BY script_revisions.scriptid", false);
  279. foreach ($response['data'] as &$row)
  280. {
  281. if ($row['clientid'] != 0)
  282. {
  283. $client = $this->db->GetRow("SELECT email, fullname FROM clients WHERE id = ?", array($row['clientid']));
  284. $row["client_name"] = $client['fullname'];
  285. }
  286. $row['dtupdated'] = date("M j, Y", strtotime($row["dtupdated"]));
  287. }
  288. $this->response->setJsonResponse($response);
  289. } catch (Exception $e) {
  290. $this->response->setJsonResponse(array('success' => false, 'error' => $e->getMessage()));
  291. }
  292. }
  293. public function getList()
  294. {
  295. $scripts = array();
  296. $sql = "SELECT scripts.*, MAX(script_revisions.dtcreated) as dtupdated from scripts INNER JOIN script_revisions
  297. ON script_revisions.scriptid = scripts.id WHERE {$this->filterSql} GROUP BY script_revisions.scriptid ORDER BY dtupdated DESC";
  298. foreach ($this->db->GetAll($sql) as $script) {
  299. $dbVersions = $this->db->Execute("SELECT * FROM script_revisions WHERE scriptid=? AND (approval_state=? OR (SELECT clientid FROM scripts WHERE scripts.id=script_revisions.scriptid) = '".$this->session->getClientId()."')",
  300. array($script['id'], APPROVAL_STATE::APPROVED)
  301. );
  302. if ($dbVersions->RecordCount() > 0) {
  303. $versions = array();
  304. while ($version = $dbVersions->FetchRow()) {
  305. $data = array();
  306. foreach ((array)self::getCustomVariables($version["script"]) as $var) {
  307. if (! in_array($var, array_keys(CONFIG::getScriptingBuiltinVariables())))
  308. $data[$var] = ucwords(str_replace("_", " ", $var));
  309. }
  310. $versions[$version['revision']] = array("revision" => $version['revision'], "fields" => $data);
  311. }
  312. $scripts[$script['id']] = array(
  313. 'id' => $script['id'],
  314. 'name' => $script['name'],
  315. 'description' => $script['description'],
  316. 'issync' => $script['issync'],
  317. 'timeout' => ($script['issync'] == 1) ? CONFIG::$SYNCHRONOUS_SCRIPT_TIMEOUT : CONFIG::$ASYNCHRONOUS_SCRIPT_TIMEOUT,
  318. 'revisions' => $versions
  319. );
  320. }
  321. }
  322. return $scripts;
  323. }
  324. public function getFarmRolesAction()
  325. {
  326. $farmRolesController = self::loadController('Roles', 'Scalr_UI_Controller_Farms');
  327. if (is_null($farmRolesController))
  328. throw new Exception('Controller Farms_Roles not created');
  329. $farmRoles = $farmRolesController->getList();
  330. if (count($farmRoles))
  331. $farmRoles[0] = array('id' => 0, 'name' =>'On all roles');
  332. $this->response->setJsonResponse(array(
  333. 'success' => true,
  334. 'farmRoles' => $farmRoles
  335. ));
  336. }
  337. public function getServersAction()
  338. {
  339. $dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId'));
  340. $dbFarm = DBFarm::LoadById($dbFarmRole->FarmID);
  341. $servers = array();
  342. if (! $this->session->getAuthToken()->hasAccessEnvironment($dbFarm->EnvID))
  343. throw new Exception('You cannot execute script on selected farm/role/server');
  344. foreach ($dbFarmRole->GetServersByFilter(array('status' => SERVER_STATUS::RUNNING)) as $key => $value)
  345. $servers[$value->serverId] = $value->remoteIp;
  346. if (count($servers))
  347. $servers[0] = 'On all servers';
  348. $this->response->setJsonResponse(array(
  349. 'success' => true,
  350. 'servers' => $servers
  351. ));
  352. }
  353. public function executeAction()
  354. {
  355. $farmId = $this->getParam('farmId');
  356. $farmRoleId = $this->getParam('farmRoleId');
  357. $serverId = $this->getParam('serverId');
  358. $scriptId = $this->getParam('scriptId');
  359. $eventName = $this->getParam('eventName');
  360. $farms = array();
  361. $farmRoles = array();
  362. $servers = array();
  363. $scripts = $this->getList();
  364. if ($eventName) {
  365. $scriptInfo = $this->db->GetRow("SELECT * FROM farm_role_scripts WHERE event_name=?", array($eventName));
  366. if (!$scriptInfo)
  367. throw new Exception("Scalr unable to find script execution options for used link");
  368. $farmId = $scriptInfo['farmid'];
  369. $farmRoleId = $scriptInfo['farm_roleid'];
  370. $scriptId = $scriptInfo['scriptid'];
  371. }
  372. if ($serverId) {
  373. $dbServer = DBServer::LoadByID($serverId);
  374. $farmRoleId = $dbServer->farmRoleId;
  375. }
  376. if ($farmRoleId) {
  377. $dbFarmRole = DBFarmRole::LoadByID($farmRoleId);
  378. $farmId = $dbFarmRole->FarmID;
  379. foreach ($dbFarmRole->GetServersByFilter(array('status' => SERVER_STATUS::RUNNING)) as $key => $value)
  380. $servers[$value->serverId] = $value->remoteIp;
  381. if (count($servers)) {
  382. $servers[0] = _('On all servers');
  383. if (!$serverId)
  384. $serverId = 0;
  385. }
  386. }
  387. if ($farmId) {
  388. $dbFarm = DBFarm::LoadById($farmId);
  389. if (! $this->session->getAuthToken()->hasAccessEnvironment($dbFarm->EnvID))
  390. throw new Exception(_('You cannot execute script on selected farm/role/server'));
  391. $this->request->setParams(array('farmId' => $farmId));
  392. $farmRolesController = self::loadController('Roles', 'Scalr_UI_Controller_Farms');
  393. if (is_null($farmRolesController))
  394. throw new Exception(_('Controller Farms_Roles not created'));
  395. $farmRoles = $farmRolesController->getList();
  396. if (count($farmRoles))
  397. $farmRoles[0] = array('id' => 0, 'name' =>'On all roles');
  398. }
  399. $farmsController = self::loadController('Farms');
  400. if (is_null($farmsController))
  401. throw new Exception(_('Controller Farms not created'));
  402. else
  403. $farms = $farmsController->getList();
  404. $this->response->setJsonResponse(array(
  405. 'success' => true,
  406. 'moduleParams' => array(
  407. 'farms' => $farms,
  408. 'farmRoles' => $farmRoles,
  409. 'servers' => $servers,
  410. 'scripts' => $scripts,
  411. 'farmId' => $farmId,
  412. 'farmRoleId' => $farmRoleId,
  413. 'serverId' => $serverId,
  414. 'scriptId' => $scriptId,
  415. 'scriptIsSync' => $scriptInfo['issync'],
  416. 'scriptTimeout' => $scriptInfo['timeout'],
  417. 'scriptVersion' => $scriptInfo['version'],
  418. 'scriptOptions' => @unserialize($scriptInfo['params'])
  419. ),
  420. 'module' => $this->response->template->fetchJs('scripts/execute.js')
  421. ));
  422. }
  423. public function xExecuteAction()
  424. {
  425. $this->request->defineParams(array(
  426. 'farmId' => array('type' => 'int'),
  427. 'farmRoleId' => array('type' => 'int'),
  428. 'serverId' => array('type' => 'string'),
  429. 'scriptId' => array('type' => 'int'),
  430. 'scriptIsSync' => array('type' => 'int'),
  431. 'scriptTimeout' => array('type' => 'int'),
  432. 'scriptVersion' => array('type' => 'int'),
  433. 'scriptOptions' => array('type' => 'array'),
  434. 'createMenuLink' => array('type' => 'int')
  435. ));
  436. $eventName = 'CustomEvent-'.date("YmdHi").'-'.rand(1000,9999);
  437. $target = '';
  438. // @TODO: validation
  439. if ($this->getParam('serverId')) {
  440. $dbServer = DBServer::LoadByID($this->getParam('serverId'));
  441. if (! $this->session->getAuthToken()->hasAccessEnvironment($dbServer->envId))
  442. throw new Exception("Specified farm role not found");
  443. $target = SCRIPTING_TARGET::INSTANCE;
  444. $serverId = $dbServer->serverId;
  445. $farmRoleId = $dbServer->farmRoleId;
  446. $farmId = $dbServer->farmId;
  447. } else if ($this->getParam('farmRoleId')) {
  448. $dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId'));
  449. if (! $this->session->getAuthToken()->hasAccessEnvironment($dbFarmRole->GetFarmObject()->EnvID))
  450. throw new Exception("Specified farm role not found");
  451. $target = SCRIPTING_TARGET::ROLE;
  452. $farmRoleId = $dbFarmRole->ID;
  453. $farmId = $dbFarmRole->FarmID;
  454. } else {
  455. $dbFarm = DBFarm::LoadByID($this->getParam('farmId'));
  456. $target = SCRIPTING_TARGET::FARM;
  457. if (! $this->session->getAuthToken()->hasAccessEnvironment($dbFarm->EnvID))
  458. throw new Exception("Specified farm not found");
  459. $farmId = $dbFarm->ID;
  460. }
  461. if (! $this->getParam('eventName')) {
  462. $this->db->Execute("INSERT INTO farm_role_scripts SET
  463. scriptid = ?,
  464. farmid = ?,
  465. farm_roleid = ?,
  466. params = ?,
  467. event_name = ?,
  468. target = ?,
  469. version = ?,
  470. timeout = ?,
  471. issync = ?,
  472. ismenuitem = ?
  473. ", array(
  474. $this->getParam('scriptId'),
  475. (int)$farmId,
  476. (int)$farmRoleId,
  477. serialize($this->getParam('scriptOptions')),
  478. $eventName,
  479. $target,
  480. $this->getParam('scriptVersion'),
  481. $this->getParam('scriptTimeout'),
  482. $this->getParam('scriptIsSync'),
  483. $this->getParam('createMenuLink')
  484. ));
  485. $farmScriptId = $this->db->Insert_ID();
  486. $executeScript = true;
  487. } else {
  488. $info = $this->db->Execute("SELECT farmid FROM farm_role_scripts WHERE event_name=?", array($this->getParam('eventName')));
  489. if ($info['farmid'] != $dbFarm->ID)
  490. throw new Exception("You cannot change farm for script shortcut");
  491. $this->db->Execute("UPDATE farm_role_scripts SET
  492. scriptid = ?,
  493. farm_roleid = ?,
  494. params = ?,
  495. target = ?,
  496. version = ?,
  497. timeout = ?,
  498. issync = ?
  499. WHERE event_name = ? AND farmid = ?
  500. ", array(
  501. $this->getParam('scriptId'),
  502. (int)$farmRoleId,
  503. serialize($this->getParam('scriptOptions')),
  504. $target,
  505. $this->getParam('scriptVersion'),
  506. $this->getParam('scriptTimeout'),
  507. $this->getParam('scriptIsSync'),
  508. $this->getParam('eventName'),
  509. $farmId
  510. ));
  511. if (!$this->getParam('isShortcut'))
  512. $executeScript = true;
  513. }
  514. if ($executeScript) {
  515. switch($target) {
  516. case SCRIPTING_TARGET::FARM:
  517. $servers = $this->db->GetAll("SELECT server_id FROM servers WHERE status IN (?,?) AND farm_id=?",
  518. array(SERVER_STATUS::INIT, SERVER_STATUS::RUNNING, $farmId)
  519. );
  520. break;
  521. case SCRIPTING_TARGET::ROLE:
  522. $servers = $this->db->GetAll("SELECT server_id FROM servers WHERE status IN (?,?) AND farm_roleid=?",
  523. array(SERVER_STATUS::INIT, SERVER_STATUS::RUNNING, $farmRoleId)
  524. );
  525. break;
  526. case SCRIPTING_TARGET::INSTANCE:
  527. $servers = $this->db->GetAll("SELECT server_id FROM servers WHERE status IN (?,?) AND server_id=?",
  528. array(SERVER_STATUS::INIT, SERVER_STATUS::RUNNING, $serverId)
  529. );
  530. break;
  531. }
  532. if (count($servers) > 0) {
  533. foreach ($servers as $server) {
  534. $DBServer = DBServer::LoadByID($server['server_id']);
  535. $message = new Scalr_Messaging_Msg_ExecScript($eventName);
  536. $message->meta[Scalr_Messaging_MsgMeta::EVENT_ID] = "FRSID-{$farmScriptId}";
  537. $DBServer->SendMessage($message);
  538. }
  539. }
  540. }
  541. $this->response->setJsonResponse(array('success' => true));
  542. }
  543. }