State.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * State of both Services (HDFS, MapReduce etc.) and
  21. * ServiceComponents (NameNode, JobTracker etc.).
  22. */
  23. class State {
  24. const UNKNOWN = 0;
  25. const UNINSTALLING = 1;
  26. const UNINSTALLED = 2;
  27. const INSTALLING = 3;
  28. const INSTALLED = 4;
  29. const STARTING = 5;
  30. const STARTED = 6;
  31. const STOPPING = 7;
  32. const STOPPED = 8;
  33. const FAILED = 9;
  34. public static $STATE =
  35. array (
  36. "UNKNOWN",
  37. "UNINSTALLING",
  38. "UNINSTALLED",
  39. "INSTALLING",
  40. "INSTALLED",
  41. "STARTING",
  42. "STARTED",
  43. "STOPPING",
  44. "STOPPED",
  45. "FAILED"
  46. );
  47. public static $DESIRED_STATE =
  48. array (
  49. self::UNINSTALLING => "uninstalled",
  50. self::UNINSTALLED => "uninstalled",
  51. self::INSTALLING => "installed_and_configured",
  52. self::INSTALLED => "installed_and_configured",
  53. self::STARTING => "running",
  54. self::STARTED => "running",
  55. self::STOPPING => "stopped",
  56. self::STOPPED => "stopped"
  57. );
  58. public static function getStateFromString($stateStr) {
  59. $found = array_keys(self::$STATE, $stateStr);
  60. if (is_array($found) && count($found) == 1) {
  61. return $found[0];
  62. }
  63. return FALSE;
  64. }
  65. /**
  66. * Converts INSTALLED to INSTALLING and likewise.
  67. * No-op for failed or unknown states
  68. * @param int $state
  69. */
  70. public static function convertStateToProgressingState($state) {
  71. $retState = self::UNKNOWN;
  72. switch ($state) {
  73. case State::UNKNOWN:
  74. case State::UNINSTALLING:
  75. case State::INSTALLING:
  76. case State::STARTING:
  77. case State::STOPPING:
  78. case State::FAILED:
  79. $retState = $state;
  80. break;
  81. case State::UNINSTALLED:
  82. $retState = self::UNINSTALLING;
  83. break;
  84. case State::INSTALLED:
  85. $retState = self::INSTALLING;
  86. break;
  87. case State::STARTED:
  88. $retState = self::STARTING;
  89. break;
  90. case State::STOPPED:
  91. $retState = self::STOPPING;
  92. break;
  93. default:
  94. $retState = self::UNKNOWN;
  95. break;
  96. }
  97. return $retState;
  98. }
  99. }
  100. class TransactionProgress {
  101. const PENDING = 0;
  102. const IN_PROGRESS = 1;
  103. const COMPLETED = 2;
  104. const FAILED = 3;
  105. public static $PROGRESS =
  106. array (
  107. "PENDING",
  108. "IN_PROGRESS",
  109. "COMPLETED",
  110. "FAILED"
  111. );
  112. public static function getProgressFromString($progressStr) {
  113. $found = array_keys(self::$PROGRESS, $progressStr);
  114. if (is_array($found) && count($found) == 1) {
  115. return $found[0];
  116. }
  117. return FALSE;
  118. }
  119. }
  120. function getTransactionProgressFromState($state) {
  121. $txnState = TransactionProgress::PENDING;
  122. switch ($state) {
  123. case State::UNKNOWN:
  124. $txnState = TransactionProgress::PENDING;
  125. break;
  126. case State::UNINSTALLING:
  127. case State::INSTALLING:
  128. case State::STARTING:
  129. case State::STOPPING:
  130. $txnState = TransactionProgress::IN_PROGRESS;
  131. break;
  132. case State::UNINSTALLED:
  133. case State::INSTALLED:
  134. case State::STARTED:
  135. case State::STOPPED:
  136. $txnState = TransactionProgress::COMPLETED;
  137. break;
  138. case State::FAILED:
  139. $txnState = TransactionProgress::FAILED;
  140. break;
  141. default:
  142. $txnState = TransactionProgress::PENDING;
  143. }
  144. return $txnState;
  145. }
  146. function getActionDescription($name, $action, $state) {
  147. $stateInfo = "";
  148. if ($state == "PENDING") {
  149. $stateInfo = "is $state";
  150. } else if ($state == "IN_PROGRESS") {
  151. $stateInfo = "is IN PROGRESS";
  152. } else if ($state == "COMPLETED" || $state =="FAILED") {
  153. $stateInfo = "has $state";
  154. }
  155. $description = $name . " " . strtolower($action) . " " . strtolower($stateInfo);
  156. return $description;
  157. }
  158. ?>