Commands.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\Models;
  3. /**
  4. * Class Commands
  5. * @property $uuid
  6. * @property $name
  7. * @property $parent_uuid
  8. * @property $status
  9. * @property $action
  10. * @package ModulesGarden\ProxmoxAddon\Core\Models
  11. */
  12. class Commands extends ExtendedEloquentModel
  13. {
  14. const STATUS_STOPPED = 'stopped';
  15. const STATUS_RUNNING = 'running';
  16. const STATUS_ERROR = 'error';
  17. const STATUS_SLEEPING = 'sleeping';
  18. const ACTION_START = 'start';
  19. const ACTION_STOP = 'stop';
  20. const ACTION_REBOOT = 'reboot';
  21. const ACTION_NONE = 'none';
  22. /**
  23. * Table name
  24. *
  25. * @var string
  26. */
  27. protected $table = 'Commands';
  28. /**
  29. * @var string
  30. */
  31. protected $primaryKey = 'name';
  32. public $incrementing = false;
  33. /**
  34. * @return bool
  35. */
  36. public function isStopped()
  37. {
  38. return $this->status === self::STATUS_STOPPED;
  39. }
  40. /**
  41. * @return bool
  42. */
  43. public function isRunning()
  44. {
  45. return $this->status === self::STATUS_RUNNING;
  46. }
  47. public function isSleeping()
  48. {
  49. return $this->status === self::STATUS_SLEEPING;
  50. }
  51. /**
  52. * @return $this
  53. */
  54. public function setRunning()
  55. {
  56. $this->setStatus(self::STATUS_RUNNING);
  57. return $this;
  58. }
  59. /**
  60. * @return $this
  61. */
  62. public function setStopped()
  63. {
  64. $this->setStatus(self::STATUS_STOPPED);
  65. return $this;
  66. }
  67. /**
  68. * @return $this
  69. */
  70. public function setSleeping()
  71. {
  72. $this->setStatus(self::STATUS_SLEEPING);
  73. return $this;
  74. }
  75. /**
  76. * @param $status
  77. * @return $this
  78. */
  79. public function setStatus($status)
  80. {
  81. $this->status = $status;
  82. $this->save();
  83. return $this;
  84. }
  85. /**
  86. * @return $this
  87. */
  88. public function start()
  89. {
  90. $this->setAction(self::ACTION_START);
  91. return $this;
  92. }
  93. /**
  94. * @return $this
  95. */
  96. public function stop()
  97. {
  98. $this->setAction(self::ACTION_STOP);
  99. return $this;
  100. }
  101. /**
  102. * @return $this
  103. */
  104. public function reboot()
  105. {
  106. $this->setAction(self::ACTION_REBOOT);
  107. return $this;
  108. }
  109. /**
  110. *
  111. */
  112. public function ping()
  113. {
  114. $this->touch();
  115. $this->save();
  116. }
  117. /**
  118. * @return $this
  119. */
  120. public function clearAction()
  121. {
  122. $this->setAction(self::ACTION_NONE);
  123. return $this;
  124. }
  125. /**
  126. * @param $action
  127. * @return $this
  128. */
  129. public function setAction($action)
  130. {
  131. $this->action = $action;
  132. $this->save();
  133. return $this;
  134. }
  135. /**
  136. * @return bool
  137. */
  138. public function shouldBeStopped()
  139. {
  140. return $this->action === self::ACTION_STOP;
  141. }
  142. public function shouldBeRestared()
  143. {
  144. return $this->action === self::ACTION_REBOOT;
  145. }
  146. }