PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/scalr-2/tags/scalr-2.0.0/app/www/farms_add.php

http://scalr.googlecode.com/
PHP | 327 lines | 258 code | 61 blank | 8 comment | 51 complexity | b9163f0b6e2b6858b240959cba820026 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. set_time_limit(360);
  5. $req_farmid = ($req_farmid) ? $req_farmid : $req_id;
  6. if ($_SESSION['uid'] == 0)
  7. {
  8. if (!$req_farmid)
  9. UI::Redirect("farms_view.php");
  10. else
  11. {
  12. $uid = $db->GetOne("SELECT clientid FROM farms WHERE id='{$req_farmid}'");
  13. }
  14. }
  15. else
  16. {
  17. $uid = $_SESSION['uid'];
  18. }
  19. $Client = Client::Load($uid);
  20. $used_slots = $db->GetOne("SELECT SUM(value) FROM farm_role_settings WHERE name=?
  21. AND farm_roleid IN (SELECT id FROM farm_roles WHERE farmid IN (SELECT id FROM farms WHERE clientid=?))",
  22. array(DBFarmRole::SETTING_SCALING_MAX_INSTANCES, $Client->ID)
  23. );
  24. $client_max_instances = $Client->GetSettingValue(CLIENT_SETTINGS::MAX_INSTANCES_LIMIT);
  25. $i_limit = $client_max_instances ? $client_max_instances : CONFIG::$CLIENT_MAX_INSTANCES;
  26. $client_max_eips = $Client->GetSettingValue(CLIENT_SETTINGS::MAX_EIPS_LIMIT);
  27. $eips_limit = $client_max_eips ? $client_max_eips : CONFIG::$CLIENT_MAX_EIPS;
  28. $avail_slots = $i_limit - $used_slots;
  29. if ($avail_slots <= 5)
  30. $display["warnmsg"] = sprintf(_("You have %s <a href='http://wiki.scalr.net/What_is.../Spare_instances'>spare instances</a> available on your account."), $avail_slots);
  31. if ($req_farmid)
  32. {
  33. $region = $db->GetOne("SELECT region FROM farms WHERE id=?", array($req_farmid));
  34. $display["title"] = _("Edit farm");
  35. }
  36. else
  37. {
  38. $display["title"] = _("Farm builder");
  39. if (!$req_region)
  40. {
  41. $Smarty->assign($display);
  42. $Smarty->display("region_information_step.tpl");
  43. exit();
  44. }
  45. else
  46. $region = $req_region;
  47. }
  48. $display['region'] = $region;
  49. $_SESSION['farm_builder_region'] = $region;
  50. $Client = Client::Load($uid);
  51. $AmazonEC2Client = AmazonEC2::GetInstance(AWSRegions::GetAPIURL($region));
  52. $AmazonEC2Client->SetAuthKeys($Client->AWSPrivateKey, $Client->AWSCertificate);
  53. try
  54. {
  55. // Get Avail zones
  56. $avail_zones_resp = $AmazonEC2Client->DescribeAvailabilityZones();
  57. $display["avail_zones"] = array();
  58. // Random assign zone
  59. array_push($display["avail_zones"], "");
  60. foreach ($avail_zones_resp->availabilityZoneInfo->item as $zone)
  61. {
  62. if (stristr($zone->zoneState,'available'))
  63. array_push($display["avail_zones"], (string)$zone->zoneName);
  64. }
  65. // Get EBS Snapshots list
  66. $response = $AmazonEC2Client->DescribeSnapshots();
  67. $rowz = $response->snapshotSet->item;
  68. if ($rowz instanceof stdClass)
  69. $rowz = array($rowz);
  70. foreach ($rowz as $pk=>$pv)
  71. {
  72. if ($pv->status == 'completed')
  73. $display['snapshots'][(string)$pv->snapshotId] = array(
  74. "snapid" => (string)$pv->snapshotId,
  75. "createdat" => date("M j, Y H:i:s", strtotime((string)$pv->startTime)),
  76. "size" => (string)$pv->volumeSize
  77. );
  78. }
  79. }
  80. catch(Exception $e)
  81. {
  82. $errmsg = $e->getMessage();
  83. }
  84. ksort($display['snapshots']);
  85. //Get List of registered Scaling Algorithms
  86. $display['scaling_algos'] = array();
  87. foreach (RoleScalingManager::$ScalingAlgos as $Algo)
  88. {
  89. $algo_name = strtolower(str_replace("ScalingAlgo", "", get_class($Algo)));
  90. if ($algo_name != 'base')
  91. {
  92. $display['scaling_algos'][$algo_name] = array(
  93. 'based_on' => $Algo->GetAlgoDescription(),
  94. 'settings' => $Algo->GetConfigurationForm($display["farminfo"]["clientid"])
  95. );
  96. }
  97. }
  98. if ($req_id)
  99. {
  100. try
  101. {
  102. $display["farminfo"] = $db->GetRow("SELECT * FROM farms WHERE id=?", array($req_id));
  103. $DBFarm = DBFarm::LoadByID($req_id);
  104. if ($_SESSION['uid'] != 0 && $_SESSION['uid'] != $DBFarm->ClientID)
  105. UI::Redirect("farms_view.php");
  106. if (!$display["farminfo"])
  107. {
  108. $errmsg = _("Farm not found");
  109. UI::Redirect("farms_view.php");
  110. }
  111. }
  112. catch(Exception $e)
  113. {
  114. $errmsg = _("Farm not found");
  115. UI::Redirect("farms_view.php");
  116. }
  117. $roles = $db->GetAll("SELECT * FROM farm_roles WHERE farmid=? ORDER BY launch_index ASC", array($DBFarm->ID));
  118. $display['roles'] = array();
  119. foreach ($roles as &$row)
  120. {
  121. $role_info = $db->GetRow("SELECT * FROM roles WHERE id=?", array($row['role_id']));
  122. if (!$role_info)
  123. continue;
  124. $row["role"] = $role_info["name"];
  125. $scripts = $db->GetAll("SELECT * FROM farm_role_scripts WHERE farm_roleid=? AND event_name NOT LIKE 'CustomEvent%'", array($row['id']));
  126. $scripts_object = new stdClass();
  127. foreach ($scripts as $script)
  128. {
  129. if (substr($script['event_name'], 0, 11) != 'CustomEvent')
  130. {
  131. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"} = new stdClass();
  132. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->config = unserialize($script['params']);
  133. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->target = $script['target'];
  134. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->version = $script['version'];
  135. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->issync = $script['issync'];
  136. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->timeout = $script['timeout'];
  137. $scripts_object->{"{$script['event_name']}_{$script['scriptid']}"}->order_index = $script['order_index'];
  138. }
  139. }
  140. if ($role_info['roletype'] == ROLE_TYPE::SHARED && $role_info['clientid'] != 0)
  141. {
  142. $author_info = $db->GetRow("SELECT fullname FROM clients WHERE id=?", array($role_info['clientid']));
  143. $author = ($author_info['fullname']) ? $author_info['fullname'] : _('Scalr user');
  144. }
  145. else
  146. $author = false;
  147. $DBFarmRole = DBFarmRole::LoadByID($row['id']);
  148. $role_settings = $DBFarmRole->GetAllSettings();
  149. if ($DBFarmRole->GetRoleAlias() == ROLE_ALIAS::MYSQL)
  150. {
  151. if (!$role_settings['mysql.pbw1_hh'])
  152. $role_settings['mysql.pbw1_hh'] = '05';
  153. if (!$role_settings['mysql.pbw2_hh'])
  154. $role_settings['mysql.pbw2_hh'] = '09';
  155. if (!$role_settings['mysql.pbw1_mm'])
  156. $role_settings['mysql.pbw1_mm'] = '00';
  157. if (!$role_settings['mysql.pbw2_mm'])
  158. $role_settings['mysql.pbw2_mm'] = '00';
  159. if (!$role_settings[DBFarmRole::SETTING_MYSQL_BUNDLE_EVERY])
  160. $role_settings[DBFarmRole::SETTING_MYSQL_BUNDLE_EVERY] = 48;
  161. if (!$role_settings[DBFarmRole::SETTING_MYSQL_BCP_EVERY])
  162. $role_settings[DBFarmRole::SETTING_MYSQL_BCP_EVERY] = 180;
  163. if (!$role_settings[DBFarmRole::SETTING_MYSQL_DATA_STORAGE_ENGINE])
  164. $role_settings[DBFarmRole::SETTING_MYSQL_DATA_STORAGE_ENGINE] = MYSQL_STORAGE_ENGINE::EBS;
  165. if (!$role_settings[DBFarmRole::SETTING_MYSQL_EBS_VOLUME_SIZE])
  166. $role_settings[DBFarmRole::SETTING_MYSQL_EBS_VOLUME_SIZE] = 100;
  167. }
  168. $role = array(
  169. 'role_id' => $role_info['id'],
  170. 'name' => $role_info["name"],
  171. 'arch' => $role_info["architecture"],
  172. 'alias' => $role_info["alias"],
  173. 'ami_id' => $role_info["ami_id"],
  174. 'platform' => $role_info['platform'],
  175. 'type' => ROLE_ALIAS::GetTypeByAlias($role_info["alias"]),
  176. 'description' => $role_info["description"],
  177. 'scripts' => $scripts_object,
  178. 'author' => $author,
  179. 'settings' => $role_settings,
  180. 'launch_index' => (int)$row['launch_index'],
  181. 'options' => array(
  182. 'reboot_timeout' => $row['reboot_timeout'],
  183. 'launch_timeout' => $row['launch_timeout'],
  184. 'status_timeout' => $row['status_timeout'],
  185. ));
  186. $scaling_algo_props = array();
  187. $RoleScalingManager = new RoleScalingManager($DBFarmRole);
  188. foreach ($RoleScalingManager->GetRegisteredAlgos() as $Algo)
  189. {
  190. $scaling_algo_props = array_merge($scaling_algo_props, $Algo->GetProperties());
  191. $algo_name = strtolower(str_replace("ScalingAlgo", "", get_class($Algo)));
  192. if ($algo_name == 'base')
  193. continue;
  194. $scaling_algo_props["scaling.{$algo_name}.enabled"] = $RoleScalingManager->IsAlgoEnabled($algo_name);
  195. if ($algo_name == 'time' && $scaling_algo_props["scaling.time.enabled"] == 1)
  196. {
  197. $periods = $db->GetAll("SELECT * FROM farm_role_scaling_times WHERE farm_roleid=?", array($DBFarmRole->ID));
  198. foreach ($periods as $period)
  199. {
  200. $scaling_algo_props[TimeScalingAlgo::PROPERTY_TIME_PERIODS][] = implode(":", array(
  201. $period['start_time'],
  202. $period['end_time'],
  203. $period['days_of_week'],
  204. $period['instances_count']
  205. ));
  206. }
  207. }
  208. }
  209. $role['options']['scaling_algos'] = $scaling_algo_props;
  210. if ($DBFarmRole->GetRoleAlias() == ROLE_ALIAS::MYSQL)
  211. $display['farm_mysql_role'] = $role_info['id'];
  212. array_push($display['roles'], $role);
  213. }
  214. $display["id"] = $req_id;
  215. }
  216. //Defaults
  217. $display["default_scaling_algos"] = array();
  218. foreach (RoleScalingManager::$ScalingAlgos as $Algo)
  219. {
  220. $DataForm = $Algo->GetConfigurationForm();
  221. foreach ($DataForm->ListFields() as $field)
  222. {
  223. if ($field->FieldType != FORM_FIELD_TYPE::MIN_MAX_SLIDER)
  224. $display['default_scaling_algos'][$field->Name] = $field->DefaultValue;
  225. else
  226. {
  227. $s = explode(",", $field->DefaultValue);
  228. $display['default_scaling_algos'][$field->Name.".min"] = $s[0];
  229. $display['default_scaling_algos'][$field->Name.".max"] = $s[1];
  230. }
  231. }
  232. $algo_name = strtolower(str_replace("ScalingAlgo", "", get_class($Algo)));
  233. if ($algo_name == 'base')
  234. continue;
  235. $display['default_scaling_algos']["scaling.{$algo_name}.enabled"] = 0;
  236. }
  237. $display['default_scaling_algos'] = json_encode($display['default_scaling_algos']);
  238. $display['roles'] = json_encode($display['roles']);
  239. $r = new ReflectionClass("X86_64_TYPE");
  240. $display["64bit_types"] = array_values($r->getConstants());
  241. $r = new ReflectionClass("I386_TYPE");
  242. $display["32bit_types"] = array_values($r->getConstants());
  243. unset($r);
  244. $display["roles_descr"] = $db->GetAll("SELECT id, ami_id, name, description FROM roles WHERE roletype=? OR (roletype=? and clientid=?)",
  245. array(ROLE_TYPE::SHARED, ROLE_TYPE::CUSTOM, $uid)
  246. );
  247. if ($req_configure == 1)
  248. {
  249. $display["role_id"] = $req_role_id;
  250. $display["return_to"] = $req_return_to;
  251. }
  252. /**
  253. * Tabs
  254. */
  255. $display["tabs_list"] = array(
  256. "general" => _("Settings"),
  257. "roles" => _("Roles"),
  258. "rso" => _("Roles startup order")
  259. );
  260. $display["help"] = _("Tick the checkbox to add the role to your farm.<br> Click on the role name to customize it's behavior");
  261. $display['current_time_zone'] = @date_default_timezone_get();
  262. $display['current_time'] = date("D h:i a");
  263. require("src/append.inc.php");
  264. ?>