Search in sources :

Example 1 with ILaunchManager

use of org.eclipse.debug.core.ILaunchManager in project linuxtools by eclipse.

the class LaunchConfigurationUtils method createRunImageLaunchConfiguration.

/**
 * Creates a new {@link ILaunchConfiguration} for the given
 * {@link IDockerContainer}.
 *
 * @param baseConfigurationName
 *            the base configuration name to use when creating the
 *            {@link ILaunchConfiguration}.
 * @param image
 *            the {@link IDockerImage} used to create the container
 * @param containerName
 *            the actual container name (given by the user or generated by
 *            the Docker daemon)
 * @param containerConfig
 * @param hostConfig
 *            the user-provided {@link IDockerHostConfig} (created
 *            container's one)
 * @param removeWhenExits
 *            flag to indicate if container should be removed when exited
 * @return the generated {@link ILaunchConfiguration}
 */
public static ILaunchConfiguration createRunImageLaunchConfiguration(final IDockerImage image, final IDockerContainerConfig containerConfig, final IDockerHostConfig hostConfig, final String containerName, final boolean removeWhenExits) {
    try {
        final ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
        final ILaunchConfigurationType type = manager.getLaunchConfigurationType(IRunDockerImageLaunchConfigurationConstants.CONFIG_TYPE_ID);
        final String imageName = createRunImageLaunchConfigurationName(image);
        // using the image repo + first tag
        final ILaunchConfigurationWorkingCopy workingCopy = getLaunchConfigurationWorkingCopy(type, imageName);
        workingCopy.setAttribute(CREATION_DATE, DATE_FORMAT.format(new Date()));
        workingCopy.setAttribute(CONNECTION_NAME, image.getConnection().getName());
        workingCopy.setAttribute(IMAGE_ID, image.id());
        workingCopy.setAttribute(IMAGE_NAME, createRunImageLaunchConfigurationName(image));
        if (containerName != null && !containerName.isEmpty()) {
            workingCopy.setAttribute(CONTAINER_NAME, containerName);
        }
        // if we know the raw command string, use it since the container
        // config will remove quotes to split up command properly
        DockerContainerConfig config = (DockerContainerConfig) containerConfig;
        if (config.rawcmd() != null) {
            workingCopy.setAttribute(COMMAND, config.rawcmd());
        } else {
            workingCopy.setAttribute(COMMAND, toString(containerConfig.cmd()));
        }
        workingCopy.setAttribute(ENTRYPOINT, toString(containerConfig.entrypoint()));
        // selected ports
        workingCopy.setAttribute(PUBLISH_ALL_PORTS, hostConfig.publishAllPorts());
        // format: <containerPort><type>:<hostIP>:<hostPort>
        if (hostConfig.publishAllPorts()) {
            final IDockerImageInfo imageInfo = image.getConnection().getImageInfo(image.id());
            if (imageInfo != null) {
                workingCopy.setAttribute(PUBLISHED_PORTS, serializePortBindings(imageInfo.containerConfig().exposedPorts()));
            }
        } else {
            workingCopy.setAttribute(PUBLISHED_PORTS, serializePortBindings(hostConfig.portBindings()));
        }
        // links (with format being: "<containerName>:<containerAlias>")
        workingCopy.setAttribute(LINKS, hostConfig.links());
        // env variables
        workingCopy.setAttribute(ENV_VARIABLES, containerConfig.env());
        // labels
        workingCopy.setAttribute(LABELS, containerConfig.labels());
        // volumes
        final List<String> volumes = new ArrayList<>();
        // volumes from other containers
        for (String volumeFrom : hostConfig.volumesFrom()) {
            final DataVolumeModel volume = DataVolumeModel.parseVolumeFrom(volumeFrom);
            if (volume != null) {
                volumes.add(volume.toString());
            }
        }
        // bindings to host directory or file
        for (String bind : hostConfig.binds()) {
            final DataVolumeModel volume = DataVolumeModel.parseHostBinding(bind);
            if (volume != null) {
                volumes.add(volume.toString());
            }
        }
        // TODO: container path declaration
        workingCopy.setAttribute(DATA_VOLUMES, volumes);
        // options
        workingCopy.setAttribute(AUTO_REMOVE, removeWhenExits);
        workingCopy.setAttribute(ALLOCATE_PSEUDO_CONSOLE, containerConfig.tty());
        workingCopy.setAttribute(INTERACTIVE, containerConfig.openStdin());
        workingCopy.setAttribute(PRIVILEGED, hostConfig.privileged());
        workingCopy.setAttribute(READONLY, ((DockerHostConfig) hostConfig).readonlyRootfs());
        if (hostConfig.networkMode() != null)
            workingCopy.setAttribute(NETWORK_MODE, hostConfig.networkMode());
        // resources limitations
        if (containerConfig.memory() != null) {
            workingCopy.setAttribute(ENABLE_LIMITS, true);
            // memory in containerConfig is expressed in bytes
            workingCopy.setAttribute(MEMORY_LIMIT, Long.toString(containerConfig.memory().longValue() / MB));
        }
        if (containerConfig.cpuShares() != null) {
            workingCopy.setAttribute(ENABLE_LIMITS, true);
            workingCopy.setAttribute(CPU_PRIORITY, containerConfig.cpuShares().toString());
        }
        return workingCopy.doSave();
    } catch (CoreException e) {
        Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, LaunchMessages.getString(// $NON-NLS-1$
        "RunDockerImageLaunchConfiguration.creation.failure"), e));
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) DataVolumeModel(org.eclipse.linuxtools.internal.docker.ui.wizards.DataVolumeModel) DockerContainerConfig(org.eclipse.linuxtools.internal.docker.core.DockerContainerConfig) IDockerContainerConfig(org.eclipse.linuxtools.docker.core.IDockerContainerConfig) CoreException(org.eclipse.core.runtime.CoreException) ILaunchConfigurationType(org.eclipse.debug.core.ILaunchConfigurationType) ArrayList(java.util.ArrayList) ILaunchManager(org.eclipse.debug.core.ILaunchManager) IDockerImageInfo(org.eclipse.linuxtools.docker.core.IDockerImageInfo) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) Date(java.util.Date)

