Esempio n. 1
0
 function validateConfig()
 {
     if (empty($this->config['src'])) {
         printf("ERR: not specifies source files directory\n");
         return false;
     }
     if (empty($this->config['output'])) {
         printf("ERR: not output filename or output directory\n");
         return false;
     }
     if (empty($this->config['sign'])) {
         $this->config['sign'] = self::ENCRYPT_XXTEA_DEFAULT_SIGN;
     }
     if (!$this->config['quiet']) {
         dumpConfig($this->config, $this->options);
     }
     // check src path
     $srcpath = realpath($this->config['src']);
     if (!is_dir($srcpath)) {
         printf("ERR: invalid src dir %s\n", $srcpath);
         return false;
     }
     $this->config['srcpath'] = $srcpath;
     $this->config['srcpathLength'] = strlen($srcpath) + 1;
     // check output path
     @mkdir($this->config['output'], 0755, true);
     $this->config['output'] = realpath($this->config['output']);
     if (empty($this->config['output']) || !is_dir($this->config['output'])) {
         printf("ERR: invalid output dir %s\n", $this->config['output']);
         return false;
     }
     $this->validated = true;
     return true;
 }
Esempio n. 2
0
File: cli.php Progetto: qyqx/unetlab
/**
 * Function to prepare a node before starging it
 *
 * @param   Node    $n                  The Node
 * @param   Int     $id                 Node ID
 * @param   Int     $t                  Tenant ID
 * @param   Array   $nets               Array of networks
 * @return  int                         0 Means ok
 */
