<commit_before>/*
 *  Copyright (C) 2010 <NAME> <<EMAIL>ain.reuillon at openmole.org>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openmole.plugin.resource.virtual;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.logging.Logger;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.ShutdownHookProcessDestroyer;
import org.apache.commons.exec.launcher.CommandLauncher;
import org.apache.commons.exec.launcher.CommandLauncherFactory;
import org.openmole.commons.aspect.caching.Cachable;
import org.openmole.commons.exception.InternalProcessingError;
import org.openmole.commons.exception.UserBadDataError;
import org.openmole.commons.tools.io.FileUtil;
import org.openmole.core.implementation.resource.ComposedResource;
import org.openmole.core.implementation.resource.FileResource;
import org.openmole.core.model.task.annotations.Resource;
import org.openmole.plugin.resource.virtual.internal.Activator;
import static org.openmole.commons.tools.io.Network.*;

/**
 *
 * @author <NAME> <<EMAIL> at openmole.org>
 */
public class VirtualMachineResource extends ComposedResource {

    final static String[] CommonFiles = {"bios.bin"};
    final static String Executable = "qemu";
    @Resource
    final FileResource systemResource;
    final String user;
    final String password;
    final int memory;
    final int vcore;

    public VirtualMachineResource(String system, String user, String password) {
        this(new File(system), user, password);
    }

    public VirtualMachineResource(File system, String user, String password) {
        this(system, user, password, 256);
    }

    public VirtualMachineResource(String system, String user, String password, int memory) {
        this(new File(system), user, password, memory);
    }

    public VirtualMachineResource(File system, String user, String password, int memory) {
        this(system, user, password, memory, 1);
    }

    public VirtualMachineResource(String system, String user, String password, int memory, int vcore) {
        this(new File(system), user, password, memory, vcore);
    }

    public VirtualMachineResource(File system, String user, String password, int memory, int vcore) {
        this.systemResource = new FileResource(system);
        this.user = user;
        this.password = password;
        this.memory = memory;
        this.vcore = vcore;
    }

    public IVirtualMachine launchAVirtualMachine() throws UserBadDataError, InternalProcessingError {
        File vmImage = systemResource.getDeployedFile();
        if (!vmImage.isFile()) {
            throw new UserBadDataError("Image " + vmImage.getAbsolutePath() + " doesn't exist or is not a file.");
        }

        class VirtualMachineConnector implements IConnectable {

            IVirtualMachine virtualMachine;

            @Override
            public void connect(int port) throws IOException, InternalProcessingError {
                File qemuDir = getQEmuDir();  

                CommandLine commandLine = new CommandLine(new File(qemuDir, Executable));
                commandLine.addArguments("-m " + memory + " -smp " + vcore +  " -redir tcp:" + port + "::22 -nographic -hda ");
                commandLine.addArgument(systemResource.getDeployedFile().getAbsolutePath());
                commandLine.addArguments("-L");
                commandLine.addArgument(qemuDir.getAbsolutePath());
                commandLine.addArguments("-monitor null -serial none");

               // Logger.getLogger(VirtualMachineResource.class.getName()).info(commandLine.toString());
<commit_msg>Secure concurent use of the same virtual machine file.<commit_after>/*
 *  Copyright (C) 2010 <NAME> <romain.reuillon at openmole.org>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openmole.plugin.resource.virtual;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.ShutdownHookProcessDestroyer;
import org.apache.commons.exec.launcher.CommandLauncher;
import org.apache.commons.exec.launcher.CommandLauncherFactory;
import org.openmole.commons.aspect.caching.Cachable;
import org.openmole.commons.exception.InternalProcessingError;
import org.openmole.commons.exception.UserBadDataError;
import org.openmole.commons.tools.io.FileUtil;
import org.openmole.core.implementation.resource.ComposedResource;
import org.openmole.core.implementation.resource.FileResource;
import org.openmole.core.model.task.annotations.Resource;
import org.openmole.plugin.resource.virtual.internal.Activator;
import static org.openmole.commons.tools.io.Network.*;

/**
 *
 * @author <NAME> <romain.reuillon at openmole.org>
 */
public class VirtualMachineResource extends ComposedResource {

    final static String[] CommonFiles = {"bios.bin"};
    final static String Executable = "qemu";
    @Resource
    final FileResource systemResource;
    final String user;
    final String password;
    final int memory;
    final int vcore;

    public VirtualMachineResource(String system, String user, String password) {
        this(new File(system), user, password);
    }

    public VirtualMachineResource(File system, String user, String password) {
        this(system, user, password, 256);
    }

    public VirtualMachineResource(String system, String user, String password, int memory) {
        this(new File(system), user, password, memory);
    }

    public VirtualMachineResource(File system, String user, String password, int memory) {
        this(system, user, password, memory, 1);
    }

    public VirtualMachineResource(String system, String user, String password, int memory, int vcore) {
        this(new File(system), user, password, memory, vcore);
    }

    public VirtualMachineResource(File system, String user, String password, int memory, int vcore) {
        this.systemResource = new FileResource(system);
        this.user = user;
        this.password = password;
        this.memory = memory;
        this.vcore = vcore;
    }

    public IVirtualMachine launchAVirtualMachine() throws UserBadDataError, InternalProcessingError {
        if (!systemResource.getDeployedFile().isFile()) {
            throw new UserBadDataError("Image " + systemResource.getDeployedFile().getAbsolutePath() + " doesn't exist or is not a file.");
        }

        final File vmImage;
        try {
            vmImage = Activator.getWorkspace().newTmpFile();
            FileUtil.copy(systemResource.getDeployedFile(), vmImage);
        } catch (IOException ex) {
            throw new InternalProcessingError(ex);
        }

        class VirtualMachineConnector implements IConnectable {

            IVirtualMachine virtualMachine;

            @Override
            public void connect(int port) throws IOException, InternalProcessingError {
                File qemuDir = getQEmuDir();  

                CommandLine commandLine = new CommandLine(new File(qemuDir, Executable));
                commandLine.addArguments("-m " + memory + " -smp " + vcore +  " -redir tcp:" + port + "::22 -nographic -hda ");
                commandLine.addArgument(systemResource.getDeployedFile().getAbsolutePath());
                commandLine.addArguments("-L");
                commandLine.addArgument(qemuDir.getAbsolutePath());
                commandLine.addArguments("-monitor null -serial none");

               // Logger.getLogger(VirtualMachineResource.class.getName()).info(commandLine.toString());

{'fraction_non_alphanumeric': 0.09453781512605042, 'fraction_numerical': 0.0024325519681556835, 'mean_word_length': 3.488833746898263, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1096151', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2378, 'n_words': 697}