Esempio n. 1
0
/**
 * Function to check if a string is valid as node_image.
 *
 * @param	string	$s					String to check
 * @param	string	$s					Node type
 * @param	string	$s					Node template
 * @return	bool						True if valid
 */
function checkNodeImage($s, $t, $p)
{
    if (in_array($s, listNodeImages($t, $p))) {
        return True;
    } else {
        return False;
    }
}
Esempio n. 2
0
 /**
  * Constructor which creates a node.
  * Parameters:
  * - config
  * - console
  * - cpu
  * - delay
  * - ethernet
  * - icon
  * - idlepc
  * - image
  * - lab_id
  * - left
  * - name
  * - nvram
  * - ram
  * - serial
  * - slots
  * - top
  * - template*
  * - type*
  * - uuid
  * *mandatory
  *
  * @param   Array   $p                  Parameters
  * @param   int     $id                 Node ID
  * @param   int     $tenant             Tenant ID
  * @param   string  $lab_id             Lab ID
  * @return  void
  *
  */
 public function __construct($p, $id, $tenant, $lab_id)
 {
     // Mandatory parameters
     if (!isset($p['type']) || !isset($p['template'])) {
         // Missing mandatory parameters
         error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][40000]);
         throw new Exception('40000');
         return 40000;
     }
     if (!checkNodeType($p['type'])) {
         // Type is not valid
         error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][40001]);
         throw new Exception('40001');
         return 40001;
     }
     if (!isset($GLOBALS['node_templates'][$p['template']])) {
         // Template is not valid or not available
         error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][40002]);
         throw new Exception('40002');
         return 40002;
     }
     // Optional parameters
     if (isset($p['config']) && !checkNodeConfig($p['config'])) {
         // Config is invalid, ignored
         unset($p['config']);
         error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40003]);
     }
     if (isset($p['delay']) && (int) $p['delay'] < 0) {
         // Delay is invalid, ignored
         unset($p['delay']);
         error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40004]);
     }
     if (isset($p['icon']) && !checkNodeIcon($p['icon'])) {
         // Icon is invalid, ignored
         unset($p['icon']);
         error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40005]);
     }
     if (isset($p['image']) && !checkNodeImage($p['image'], $p['type'], $p['template'])) {
         // Image is invalid, ignored
         unset($p['image']);
         error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40006]);
     }
     if (isset($p['left']) && !checkPosition($p['left'])) {
         if (preg_match('/^[0-9]+%$/', $p['left']) && substr($p['left'], 0, -1) >= 0 && substr($p['left'], 0, -1) <= 100) {
             // Converting percentage to absolute using a 800x600 viewport
             $p['left'] = 1024 * substr($p['left'], 0, -1) / 100;
         } else {
             // Left is invalid, ignored
             unset($p['left']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40007]);
         }
     }
     if (isset($p['name']) && !checkNodeName($p['name'])) {
         // Name is invalid, ignored
         unset($p['name']);
         error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40008]);
     }
     if (isset($p['top']) && !checkPosition($p['top'])) {
         if (preg_match('/^[0-9]+%$/', $p['top']) && substr($p['top'], 0, -1) >= 0 && substr($p['top'], 0, -1) <= 100) {
             // Converting percentage to absolute using a 800x600 viewport
             $p['top'] = 768 * substr($p['top'], 0, -1) / 100;
         } else {
             // Top is invalid, ignored
             unset($p['top']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40010]);
         }
     }
     // Specific parameters
     if ($p['type'] == 'iol') {
         if (isset($p['ethernet']) && (int) $p['ethernet'] < 0) {
             // Ethernet interfaces is invalid, default to 4
             $p['ethernet'] = 2;
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40012]);
         }
         if (isset($p['nvram']) && (int) $p['nvram'] <= 0) {
             // NVRAM is invalid, ignored
             unset($p['nvram']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40011]);
         }
         if (isset($p['ram']) && (int) $p['ram'] <= 0) {
             // RAM is invalid, ignored
             unset($p['ram']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40009]);
         }
         if (isset($p['serial']) && (int) $p['serial'] < 0) {
             // Serial interfaces is invalid, default to 4
             $p['serial'] = 2;
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40013]);
         }
     }
     if ($p['type'] == 'dynamips') {
         if (isset($p['idlepc']) && !checkNodeIdlepc($p['idlepc'])) {
             // Idle PC is invalid, ignored
             unset($p['idlepc']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40014]);
         }
         if (isset($p['nvram']) && (int) $p['nvram'] <= 0) {
             // NVRAM is invalid, ignored
             unset($p['nvram']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40011]);
         }
         if (isset($p['ram']) && (int) $p['ram'] <= 0) {
             // RAM is invalid, ignored
             unset($p['ram']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40009]);
         }
     }
     if ($p['type'] == 'qemu') {
         if (isset($p['console']) && !checkNodeConsole($p['console'])) {
             // Configured console is invalid, ignored
             unset($p['console']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40027]);
         }
         if (isset($p['cpu']) && (int) $p['cpu'] <= 0) {
             // Configured CPUs is invalid, ignored
             unset($p['cpu']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40015]);
         }
         if (isset($p['ethernet']) && (int) $p['ethernet'] <= 0) {
             // Ethernet interfaces is invalid, default to 4
             $p['ethernet'] = 4;
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40012]);
         }
         if (isset($p['ram']) && (int) $p['ram'] <= 0) {
             // RAM is invalid, ignored
             unset($p['ram']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40009]);
         }
         if (isset($p['uuid']) && !checkUuid($p['uuid'])) {
             // Configured UUID is invalid, ignored
             unset($p['uuid']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40026]);
         }
     }
     if ($p['type'] == 'docker') {
         if (isset($p['ethernet']) && (int) $p['ethernet'] <= 0) {
             // Ethernet interfaces is invalid, default to 1
             $p['ethernet'] = 1;
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40012]);
         }
         if (isset($p['ram']) && (int) $p['ram'] <= 0) {
             // RAM is invalid, ignored
             unset($p['ram']);
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40009]);
         }
     }
     // If image is not set, choose the latest one available
     if (!isset($p['image'])) {
         if (empty(listNodeImages($p['type'], $p['template']))) {
             error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][40025]);
             $p['image'] = '';
         } else {
             $p['image'] = end(listNodeImages($p['type'], $p['template']));
         }
     }
     // Now building the node
     $this->id = (int) $id;
     $this->lab_id = $lab_id;
     $this->port = 32768 + 128 * (int) $tenant + (int) $id;
     $this->template = $p['template'];
     $this->tenant = (int) $tenant;
     $this->type = $p['type'];
     $this->image = $p['image'];
     if (isset($p['config'])) {
         $this->config = $p['config'];
     }
     // TODO it's a template and must exists or set to saved
     if (isset($p['delay'])) {
         $this->delay = (int) $p['delay'];
     }
     if (isset($p['icon'])) {
         $this->icon = $p['icon'];
     }
     if (isset($p['left'])) {
         $this->left = (int) $p['left'];
     }
     if (isset($p['name'])) {
         $this->name = $p['name'];
     }
     if (isset($p['top'])) {
         $this->top = (int) $p['top'];
     }
     // Building iol node
     if ($p['type'] == 'iol') {
         if (isset($p['nvram'])) {
             $this->nvram = (int) $p['nvram'];
         }
         if (isset($p['ethernet'])) {
             $this->ethernet = (int) $p['ethernet'];
         }
         if (isset($p['ram'])) {
             $this->ram = (int) $p['ram'];
         }
         if (isset($p['serial'])) {
             $this->serial = (int) $p['serial'];
         }
     }
     // Building dynamips node
     if ($p['type'] == 'dynamips') {
         if (isset($p['idlepc'])) {
             $this->idlepc = $p['idlepc'];
         }
         if (isset($p['nvram'])) {
             $this->nvram = (int) $p['nvram'];
         }
         if (isset($p['ram'])) {
             $this->ram = (int) $p['ram'];
         }
         foreach ($p as $key => $module) {
             if (preg_match('/^slot[0-9]+$/', $key)) {
                 // Found a slot
                 $slot_id = substr($key, 4);
                 $this->setSlot($slot_id, $module);
             }
         }
     }
     // Building qemu node
     if ($p['type'] == 'qemu') {
         if (isset($p['console'])) {
             $this->console = $p['console'];
         }
         if (isset($p['cpu'])) {
             $this->cpu = (int) $p['cpu'];
         }
         if (isset($p['ethernet'])) {
             $this->ethernet = (int) $p['ethernet'];
         }
         if (isset($p['uuid'])) {
             $this->uuid = $p['uuid'];
         }
         if (isset($p['ram'])) {
             $this->ram = (int) $p['ram'];
         }
     }
     // Building docker node
     if ($p['type'] == 'docker') {
         if (isset($p['ram'])) {
             $this->ram = (int) $p['ram'];
         }
         if (isset($p['ethernet'])) {
             $this->ethernet = (int) $p['ethernet'];
         }
     }
     // Set interface name
     $this->setEthernets();
     if ($p['type'] == 'iol') {
         $this->setSerials();
     }
 }
