-
Notifications
You must be signed in to change notification settings - Fork 422
Features/windows agents with pathing issues fixed #184
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
Merged
oleg-nenashev
merged 11 commits into
jenkinsci:master
from
3shape:features/windows_slaves
Oct 9, 2019
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
12f27b0
Added support to run Docker stages on Windows slaves
rbutcher 3b3d798
Updated changes to support docker.inside in reference to PR-98
rbutcher 348b1d9
Cleaning up imports
rbutcher 3bcadcd
Fixing build issues
rbutcher 1722a14
Removing annotations
rbutcher 2d19156
Fixing DockerWindowsClient test_run()
rbutcher 5c43bbe
Updated whoAmI to properly get user on Windows
rbutcher b9baff6
Fixing findBugs issues with default charset
rbutcher 20a2a26
Fixed imports in WithContainerStep.java
rbutcher b0d3fae
fix windows path issue and skip whoami on Windows for now
jetersen 6982db7
fix imports
jetersen 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
137 changes: 137 additions & 0 deletions
137
src/main/java/org/jenkinsci/plugins/docker/workflow/client/WindowsDockerClient.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,137 @@ | ||
| 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 WindowsDockerClient extends DockerClient { | ||
| private static final Logger LOGGER = Logger.getLogger(WindowsDockerClient.class.getName()); | ||
|
|
||
| private final Launcher launcher; | ||
| private final Node node; | ||
|
|
||
| public WindowsDockerClient(@Nonnull Launcher launcher, @CheckForNull Node node, @CheckForNull String toolName) { | ||
| super(launcher, node, toolName); | ||
| this.launcher = launcher; | ||
| this.node = node; | ||
| } | ||
|
|
||
| @Override | ||
| public String run(@Nonnull EnvVars launchEnv, @Nonnull String image, @CheckForNull String args, @CheckForNull String workdir, @Nonnull Map<String, String> volumes, @Nonnull Collection<String> volumesFromContainers, @Nonnull EnvVars containerEnv, @Nonnull String user, @Nonnull String... command) throws IOException, InterruptedException { | ||
| ArgumentListBuilder argb = new ArgumentListBuilder("docker", "run", "-d", "-t"); | ||
| if (args != null) { | ||
| argb.addTokenized(args); | ||
| } | ||
|
|
||
| if (workdir != null) { | ||
| argb.add("-w", workdir); | ||
| } | ||
| for (Map.Entry<String, String> volume : volumes.entrySet()) { | ||
| argb.add("-v", volume.getKey() + ":" + volume.getValue()); | ||
| } | ||
| for (String containerId : volumesFromContainers) { | ||
| argb.add("--volumes-from", containerId); | ||
| } | ||
| for (Map.Entry<String, String> variable : containerEnv.entrySet()) { | ||
| argb.add("-e"); | ||
| argb.addMasked(variable.getKey()+"="+variable.getValue()); | ||
| } | ||
| argb.add(image).add(command); | ||
|
|
||
| LaunchResult result = launch(launchEnv, false, null, argb); | ||
| if (result.getStatus() == 0) { | ||
| return result.getOut(); | ||
| } else { | ||
| throw new IOException(String.format("Failed to run image '%s'. Error: %s", image, result.getErr())); | ||
| } | ||
| } | ||
|
|
||
| @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() < 1) { | ||
| throw new IOException("Unexpected `docker top` output : "+line); | ||
| } | ||
|
|
||
| processes.add(stringTokenizer.nextToken()); // COMMAND | ||
| } | ||
| } | ||
| return processes; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<String> getContainerIdIfContainerized() throws IOException, InterruptedException { | ||
| if (node == null || | ||
| launch(new EnvVars(), true, null, "sc.exe", "query", "cexecsvc").getStatus() != 0) { | ||
| return Optional.absent(); | ||
| } | ||
|
|
||
| LaunchResult getComputerName = launch(new EnvVars(), true, null, "hostname"); | ||
| if(getComputerName.getStatus() != 0) { | ||
| throw new IOException("Failed to get hostname."); | ||
| } | ||
|
|
||
| String shortID = getComputerName.getOut().toLowerCase(); | ||
| LaunchResult getLongIdResult = launch(new EnvVars(), true, null, "docker", "inspect", shortID, "--format={{.Id}}"); | ||
| if(getLongIdResult.getStatus() != 0) { | ||
| LOGGER.log(Level.INFO, "Running inside of a container but cannot determine container ID from current environment."); | ||
| return Optional.absent(); | ||
| } | ||
|
|
||
| return Optional.of(getLongIdResult.getOut()); | ||
| } | ||
|
|
||
| @Override | ||
| public String whoAmI() throws IOException, InterruptedException { | ||
| try (ByteArrayOutputStream userId = new ByteArrayOutputStream()) { | ||
| launcher.launch().cmds("whoami").quiet(true).stdout(userId).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener()); | ||
| return userId.toString(Charset.defaultCharset().name()).trim(); | ||
| } | ||
| } | ||
|
|
||
| private LaunchResult launch(EnvVars env, boolean quiet, FilePath workDir, String... args) throws IOException, InterruptedException { | ||
| return launch(env, quiet, workDir, new ArgumentListBuilder(args)); | ||
| } | ||
| private LaunchResult launch(EnvVars env, boolean quiet, FilePath workDir, ArgumentListBuilder argb) throws IOException, InterruptedException { | ||
| if (LOGGER.isLoggable(Level.FINE)) { | ||
| LOGGER.log(Level.FINE, "Executing command \"{0}\"", argb); | ||
| } | ||
|
|
||
| Launcher.ProcStarter procStarter = launcher.launch(); | ||
| if (workDir != null) { | ||
| procStarter.pwd(workDir); | ||
| } | ||
|
|
||
| LaunchResult result = new LaunchResult(); | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| ByteArrayOutputStream err = new ByteArrayOutputStream(); | ||
| result.setStatus(procStarter.quiet(quiet).cmds(argb).envs(env).stdout(out).stderr(err).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener())); | ||
| final String charsetName = Charset.defaultCharset().name(); | ||
| result.setOut(out.toString(charsetName)); | ||
| result.setErr(err.toString(charsetName)); | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
Oops, something went wrong.
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 we use
--mountinstead?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.
As an aside, I notice the regular docker client uses volumes instead of mounts as well. Should probably keep them consistent (which I suspect means switching DockerClient.java to mounts too mind you - and a separate PR)