PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/1.2.0/src/api/class.ScalrAPI20090507.php

http://scalr.googlecode.com/
PHP | 347 lines | 262 code | 69 blank | 16 comment | 30 complexity | 95e1d4aaccf4de198a7878e8ab4e84c5 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. class ScalrAPI20090507 extends ScalrAPICore
  3. {
  4. public function TerminateFarm($FarmID, $KeepEBS, $KeepEIP, $KeepDNSZone)
  5. {
  6. $response = $this->CreateInitialResponse();
  7. $farminfo = $this->DB->GetRow("SELECT * FROM farms WHERE id=? AND clientid=?",
  8. array($FarmID, $this->Client->ID)
  9. );
  10. if (!$farminfo)
  11. throw new Exception(sprintf("Farm #%s not found", $FarmID));
  12. if ($farminfo['status'] != FARM_STATUS::RUNNING)
  13. throw new Exception(sprintf("Farm already terminated", $FarmID));
  14. $event = new FarmTerminatedEvent(
  15. (($KeepDNSZone) ? 0 : 1),
  16. (($KeepEIP) ? 1 : 0),
  17. true,
  18. (($KeepEBS) ? 1 : 0)
  19. );
  20. Scalr::FireEvent($FarmID, $event);
  21. $response->Result = true;
  22. return $response;
  23. }
  24. public function LaunchFarm($FarmID)
  25. {
  26. $response = $this->CreateInitialResponse();
  27. $farminfo = $this->DB->GetRow("SELECT * FROM farms WHERE id=? AND clientid=?",
  28. array($FarmID, $this->Client->ID)
  29. );
  30. if (!$farminfo)
  31. throw new Exception(sprintf("Farm #%s not found", $FarmID));
  32. if ($farminfo['status'] == FARM_STATUS::RUNNING)
  33. throw new Exception(sprintf("Farm already running", $FarmID));
  34. Scalr::FireEvent($FarmID, new FarmLaunchedEvent(1));
  35. $response->Result = true;
  36. return $response;
  37. }
  38. public function ListRoles($Region, $AmiID = null, $Name = null, $Prefix = null)
  39. {
  40. $response = $this->CreateInitialResponse();
  41. $response->RoleSet = new stdClass();
  42. $response->RoleSet->Item = array();
  43. $sql = "SELECT * FROM roles WHERE iscompleted='1' AND (clientid='0' OR clientid='{$this->Client->ID}') AND region={$this->DB->qstr($Region)}";
  44. if ($AmiID)
  45. $sql .= " AND ami_id={$this->DB->qstr($AmiID)}";
  46. if ($Name)
  47. $sql .= " AND name={$this->DB->qstr($Name)}";
  48. if ($Prefix)
  49. $sql .= " AND name LIKE{$this->DB->qstr("%{$Prefix}%")}";
  50. $rows = $this->DB->Execute($sql);
  51. while ($row = $rows->FetchRow())
  52. {
  53. if ($row['clientid'] == 0)
  54. $row["client_name"] = "Scalr";
  55. else
  56. $row["client_name"] = $this->DB->GetOne("SELECT fullname FROM clients WHERE id='{$row['clientid']}'");
  57. if (!$row["client_name"])
  58. $row["client_name"] = "";
  59. $itm = new stdClass();
  60. $itm->{"Name"} = $row['name'];
  61. $itm->{"Owner"} = $row["client_name"];
  62. $itm->{"Category"} = ROLE_ALIAS::GetTypeByAlias($row['alias']);
  63. $itm->{"AmiID"} = $row['ami_id'];
  64. $itm->{"Architecture"} = $row['architecture'];
  65. $itm->{"BuildDate"} = $row['dtbuilt'];
  66. $response->RoleSet->Item[] = $itm;
  67. }
  68. return $response;
  69. }
  70. /**
  71. *
  72. * @return object
  73. */
  74. public function GetFarmStats($FarmID, $Date = null)
  75. {
  76. $response = $this->CreateInitialResponse();
  77. $response->StatisticsSet = new stdClass();
  78. $response->StatisticsSet->Item = array();
  79. preg_match("/([0-9]{2})\-([0-9]{4})/", $Date, $m);
  80. if ($m[1] && $m[2])
  81. $filter_sql = " AND month='{$m[1]}' AND year='{$m[2]}'";
  82. $farminfo = $this->DB->GetRow("SELECT * FROM farms WHERE id=? AND clientid=?",
  83. array($FarmID, $this->Client->ID)
  84. );
  85. if (!$farminfo)
  86. throw new Exception(sprintf("Farm #%s not found", $FarmID));
  87. $rows = $this->DB->Execute("SELECT *, bw_out/1024 as bw_out, bw_in/1024 as bw_in FROM farm_stats WHERE farmid=? {$filter_sql} ORDER BY id DESC",
  88. array($FarmID)
  89. );
  90. while ($row = $rows->FetchRow())
  91. {
  92. $itm = new stdClass();
  93. $itm->Month = $row['month'];
  94. $itm->Year = $row['year'];
  95. $itm->Statistics = new stdClass();
  96. $itm->Statistics->{"BandwidthIn"} = round($row["bw_in"], 2);
  97. $itm->Statistics->{"BandwidthOut"} = round($row["bw_out"], 2);
  98. $itm->Statistics->{"BandwidthTotal"} = (int)($row["bw_out"]+$row["bw_in"]);
  99. $Reflect = new ReflectionClass("INSTANCE_FLAVOR");
  100. foreach ($Reflect->getConstants() as $n=>$v)
  101. {
  102. $field = str_replace(".", "_", $v);
  103. $itm->Statistics->{"{$v}Usage"} = round($row[$field]/60/60, 1);
  104. }
  105. $response->StatisticsSet->Item[] = $itm;
  106. }
  107. return $response;
  108. }
  109. /**
  110. *
  111. * @return object
  112. * @todo: More checks and better validation
  113. */
  114. public function ExecuteScript($ScriptID, $Timeout, $Async, $FarmID, $FarmRoleID = null, $InstanceID = null)
  115. {
  116. $response = $this->CreateInitialResponse();
  117. $farminfo = $this->DB->GetRow("SELECT * FROM farms WHERE id=? AND clientid=?",
  118. array($FarmID, $this->Client->ID)
  119. );
  120. if (!$farminfo)
  121. throw new Exception(sprintf("Farm #%s not found", $FarmID));
  122. /* * */
  123. if ($InstanceID && !$FarmRoleID)
  124. {
  125. $DBInstance = DBInstance::LoadByIID($InstanceID);
  126. $FarmRoleID = $DBInstance->FarmRoleID;
  127. }
  128. $config = array(); //TODO:
  129. $scriptid = (int)$ScriptID;
  130. if ($InstanceID)
  131. $target = SCRIPTING_TARGET::INSTANCE;
  132. else if ($RoleName)
  133. $target = SCRIPTING_TARGET::ROLE;
  134. else
  135. $target = SCRIPTING_TARGET::FARM;
  136. $event_name = 'APIEvent-'.date("YmdHi").'-'.rand(1000,9999);
  137. $version = 'latest';
  138. $farmid = (int)$FarmID;
  139. $timeout = (int)$Timeout;
  140. $issync = ($Async == 1) ? 0 : 1;
  141. $this->DB->Execute("INSERT INTO farm_role_scripts SET
  142. scriptid = ?,
  143. farmid = ?,
  144. farm_roleid = ?,
  145. params = ?,
  146. event_name = ?,
  147. target = ?,
  148. version = ?,
  149. timeout = ?,
  150. issync = ?,
  151. ismenuitem = ?
  152. ", array(
  153. $scriptid, $farmid, $FarmRoleID, serialize($config), $event_name, $target, $version, $timeout, $issync, 0
  154. ));
  155. $farm_rolescript_id = $this->DB->Insert_ID();
  156. switch($target)
  157. {
  158. case SCRIPTING_TARGET::FARM:
  159. $instances = $this->DB->GetAll("SELECT * FROM farm_instances WHERE state IN (?,?) AND farmid=?",
  160. array(INSTANCE_STATE::INIT, INSTANCE_STATE::RUNNING, $farmid)
  161. );
  162. break;
  163. case SCRIPTING_TARGET::ROLE:
  164. $instances = $this->DB->GetAll("SELECT * FROM farm_instances WHERE state IN (?,?) AND farm_roleid=?",
  165. array(INSTANCE_STATE::INIT, INSTANCE_STATE::RUNNING, $FarmRoleID)
  166. );
  167. break;
  168. case SCRIPTING_TARGET::INSTANCE:
  169. $instances = $this->DB->GetAll("SELECT * FROM farm_instances WHERE state IN (?,?) AND instance_id=? AND farmid=?",
  170. array(INSTANCE_STATE::INIT, INSTANCE_STATE::RUNNING, $InstanceID, $farmid)
  171. );
  172. break;
  173. }
  174. //
  175. // Send Trap
  176. //
  177. if (count($instances) > 0)
  178. {
  179. foreach ($instances as $instance)
  180. {
  181. $DBInstance = DBInstance::LoadByID($instance['id']);
  182. $DBInstance->SendMessage(new EventNoticeScalrMessage(
  183. $instance['internal_ip'],
  184. "FRSID-{$farm_rolescript_id}",
  185. $instance['role_name'],
  186. $event_name
  187. ));
  188. }
  189. }
  190. $response->Result = true;
  191. return $response;
  192. }
  193. public function ListFarms()
  194. {
  195. $response = $this->CreateInitialResponse();
  196. $response->FarmSet = new stdClass();
  197. $response->FarmSet->Item = array();
  198. $farms = $this->DB->Execute("SELECT * FROM farms WHERE clientid=?", array($this->Client->ID));
  199. while ($farm = $farms->FetchRow())
  200. {
  201. $itm = new stdClass();
  202. $itm->{"ID"} = $farm['id'];
  203. $itm->{"Name"} = $farm['name'];
  204. $itm->{"Region"} = $farm['region'];
  205. $itm->{"Status"} = $farm['status'];
  206. $response->FarmSet->Item[] = $itm;
  207. }
  208. return $response;
  209. }
  210. public function ListScripts()
  211. {
  212. $response = $this->CreateInitialResponse();
  213. $response->ScriptSet = new stdClass();
  214. $response->ScriptSet->Item = array();
  215. $filter_sql .= " AND (";
  216. // Show shared roles
  217. $filter_sql .= " origin='".SCRIPT_ORIGIN_TYPE::SHARED."'";
  218. // Show custom roles
  219. $filter_sql .= " OR (origin='".SCRIPT_ORIGIN_TYPE::CUSTOM."' AND clientid='{$this->Client->ID}')";
  220. //Show approved contributed roles
  221. $filter_sql .= " OR (origin='".SCRIPT_ORIGIN_TYPE::USER_CONTRIBUTED."' AND (scripts.approval_state='".APPROVAL_STATE::APPROVED."' OR clientid='{$this->Client->ID}'))";
  222. $filter_sql .= ")";
  223. $sql = "SELECT
  224. scripts.id,
  225. scripts.name,
  226. scripts.description,
  227. scripts.origin,
  228. scripts.clientid,
  229. scripts.approval_state,
  230. MAX(script_revisions.dtcreated) as dtupdated, MAX(script_revisions.revision) AS version FROM scripts
  231. INNER JOIN script_revisions ON script_revisions.scriptid = scripts.id
  232. WHERE 1=1 {$filter_sql}";
  233. $sql .= " GROUP BY script_revisions.scriptid";
  234. $rows = $this->DB->Execute($sql);
  235. while ($row = $rows->FetchRow())
  236. {
  237. $itm = new stdClass();
  238. $itm->{"ID"} = $row['id'];
  239. $itm->{"Name"} = $row['name'];
  240. $itm->{"Description"} = $row['description'];
  241. $itm->{"LatestRevision"} = $row['version'];
  242. $response->ScriptSet->Item[] = $itm;
  243. }
  244. return $response;
  245. }
  246. public function ListApplications()
  247. {
  248. $response = $this->CreateInitialResponse();
  249. $response->ApplicationSet = new stdClass();
  250. $response->ApplicationSet->Item = array();
  251. $rows = $this->DB->Execute("SELECT * FROM zones WHERE clientid=?", array($this->Client->ID));
  252. while ($row = $rows->FetchRow())
  253. {
  254. $itm = new stdClass();
  255. $itm->{"DomainName"} = $row['zone'];
  256. $itm->{"FarmID"} = $row['farmid'];
  257. $itm->{"FarmRole"} = $row['role_name'];
  258. $itm->{"Status"} = $row['status'];
  259. $itm->{"IPSet"} = new stdClass();
  260. $itm->{"IPSet"}->item = array();
  261. if ($row['status'] == ZONE_STATUS::ACTIVE)
  262. {
  263. $instances = $this->DB->GetAll("SELECT rvalue FROM records WHERE zoneid=? AND rtype=? AND rkey=? AND issystem=?",
  264. array($row['id'], 'A', '@', 1)
  265. );
  266. foreach ($instances as $instance)
  267. {
  268. $itm_ip = new stdClass();
  269. $itm_ip->IPAddress = $instance['rvalue'];
  270. $itm->{"IPSet"}->Item[] = $itm_ip;
  271. }
  272. }
  273. $response->ApplicationSet->Item[] = $itm;
  274. }
  275. return $response;
  276. }
  277. }
  278. ?>