Adding VM support see #8183 need testing.
parent
2f990bb9f3
commit
8ca3dbe70a
@ -0,0 +1,35 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* DO vm_min_size_id/vm_max_size_id sizes |
||||
* |
||||
* ID Name |
||||
* 66 512MB |
||||
* 63 1GB |
||||
* 62 2GB |
||||
* 64 4GB |
||||
* 65 8GB |
||||
* 61 16GB |
||||
* 60 32GB |
||||
* 70 48GB |
||||
* 70 48GB |
||||
* 69 64GB |
||||
* 68 96GB |
||||
*/ |
||||
|
||||
return array( |
||||
'enabled' => true, |
||||
'vms' => array( |
||||
array( |
||||
'enabled' => true, |
||||
'name' => 'DigitalOcean', |
||||
//'host' => 'server-address', |
||||
//'user'=> 'AjZjoXMEg0vm7P8QXEWOC', |
||||
'vm_client_id' => 'client_id', |
||||
'api_key' => '123456', |
||||
'vm_id' => '123456', // The VM ID we want to access |
||||
'vm_min_size_id' => '66', // VM size ID example for 512mb use 66 |
||||
'vm_max_size_id' => '65' // For 1GB use 63 |
||||
) |
||||
) |
||||
); |
||||
@ -0,0 +1,28 @@ |
||||
<?php |
||||
|
||||
require __DIR__ . '/../../vendor/autoload.php'; |
||||
if (file_exists(__DIR__ . '/config.vm.php')) { |
||||
|
||||
require_once dirname(__FILE__) . '/config.php'; |
||||
|
||||
require __DIR__ . '/lib/vm/AbstractVM.php'; |
||||
require __DIR__ . '/lib/vm/VMInterface.php'; |
||||
require __DIR__ . '/lib/vm/DigitalOceanVM.php'; |
||||
require __DIR__ . '/lib/VM.php'; |
||||
|
||||
$config = require __DIR__ . '/config.vm.php'; |
||||
|
||||
$vm = new VM($config); |
||||
|
||||
if ($vm->IsEnabled()) { |
||||
$bbb = new bbb(); |
||||
if ($bbb->plugin_enabled) { |
||||
$activeSessions = $bbb->getActiveSessionsCount(); |
||||
if (empty($activeSessions)) { |
||||
$vm->runCron(); |
||||
} else { |
||||
echo "Can't run cron active sessions found: " . $activeSessions; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,109 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Class VM |
||||
*/ |
||||
class VM |
||||
{ |
||||
protected $config; |
||||
public $virtualMachine; |
||||
|
||||
public function __construct($config) |
||||
{ |
||||
$this->config = $config; |
||||
} |
||||
|
||||
/** |
||||
* @return array |
||||
*/ |
||||
public function getConfig() |
||||
{ |
||||
return $this->config; |
||||
} |
||||
|
||||
/** |
||||
* @return bool |
||||
*/ |
||||
public function isEnabled() |
||||
{ |
||||
$config = $this->getConfig(); |
||||
|
||||
if (!isset($config)) { |
||||
return false; |
||||
} |
||||
|
||||
if (!is_array($config)) { |
||||
return false; |
||||
} |
||||
|
||||
if (isset($config['enabled']) && $config['enabled']) { |
||||
|
||||
return true; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* @return VirtualMachineInterface |
||||
*/ |
||||
public function getVirtualMachine() |
||||
{ |
||||
return $this->virtualMachine; |
||||
} |
||||
|
||||
/** |
||||
* @param VirtualMachineInterface $virtualMachine |
||||
*/ |
||||
public function setVirtualMachine(VirtualMachineInterface $virtualMachine) |
||||
{ |
||||
$this->virtualMachine = $virtualMachine; |
||||
} |
||||
|
||||
/** |
||||
* @return VirtualMachineInterface |
||||
*/ |
||||
public function getVirtualMachineFromConfig() |
||||
{ |
||||
$vmList = $this->config['vms']; |
||||
|
||||
foreach ($vmList as $vm) { |
||||
if (isset($vm['enabled']) && $vm['enabled'] == true) { |
||||
$className = $vm['name'] . 'VM'; |
||||
|
||||
return new $className($vm); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* Resize the VM to the max size |
||||
*/ |
||||
public function resizeToMaxLimit() |
||||
{ |
||||
$virtualMachine = $this->getVirtualMachineFromConfig(); |
||||
$this->setVirtualMachine($virtualMachine); |
||||
$virtualMachine->resizeToMaxLimit(); |
||||
} |
||||
|
||||
/** |
||||
* Resize the VM to the min size |
||||
*/ |
||||
public function resizeToMinLimit() |
||||
{ |
||||
$virtualMachine = $this->getVirtualMachineFromConfig(); |
||||
$this->setVirtualMachine($virtualMachine); |
||||
$virtualMachine->resizeToMinLimit(); |
||||
} |
||||
|
||||
public function runCron() |
||||
{ |
||||
$virtualMachine = $this->getVirtualMachineFromConfig(); |
||||
$this->setVirtualMachine($virtualMachine); |
||||
|
||||
$virtualMachine->runCron(); |
||||
} |
||||
} |
||||
@ -0,0 +1,49 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Class AbstractVM |
||||
*/ |
||||
abstract class AbstractVM |
||||
{ |
||||
public $name; |
||||
public $host; |
||||
public $user; |
||||
public $vmId; |
||||
public $vmMinSize; |
||||
public $vmMaxSize; |
||||
public $apiKey; |
||||
public $vmClientId; |
||||
public $messages = array(); |
||||
protected $connector; |
||||
|
||||
/** |
||||
* @param array $settings |
||||
*/ |
||||
public function __construct($settings) |
||||
{ |
||||
$this->name = $settings['name']; |
||||
$this->host = $settings['host']; |
||||
$this->user = $settings['user']; |
||||
$this->apiKey = $settings['api_key']; |
||||
$this->vmId = $settings['vm_id']; |
||||
$this->vmMinSize = $settings['vm_min_size_id']; |
||||
$this->vmMaxSize = $settings['vm_max_size_id']; |
||||
$this->vmClientId = $settings['vm_client_id']; |
||||
} |
||||
|
||||
/** |
||||
* @param string $message |
||||
*/ |
||||
public function addMessage($message) |
||||
{ |
||||
$this->messages[] = $message; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getMessageToString() |
||||
{ |
||||
return implode(PHP_EOL, $this->messages); |
||||
} |
||||
} |
||||
@ -0,0 +1,43 @@ |
||||
<?php |
||||
|
||||
use DigitalOcean\DigitalOcean; |
||||
use DigitalOcean\Credentials; |
||||
|
||||
/** |
||||
* Class DigitalOceanWrapper |
||||
*/ |
||||
class AmazonVM extends AbstractVM implements VirtualMachineInterface |
||||
{ |
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function connect() |
||||
{ |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function runCron() |
||||
{ |
||||
|
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function resizeToMaxLimit() |
||||
{ |
||||
|
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function resizeToMinLimit() |
||||
{ |
||||
|
||||
} |
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,179 @@ |
||||
<?php |
||||
|
||||
use DigitalOcean\DigitalOcean; |
||||
use DigitalOcean\Credentials; |
||||
|
||||
/** |
||||
* Class DigitalOceanWrapper |
||||
*/ |
||||
class DigitalOceanVM extends AbstractVM implements VirtualMachineInterface |
||||
{ |
||||
/** |
||||
* |
||||
*/ |
||||
public function __construct($settings) |
||||
{ |
||||
parent::__construct($settings); |
||||
$this->connect(); |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function connect() |
||||
{ |
||||
// Set up your credentials. |
||||
$credentials = new Credentials($this->vmClientId, $this->apiKey); |
||||
// Use the default adapter, CurlHttpAdapter. |
||||
$this->connector = new DigitalOcean($credentials); |
||||
|
||||
// Or use BuzzHttpAdapter. |
||||
//$this->connector = new DigitalOcean($credentials, new BuzzHttpAdapter()); |
||||
} |
||||
|
||||
/** |
||||
* @return DigitalOcean |
||||
*/ |
||||
public function getConnector() |
||||
{ |
||||
return $this->connector; |
||||
} |
||||
|
||||
/** |
||||
* @param string $type min or max |
||||
*/ |
||||
public function resizeTo($type = 'min') |
||||
{ |
||||
try { |
||||
$droplets = $this->getConnector()->droplets(); |
||||
$sizes = $this->getConnector()->sizes(); |
||||
$availableSizes = $sizes->getAll(); |
||||
|
||||
if (isset($availableSizes->status) && $availableSizes->status == 'OK') { |
||||
|
||||
$minSizeIdExists = false; |
||||
$maxSizeIdExists = false; |
||||
|
||||
foreach ($availableSizes->sizes as $size) { |
||||
if ($size->id == $this->vmMaxSize) { |
||||
$maxSizeIdExists = true; |
||||
} |
||||
if ($size->id == $this->vmMinSizeSize) { |
||||
$minSizeIdExists = true; |
||||
} |
||||
} |
||||
if ($maxSizeIdExists && $minSizeIdExists) { |
||||
throw new \Exception('Sizes are not well configured'); |
||||
} |
||||
} else { |
||||
throw new \Exception('Sizes not available'); |
||||
} |
||||
|
||||
// Returns all active droplets that are currently running in your account. |
||||
//$allActive = $droplets->showAllActive(); |
||||
|
||||
$dropletInfo = $droplets->show($this->vmId); |
||||
|
||||
if ($dropletInfo->status == 'OK') { |
||||
switch ($type) { |
||||
case 'min': |
||||
if ($dropletInfo->droplet->size_id == $this->vmMinSize) { |
||||
// No resize |
||||
$this->addMessage( |
||||
'Nothing to execute. The size was already reduced.' |
||||
); |
||||
} else { |
||||
$this->resize($this->vmMinSize); |
||||
} |
||||
|
||||
break; |
||||
case 'max': |
||||
if ($dropletInfo->droplet->size_id == $this->vmMaxSize) { |
||||
// No resize |
||||
$this->addMessage( |
||||
'Nothing to execute. The size was already boost.' |
||||
); |
||||
} else { |
||||
$this->resize($this->vmMaxSize); |
||||
} |
||||
break; |
||||
} |
||||
} else { |
||||
throw new \Exception(" Id " . $this->vmId . " doesn't exists."); |
||||
} |
||||
} catch (Exception $e) { |
||||
die($e->getMessage()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Turns off / resize / turns on |
||||
* @param int $sizeId |
||||
*/ |
||||
public function resize($sizeId) |
||||
{ |
||||
$droplets = $this->getConnector()->droplets(); |
||||
$dropletInfo = $droplets->show($this->vmId); |
||||
|
||||
$powerOff = $droplets->powerOff($this->vmId); |
||||
|
||||
$this->addMessage('Power off droplet #' . $this->vmId); |
||||
|
||||
$this->waitForEvent($powerOff->event_id); |
||||
|
||||
$this->addMessage('Current status: ' . $dropletInfo->droplet->status); |
||||
|
||||
$resizeDroplet = $droplets->resize( |
||||
$this->vmId, |
||||
array('size_id' => intval($sizeId)) |
||||
); |
||||
$this->addMessage('Resize droplet to size id: ' . $sizeId); |
||||
$this->waitForEvent($resizeDroplet->event_id); |
||||
|
||||
$powerOn = $droplets->powerOn($this->vmId); |
||||
$this->waitForEvent($powerOn->event_id); |
||||
$this->addMessage('Power on droplet #' . $this->vmId); |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Loops until an event answer 100 percentage |
||||
* @param int $eventId |
||||
*/ |
||||
public function waitForEvent($eventId) |
||||
{ |
||||
$events = $this->getConnector()->events(); |
||||
$status = false; |
||||
while ($status == false) { |
||||
$infoStatus = $events->show($eventId); |
||||
if ($infoStatus->status == 'OK' && $infoStatus->event->percentage == 100) { |
||||
$status = true; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function runCron() |
||||
{ |
||||
$this->resizeToMinLimit(); |
||||
echo $this->getMessageToString(); |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function resizeToMaxLimit() |
||||
{ |
||||
$this->resizeTo('max'); |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function resizeToMinLimit() |
||||
{ |
||||
$this->resizeTo('min'); |
||||
} |
||||
} |
||||
@ -0,0 +1,28 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Interface VirtualMachineInterface |
||||
*/ |
||||
interface VirtualMachineInterface |
||||
{ |
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
function connect(); |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
function runCron(); |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
function resizeToMaxLimit(); |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
function resizeToMinLimit(); |
||||
} |
||||
|
||||
@ -1,4 +1,5 @@ |
||||
<?php |
||||
|
||||
require_once dirname(__FILE__).'/config.php'; |
||||
$plugin_info = BBBPlugin::create()->get_info(); |
||||
|
||||
$plugin_info = BBBPlugin::create()->get_info(); |
||||
|
||||
Loading…
Reference in new issue