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

/branches/1.0/www/farms_add.php

http://scalr.googlecode.com/
PHP | 278 lines | 219 code | 50 blank | 9 comment | 36 complexity | a634c653d4fa8ff74a96dc420c367b69 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?
  2. require("src/prepend.inc.php");
  3. $display['load_extjs'] = true;
  4. //$display['no_extjs_style'] = true;
  5. set_time_limit(360);
  6. $req_farmid = ($req_farmid) ? $req_farmid : $req_id;
  7. if ($_SESSION['uid'] == 0)
  8. {
  9. if (!$req_farmid)
  10. UI::Redirect("farms_view.php");
  11. else
  12. {
  13. $uid = $db->GetOne("SELECT clientid FROM farms WHERE id='{$req_farmid}'");
  14. }
  15. }
  16. else
  17. {
  18. $uid = $_SESSION['uid'];
  19. }
  20. $Client = Client::Load($uid);
  21. $used_slots = $db->GetOne("SELECT SUM(value) FROM farm_role_settings WHERE name=?
  22. AND farm_roleid IN (SELECT id FROM farm_amis WHERE farmid IN (SELECT id FROM farms WHERE clientid=?))",
  23. array(DBFarmRole::SETTING_SCALING_MAX_INSTANCES, $Client->ID)
  24. );
  25. $client_max_instances = $Client->GetSettingValue(CLIENT_SETTINGS::MAX_INSTANCES_LIMIT);
  26. $i_limit = $client_max_instances ? $client_max_instances : CONFIG::$CLIENT_MAX_INSTANCES;
  27. $client_max_eips = $Client->GetSettingValue(CLIENT_SETTINGS::MAX_EIPS_LIMIT);
  28. $eips_limit = $client_max_eips ? $client_max_eips : CONFIG::$CLIENT_MAX_EIPS;
  29. $avail_slots = $i_limit - $used_slots;
  30. if ($avail_slots <= 5)
  31. $display["warnmsg"] = sprintf(_("You have %s spare instances available on your account."), $avail_slots);
  32. if ($req_farmid)
  33. {
  34. $region = $db->GetOne("SELECT region FROM farms WHERE id=?", array($req_farmid));
  35. $display["title"] = _("Edit farm");
  36. }
  37. else
  38. {
  39. $display["title"] = _("Farm builder");
  40. if (!$req_region)
  41. {
  42. $Smarty->assign($display);
  43. $Smarty->display("region_information_step.tpl");
  44. exit();
  45. }
  46. else
  47. $region = $req_region;
  48. }
  49. $display['region'] = $region;
  50. $_SESSION['farm_builder_region'] = $region;
  51. $Client = Client::Load($uid);
  52. $AmazonEC2Client = AmazonEC2::GetInstance(AWSRegions::GetAPIURL($region));
  53. $AmazonEC2Client->SetAuthKeys($Client->AWSPrivateKey, $Client->AWSCertificate);
  54. // Get Avail zones
  55. $avail_zones_resp = $AmazonEC2Client->DescribeAvailabilityZones();
  56. $display["avail_zones"] = array();
  57. // Random assign zone
  58. array_push($display["avail_zones"], "");
  59. foreach ($avail_zones_resp->availabilityZoneInfo->item as $zone)
  60. {
  61. if (stristr($zone->zoneState,'available'))
  62. array_push($display["avail_zones"], (string)$zone->zoneName);
  63. }
  64. // Get EBS Snapshots list
  65. $response = $AmazonEC2Client->DescribeSnapshots();
  66. $rowz = $response->snapshotSet->item;
  67. if ($rowz instanceof stdClass)
  68. $rowz = array($rowz);
  69. foreach ($rowz as $pk=>$pv)
  70. {
  71. if ($pv->status == 'completed')
  72. $display['snapshots'][] = $pv->snapshotId;
  73. }
  74. //Get List of registered Scaling Algorithms
  75. $display['scaling_algos'] = array();
  76. foreach (RoleScalingManager::$ScalingAlgos as $Algo)
  77. {
  78. $algo_name = strtolower(str_replace("ScalingAlgo", "", get_class($Algo)));
  79. $display['scaling_algos'][$algo_name] = array(
  80. 'based_on' => $Algo->GetAlgoDescription(),
  81. 'settings' => $Algo->GetConfigurationForm($display["farminfo"]["clientid"])
  82. );
  83. }
  84. if ($req_id)
  85. {
  86. $display["farminfo"] = $db->GetRow("SELECT * FROM farms WHERE id=?", array($req_id));
  87. if ($_SESSION['uid'] != 0 && $_SESSION['uid'] != $display["farminfo"]["clientid"])
  88. UI::Redirect("farms_view.php");
  89. if (!$display["farminfo"])
  90. {
  91. $errmsg = _("Farm not found");
  92. UI::Redirect("farms_view.php");
  93. }
  94. $servers = $db->GetAll("SELECT * FROM farm_amis WHERE farmid=? ORDER BY launch_index ASC", array($req_id));
  95. $display['roles'] = array();
  96. foreach ($servers as &$row)
  97. {
  98. $ami_info = $db->GetRow("SELECT * FROM ami_roles WHERE ami_id=?", array($row['ami_id']));
  99. if (!$ami_info)
  100. continue;
  101. $row["role"] = $ami_info["name"];
  102. $scripts = $db->GetAll("SELECT * FROM farm_role_scripts WHERE farmid=? AND ami_id=?", array($display["farminfo"]["id"], $row['ami_id']));
  103. $scripts_object = new stdClass();
  104. foreach ($scripts as $script)
  105. {
  106. if (substr($script['event_name'], 0, 11) != 'CustomEvent')
  107. {
  108. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"} = new stdClass();
  109. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->config = unserialize($script['params']);
  110. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->target = $script['target'];
  111. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->version = $script['version'];
  112. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->issync = $script['issync'];
  113. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->timeout = $script['timeout'];
  114. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->order_index = $script['order_index'];
  115. }
  116. }
  117. if ($ami_info['roletype'] == ROLE_TYPE::SHARED && $ami_info['clientid'] != 0)
  118. {
  119. $author_info = $db->GetRow("SELECT fullname FROM clients WHERE id=?", array($ami_info['clientid']));
  120. $author = ($author_info['fullname']) ? $author_info['fullname'] : _('Scalr user');
  121. }
  122. else
  123. $author = false;
  124. $DBFarmRole = DBFarmRole::LoadByID($row['id']);
  125. $role_settings = $DBFarmRole->GetAllSettings();
  126. $role = array(
  127. 'name' => $ami_info["name"],
  128. 'arch' => $ami_info["architecture"],
  129. 'alias' => $ami_info["alias"],
  130. 'ami_id' => $ami_info["ami_id"],
  131. 'type' => ROLE_ALIAS::GetTypeByAlias($ami_info["alias"]),
  132. 'description' => $ami_info["description"],
  133. 'scripts' => $scripts_object,
  134. 'author' => $author,
  135. 'settings' => $role_settings,
  136. 'launch_index' => (int)$row['launch_index'],
  137. 'options' => array(
  138. 'reboot_timeout' => $row['reboot_timeout'],
  139. 'launch_timeout' => $row['launch_timeout'],
  140. 'status_timeout' => $row['status_timeout'],
  141. 'placement' => ($row['avail_zone']) ? $row['avail_zone'] : "",
  142. 'i_type' => $row['instance_type'],
  143. 'use_elastic_ips' => ($row['use_elastic_ips'] == 1) ? true : false,
  144. 'use_ebs' => ($row['use_ebs'] == 1) ? true : false,
  145. 'ebs_size' => ($row['ebs_snapid']) ? 0 : $row['ebs_size'],
  146. 'ebs_snapid' => $row['ebs_snapid'],
  147. 'ebs_mount' => ($row['ebs_mount'] == 1) ? true : false,
  148. 'ebs_mountpoint' => $row['ebs_mountpoint']
  149. ));
  150. $scaling_algo_props = array();
  151. $RoleScalingManager = new RoleScalingManager($DBFarmRole);
  152. foreach ($RoleScalingManager->GetRegisteredAlgos() as $Algo)
  153. {
  154. $scaling_algo_props = array_merge($scaling_algo_props, $Algo->GetProperties());
  155. $algo_name = strtolower(str_replace("ScalingAlgo", "", get_class($Algo)));
  156. $scaling_algo_props["scaling.{$algo_name}.enabled"] = $RoleScalingManager->IsAlgoEnabled($algo_name);
  157. }
  158. $role['options']['scaling_algos'] = $scaling_algo_props;
  159. if ($ami_info["alias"] == ROLE_ALIAS::MYSQL)
  160. {
  161. $display['farm_mysql_role'] = $ami_info['ami_id'];
  162. $role['options']['mysql_bundle_every'] = $display["farminfo"]['mysql_rebundle_every'] ? $display["farminfo"]['mysql_rebundle_every'] : 48;
  163. $role['options']['mysql_make_backup_every'] = $display["farminfo"]['mysql_bcp_every'] ? $display["farminfo"]['mysql_bcp_every'] : 180;
  164. $role['options']['mysql_make_backup'] = ($display["farminfo"]['mysql_bcp'] == 1) ? true : false;
  165. $role['options']['mysql_bundle'] = ($display["farminfo"]['mysql_bundle'] == 1) ? true : false;
  166. $role['options']['mysql_data_storage_engine'] = $display["farminfo"]['mysql_data_storage_engine'] ? $display["farminfo"]['mysql_data_storage_engine'] : 'lvm';
  167. $role['options']['mysql_ebs_size'] = $display["farminfo"]['mysql_ebs_size'] ? $display["farminfo"]['mysql_ebs_size'] : 100;
  168. }
  169. array_push($display['roles'], $role);
  170. }
  171. $display["id"] = $req_id;
  172. }
  173. //Defaults
  174. $display["default_scaling_algos"] = array();
  175. foreach (RoleScalingManager::$ScalingAlgos as $Algo)
  176. {
  177. $DataForm = $Algo->GetConfigurationForm();
  178. foreach ($DataForm->ListFields() as $field)
  179. {
  180. if ($field->FieldType != FORM_FIELD_TYPE::MIN_MAX_SLIDER)
  181. $display['default_scaling_algos'][$field->Name] = $field->DefaultValue;
  182. else
  183. {
  184. $s = explode(",", $field->DefaultValue);
  185. $display['default_scaling_algos'][$field->Name.".min"] = $s[0];
  186. $display['default_scaling_algos'][$field->Name.".max"] = $s[1];
  187. }
  188. }
  189. $algo_name = strtolower(str_replace("ScalingAlgo", "", get_class($Algo)));
  190. $display['default_scaling_algos']["scaling.{$algo_name}.enabled"] = 0;
  191. }
  192. $display['default_scaling_algos'] = json_encode($display['default_scaling_algos']);
  193. $display['roles'] = json_encode($display['roles']);
  194. $r = new ReflectionClass("X86_64_TYPE");
  195. $display["64bit_types"] = array_values($r->getConstants());
  196. $r = new ReflectionClass("I386_TYPE");
  197. $display["32bit_types"] = array_values($r->getConstants());
  198. unset($r);
  199. $display["roles_descr"] = $db->GetAll("SELECT ami_id, name, description FROM ami_roles WHERE roletype=? OR (roletype=? and clientid=?)",
  200. array(ROLE_TYPE::SHARED, ROLE_TYPE::CUSTOM, $uid)
  201. );
  202. if ($req_configure == 1)
  203. $display["ami_id"] = $req_ami_id;
  204. /**
  205. * Tabs
  206. */
  207. $display["tabs_list"] = array(
  208. "general" => _("Settings"),
  209. "roles" => _("Roles"),
  210. "rso" => _("Roles startup order")
  211. );
  212. $display["intable_tabs"] = array(
  213. array("id" => "info", "name" => _("About"), "display" => ""),
  214. array("id" => "scaling", "name" => _("Scaling"), "display" => ""),
  215. array("id" => "mysql", "name" => _("MySQL settings"), "display" => "none"),
  216. array("id" => "placement", "name" => _("Placement and type"), "display" => ""),
  217. array("id" => "eips", "name" => _("Elastic IPs"), "display" => ""),
  218. array("id" => "ebs", "name" => _("EBS"), "display" => ""),
  219. array("id" => "dns", "name" => _("DNS"), "display" => ""),
  220. array("id" => "timeouts", "name" => _("Timeouts"), "display" => ""),
  221. array("id" => "scripts", "name" => _("Scripting"), "display" => ""),
  222. array("id" => "params", "name" => _("Parameters"), "display" => "")
  223. );
  224. $display['intable_selected_tab'] = "info";
  225. $display["help"] = _("Tick the checkbox to add the role to your farm.<br> Click on the role name to customize it's behavior");
  226. require("src/append.inc.php");
  227. ?>