Exemple #1
0
    exit(1);
}
require_once '/opt/unetlab/html/includes/init.php';
if (!isset($argv[1])) {
    // File does not exists or not set
    error_log('ERROR: Must need a lab file as argument.');
    exit(1);
}
if (!is_file($argv[1])) {
    // File does not exists
    error_log('ERROR: File does not exist.');
    exit(2);
}
try {
    $lab = new Lab($argv[1], 0);
    if (is_dir(BASE_TMP . '/0/' . $lab->getId()) && is_dir(BASE_TMP . '/0/' . $lab->getName())) {
        // Source dir and destinaion dir both exists
        error_log('ERROR: Destination folder already exists.');
        error_log('Cannot migrate "' . $argv[1] . '", manually delete directory and loose data to migrate the old configs:');
        error_log('rm -rf "' . BASE_TMP . '/0/' . $lab->getId() . '"');
        error_log($argv[0] . ' ' . $argv[1]);
        error_log('Or remove the old directory if not needed anymore:');
        error_log('rm -rf "' . BASE_TMP . '/0/' . $lab->getName() . '"');
        exit(3);
    } else {
        if (!is_dir(BASE_TMP . '/0/' . $lab->getId()) && is_dir(BASE_TMP . '/0/' . $lab->getName())) {
            if (rename(BASE_TMP . '/0/' . $lab->getName(), BASE_TMP . '/0/' . $lab->getId())) {
                // Mark as configured all nodes
                $cmd = 'find /opt/unetlab/tmp/0/' . $lab->getId() . '/[0-9]* -type d -exec touch {}/.configured \\;';
                exec($cmd, $o, $rc);
            } else {
        // Node ID must be numeric, greater than 0 and exists on lab
        usage();
        error_log(date('M d H:i:s ') . date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][8]);
        exit(8);
    }
}
switch ($action) {
    default:
        // Invalid action
        usage();
        error_log(date('M d H:i:s ') . date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][9]);
        exit(9);
    case 'delete':
        if (isset($node_id)) {
            // Removing temporary files for a single node in all tenants
            $cmd = 'rm -rf /opt/unetlab/tmp/*/' . $lab->getId() . '/' . $node_id . '/';
        } else {
            // Removing all temporary files in all tenants
            $cmd = 'rm -rf /opt/unetlab/tmp/*/' . $lab->getId() . '/';
        }
        exec($cmd, $o, $rc);
        break;
    case 'export':
        // Exporting node(s) running-config
        if (isset($node_id)) {
            // Node ID is set, export a single node
            export($node_id, $lab->getNodes()[$node_id], $lab);
        } else {
            // Node ID is not set, export all nodes
            foreach ($lab->getNodes() as $node_id => $node) {
                export($node_id, $node, $lab);