function prepareNode($n, $id, $t, $nets)
{
    $user = '******' . $t;
    // Get UID from username
    $cmd = 'id -u ' . $user . ' 2>&1';
    exec($cmd, $o, $rc);
    $uid = $o[0];
    // Creating TAP interfaces
    foreach ($n->getEthernets() as $interface_id => $interface) {
        $tap_name = 'vunl' . $t . '_' . $id . '_' . $interface_id;
        if (isset($nets[$interface->getNetworkId()]) && $nets[$interface->getNetworkId()]->isCloud()) {
            // Network is a Cloud
            $net_name = $nets[$interface->getNetworkId()]->getNType();
        } else {
            $net_name = 'vnet' . $t . '_' . $interface->getNetworkId();
        }
        // Remove interface
        $rc = delTap($tap_name);
        if ($rc !== 0) {
            // Failed to delete TAP interface
            return $rc;
        }
        // Add interface
        $rc = addTap($tap_name, $user);
        if ($rc !== 0) {
            // Failed to add TAP interface
            return $rc;
        }
        if ($interface->getNetworkId() !== 0) {
            // Connect interface to network
            $rc = connectInterface($net_name, $tap_name);
            if ($rc !== 0) {
                // Failed to connect interface to network
                return $rc;
            }
        }
    }
    // Preparing image
    // Dropping privileges
    posix_setsid();
    posix_setgid(32768);
    if ($n->getNType() == 'iol' && !posix_setuid($uid)) {
        error_log('ERROR: ' . $GLOBALS['messages'][80036]);
        return 80036;
    }
    if (!is_file($n->getRunningPath() . '/.prepared') && !is_file($n->getRunningPath() . '/.lock')) {
        // Node is not prepared/locked
        if (!is_dir($n->getRunningPath()) && !mkdir($n->getRunningPath(), 0775, True)) {
            // Cannot create running directory
            error_log('ERROR: ' . $GLOBALS['messages'][80037]);
            return 80037;
        }
        if ($n->getConfig() == 'Saved' && $n->getConfigData() != '') {
            // Node should use saved startup-config
            if (!dumpConfig($n->getConfigData(), $n->getRunningPath() . '/startup-config')) {
                // Cannot dump config to startup-config file
                error_log('WARNING: ' . $GLOBALS['messages'][80067]);
            }
        }
        switch ($n->getNType()) {
            default:
                // Invalid node_type
                error_log('ERROR: ' . $GLOBALS['messages'][80038]);
                return 80038;
            case 'iol':
                // Check license
                if (!is_file('/opt/unetlab/addons/iol/bin/iourc')) {
                    // IOL license not found
                    error_log('ERROR: ' . $GLOBALS['messages'][80039]);
                    return 80039;
                }
                if (!file_exists($n->getRunningPath() . '/iourc') && !symlink('/opt/unetlab/addons/iol/bin/iourc', $n->getRunningPath() . '/iourc')) {
                    // Cannot link IOL license
                    error_log('ERROR: ' . $GLOBALS['messages'][80040]);
                    return 80040;
                }
                break;
            case 'docker':
                if (!is_file('/usr/bin/docker')) {
                    // docker.io is not installed
                    error_log('ERROR: ' . $GLOBALS['messages'][80082]);
                    return 80082;
                }
                $cmd = 'docker inspect --format="{{ .State.Running }}" ' . $n->getUuid();
                exec($cmd, $o, $rc);
                if ($rc != 0) {
                    // Must create docker.io container
                    $cmd = 'docker create -ti --net=none --name=' . $n->getUuid() . ' -h ' . $n->getName() . ' ' . $n->getImage();
                    exec($cmd, $o, $rc);
                    if ($rc != 0) {
                        // Failed to create container
                        error_log('ERROR: ' . $GLOBALS['messages'][80083]);
                        return 80083;
                    }
                }
                break;
            case 'dynamips':
                // Nothing to do
                break;
            case 'qemu':
                $image = '/opt/unetlab/addons/qemu/' . $n->getImage();
                if (!touch($n->getRunningPath() . '/.lock')) {
                    // Cannot lock directory
                    error_log('ERROR: ' . $GLOBALS['messages'][80041]);
                    return 80041;
                }
                // Copy files from template
                foreach (scandir($image) as $filename) {
                    if (preg_match('/^[a-zA-Z0-9]+.qcow2$/', $filename)) {
                        // TODO should check if file exists
                        $cmd = '/opt/qemu/bin/qemu-img create -b "' . $image . '/' . $filename . '" -f qcow2 "' . $n->getRunningPath() . '/' . $filename . '"';
                        exec($cmd, $o, $rc);
                        if ($rc !== 0) {
                            // Cannot make linked clone
                            error_log('ERROR: ' . $GLOBALS['messages'][80045]);
                            error_log(implode("\n", $o));
                            return 80045;
                        }
                    }
                }
                if (!unlink($n->getRunningPath() . '/.lock')) {
                    // Cannot unlock directory
                    error_log('ERROR: ' . $GLOBALS['messages'][80042]);
                    return 80042;
                }
                break;
        }
        // Mark the node as prepared
        if (!touch($n->getRunningPath() . '/.prepared')) {
            // Cannot write on directory
            error_log('ERROR: ' . $GLOBALS['messages'][80044]);
            return 80044;
        }
    }
    return 0;
}
 function validateConfig()
 {
     // check template
     $templatePath = rtrim($this->config['template'], "/\\") . DS;
     if (!is_dir($templatePath)) {
         printf("ERROR: invalid template path \"%s\"\n", $templatePath);
         return false;
     }
     if (!file_exists($templatePath . 'TEMPLATE_INFO.json')) {
         printf("ERROR: not found TEMPLATE_INFO.json in template path \"%s\"\n", $templatePath);
         return false;
     }
     $info = file_get_contents($templatePath . 'TEMPLATE_INFO.json');
     $info = json_decode($info, true);
     if (!is_array($info) || empty($info['name'])) {
         printf("ERROR: invalid TEMPLATE_INFO.json in template path \"%s\"\n", $templatePath);
         return false;
     }
     $this->config['template'] = $templatePath;
     // check package name
     $packageName = str_replace('-', '_', strtolower($this->config['package']));
     $parts = explode('.', $packageName);
     $packageName = array();
     for ($i = 0; $i < count($parts); $i++) {
         $parts[$i] = preg_replace('/[^a-z0-9_]/', '', $parts[$i]);
         if (!empty($parts[$i])) {
             $packageName[] = $parts[$i];
         }
     }
     if (count($packageName) < 2) {
         printf("ERROR: invalid package name \"%s\"\n", implode('.', $packageName));
         return false;
     }
     $lastname = $packageName[count($packageName) - 1];
     array_pop($packageName);
     $packageName = implode('.', $packageName);
     $this->config['package'] = $packageName . '.' . $lastname;
     $this->config['packageLastName'] = $lastname;
     $this->config['packageFullName'] = $packageName . '.' . $lastname;
     $this->config['packageModuleName'] = $packageName;
     // check output path
     if (empty($this->config['output'])) {
         $this->config['output'] = rtrim(getcwd(), '/\\') . DS . $lastname . DS;
     } else {
         $this->config['output'] = rtrim($this->config['output'], '/\\') . DS;
     }
     if (!$this->config['force'] && (is_dir($this->config['output']) || file_exists($this->config['output']))) {
         printf("ERROR: project path \"%s\" exists\n", $this->config['output']);
         return false;
     }
     // check screen orientation
     $orientation = strtolower($this->config['orientation']);
     if ($orientation != 'landscape' && $orientation != 'portrait') {
         printf("ERROR: invalid screen orientation \"%s\"\n", $orientation);
         return false;
     }
     $this->config['orientation'] = $orientation;
     // check more configs
     if ($this->config['noproj'] && $this->config['onlyproj']) {
         print "ERROR: noproj and onlyproj cannot enable at the same time\n";
         return false;
     }
     if (!$this->config['quiet']) {
         dumpConfig($this->config, $this->options);
     }
     $this->validated = true;
     return true;
 }
 function validateConfig()
 {
     if (empty($this->config['src'])) {
         printf("ERR: not specifies source files directory\n");
         return false;
     }
     if (empty($this->config['output'])) {
         printf("ERR: not output filename or output directory\n");
         return false;
     }
     if (!empty($this->config['prefix'])) {
         $this->config['prefix'] = $this->config['prefix'] . '.';
     } else {
         $this->config['prefix'] = '';
     }
     if (!empty($this->config['excludes'])) {
         $excludes = explode(',', $this->config['excludes']);
         array_walk($excludes, function ($value) {
             return trim($value);
         });
         $this->config['excludes'] = array_filter($excludes, function ($value) {
             return !empty($value);
         });
     } else {
         $this->config['excludes'] = array();
     }
     if ($this->config['compile'] != self::COMPILE_ZIP && $this->config['compile'] != self::COMPILE_FILES && $this->config['compile'] != self::COMPILE_C && $this->config['compile'] != self::COMPILE_CSRC) {
         printf("ERR: invalid compile mode %s\n", $this->config['compile']);
         return false;
     }
     if (!empty($this->config['encrypt']) && $this->config['encrypt'] != self::ENCRYPT_XXTEA_ZIP && $this->config['encrypt'] != self::ENCRYPT_XXTEA_CHUNK) {
         printf("ERR: invalid encrypt mode %s\n", $this->config['encrypt']);
         return false;
     }
     if (!empty($this->config['encrypt']) && empty($this->config['key'])) {
         print "ERR: not set encrypt key\n";
         return false;
     }
     if ($this->config['encrypt'] == self::ENCRYPT_XXTEA_ZIP || $this->config['encrypt'] == self::ENCRYPT_XXTEA_CHUNK) {
         if (($this->config['compile'] == self::COMPILE_FILES || $this->config['compile'] == self::COMPILE_C) && $this->config['encrypt'] != self::ENCRYPT_XXTEA_CHUNK) {
             print "ERR: compile mode \"files\" or \"c\" must use encrypt mode \"xxtea_chunk\"\n";
             return false;
         }
         if (empty($this->config['sign'])) {
             $this->config['sign'] = self::ENCRYPT_XXTEA_DEFAULT_SIGN;
         }
     }
     if (!empty($this->config['encrypt']) && empty($this->config['sign'])) {
         print "ERR: not set encrypt sign\n";
         return false;
     }
     if (empty($this->config['extname'])) {
         print "ERR: not specifies encrypted file extension name\n";
         return false;
     }
     if (!$this->config['quiet']) {
         dumpConfig($this->config, $this->options);
     }
     // check src path
     $srcpath = realpath($this->config['src']);
     if (!is_dir($srcpath)) {
         printf("ERR: invalid src dir %s\n", $srcpath);
         return false;
     }
     $this->config['srcpath'] = $srcpath;
     $this->config['srcpathLength'] = strlen($srcpath) + 1;
     if ($this->config['compile'] == self::COMPILE_ZIP) {
         if (is_dir($this->config['output'])) {
             printf("ERR: output file is dir %s\n", $this->config['output']);
             return false;
         }
     } else {
         if ($this->config['compile'] == self::COMPILE_FILES) {
             @mkdir($this->config['output'], 0777, true);
             $this->config['output'] = realpath($this->config['output']);
             if (empty($this->config['output']) || !is_dir($this->config['output'])) {
                 printf("ERR: invalid output dir %s\n", $this->config['output']);
                 return false;
             }
         }
     }
     $this->validated = true;
     return true;
 }
 function validateConfig()
 {
     // check template
     $templatePath = rtrim($this->config['template'], "/\\");
     $templatePath = $templatePath . DS;
     if (!is_dir($templatePath)) {
         printf("ERROR: invalid template path \"%s\"\n", $templatePath);
         return false;
     }
     $this->config['template'] = $templatePath;
     // check package name
     $packageName = str_replace('-', '_', strtolower($this->config['package']));
     $parts = explode('.', $packageName);
     $packageName = array();
     for ($i = 0; $i < count($parts); $i++) {
         $parts[$i] = preg_replace('/[^a-z0-9_]/', '', $parts[$i]);
         if (!empty($parts[$i])) {
             $packageName[] = $parts[$i];
         }
     }
     if (count($packageName) < 2) {
         printf("ERROR: invalid package name \"%s\"\n", implode('.', $packageName));
         return false;
     }
     $lastname = $packageName[count($packageName) - 1];
     array_pop($packageName);
     $packageName = implode('.', $packageName);
     $this->config['package'] = $packageName . '.' . $lastname;
     $this->config['packageLastName'] = $lastname;
     $this->config['packageFullName'] = $packageName . '.' . $lastname;
     $this->config['packageModuleName'] = $packageName;
     // check output path
     if (empty($this->config['output'])) {
         $curpath = rtrim(getcwd(), '/\\');
         $this->config['output'] = $curpath . DS . $lastname . DS;
         $this->config['cocos_output'] = $curpath;
         $this->config['cocos_project'] = $lastname;
     } else {
         $outpath = rtrim($this->config['output'], '/\\');
         $this->config['output'] = $outpath . DS;
         $pos = strrpos($outpath, DS);
         if ($pos != false) {
             $this->config['cocos_output'] = substr($outpath, 0, $pos);
             $this->config['cocos_project'] = substr($outpath, $pos + 1);
         } else {
             $this->config['cocos_output'] = $outpath;
             $this->config['cocos_project'] = $lastname;
         }
     }
     if (!$this->config['force'] && (is_dir($this->config['output']) || file_exists($this->config['output']))) {
         printf("ERROR: project path \"%s\" exists\n", $this->config['output']);
         return false;
     }
     // check screen orientation
     $orientation = strtolower($this->config['orientation']);
     if ($orientation != 'landscape' && $orientation != 'portrait') {
         printf("ERROR: invalid screen orientation \"%s\"\n", $orientation);
         return false;
     }
     $this->config['orientation'] = $orientation;
     if (!$this->config['quiet']) {
         dumpConfig($this->config, $this->options);
     }
     $this->validated = true;
     return true;
 }