Example 2 with ILaunchManager

use of org.eclipse.debug.core.ILaunchManager in project linuxtools by eclipse.

the class LaunchConfigurationUtils method getLaunchConfigurationByImageName.

/**
 * Looks-up the {@link ILaunchConfiguration} with the given type and
 * <strong>IDockerImage's name</strong>.
 *
 * @param type
 *            the configuration type
 * @param imageName
 *            the associated {@link IDockerImage} name
 * @return the first matching {@link ILaunchConfiguration} or
 *         <code>null</code> if none was found.
 * @throws CoreException
 */
public static ILaunchConfiguration getLaunchConfigurationByImageName(final ILaunchConfigurationType type, final String imageName) throws CoreException {
    final ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
    ILaunchConfiguration lastLaunchConfiguration = null;
    // $NON-NLS-1$
    String lastCreationDate = "";
    for (ILaunchConfiguration launchConfiguration : manager.getLaunchConfigurations(type)) {
        final String launchConfigImageName = launchConfiguration.getAttribute(IMAGE_NAME, // $NON-NLS-1$
        "");
        final String launchConfigCreationDate = launchConfiguration.getAttribute(CREATION_DATE, // $NON-NLS-1$
        "");
        if (launchConfigImageName.equals(imageName) && launchConfigCreationDate.compareTo(lastCreationDate) > 0) {
            lastCreationDate = launchConfigCreationDate;
            lastLaunchConfiguration = launchConfiguration;
        }
    }
    return lastLaunchConfiguration;
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) ILaunchManager(org.eclipse.debug.core.ILaunchManager)

Example 3 with ILaunchManager

use of org.eclipse.debug.core.ILaunchManager in project linuxtools by eclipse.

the class OprofileLaunchConfigurationDelegate method preExec.

@Override
protected boolean preExec(LaunchOptions options, OprofileDaemonEvent[] daemonEvents, ILaunch launch) {
    // set up and launch the oprofile daemon
    IProject project = getProject();
    // Set current project to allow using the oprofile path that
    // was chosen for the project
    Oprofile.OprofileProject.setProject(project);
    if (!oprofileStatus()) {
        // $NON-NLS-1$
        OprofileCorePlugin.showErrorDialog("opcontrolProvider", null);
        return false;
    }
    // add a listener for termination of the launch prior to execution of launch
    ILaunchManager lmgr = DebugPlugin.getDefault().getLaunchManager();
    lmgr.addLaunchListener(new LaunchTerminationWatcher(launch, options.getExecutionsNumber()));
    return true;
}
Also used : ILaunchManager(org.eclipse.debug.core.ILaunchManager) IProject(org.eclipse.core.resources.IProject)