Esempio n. 3
0
/**
 * Function to get node template.
 *
 * @param   Array   $p                  Parameters
 * @return  Array                       Node template (JSend data)
 */
function apiGetLabNodeTemplate($p)
{
    // Check mandatory parameters
    if (!isset($p['type']) || !isset($p['template'])) {
        $output['code'] = 400;
        $output['status'] = 'fail';
        $output['message'] = $GLOBALS['messages'][60033];
        return $output;
    }
    // TODO must check lot of parameters
    $output['code'] = 200;
    $output['status'] = 'success';
    $output['message'] = $GLOBALS['messages'][60032];
    $output['data'] = array();
    $output['data']['options'] = array();
    // Name
    $output['data']['description'] = $GLOBALS['node_templates'][$p['template']];
    // Type
    $output['data']['type'] = $p['type'];
    // Image
    $node_images = listNodeImages($p['type'], $p['template']);
    if (empty($node_images)) {
        $output['data']['options']['image'] = array('name' => $GLOBALS['messages'][70002], 'type' => 'list', 'value' => '', 'list' => array());
    } else {
        $output['data']['options']['image'] = array('name' => $GLOBALS['messages'][70002], 'type' => 'list', 'value' => end($node_images), 'list' => $node_images);
    }
    // Node Name/Prefix
    $output['data']['options']['name'] = array('name' => $GLOBALS['messages'][70000], 'type' => 'input', 'value' => $p['name']);
    // Icon
    $output['data']['options']['icon'] = array('name' => $GLOBALS['messages'][70001], 'type' => 'list', 'value' => $p['icon'], 'list' => listNodeIcons());
    // UUID
    if ($p['type'] == 'qemu') {
        $output['data']['options']['uuid'] = array('name' => $GLOBALS['messages'][70008], 'type' => 'input', 'value' => genUuid());
    }
    // CPU
    if ($p['type'] == 'qemu') {
        $output['data']['options']['cpu'] = array('name' => $GLOBALS['messages'][70003], 'type' => 'input', 'value' => $p['cpu']);
    }
    // Idle PC
    if ($p['type'] == 'dynamips') {
        $output['data']['options']['idlepc'] = array('name' => $GLOBALS['messages'][70009], 'type' => 'input', 'value' => $p['idlepc']);
    }
    // NVRAM
    if ($p['type'] == 'dynamips') {
        $output['data']['options']['nvram'] = array('name' => $GLOBALS['messages'][70010], 'type' => 'input', 'value' => $p['nvram']);
    }
    // RAM
    $output['data']['options']['ram'] = array('name' => $GLOBALS['messages'][70011], 'type' => 'input', 'value' => $p['ram']);
    // Slots
    if ($p['type'] == 'dynamips') {
        foreach ($p as $key => $module) {
            if (preg_match('/^slot[0-9]+$/', $key)) {
                // Found a slot
                $slot_id = substr($key, 4);
                $output['data']['options']['slot' . $slot_id] = array('name' => $GLOBALS['messages'][70016] . ' ' . $slot_id, 'type' => 'list', 'value' => $p['slot' . $slot_id], 'list' => $p['modules']);
            }
        }
    }
    // Ethernet
    if ($p['type'] == 'qemu') {
        $output['data']['options']['ethernet'] = array('name' => $GLOBALS['messages'][70012], 'type' => 'input', 'value' => $p['ethernet']);
    }
    if ($p['type'] == 'iol') {
        $output['data']['options']['ethernet'] = array('name' => $GLOBALS['messages'][70018], 'type' => 'input', 'value' => $p['ethernet']);
    }
    // Serial
    if ($p['type'] == 'iol') {
        $output['data']['options']['serial'] = array('name' => $GLOBALS['messages'][70017], 'type' => 'input', 'value' => $p['serial']);
    }
    // Startup configs
    if (in_array($p['type'], array('dynamips', 'iol'))) {
        $output['data']['options']['config'] = array('name' => $GLOBALS['messages'][70013], 'type' => 'list', 'value' => 'Unconfigured', 'list' => array('Saved' => 'Saved', 'Unconfigured' => 'Unconfigured'));
    }
    // Delay
    $output['data']['options']['delay'] = array('name' => $GLOBALS['messages'][70014], 'type' => 'input', 'value' => 0);
    // Console
    if ($p['type'] == 'qemu') {
        $output['data']['options']['console'] = array('name' => $GLOBALS['messages'][70015], 'type' => 'list', 'value' => $p['console'], 'list' => array('telnet' => 'telnet', 'vnc' => 'vnc'));
    }
    // Dynamips options
    if ($p['type'] == 'dynamips') {
        $output['data']['dynamips'] = array();
        if (isset($p['dynamips_options'])) {
            $output['data']['dynamips']['options'] = $p['dynamips_options'];
        }
    }
    // QEMU options
    if ($p['type'] == 'qemu') {
        $output['data']['qemu'] = array();
        if (isset($p['qemu_arch'])) {
            $output['data']['qemu']['arch'] = $p['qemu_arch'];
        }
        if (isset($p['qemu_version'])) {
            $output['data']['qemu']['version'] = $p['qemu_version'];
        }
        if (isset($p['qemu_nic'])) {
            $output['data']['qemu']['nic'] = $p['qemu_nic'];
        }
        if (isset($p['qemu_options'])) {
            $output['data']['qemu']['options'] = $p['qemu_options'];
        }
    }
    return $output;
}