PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/scalr-2/tags/scalr-2.0.0/app/www/src/scheduler_task_save.php

http://scalr.googlecode.com/
PHP | 311 lines | 256 code | 36 blank | 19 comment | 39 complexity | 09daa48ec36ca9dfb9e76eefad559f4a MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. // functions adds new task to scheduler task table or edits
  3. // available tasks
  4. if ($_SESSION['uid'] == 0)
  5. {
  6. $err[] = _("Request cannot be executed from admin account");
  7. throw new Exception(_("Request cannot be executed from admin account"));
  8. }
  9. if($req_task == 'create')
  10. {
  11. try
  12. {
  13. if($req_task_type == SCHEDULE_TASK_TYPE::SCRIPT_EXEC)
  14. {
  15. switch($req_target_type)
  16. {
  17. case SCRIPTING_TARGET::FARM:
  18. if($req_farm_target == '')
  19. throw new Exception("Unknown or empty task's target parameters");
  20. $DBFarm = DBFarm::LoadByID($req_farm_target);
  21. $target_id = $DBFarm->ID;
  22. $target_object_clientid = $DBFarm->ClientID;
  23. break;
  24. case SCRIPTING_TARGET::ROLE:
  25. if($req_role_target == '')
  26. throw new Exception("Unknown or empty task's target parameters");
  27. $DBFarmRole = DBFarmRole::LoadByID($req_role_target);
  28. $target_id = $DBFarmRole->ID;
  29. $target_object_clientid = $DBFarmRole->GetFarmObject()->ClientID;
  30. break;
  31. case SCRIPTING_TARGET::INSTANCE:
  32. if($req_server_target == '')
  33. throw new Exception("Unknown or empty task's target parameters");
  34. $DBServer = DBServer::LoadByID($req_server_target);
  35. $target_id = "{$DBServer->farmRoleId}:{$DBServer->index}";
  36. $target_object_clientid = $DBServer->GetFarmObject()->ClientID;
  37. break;
  38. default:
  39. throw new Exception("Unknown or empty task's target parameters");
  40. break;
  41. }
  42. }
  43. elseif( $req_task_type == SCHEDULE_TASK_TYPE::TERMINATE_FARM ||
  44. $req_task_type == SCHEDULE_TASK_TYPE::LAUNCH_FARM)
  45. {
  46. if($req_farm_target == '')
  47. throw new Exception("Unknown or empty task's target parameters");
  48. $DBFarm = DBFarm::LoadByID($req_farm_target);
  49. $target_object_clientid = $DBFarm->ClientID;
  50. $target_id = $DBFarm->ID;
  51. }
  52. else
  53. {
  54. throw new Exception(_("Unknown or empty task type"));
  55. }
  56. if (($_SESSION['uid'] && $target_object_clientid != $_SESSION['uid']) || $target_id == 0)
  57. throw new Exception(_("Specified target not found, please select correct target object"));
  58. }
  59. catch (Exception $e)
  60. {
  61. $err[] = $e->getMessage();
  62. throw $e;
  63. }
  64. }
  65. if(($req_task == 'create') || ($req_task == 'edit'))
  66. {
  67. // form error message for entering data correction
  68. $exception = false;
  69. $Validator = new Validator();
  70. // check entering data
  71. try
  72. {
  73. if(!$Validator->IsAlphaNumeric($req_task_name))
  74. $err[] = _("Task name contains invalid symbols or empty");
  75. if (!$req_startDateTime)
  76. $err[] = _("Start date has incorrect date format or empty");
  77. if (!$req_endDateTime)
  78. $err[] = _("End date has incorrect date format or empty");
  79. if (!$Validator->IsNumeric($req_restart_timeout))
  80. $err[] = _("Restart timeout is not a numeric variable or empty");
  81. if (!$Validator->IsNumeric($req_order_index))
  82. $err[] = _("Priority is not a numeric variable or empty");
  83. if(!$Validator->IsNumeric($req_timeout) && $req_task_type == SCHEDULE_TASK_TYPE::SCRIPT_EXEC)
  84. $err[] = _("Timeout is not a numeric variable or empty");
  85. // check correct date and time
  86. // new task start date can't be older then current date
  87. if($req_task == 'create')
  88. {
  89. if(CompareDateTime($req_startDateTime) < 0)
  90. $err[] = _("Start time must be later or equal to the current date and time");
  91. }
  92. if(CompareDateTime($req_endDateTime) < 1)
  93. $err[] = _("End time must be later than current date and time");
  94. if(CompareDateTime($req_startDateTime,$req_endDateTime) != -1 && $req_restart_timeout != 0)
  95. $err[] = _("End time must be later than start time");
  96. if($err)
  97. throw new Exception();
  98. // add/update database
  99. $req_config = array();
  100. // Rewrite script info
  101. if($req_task_type == SCHEDULE_TASK_TYPE::SCRIPT_EXEC)
  102. {
  103. if(!$req_scriptid || !$req_script_version)
  104. {
  105. $err[] = _("Some important script parameters were not selected");
  106. throw new Exception();
  107. }
  108. // sql script filter for correct ckeck
  109. $script_filter_sql .= " AND (";
  110. // Show shared roles
  111. $script_filter_sql .= " origin='".SCRIPT_ORIGIN_TYPE::SHARED."'";
  112. // Show custom roles
  113. $script_filter_sql .= " OR (origin='".SCRIPT_ORIGIN_TYPE::CUSTOM."'
  114. AND clientid='{$_SESSION['uid']}')";
  115. //Show approved contributed roles
  116. $script_filter_sql .= " OR (origin='".SCRIPT_ORIGIN_TYPE::USER_CONTRIBUTED."'
  117. AND (scripts.approval_state='".APPROVAL_STATE::APPROVED."'
  118. OR clientid='{$_SESSION['uid']}'))";
  119. $script_filter_sql .= ")";
  120. // check script availble for current user
  121. //TODO:
  122. $Info = $db->GetRow("SELECT name FROM scripts WHERE id = ? {$script_filter_sql}", array($req_scriptid));
  123. if(!$Info)
  124. {
  125. $err[] =_("Script {$req_scriptid} not found");
  126. throw new Exception();
  127. }
  128. // check correct revision of user's script
  129. $Info = $db->GetRow("SELECT id FROM script_revisions WHERE scriptid = ? AND revision = ?", array($req_scriptid,$req_script_version));
  130. if(!$Info)
  131. {
  132. $err[] = _("Script {$req_scriptid} revision #{$req_script_version} not found");
  133. throw new Exception();
  134. }
  135. unset($script_filter_sql);
  136. unset($Info);
  137. $req_config['script_id']= (int)$req_scriptid;
  138. $req_config['revision'] = (int)$req_script_version;
  139. $req_config['issync'] = ($req_issync)? 1 : 0;
  140. $req_config['timeout'] = (int)$req_timeout;
  141. // add unique parameters for selected script version
  142. foreach($req_script_args as $key => $value)
  143. {
  144. $req_config[$key] = $value;
  145. // form an array of script args for dinamic ajax update
  146. $return_script_args[$key]['value'] = $value;
  147. }
  148. $return_script_args = json_encode($return_script_args);
  149. }
  150. if($req_task_type == SCHEDULE_TASK_TYPE::TERMINATE_FARM)
  151. {
  152. $req_config['deleteDNS'] = ($req_deleteDNS) ? 1 : 0;
  153. $req_config['keep_elastic_ips'] = (int)$req_keep_elastic_ips;
  154. $req_config['keep_ebs'] = (int)$req_keep_ebs;
  155. }
  156. if ($req_task == 'edit')
  157. {
  158. // update scheduler's record
  159. if(!$req_task_id)
  160. throw new Exception(_("Unknown or empty task's ID"));
  161. $db->Execute("UPDATE scheduler_tasks SET
  162. task_name = ?,
  163. task_type = ?,
  164. start_time_date = ?,
  165. end_time_date = ?,
  166. last_start_time = ?,
  167. restart_every = ?,
  168. task_config = ?,
  169. order_index = ?,
  170. status = ?
  171. WHERE id = ? AND client_id = ?",
  172. array(
  173. $req_task_name,
  174. $req_task_type,
  175. $req_startDateTime, // time
  176. $req_endDateTime,
  177. null,
  178. (int)$req_restart_timeout,
  179. ($req_config) ? serialize($req_config) : null,
  180. (int)$req_order_index,
  181. TASK_STATUS::ACTIVE,
  182. (int)$req_task_id,
  183. $_SESSION['uid']
  184. )
  185. );
  186. $okmsg[] =_("Task {$req_task_name} successfully updated");
  187. }
  188. elseif($req_task == 'create')
  189. {
  190. // add new scheduler's record
  191. $db->Execute("INSERT INTO scheduler_tasks SET
  192. task_name = ?,
  193. task_type = ?,
  194. target_id = ?,
  195. target_type =?,
  196. start_time_date = ?,
  197. end_time_date = ?,
  198. last_start_time = ?,
  199. restart_every = ?,
  200. task_config = ?,
  201. order_index = ?,
  202. status = ?,
  203. client_id = ?",
  204. array($req_task_name,
  205. $req_task_type,
  206. $target_id,
  207. $req_target_type,
  208. $req_startDateTime, // time
  209. $req_endDateTime,
  210. null,
  211. (int)$req_restart_timeout,
  212. ($req_config)? serialize($req_config):null,
  213. $req_order_index,
  214. TASK_STATUS::ACTIVE,
  215. $_SESSION['uid']
  216. )
  217. );
  218. $okmsg[] =_("New task successfully added");
  219. }
  220. }
  221. catch(Exception $e)
  222. {
  223. if($e->getMessage())
  224. $err[] = $e->getMessage();
  225. throw $e;
  226. }
  227. }
  228. else
  229. {
  230. $err[] = _("Unknown task");
  231. throw new Exception(_("Unknown task"));
  232. }
  233. function CompareDateTime($date1,$date2 = null)
  234. {
  235. // compatere 2 dates
  236. // if date1 later then date 2 returns 1;
  237. // if date2 later then date 1 returns -1;
  238. // if date1 equal to date 2 returns 0;
  239. // if date2 is null function compateres date1 with current time (as date2 variable)
  240. $checkingDate1 = strtotime($date1);
  241. if($date2)
  242. $checkingDate2 = strtotime($date2); // get compareing time
  243. else
  244. $checkingDate2 = time(); // get current time
  245. if($checkingDate1 > $checkingDate2) // e.g. selected date later then current date
  246. return 1;
  247. elseif($checkingDate1 < $checkingDate2) // e.g. end date is later then start date or current date is la
  248. return -1;
  249. else
  250. return 0;
  251. }
  252. ?>