VM.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class VM
  5. */
  6. class VM
  7. {
  8. protected $config;
  9. public $virtualMachine;
  10. public function __construct($config)
  11. {
  12. $this->config = $config;
  13. }
  14. /**
  15. * @return array
  16. */
  17. public function getConfig()
  18. {
  19. return $this->config;
  20. }
  21. /**
  22. * @return bool
  23. */
  24. public function isEnabled()
  25. {
  26. $config = $this->getConfig();
  27. if (!isset($config)) {
  28. return false;
  29. }
  30. if (!is_array($config)) {
  31. return false;
  32. }
  33. if (isset($config['enabled']) && $config['enabled']) {
  34. return true;
  35. }
  36. return false;
  37. }
  38. /**
  39. * @return VirtualMachineInterface
  40. */
  41. public function getVirtualMachine()
  42. {
  43. return $this->virtualMachine;
  44. }
  45. /**
  46. * @param VirtualMachineInterface $virtualMachine
  47. */
  48. public function setVirtualMachine(VirtualMachineInterface $virtualMachine)
  49. {
  50. $this->virtualMachine = $virtualMachine;
  51. }
  52. /**
  53. * @return VirtualMachineInterface
  54. */
  55. public function getVirtualMachineFromConfig()
  56. {
  57. $vmList = $this->config['vms'];
  58. foreach ($vmList as $vm) {
  59. if (isset($vm['enabled']) && $vm['enabled'] == true) {
  60. $className = $vm['name'] . 'VM';
  61. return new $className($vm);
  62. break;
  63. }
  64. }
  65. return false;
  66. }
  67. /**
  68. * Resize the VM to the max size
  69. */
  70. public function resizeToMaxLimit()
  71. {
  72. $virtualMachine = $this->getVirtualMachineFromConfig();
  73. $this->setVirtualMachine($virtualMachine);
  74. $virtualMachine->resizeToMaxLimit();
  75. }
  76. /**
  77. * Resize the VM to the min size
  78. */
  79. public function resizeToMinLimit()
  80. {
  81. $virtualMachine = $this->getVirtualMachineFromConfig();
  82. $this->setVirtualMachine($virtualMachine);
  83. $virtualMachine->resizeToMinLimit();
  84. }
  85. public function runCron()
  86. {
  87. $virtualMachine = $this->getVirtualMachineFromConfig();
  88. $this->setVirtualMachine($virtualMachine);
  89. $virtualMachine->runCron();
  90. }
  91. }