Example 4 with ILaunchManager

use of org.eclipse.debug.core.ILaunchManager in project linuxtools by eclipse.

the class SystemTapScriptLaunchConfigurationDelegate method launch.

@Override
public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
    // Wait for other stap launches' consoles to be initiated before starting a new launch.
    ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
    for (ILaunch olaunch : manager.getLaunches()) {
        if (olaunch.equals(launch)) {
            continue;
        }
        if (olaunch instanceof SystemTapScriptLaunch && ((SystemTapScriptLaunch) olaunch).getConsole() == null) {
            throw new CoreException(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, Messages.SystemTapScriptLaunchError_waitForConsoles));
        }
    }
    if (!SystemTapScriptGraphOptionsTab.isValidLaunch(configuration)) {
        throw new CoreException(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, Messages.SystemTapScriptLaunchError_graph));
    }
    RunScriptHandler action;
    boolean runWithChart = configuration.getAttribute(SystemTapScriptGraphOptionsTab.RUN_WITH_CHART, false);
    // If runWithChart is true there must be at least one graph, but this isn't guaranteed
    // to be true for outdated Launch Configurations. So for safety, make sure there are graphs.
    int numGraphs = configuration.getAttribute(SystemTapScriptGraphOptionsTab.NUMBER_OF_REGEXS, 0);
    if (runWithChart && numGraphs > 0) {
        List<IDataSetParser> parsers = SystemTapScriptGraphOptionsTab.createDatasetParsers(configuration);
        List<IFilteredDataSet> dataSets = SystemTapScriptGraphOptionsTab.createDataset(configuration);
        List<String> names = SystemTapScriptGraphOptionsTab.createDatasetNames(configuration);
        List<LinkedList<GraphData>> graphs = SystemTapScriptGraphOptionsTab.createGraphsFromConfiguration(configuration);
        action = new RunScriptChartHandler(parsers, dataSets, names, graphs);
    } else {
        action = new RunScriptHandler();
    }
    // Path
    // $NON-NLS-1$
    IPath scriptPath = new Path(configuration.getAttribute(SystemTapScriptLaunchConfigurationTab.SCRIPT_PATH_ATTR, ""));
    if (!scriptPath.toFile().exists()) {
        throw new CoreException(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, MessageFormat.format(Messages.SystemTapScriptLaunchError_fileNotFound, scriptPath.toString())));
    }
    String extension = scriptPath.getFileExtension();
    if (extension == null || !extension.equals("stp")) {
        // $NON-NLS-1$
        throw new CoreException(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, MessageFormat.format(Messages.SystemTapScriptLaunchError_fileNotStp, scriptPath.toString())));
    }
    action.setPath(scriptPath);
    // Run locally and/or as current user.
    action.setRemoteScriptOptions(configuration.getAttribute(SystemTapScriptLaunchConfigurationTab.CURRENT_USER_ATTR, true) ? null : new RemoteScriptOptions(// $NON-NLS-1$
    configuration.getAttribute(SystemTapScriptLaunchConfigurationTab.USER_NAME_ATTR, ""), // $NON-NLS-1$
    configuration.getAttribute(SystemTapScriptLaunchConfigurationTab.USER_PASS_ATTR, ""), configuration.getAttribute(SystemTapScriptLaunchConfigurationTab.LOCAL_HOST_ATTR, true) ? LOCALHOST : configuration.getAttribute(SystemTapScriptLaunchConfigurationTab.HOST_NAME_ATTR, LOCALHOST), configuration.getAttribute(SystemTapScriptLaunchConfigurationTab.USE_DEFAULT_PORT_ATTR, true) ? DEFAULT_PORT : configuration.getAttribute(SystemTapScriptLaunchConfigurationTab.PORT_ATTR, DEFAULT_PORT)));
    // $NON-NLS-1$
    String value = configuration.getAttribute(IDEPreferenceConstants.STAP_CMD_OPTION[IDEPreferenceConstants.KEY], "");
    if (!value.isEmpty()) {
        // $NON-NLS-1$
        action.addComandLineOptions(IDEPreferenceConstants.STAP_CMD_OPTION[IDEPreferenceConstants.FLAG] + " " + value);
    }
    // Add command line options
    for (int i = 0; i < IDEPreferenceConstants.STAP_BOOLEAN_OPTIONS.length; i++) {
        boolean flag = configuration.getAttribute(IDEPreferenceConstants.STAP_BOOLEAN_OPTIONS[i][IDEPreferenceConstants.KEY], false);
        if (flag) {
            action.addComandLineOptions(IDEPreferenceConstants.STAP_BOOLEAN_OPTIONS[i][IDEPreferenceConstants.FLAG]);
        }
    }
    for (int i = 0; i < IDEPreferenceConstants.STAP_STRING_OPTIONS.length; i++) {
        // $NON-NLS-1$
        value = configuration.getAttribute(IDEPreferenceConstants.STAP_STRING_OPTIONS[i][IDEPreferenceConstants.KEY], "");
        if (!value.isEmpty()) {
            // $NON-NLS-1$
            action.addComandLineOptions(IDEPreferenceConstants.STAP_STRING_OPTIONS[i][IDEPreferenceConstants.FLAG] + " " + value);
        }
    }
    // $NON-NLS-1$
    value = configuration.getAttribute(SystemTapScriptOptionsTab.MISC_COMMANDLINE_OPTIONS, "");
    if (!value.isEmpty()) {
        action.addComandLineOptions(value);
    }
    action.setLaunch((SystemTapScriptLaunch) launch);
    try {
        action.execute(null);
    } catch (ExecutionException e) {
        throw new CoreException(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, e.getMessage()));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) RunScriptChartHandler(org.eclipse.linuxtools.internal.systemtap.ui.ide.handlers.RunScriptChartHandler) IPath(org.eclipse.core.runtime.IPath) ILaunchManager(org.eclipse.debug.core.ILaunchManager) IFilteredDataSet(org.eclipse.linuxtools.systemtap.graphing.core.datasets.IFilteredDataSet) LinkedList(java.util.LinkedList) RemoteScriptOptions(org.eclipse.linuxtools.systemtap.ui.consolelog.structures.RemoteScriptOptions) CoreException(org.eclipse.core.runtime.CoreException) IDataSetParser(org.eclipse.linuxtools.systemtap.graphing.core.datasets.IDataSetParser) ILaunch(org.eclipse.debug.core.ILaunch) RunScriptHandler(org.eclipse.linuxtools.internal.systemtap.ui.ide.handlers.RunScriptHandler) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 5 with ILaunchManager

