PageRenderTime 74ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/1.0RC3/www/server/server.php

http://scalr.googlecode.com/
PHP | 423 lines | 332 code | 86 blank | 5 comment | 72 complexity | 633a7423df1eaa655ea3e8cac3dde583 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?
  2. require("../src/prepend.inc.php");
  3. switch($_GET["_cmd"])
  4. {
  5. case "get_script_args":
  6. $scriptid = (int)$req_scriptid;
  7. $dbversions = $db->GetAll("SELECT * FROM script_revisions WHERE scriptid=? AND approval_state=? ORDER BY revision ASC",
  8. array($scriptid, APPROVAL_STATE::APPROVED)
  9. );
  10. $versions = array();
  11. foreach ($dbversions as $version)
  12. {
  13. preg_match_all("/\%([^\%]+)\%/si", $version["script"], $matches);
  14. $vars = $matches[1];
  15. $data = array();
  16. foreach ($vars as $var)
  17. {
  18. if (!in_array($var, CONFIG::$SCRIPT_BUILTIN_VARIABLES))
  19. $data[$var] = ucwords(str_replace("_", " ", $var));
  20. }
  21. $data = json_encode($data);
  22. $versions[] = array("revision" => $version['revision'], "fields" => $data);
  23. }
  24. print json_encode($versions);
  25. exit();
  26. break;
  27. case "check_role_name":
  28. $role_name = $req_name;
  29. $ami_id = $req_ami_id;
  30. if (!preg_match("/^[A-Za-z0-9-]+$/", $role_name))
  31. die(_("Allowed chars for role name is [A-Za-z0-9-]"));
  32. $role_info = $db->GetRow("SELECT * FROM ami_roles WHERE ami_id=? AND clientid=? AND roletype=?",
  33. array($ami_id, $_SESSION['uid'], ROLE_TYPE::CUSTOM)
  34. );
  35. if (!$role_info)
  36. die("REDIRECT");
  37. if ($role_info['name'] == $role_name)
  38. die("ok");
  39. $chk = $db->GetOne("SELECT id FROM ami_roles WHERE name=? AND iscompleted != '2' AND ami_id != ?",
  40. array($role_name, $ami_id)
  41. );
  42. if (!$chk)
  43. die("ok");
  44. else
  45. die(_("Name is already used by an existing role. Please choose another name."));
  46. break;
  47. case "get_script_props":
  48. $script_id = (int)$req_id;
  49. $version = (int)$req_version;
  50. $script_info = $db->GetRow("SELECT * FROM scripts WHERE id=?",
  51. array($script_id)
  52. );
  53. $script_info['revision'] = $version;
  54. $script_info['script'] = $db->GetOne("SELECT script FROM script_revisions WHERE scriptid=? AND revision=?",
  55. array($script_id, $version)
  56. );
  57. if ($_SESSION['uid'] != 0)
  58. {
  59. if ($script_info['origin'] != SCRIPT_ORIGIN_TYPE::SHARED && $script_info['clientid'] != $_SESSION['uid'])
  60. die();
  61. }
  62. print json_encode($script_info);
  63. exit();
  64. break;
  65. case "get_role_params":
  66. $farmid = (int)$req_farmid;
  67. $ami_info = $db->GetRow("SELECT * FROM ami_roles WHERE ami_id=?", array($req_ami_id));
  68. if ($ami_info['clientid'] != 0 && $ami_info['clientid'] != $_SESSION['uid'] && $_SESSION['uid'] != 0)
  69. die(_("There are no parameters for this role"));
  70. $params = $db->GetAll("SELECT * FROM role_options WHERE ami_id=?", array($req_ami_id));
  71. if (count($params) > 0)
  72. {
  73. $DataForm = new DataForm();
  74. foreach ($params as $param)
  75. {
  76. // Prepare options array
  77. if ($param['options'])
  78. {
  79. $options = json_decode($param['options'], true);
  80. $fopts = array();
  81. foreach ($options as $option)
  82. $fopts[$option[0]] = $option[1];
  83. }
  84. // Get field value
  85. $value = $db->GetOne("SELECT value FROM farm_role_options WHERE farmid=? AND ami_id=? AND name=?",
  86. array($farmid, $req_ami_id, $param['name'])
  87. );
  88. if ($value === false)
  89. $value = $param['defval'];
  90. $field = new DataFormField(
  91. $param['name'],
  92. $param['type'],
  93. $param['name'],
  94. $param['isrequired'],
  95. $fopts,
  96. $param['defval'],
  97. $value,
  98. null,
  99. $param['allow_multiple_choice']
  100. );
  101. $DataForm->AppendField($field);
  102. }
  103. $fields = $DataForm->ListFields();
  104. if (count($fields) != 0)
  105. {
  106. $Smarty->assign(array("DataForm" => $DataForm, "elem_id"=> "role_params", "field_prefix" => "", "field_suffix" => ""));
  107. print $Smarty->fetch("inc/dynamicform.tpl");
  108. }
  109. else
  110. die(_("There are no parameters for this role"));
  111. }
  112. else
  113. die(_("There are no parameters for this role"));
  114. exit();
  115. break;
  116. case "get_script_template_source":
  117. $id = (int)$req_scriptid;
  118. $version = $req_version;
  119. $templateinfo = $db->GetRow("SELECT * FROM scripts WHERE id=?", array($id));
  120. if ($_SESSION['uid'] != 0)
  121. {
  122. if ($templateinfo['origin'] == SCRIPT_ORIGIN_TYPE::CUSTOM && $templateinfo['clientid'] != $_SESSION['uid'])
  123. die(_('There is no source avaiable for selected script'));
  124. }
  125. $sql = "SELECT * FROM script_revisions WHERE scriptid='{$id}'";
  126. if ($version == "latest")
  127. $sql .= " AND revision=(SELECT MAX(revision) FROM script_revisions WHERE scriptid='{$id}' AND approval_state='".APPROVAL_STATE::APPROVED."')";
  128. else
  129. {
  130. $version = (int)$version;
  131. $sql .= " AND revision='{$version}'";
  132. }
  133. $script = $db->GetRow($sql);
  134. if ($templateinfo['origin'] == SCRIPT_ORIGIN_TYPE::USER_CONTRIBUTED)
  135. {
  136. if ($templateinfo['clientid'] != $_SESSION['uid'] && $script['approval_state'] != APPROVAL_STATE::APPROVED)
  137. die(_('There is no source avaiable for selected script'));
  138. }
  139. if ($script)
  140. {
  141. print $script['script'];
  142. }
  143. else
  144. print _('There is no source avaiable for selected script');
  145. exit();
  146. break;
  147. case "get_snapshots_list":
  148. $AmazonEC2Client = new AmazonEC2($_SESSION["aws_private_key"], $_SESSION["aws_certificate"]);
  149. // Rows
  150. $response = $AmazonEC2Client->DescribeSnapshots();
  151. $rowz = $response->snapshotSet->item;
  152. if ($rowz instanceof stdClass)
  153. $rowz = array($rowz);
  154. foreach ($rowz as $pk=>$pv)
  155. {
  156. $pv->startTime = date("Y-m-d H:i:s", strtotime($pv->startTime));
  157. $item = $pv;
  158. $item->comment = $db->GetOne("SELECT comment FROM ebs_snaps_info WHERE snapid=?", array(
  159. $item->snapshotId
  160. ));
  161. $item->progress = (int)preg_replace("/[^0-9]+/", "", $item->progress);
  162. $item->free = 100 - $item->progress;
  163. $item->bar_begin = ($item->progress == 0) ? "empty" : "filled";
  164. $item->bar_end = ($item->free != 0) ? "empty" : "filled";
  165. $item->used_percent_width = round(120/100*$item->progress, 2);
  166. $item->free_percent_width = round(120/100*$item->free, 2);
  167. if ($req_volumeid)
  168. {
  169. if ($req_volumeid == $item->volumeId)
  170. {
  171. $snaps[] = $item;
  172. }
  173. $display["snaps_header"] = sprintf(_("Snapshots for %s"), $item->volumeId);
  174. }
  175. elseif (!$req_snapid || $req_snapid == $item->snapshotId)
  176. {
  177. $snaps[] = $item;
  178. }
  179. }
  180. $Smarty->assign(array("snaps" => $snaps, "error" => $error));
  181. $content = $Smarty->fetch("ajax_tables/snapshots_list.tpl");
  182. print $content;
  183. exit();
  184. break;
  185. case "get_instance_process_list":
  186. Core::Load("NET/SNMP");
  187. $SNMP = new SNMP();
  188. // Check cache
  189. $plist_cache = CACHEPATH."/ajax_plist.{$get_iid}.cache";
  190. if (file_exists($plist_cache))
  191. {
  192. clearstatcache();
  193. $time = filemtime($plist_cache);
  194. if ($time > time()-CONFIG::$AJAX_PROCESSLIST_CACHE_LIFETIME) //TODO: Move to config
  195. {
  196. readfile($plist_cache);
  197. exit();
  198. }
  199. }
  200. try
  201. {
  202. $instanceinfo = $db->GetRow("SELECT * FROM farm_instances WHERE instance_id=?", array($get_iid));
  203. if (!$instanceinfo)
  204. throw new Exception(_("Instance not found in database."));
  205. $farminfo = $db->GetRow("SELECT * FROM farms WHERE id=?", array($instanceinfo['farmid']));
  206. if (!$farminfo || ($_SESSION['uid'] != 0 && $farminfo['clientid'] != $_SESSION['uid']))
  207. throw new Exception("Instance not found in database.");
  208. $SNMP->Connect($instanceinfo['external_ip'], null, $farminfo['hash'], null, null, true);
  209. $res = $SNMP->GetFullTree(".1.3.6.1.2.1.25.4.2");
  210. foreach ((array)$res as $k=>$v)
  211. {
  212. if (stristr($k, "HOST-RESOURCES-MIB::hrSWRunIndex"))
  213. {
  214. //
  215. }
  216. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunName"))
  217. {
  218. preg_match("/HOST-RESOURCES-MIB::hrSWRunName.([0-9]+)/si", $k, $matches);
  219. $processes[$matches[1]]["hrSWRunName"] = $v;
  220. }
  221. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunPath"))
  222. {
  223. preg_match("/HOST-RESOURCES-MIB::hrSWRunPath.([0-9]+)/si", $k, $matches);
  224. $processes[$matches[1]]["hrSWRunPath"] = $v;
  225. }
  226. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunParameters"))
  227. {
  228. preg_match("/HOST-RESOURCES-MIB::hrSWRunParameters.([0-9]+)/si", $k, $matches);
  229. $processes[$matches[1]]["hrSWRunParameters"] = trim($v);
  230. }
  231. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunType"))
  232. {
  233. preg_match("/HOST-RESOURCES-MIB::hrSWRunType.([0-9]+)/si", $k, $matches);
  234. switch(trim($v))
  235. {
  236. case 1:
  237. $processes[$matches[1]]["hrSWRunType"] = "unknown";
  238. break;
  239. case 2:
  240. $processes[$matches[1]]["hrSWRunType"] = "operatingSystem";
  241. break;
  242. case 3:
  243. $processes[$matches[1]]["hrSWRunType"] = "deviceDriver";
  244. break;
  245. case 4:
  246. $processes[$matches[1]]["hrSWRunType"] = "application";
  247. break;
  248. }
  249. }
  250. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunStatus"))
  251. {
  252. preg_match("/HOST-RESOURCES-MIB::hrSWRunStatus.([0-9]+)/si", $k, $matches);
  253. switch(trim($v))
  254. {
  255. case 1:
  256. $processes[$matches[1]]["hrSWRunStatus"] = "running";
  257. break;
  258. case 2:
  259. $processes[$matches[1]]["hrSWRunStatus"] = "runnable";
  260. break;
  261. case 3:
  262. $processes[$matches[1]]["hrSWRunStatus"] = "notRunnable";
  263. break;
  264. case 4:
  265. $processes[$matches[1]]["hrSWRunStatus"] = "invalid";
  266. break;
  267. }
  268. }
  269. }
  270. $res = $SNMP->GetFullTree(".1.3.6.1.2.1.25.5.1");
  271. foreach ((array)$res as $k=>$v)
  272. {
  273. if (stristr($k, "hrSWRunPerfCPU"))
  274. {
  275. preg_match("/HOST-RESOURCES-MIB::hrSWRunPerfCPU.([0-9]+)/si", $k, $matches);
  276. $processes[$matches[1]]["hrSWRunPerfCPU"] = trim($v);
  277. }
  278. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunPerfMem"))
  279. {
  280. preg_match("/HOST-RESOURCES-MIB::hrSWRunPerfMem.([0-9]+)/si", $k, $matches);
  281. $processes[$matches[1]]["hrSWRunPerfMem"] = Formater::Bytes2String(((int)trim($v))*1024);
  282. }
  283. }
  284. sort($processes);
  285. }
  286. catch(Exception $e)
  287. {
  288. $error = $e->getMessage();
  289. }
  290. $Smarty->assign(array("processes" => $processes, "error" => $error));
  291. $content = $Smarty->fetch("ajax_tables/process_list.tpl");
  292. @file_put_contents($plist_cache, $content);
  293. print $content;
  294. exit();
  295. break;
  296. case "get_instance_info":
  297. Core::Load("NET/SNMP");
  298. $SNMP = new SNMP();
  299. $instanceinfo = $db->GetRow("SELECT * FROM farm_instances WHERE instance_id=?", array($get_iid));
  300. if (!$instanceinfo)
  301. die(_("Instance not found in database."));
  302. $farminfo = $db->GetRow("SELECT * FROM farms WHERE id=?", array($instanceinfo['farmid']));
  303. if (!$farminfo || ($_SESSION['uid'] != 0 && $farminfo['clientid'] != $_SESSION['uid']))
  304. die(_("Instance not found in database."));
  305. $Client = Client::Load($farminfo['clientid']);
  306. try
  307. {
  308. $AmazonEC2Client = new AmazonEC2($Client->AWSPrivateKey, $Client->AWSCertificate);
  309. $response = $AmazonEC2Client->DescribeInstances($get_iid);
  310. $instanceset = $response->reservationSet->item->instancesSet;
  311. $instanceinfo['type'] = $instanceset->item->instanceType;
  312. $instanceinfo['placement'] = $instanceset->item->placement->availabilityZone;
  313. $instanceinfo['launchtime'] = date("Y-m-d H:i:s", strtotime($instanceset->item->launchTime));
  314. }
  315. catch(Exception $e)
  316. {
  317. die(sprintf(_("Cannot fetch instance information from EC2: %s"), $e->getMessage()));
  318. }
  319. $instanceinfo['LA'] = _("Unknown");
  320. if ($instanceinfo['external_ip'])
  321. {
  322. $SNMP->Connect($instanceinfo['external_ip'], null, $farminfo['hash'], null, null, true);
  323. $res = $SNMP->Get(".1.3.6.1.4.1.2021.10.1.3.3");
  324. if ($res)
  325. $instanceinfo['LA'] = number_format((float)$res, 2);
  326. }
  327. $eip = $db->GetOne("SELECT id FROM elastic_ips WHERE ipaddress=? AND farmid=?",
  328. array($instanceinfo["external_ip"], $instanceinfo['farmid'])
  329. );
  330. $instanceinfo['IsElastic'] = ($instanceinfo['custom_elastic_ip'] || $eip) ? 1 : 0;
  331. $instanceinfo['role_alias'] = $db->GetOne("SELECT alias FROM ami_roles WHERE ami_id=?", array($instanceinfo['ami_id']));
  332. $Smarty->assign(array("i" => $instanceinfo));
  333. $Smarty->display("inc/popup_instanceinfo.tpl");
  334. break;
  335. }
  336. exit();
  337. ?>