Example #1
0
/**
 * Function to check if a string is valid as node_config.
 *
 * @param	string	$s					Parameter
 * @return	bool						True if valid
 */
function checkNodeConfig($s)
{
    if (in_array($s, array('0', '1')) || in_array($s, listNodeConfigTemplates())) {
        return True;
    } else {
        return False;
    }
}
Example #2
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
    if ($p['type'] != 'vpcs') {
        $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', 'list' => $node_images);
            if (isset($p['image'])) {
                $output['data']['options']['image']['value'] = $p['image'];
            } else {
                $output['data']['options']['image']['value'] = end($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' => '');
    }
    // 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 (in_array($p['type'], array('dynamips', 'iol'))) {
        $output['data']['options']['nvram'] = array('name' => $GLOBALS['messages'][70010], 'type' => 'input', 'value' => $p['nvram']);
    }
    // RAM
    if (in_array($p['type'], array('dynamips', 'iol', 'qemu', 'docker'))) {
        $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 (in_array($p['type'], array('qemu', 'docker'))) {
        $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']);
    }
    // First Mac
    if ($p['template'] == "bigip") {
        $output['data']['options']['firstmac'] = array('name' => $GLOBALS['messages'][70021], 'type' => 'input', 'value' => isset($p['firstmac']) ? $p['firstmac'] : "");
    }
    // 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', 'qemu', 'docker', 'vpcs'))) {
        $output['data']['options']['config'] = array('name' => $GLOBALS['messages'][70013], 'type' => 'list', 'value' => '0', 'list' => listNodeConfigTemplates());
        $output['data']['options']['config']['list'][0] = $GLOBALS['messages'][70020];
        // None
        $output['data']['options']['config']['list'][1] = $GLOBALS['messages'][70019];
        // Exported
    }
    // 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;
}