Esempio n. 6
0
 function validateConfig()
 {
     $isLite = $this->config['lite'];
     // check template
     $templatePath = rtrim($this->config['template'], "/\\");
     if ($isLite) {
         $templatePath = $templatePath . '-lite';
     }
     $templatePath = $templatePath . DS;
     if (!is_dir($templatePath)) {
         printf("ERROR: invalid template path \"%s\"\n", $templatePath);
         return false;
     }
     if (!file_exists($templatePath . '../cocos2dx_files.json')) {
         printf("ERROR: not found cocos2dx_files.json in template path \"%s/..\"\n", $templatePath);
         return false;
     }
     $cocos_files = file_get_contents($templatePath . '../cocos2dx_files.json');
     $cocos_files = json_decode($cocos_files, true);
     if (!is_array($cocos_files) || empty($cocos_files['common'])) {
         printf("ERROR: not found cocos2dx_files.json in template path \"%s/..\"\n", $templatePath);
         return false;
     }
     $this->config['template'] = $templatePath;
     $this->config['cocos_files'] = $cocos_files;
     // check package name
     $packageName = str_replace('-', '_', strtolower($this->config['package']));
     $parts = explode('.', $packageName);
     $packageName = array();
     for ($i = 0; $i < count($parts); $i++) {
         $parts[$i] = preg_replace('/[^a-z0-9_]/', '', $parts[$i]);
         if (!empty($parts[$i])) {
             $packageName[] = $parts[$i];
         }
     }
     if (count($packageName) < 2) {
         printf("ERROR: invalid package name \"%s\"\n", implode('.', $packageName));
         return false;
     }
     $lastname = $packageName[count($packageName) - 1];
     array_pop($packageName);
     $packageName = implode('.', $packageName);
     $this->config['package'] = $packageName . '.' . $lastname;
     $this->config['packageLastName'] = $lastname;
     $this->config['packageFullName'] = $packageName . '.' . $lastname;
     $this->config['packageModuleName'] = $packageName;
     // check output path
     if (empty($this->config['output'])) {
         $curpath = rtrim(getcwd(), '/\\');
         $this->config['output'] = $curpath . DS . $lastname . DS;
         $this->config['cocos_output'] = $curpath;
         $this->config['cocos_project'] = $lastname;
     } else {
         $outpath = rtrim($this->config['output'], '/\\');
         $this->config['output'] = $outpath . DS;
         $pos = strrpos($outpath, DS);
         if ($pos != false) {
             $this->config['cocos_output'] = substr($outpath, 0, $pos);
             $this->config['cocos_project'] = substr($outpath, $pos + 1);
         } else {
             $this->config['cocos_output'] = $outpath;
             $this->config['cocos_project'] = $lastname;
         }
     }
     if (!$this->config['force'] && (is_dir($this->config['output']) || file_exists($this->config['output']))) {
         printf("ERROR: project path \"%s\" exists\n", $this->config['output']);
         return false;
     }
     // check screen orientation
     $orientation = strtolower($this->config['orientation']);
     if ($orientation != 'landscape' && $orientation != 'portrait') {
         printf("ERROR: invalid screen orientation \"%s\"\n", $orientation);
         return false;
     }
     $this->config['orientation'] = $orientation;
     // check more configs
     if ($this->config['noproj'] && $this->config['onlyproj']) {
         print "ERROR: noproj and onlyproj cannot enable at the same time\n";
         return false;
     }
     if ($this->options['extracmd']) {
         $this->config['extracmd'] = $this->options['extracmd'];
     }
     if (!$this->config['quiet']) {
         dumpConfig($this->config, $this->options);
     }
     $this->validated = true;
     return true;
 }
Esempio n. 7
0
 private function generateNewConfig()
 {
     return new Config(dumpConfig());
 }