Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker: Allow setting a mapping for docker paths #46

Merged
merged 3 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Plugin.name
Bundle-SymbolicName: org.eclipse.cdt.docker.launcher;singleton:=true
Bundle-Version: 1.3.400.qualifier
Bundle-Version: 2.0.0.qualifier
Bundle-Activator: org.eclipse.cdt.docker.launcher.DockerLaunchUIPlugin
Bundle-Vendor: %Plugin.vendor
Bundle-Localization: plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class ContainerCommandLauncher implements ICommandLauncher, ICBuildComman
public final static String VOLUMES_ID = DockerLaunchUIPlugin.PLUGIN_ID + ".containerbuild.property.volumes"; //$NON-NLS-1$
public final static String SELECTED_VOLUMES_ID = DockerLaunchUIPlugin.PLUGIN_ID
+ ".containerbuild.property.selectedvolumes"; //$NON-NLS-1$
/** @since 2.0 */
public final static String DOCKERD_PATH = DockerLaunchUIPlugin.PLUGIN_ID + ".containerbuild.property.dockerdpath"; //$NON-NLS-1$

public final static String VOLUME_SEPARATOR_REGEX = "[|]"; //$NON-NLS-1$

Expand Down Expand Up @@ -253,14 +255,16 @@ public Process execute(IPath commandPath, String[] args, String[] env, IPath wor
boolean keepContainer = prefs.getBoolean(PreferenceConstants.KEEP_CONTAINER_AFTER_LAUNCH, false);

ICBuildConfiguration buildCfg = getBuildConfiguration();
String selectedVolumeString = null;
String connectionName = null;
String imageName = null;
final String selectedVolumeString;
final String connectionName;
final String imageName;
final String pathMapProperty;
if (buildCfg != null) {
IToolChain toolChain = buildCfg.getToolChain();
selectedVolumeString = toolChain.getProperty(SELECTED_VOLUMES_ID);
connectionName = toolChain.getProperty(IContainerLaunchTarget.ATTR_CONNECTION_URI);
imageName = toolChain.getProperty(IContainerLaunchTarget.ATTR_IMAGE_ID);
pathMapProperty = toolChain.getProperty(DOCKERD_PATH);
} else {
ICConfigurationDescription cfgd = CoreModel.getDefault().getProjectDescription(fProject)
.getActiveConfiguration();
Expand All @@ -272,6 +276,7 @@ public Process execute(IPath commandPath, String[] args, String[] env, IPath wor
selectedVolumeString = props.getProperty(SELECTED_VOLUMES_ID);
connectionName = props.getProperty(ContainerCommandLauncher.CONNECTION_ID);
imageName = props.getProperty(ContainerCommandLauncher.IMAGE_ID);
pathMapProperty = props.getProperty(DOCKERD_PATH);
}

// Add any specified volumes to additional dir list
Expand All @@ -288,8 +293,20 @@ public Process execute(IPath commandPath, String[] args, String[] env, IPath wor
}
setImageName(imageName);

additionalDirs.addAll(
additionalPaths.stream().map(p -> ContainerLaunchUtils.toDockerVolume(p)).collect(Collectors.toList()));
final Map<String, String> pathMap = new HashMap<>();

if (pathMapProperty != null && !pathMapProperty.isEmpty()) {
final var entries = pathMapProperty.split(";"); //$NON-NLS-1$
for (var e : entries) {
final var spl = e.split("\\|"); //$NON-NLS-1$
if (spl.length == 2) {
pathMap.put(spl[0], spl[1]);
}
}
}

additionalDirs.addAll(additionalPaths.stream().map(p -> ContainerLaunchUtils.toDockerVolume(pathMap, p))
.collect(Collectors.toList()));

fProcess = launcher.runCommand(connectionName, imageName, fProject, this, cmdList, workingDir, additionalDirs,
origEnv, fEnvironment, supportStdin, privilegedMode, labels, keepContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.cdt.internal.docker.launcher;

import java.util.Map;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;

Expand Down Expand Up @@ -59,11 +61,23 @@ public static final String toDockerPath(String path) {
* @param path The path on the hose
* @return The string to be passed to the docker daemon
*/
public static final String toDockerVolume(IPath path) {
IPath p = path.makeAbsolute();
String rv = toDockerPath(p);
public static final String toDockerVolume(Map<String, String> pMap, IPath path) {
// The path on the Docker host
var dhPath = path.makeAbsolute().toString();

for (var me : pMap.entrySet()) {
var elp = me.getKey();
var edhp = me.getValue();
if (dhPath.startsWith(elp)) {
dhPath = edhp + dhPath.substring(elp.length());
break;
}
}

// docker-path first, docker-host-path third
String rv = toDockerPath(path.makeAbsolute());
rv += ":HOST_FILE_SYSTEM:"; //$NON-NLS-1$
rv += p.toOSString();
rv += dhPath;
rv += ":false:true"; //$NON-NLS-1$ RO=false, selected = true
return rv;
}
Expand Down
Loading