PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/www/server/ajax-ui-server.php

http://scalr.googlecode.com/
PHP | 236 lines | 231 code | 3 blank | 2 comment | 10 complexity | fd0121f062007b1865c70b842c5b00f5 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?
  2. require("../src/prepend.inc.php");
  3. class AjaxUIServer
  4. {
  5. public function __construct()
  6. {
  7. $this->DB = Core::GetDBInstance();
  8. $this->Logger = Logger::getLogger(__CLASS__);
  9. }
  10. public function RemoveScript($scriptID)
  11. {
  12. // Get template infor from database
  13. $template = $this->DB->GetRow("SELECT * FROM scripts WHERE id=?", array($scriptID));
  14. // Check permissions
  15. if (!$template || ($template['clientid'] == 0 && $_SESSION['uid'] != 0) ||
  16. ($template['clientid'] != 0 && $_SESSION['uid'] != 0 && $_SESSION['uid'] != $template['clientid'])
  17. ) {
  18. throw new Exception(_("You don't have permissions to edit this template"));
  19. }
  20. // Check template usage
  21. $roles_count = $this->DB->GetOne("SELECT COUNT(*) FROM farm_role_scripts WHERE scriptid=? AND event_name NOT LIKE 'CustomEvent-%'",
  22. array($scriptID)
  23. );
  24. // If script used redirect and show error
  25. if ($roles_count > 0)
  26. throw new Exception(_("This template being used and cannot be deleted"));
  27. $this->DB->BeginTrans();
  28. // Delete tempalte and all revisions
  29. $this->DB->Execute("DELETE FROM farm_role_scripts WHERE scriptid=?", array($scriptID));
  30. $this->DB->Execute("DELETE FROM scripts WHERE id=?", array($scriptID));
  31. $this->DB->Execute("DELETE FROM script_revisions WHERE scriptid=?", array($scriptID));
  32. $this->DB->CommitTrans();
  33. return true;
  34. }
  35. public function RemoveApplications(array $zones)
  36. {
  37. $ZoneControler = new DNSZoneControler();
  38. $this->DB->BeginTrans();
  39. foreach ($zones as $dd)
  40. {
  41. try
  42. {
  43. if ($_SESSION["uid"] != 0)
  44. $zone = $this->DB->GetRow("SELECT * FROM zones WHERE id=? AND clientid=?", array($dd, $_SESSION["uid"]));
  45. else
  46. $zone = $this->DB->GetRow("SELECT * FROM zones WHERE id=?", array($dd));
  47. if ($zone)
  48. {
  49. $ZoneControler->Delete($zone["id"]);
  50. $i++;
  51. }
  52. }
  53. catch(Exception $e)
  54. {
  55. $this->DB->RollbackTrans();
  56. Logger::getLogger("AjaxUIServer.RemoveApplications")->error("Exception thrown during application delete: {$e->getMessage()}");
  57. throw new Exception("Cannot delete application '{$zone['name']}'. Please try again later.");
  58. }
  59. }
  60. $this->DB->CommitTrans();
  61. return true;
  62. }
  63. public function RemoveVolume($volume_id, $region = "")
  64. {
  65. $Client = Client::Load($_SESSION['uid']);
  66. $region = ($region) ? $region : $_SESSION['aws_region'];
  67. $AmazonEC2Client = AmazonEC2::GetInstance(AWSRegions::GetAPIURL($region));
  68. $AmazonEC2Client->SetAuthKeys($Client->AWSPrivateKey, $Client->AWSCertificate);
  69. $AmazonEC2Client->DeleteVolume($volume_id);
  70. return true;
  71. }
  72. public function RemoveSnapshots(array $snapshots)
  73. {
  74. $Client = Client::Load($_SESSION['uid']);
  75. $AmazonEC2Client = AmazonEC2::GetInstance(AWSRegions::GetAPIURL($_SESSION['aws_region']));
  76. $AmazonEC2Client->SetAuthKeys($Client->AWSPrivateKey, $Client->AWSCertificate);
  77. foreach ($snapshots as $snapshot)
  78. {
  79. $AmazonEC2Client->DeleteSnapshot($snapshot);
  80. }
  81. return true;
  82. }
  83. public function RebootInstances(array $instances, $farmid)
  84. {
  85. if ($farmid)
  86. {
  87. $farminfo = $this->DB->GetRow("SELECT * FROM farms WHERE id=?", array($farmid));
  88. if (!$farminfo || ($_SESSION['uid'] != 0 && $farminfo['clientid'] != $_SESSION['uid']))
  89. throw new Exception("Farm not found in database");
  90. $clientid = $farminfo['clientid'];
  91. $region = $farminfo['region'];
  92. }
  93. else
  94. {
  95. $clientid = $_SESSION['uid'];
  96. $region = $_SESSION['aws_region'];
  97. }
  98. $Client = Client::Load($clientid);
  99. $AmazonEC2Client = AmazonEC2::GetInstance(AWSRegions::GetAPIURL($region));
  100. $AmazonEC2Client->SetAuthKeys($Client->AWSPrivateKey, $Client->AWSCertificate);
  101. $AmazonEC2Client->RebootInstances($instances);
  102. return true;
  103. }
  104. public function TerminateInstances(array $instances, $farmid, $decrease_mininstances_setting = false, $force_terminate = false)
  105. {
  106. if ($farmid)
  107. {
  108. $farminfo = $this->DB->GetRow("SELECT * FROM farms WHERE id=?", array($farmid));
  109. if (!$farminfo || ($_SESSION['uid'] != 0 && $farminfo['clientid'] != $_SESSION['uid']))
  110. throw new Exception("Farm not found in database");
  111. $clientid = $farminfo['clientid'];
  112. $region = $farminfo['region'];
  113. }
  114. else
  115. {
  116. $clientid = $_SESSION['uid'];
  117. $region = $_SESSION['aws_region'];
  118. }
  119. if ($force_terminate)
  120. {
  121. $Client = Client::Load($clientid);
  122. $AmazonEC2Client = AmazonEC2::GetInstance(AWSRegions::GetAPIURL($region));
  123. $AmazonEC2Client->SetAuthKeys($Client->AWSPrivateKey, $Client->AWSCertificate);
  124. $AmazonEC2Client->TerminateInstances($instances);
  125. }
  126. else
  127. {
  128. foreach ($instances as $instance_id)
  129. {
  130. $instanceinfo = $this->DB->GetRow("SELECT * FROM farm_instances WHERE instance_id=?", array($instance_id));
  131. try
  132. {
  133. Logger::getLogger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($farmid,
  134. sprintf("Scheduled termination for instance %s (%s). It will be terminated in 3 minutes.",
  135. $instanceinfo["instance_id"],
  136. $instanceinfo["external_ip"]
  137. )
  138. ));
  139. Scalr::FireEvent($farmid, new BeforeHostTerminateEvent(DBInstance::LoadByID($instanceinfo['id']), false));
  140. }
  141. catch (Exception $e)
  142. {
  143. $this->Logger->fatal(sprintf("Cannot terminate %s: %s",
  144. $instanceinfo['instance_id'],
  145. $e->getMessage()
  146. ));
  147. }
  148. }
  149. }
  150. if ($decrease_mininstances_setting && $farmid)
  151. {
  152. $instance_info = $this->DB->GetRow("SELECT * FROM farm_instances WHERE instance_id=?", array($instances[0]));
  153. $DBFarmRole = DBFarmRole::LoadByID($instance_info['farm_roleid']);
  154. $min_instances = $DBFarmRole->GetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES);
  155. if ($min_instances > 1)
  156. {
  157. $DBFarmRole->SetSetting(DBFarmRole::SETTING_SCALING_MIN_INSTANCES,
  158. $min_instances-1
  159. );
  160. }
  161. }
  162. return true;
  163. }
  164. }
  165. // Run
  166. try
  167. {
  168. $AjaxUIServer = new AjaxUIServer();
  169. $Reflect = new ReflectionClass($AjaxUIServer);
  170. if (!$Reflect->hasMethod($req_action))
  171. throw new Exception(sprintf("Unknown action: %s", $req_action));
  172. $ReflectMethod = $Reflect->getMethod($req_action);
  173. $args = array();
  174. foreach ($ReflectMethod->getParameters() as $param)
  175. {
  176. if (!$param->isArray())
  177. $args[$param->name] = $_REQUEST[$param->name];
  178. else
  179. $args[$param->name] = json_decode($_REQUEST[$param->name]);
  180. }
  181. $ReflectMethod->invokeArgs($AjaxUIServer, $args);
  182. print json_encode(array(
  183. "result" => "ok"
  184. ));
  185. }
  186. catch(Exception $e)
  187. {
  188. print json_encode(array(
  189. "result" => "error",
  190. "msg" => $e->getMessage()
  191. ));
  192. }
  193. exit();
  194. ?>