use of org.eclipse.debug.core.ILaunchManager in project linuxtools by eclipse.

the class AbstractValgrindTest method tearDown.

@After
public void tearDown() throws CoreException {
    ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
    if (launches.size() > 0) {
        lm.removeLaunches(launches.toArray(new ILaunch[launches.size()]));
        launches.clear();
    }
    // Delete the Launch Configurations
    ILaunchConfiguration[] configs = lm.getLaunchConfigurations();
    for (ILaunchConfiguration config : configs) {
        config.delete();
    }
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) ILaunch(org.eclipse.debug.core.ILaunch) ILaunchManager(org.eclipse.debug.core.ILaunchManager) After(org.junit.After)

Aggregations

ILaunchManager (org.eclipse.debug.core.ILaunchManager)76 ILaunchConfigurationType (org.eclipse.debug.core.ILaunchConfigurationType)51 ILaunchConfigurationWorkingCopy (org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)39 ILaunchConfiguration (org.eclipse.debug.core.ILaunchConfiguration)35 CoreException (org.eclipse.core.runtime.CoreException)28 ArrayList (java.util.ArrayList)16 ILaunch (org.eclipse.debug.core.ILaunch)14 IPath (org.eclipse.core.runtime.IPath)9 File (java.io.File)7 Path (org.eclipse.core.runtime.Path)6 HashMap (java.util.HashMap)5 IProject (org.eclipse.core.resources.IProject)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 IOException (java.io.IOException)4 Map (java.util.Map)4 IStatus (org.eclipse.core.runtime.IStatus)4 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Status (org.eclipse.core.runtime.Status)3 IProcess (org.eclipse.debug.core.model.IProcess)3