-
Notifications
You must be signed in to change notification settings - Fork 412
JENKINS-60473 Containerize workspace paths when running Linux container on Windows host #197
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
Open
mrsonicblue
wants to merge
1
commit into
jenkinsci:master
Choose a base branch
from
mrsonicblue:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -378,4 +378,113 @@ public List<String> getVolumes(@Nonnull EnvVars launchEnv, String containerID) t | |
} | ||
return Arrays.asList(volumes.replace("\\", "/").split("\\n")); | ||
} | ||
|
||
public String runCommand() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add Javadoc for new API below? |
||
return "cat"; | ||
} | ||
|
||
public boolean needToContainerizePath() { | ||
return false; | ||
} | ||
|
||
public String containerizePathIfNeeded(String path, String prefix) { | ||
if (needToContainerizePath()) | ||
return DockerClient.containerizePath(path, prefix); | ||
|
||
return path; | ||
} | ||
|
||
public static String containerizePath(String path, String prefix) | ||
{ | ||
StringBuffer result = new StringBuffer(); | ||
char[] pathChars = path.toCharArray(); | ||
char[] prefixChars = (prefix == null) ? null : prefix.toCharArray(); | ||
|
||
for (int i = 0; i < pathChars.length; i++) | ||
{ | ||
char currentChar = pathChars[i]; | ||
if (currentChar == ':' && i > 0 && i < pathChars.length - 1) | ||
{ | ||
char previousChar = pathChars[i - 1]; | ||
if ((previousChar >= 'a' && previousChar <= 'z') || (previousChar >= 'A' && previousChar <= 'Z')) | ||
{ | ||
char nextChar = pathChars[i + 1]; | ||
if (nextChar == '/' || nextChar == '\\') | ||
{ | ||
char nextNextChar = (i < pathChars.length - 2) ? pathChars[i + 2] : ' '; | ||
if (nextNextChar != '/') | ||
{ | ||
if (prefix == null || checkPrefix(pathChars, i - 1, prefixChars)) | ||
{ | ||
result.setCharAt(i - 1, '/'); | ||
result.append(Character.toLowerCase(previousChar)); | ||
result.append('/'); | ||
i++; | ||
i++; | ||
|
||
boolean done = false; | ||
for ( ; i < pathChars.length; i++) | ||
{ | ||
currentChar = pathChars[i]; | ||
switch (currentChar) | ||
{ | ||
case '\\': | ||
result.append('/'); | ||
break; | ||
|
||
case '?': | ||
case '<': | ||
case '>': | ||
case ':': | ||
case '*': | ||
case '|': | ||
case '"': | ||
case '\'': | ||
result.append(currentChar); | ||
done = true; | ||
break; | ||
|
||
default: | ||
result.append(currentChar); | ||
break; | ||
} | ||
|
||
if (done) | ||
break; | ||
} | ||
|
||
continue; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
result.append(currentChar); | ||
} | ||
|
||
return result.toString(); | ||
} | ||
|
||
private static boolean checkPrefix(char[] pathChars, int index, char[] prefixChars) | ||
{ | ||
if (index + prefixChars.length > pathChars.length) | ||
return false; | ||
|
||
for (int i = 0; i < prefixChars.length; i++) | ||
{ | ||
char pathChar = pathChars[index + i]; | ||
if (pathChar == '\\') | ||
pathChar = '/'; | ||
|
||
char prefixChar = prefixChars[i]; | ||
if (prefixChar == '\\') | ||
prefixChar = '/'; | ||
|
||
if (pathChar != prefixChar) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/org/jenkinsci/plugins/docker/workflow/client/WindowsLinuxDockerClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.jenkinsci.plugins.docker.workflow.client; | ||
|
||
import com.google.common.base.Optional; | ||
import hudson.EnvVars; | ||
import hudson.FilePath; | ||
import hudson.Launcher; | ||
import hudson.model.Node; | ||
import hudson.util.ArgumentListBuilder; | ||
|
||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nonnull; | ||
import java.io.*; | ||
import java.nio.charset.Charset; | ||
import java.util.*; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class WindowsLinuxDockerClient extends WindowsDockerClient { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Taking the code, it would be rather a Unix/POSIX container launcher. Not that important tho |
||
private static final Logger LOGGER = Logger.getLogger(WindowsLinuxDockerClient.class.getName()); | ||
|
||
private final Launcher launcher; | ||
private final Node node; | ||
|
||
public WindowsLinuxDockerClient(@Nonnull Launcher launcher, @CheckForNull Node node, @CheckForNull String toolName) { | ||
super(launcher, node, toolName); | ||
this.launcher = launcher; | ||
this.node = node; | ||
} | ||
|
||
@Override | ||
public List<String> listProcess(@Nonnull EnvVars launchEnv, @Nonnull String containerId) throws IOException, InterruptedException { | ||
LaunchResult result = launch(launchEnv, false, null, "docker", "top", containerId); | ||
if (result.getStatus() != 0) { | ||
throw new IOException(String.format("Failed to run top '%s'. Error: %s", containerId, result.getErr())); | ||
} | ||
List<String> processes = new ArrayList<>(); | ||
try (Reader r = new StringReader(result.getOut()); | ||
BufferedReader in = new BufferedReader(r)) { | ||
String line; | ||
in.readLine(); // ps header | ||
while ((line = in.readLine()) != null) { | ||
final StringTokenizer stringTokenizer = new StringTokenizer(line, " "); | ||
if (stringTokenizer.countTokens() < 4) { | ||
throw new IOException("Unexpected `docker top` output : "+line); | ||
} | ||
|
||
stringTokenizer.nextToken(); // PID | ||
stringTokenizer.nextToken(); // USER | ||
stringTokenizer.nextToken(); // TIME | ||
processes.add(stringTokenizer.nextToken()); // COMMAND | ||
} | ||
} | ||
return processes; | ||
} | ||
|
||
@Override | ||
public String runCommand() | ||
{ | ||
return "cat"; | ||
} | ||
|
||
@Override | ||
public boolean needToContainerizePath() { | ||
return true; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be actually inverted? There is a lot of Unix operating systems being containerized, but Windows Client is for Windows only?