ソースを参照

Adding VM support see #8183 need testing.

Julio Montoya 10 年 前
コミット
8ca3dbe70a

+ 1 - 1
plugin/bbb/config.php

@@ -3,7 +3,7 @@
 
 /* bbb parameters that will be registered in the course settings */
 
-require_once '../../main/inc/global.inc.php';
+require_once __DIR__ . '/../../main/inc/global.inc.php';
 require_once api_get_path(LIBRARY_PATH).'plugin.class.php';
 
 require_once 'lib/bbb.lib.php';

+ 35 - 0
plugin/bbb/config.vm.dist.php

@@ -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
+        )
+    )
+);

+ 28 - 0
plugin/bbb/cron.php

@@ -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;
+            }
+        }
+    }
+}

+ 109 - 0
plugin/bbb/lib/VM.php

@@ -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();
+    }
+}

+ 1 - 1
plugin/bbb/lib/bbb_api.php

@@ -164,7 +164,7 @@ class BigBlueButtonBN {
 		*/
 		$xml = $this->_processXmlResponse($this->getCreateMeetingURL($creationParams));
 
-		if($xml) {
+        if ($xml) {
 			if($xml->meetingID)
 				return array(
 					'returncode' => $xml->returncode,

+ 49 - 0
plugin/bbb/lib/vm/AbstractVM.php

@@ -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);
+    }
+}

+ 43 - 0
plugin/bbb/lib/vm/AmazonVM.php

@@ -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()
+    {
+
+    }
+
+
+}

+ 179 - 0
plugin/bbb/lib/vm/DigitalOceanVM.php

@@ -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');
+    }
+}

+ 28 - 0
plugin/bbb/lib/vm/VMInterface.php

@@ -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();
+}
+

+ 22 - 1
plugin/bbb/listing.php

@@ -64,7 +64,28 @@ if ($teacher) {
             break;
         case 'end':
             $bbb->end_meeting($_GET['id']);
-            $message = Display::return_message(get_lang('MeetingClosed').'<br />'.get_lang('MeetingClosedComment'), 'success', false);
+            $message = Display::return_message(
+                get_lang('MeetingClosed') . '<br />' . get_lang(
+                    'MeetingClosedComment'
+                ),
+                'success',
+                false
+            );
+
+            if (file_exists(__DIR__ . '/config.vm.php')) {
+                require __DIR__ . '/../../vendor/autoload.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);
+                $vm->resizeToMinLimit();
+            }
+
             break;
         case 'publish':
             //$result = $bbb->publish_meeting($_GET['id']);

+ 2 - 1
plugin/bbb/plugin.php

@@ -1,4 +1,5 @@
 <?php
 
 require_once dirname(__FILE__).'/config.php';
-$plugin_info = BBBPlugin::create()->get_info();
+
+$plugin_info = BBBPlugin::create()->get_info();

+ 34 - 4
plugin/bbb/start.php

@@ -1,16 +1,23 @@
 <?php
+
 /**
- * This script initiates a videoconference session, calling the BigBlueButton API
+ * This script initiates a video conference session, calling the BigBlueButton API
  * @package chamilo.plugin.bigbluebutton
  */
-/**
- * Initialization
- */
+
+require __DIR__ . '/../../vendor/autoload.php';
 
 $course_plugin = 'bbb'; //needed in order to load the plugin lang variables
 require_once dirname(__FILE__).'/config.php';
+
 $tool_name = get_lang('Videoconference');
 $tpl = new Template($tool_name);
+
+$vmIsEnabled = false;
+$host = null;
+$salt = null;
+
+
 $bbb = new bbb();
 
 if ($bbb->plugin_enabled) {
@@ -18,6 +25,29 @@ if ($bbb->plugin_enabled) {
 
         if (isset($_GET['launch']) && $_GET['launch'] == 1) {
 
+            if (file_exists(__DIR__ . '/config.vm.php')) {
+                $config = require __DIR__ . '/config.vm.php';
+                $vmIsEnabled = true;
+                $host = null;
+                $salt = null;
+
+                require __DIR__ . '/lib/vm/AbstractVM.php';
+                require __DIR__ . '/lib/vm/VMInterface.php';
+                require __DIR__ . '/lib/vm/DigitalOceanVM.php';
+                require __DIR__ . '/lib/VM.php';
+
+                $vm = new VM($config);
+
+                if ($vm->IsEnabled()) {
+                    try {
+                        $vm->resizeToMaxLimit();
+                    } catch (\Exception $e) {
+                        echo $e->getMessage();
+                        exit;
+                    }
+                }
+            }
+
             $meeting_params = array();
             $meeting_params['meeting_name'] = api_get_course_id().'-'.api_get_session_id();