DigitalOceanVM.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use DigitalOcean\DigitalOcean;
  4. use DigitalOcean\Credentials;
  5. /**
  6. * Class DigitalOceanVM
  7. */
  8. class DigitalOceanVM extends AbstractVM implements VirtualMachineInterface
  9. {
  10. /**
  11. *
  12. */
  13. public function __construct($settings)
  14. {
  15. parent::__construct($settings);
  16. $this->connect();
  17. }
  18. /**
  19. * @inheritdoc
  20. */
  21. public function connect()
  22. {
  23. // Set up your credentials.
  24. $credentials = new Credentials($this->vmClientId, $this->apiKey);
  25. // Use the default adapter, CurlHttpAdapter.
  26. $this->connector = new DigitalOcean($credentials);
  27. // Or use BuzzHttpAdapter.
  28. //$this->connector = new DigitalOcean($credentials, new BuzzHttpAdapter());
  29. }
  30. /**
  31. * @return DigitalOcean
  32. */
  33. public function getConnector()
  34. {
  35. return $this->connector;
  36. }
  37. /**
  38. * @param string $type min or max
  39. */
  40. public function resizeTo($type = 'min')
  41. {
  42. try {
  43. $droplets = $this->getConnector()->droplets();
  44. $sizes = $this->getConnector()->sizes();
  45. $availableSizes = $sizes->getAll();
  46. if (isset($availableSizes->status) && $availableSizes->status == 'OK') {
  47. $minSizeIdExists = false;
  48. $maxSizeIdExists = false;
  49. foreach ($availableSizes->sizes as $size) {
  50. if ($size->id == $this->vmMaxSize) {
  51. $maxSizeIdExists = true;
  52. }
  53. if ($size->id == $this->vmMinSizeSize) {
  54. $minSizeIdExists = true;
  55. }
  56. }
  57. if ($maxSizeIdExists && $minSizeIdExists) {
  58. throw new \Exception('Sizes are not well configured');
  59. }
  60. } else {
  61. throw new \Exception('Sizes not available');
  62. }
  63. // Returns all active droplets that are currently running in your account.
  64. //$allActive = $droplets->showAllActive();
  65. $dropletInfo = $droplets->show($this->vmId);
  66. if ($dropletInfo->status == 'OK') {
  67. switch ($type) {
  68. case 'min':
  69. if ($dropletInfo->droplet->size_id == $this->vmMinSize) {
  70. // No resize
  71. $this->addMessage(
  72. 'Nothing to execute. The size was already reduced.'
  73. );
  74. } else {
  75. $this->resize($this->vmMinSize);
  76. }
  77. break;
  78. case 'max':
  79. if ($dropletInfo->droplet->size_id == $this->vmMaxSize) {
  80. // No resize
  81. $this->addMessage(
  82. 'Nothing to execute. The size was already boost.'
  83. );
  84. } else {
  85. $this->resize($this->vmMaxSize);
  86. }
  87. break;
  88. }
  89. } else {
  90. throw new \Exception(" Id ".$this->vmId." doesn't exists.");
  91. }
  92. } catch (Exception $e) {
  93. die($e->getMessage());
  94. }
  95. }
  96. /**
  97. * Turns off / resize / turns on
  98. * @param int $sizeId
  99. */
  100. public function resize($sizeId)
  101. {
  102. $droplets = $this->getConnector()->droplets();
  103. $dropletInfo = $droplets->show($this->vmId);
  104. $powerOff = $droplets->powerOff($this->vmId);
  105. $this->addMessage('Power off droplet #'.$this->vmId);
  106. $this->waitForEvent($powerOff->event_id);
  107. $this->addMessage('Current status: '.$dropletInfo->droplet->status);
  108. $resizeDroplet = $droplets->resize(
  109. $this->vmId,
  110. array('size_id' => intval($sizeId))
  111. );
  112. $this->addMessage('Resize droplet to size id: '.$sizeId);
  113. $this->waitForEvent($resizeDroplet->event_id);
  114. $powerOn = $droplets->powerOn($this->vmId);
  115. $this->waitForEvent($powerOn->event_id);
  116. $this->addMessage('Power on droplet #'.$this->vmId);
  117. }
  118. /**
  119. * Loops until an event answer 100 percentage
  120. * @param int $eventId
  121. */
  122. public function waitForEvent($eventId)
  123. {
  124. $events = $this->getConnector()->events();
  125. $status = false;
  126. while ($status == false) {
  127. $infoStatus = $events->show($eventId);
  128. if ($infoStatus->status == 'OK' && $infoStatus->event->percentage == 100) {
  129. $status = true;
  130. }
  131. }
  132. }
  133. /**
  134. * @inheritdoc
  135. */
  136. public function runCron()
  137. {
  138. $this->resizeToMinLimit();
  139. echo $this->getMessageToString();
  140. }
  141. /**
  142. * @inheritdoc
  143. */
  144. public function resizeToMaxLimit()
  145. {
  146. $this->resizeTo('max');
  147. }
  148. /**
  149. * @inheritdoc
  150. */
  151. public function resizeToMinLimit()
  152. {
  153. $this->resizeTo('min');
  154. }
  155. }