|
| 1 | +package org.jenkinsci.plugins.docker.workflow.client; |
| 2 | + |
| 3 | +import com.google.common.base.Optional; |
| 4 | +import hudson.EnvVars; |
| 5 | +import hudson.FilePath; |
| 6 | +import hudson.Launcher; |
| 7 | +import hudson.model.Node; |
| 8 | +import hudson.util.ArgumentListBuilder; |
| 9 | + |
| 10 | +import javax.annotation.CheckForNull; |
| 11 | +import javax.annotation.Nonnull; |
| 12 | +import java.io.*; |
| 13 | +import java.nio.charset.Charset; |
| 14 | +import java.util.*; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | +import java.util.logging.Level; |
| 17 | +import java.util.logging.Logger; |
| 18 | + |
| 19 | +public class WindowsDockerClient extends DockerClient { |
| 20 | + private static final Logger LOGGER = Logger.getLogger(WindowsDockerClient.class.getName()); |
| 21 | + |
| 22 | + private final Launcher launcher; |
| 23 | + private final Node node; |
| 24 | + |
| 25 | + public WindowsDockerClient(@Nonnull Launcher launcher, @CheckForNull Node node, @CheckForNull String toolName) { |
| 26 | + super(launcher, node, toolName); |
| 27 | + this.launcher = launcher; |
| 28 | + this.node = node; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + 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 { |
| 33 | + ArgumentListBuilder argb = new ArgumentListBuilder("docker", "run", "-d", "-t"); |
| 34 | + if (args != null) { |
| 35 | + argb.addTokenized(args); |
| 36 | + } |
| 37 | + |
| 38 | + if (workdir != null) { |
| 39 | + argb.add("-w", workdir); |
| 40 | + } |
| 41 | + for (Map.Entry<String, String> volume : volumes.entrySet()) { |
| 42 | + argb.add("-v", volume.getKey() + ":" + volume.getValue()); |
| 43 | + } |
| 44 | + for (String containerId : volumesFromContainers) { |
| 45 | + argb.add("--volumes-from", containerId); |
| 46 | + } |
| 47 | + for (Map.Entry<String, String> variable : containerEnv.entrySet()) { |
| 48 | + argb.add("-e"); |
| 49 | + argb.addMasked(variable.getKey()+"="+variable.getValue()); |
| 50 | + } |
| 51 | + argb.add(image).add(command); |
| 52 | + |
| 53 | + LaunchResult result = launch(launchEnv, false, null, argb); |
| 54 | + if (result.getStatus() == 0) { |
| 55 | + return result.getOut(); |
| 56 | + } else { |
| 57 | + throw new IOException(String.format("Failed to run image '%s'. Error: %s", image, result.getErr())); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public List<String> listProcess(@Nonnull EnvVars launchEnv, @Nonnull String containerId) throws IOException, InterruptedException { |
| 63 | + LaunchResult result = launch(launchEnv, false, null, "docker", "top", containerId); |
| 64 | + if (result.getStatus() != 0) { |
| 65 | + throw new IOException(String.format("Failed to run top '%s'. Error: %s", containerId, result.getErr())); |
| 66 | + } |
| 67 | + List<String> processes = new ArrayList<>(); |
| 68 | + try (Reader r = new StringReader(result.getOut()); |
| 69 | + BufferedReader in = new BufferedReader(r)) { |
| 70 | + String line; |
| 71 | + in.readLine(); // ps header |
| 72 | + while ((line = in.readLine()) != null) { |
| 73 | + final StringTokenizer stringTokenizer = new StringTokenizer(line, " "); |
| 74 | + if (stringTokenizer.countTokens() < 1) { |
| 75 | + throw new IOException("Unexpected `docker top` output : "+line); |
| 76 | + } |
| 77 | + |
| 78 | + processes.add(stringTokenizer.nextToken()); // COMMAND |
| 79 | + } |
| 80 | + } |
| 81 | + return processes; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Optional<String> getContainerIdIfContainerized() throws IOException, InterruptedException { |
| 86 | + if (node == null || |
| 87 | + launch(new EnvVars(), true, null, "sc.exe", "query", "cexecsvc").getStatus() != 0) { |
| 88 | + return Optional.absent(); |
| 89 | + } |
| 90 | + |
| 91 | + LaunchResult getComputerName = launch(new EnvVars(), true, null, "hostname"); |
| 92 | + if(getComputerName.getStatus() != 0) { |
| 93 | + throw new IOException("Failed to get hostname."); |
| 94 | + } |
| 95 | + |
| 96 | + String shortID = getComputerName.getOut().toLowerCase(); |
| 97 | + LaunchResult getLongIdResult = launch(new EnvVars(), true, null, "docker", "inspect", shortID, "--format={{.Id}}"); |
| 98 | + if(getLongIdResult.getStatus() != 0) { |
| 99 | + LOGGER.log(Level.INFO, "Running inside of a container but cannot determine container ID from current environment."); |
| 100 | + return Optional.absent(); |
| 101 | + } |
| 102 | + |
| 103 | + return Optional.of(getLongIdResult.getOut()); |
| 104 | + } |
| 105 | + |
| 106 | + private LaunchResult launch(@Nonnull EnvVars env, boolean quiet, FilePath workDir, String... args) throws IOException, InterruptedException { |
| 107 | + return launch(env, quiet, workDir, new ArgumentListBuilder(args)); |
| 108 | + } |
| 109 | + private LaunchResult launch(@CheckForNull @Nonnull EnvVars env, boolean quiet, FilePath workDir, ArgumentListBuilder argb) throws IOException, InterruptedException { |
| 110 | + if (LOGGER.isLoggable(Level.FINE)) { |
| 111 | + LOGGER.log(Level.FINE, "Executing command \"{0}\"", argb); |
| 112 | + } |
| 113 | + |
| 114 | + Launcher.ProcStarter procStarter = launcher.launch(); |
| 115 | + if (workDir != null) { |
| 116 | + procStarter.pwd(workDir); |
| 117 | + } |
| 118 | + |
| 119 | + LaunchResult result = new LaunchResult(); |
| 120 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 121 | + ByteArrayOutputStream err = new ByteArrayOutputStream(); |
| 122 | + result.setStatus(procStarter.quiet(quiet).cmds(argb).envs(env).stdout(out).stderr(err).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener())); |
| 123 | + final String charsetName = Charset.defaultCharset().name(); |
| 124 | + result.setOut(out.toString(charsetName)); |
| 125 | + result.setErr(err.toString(charsetName)); |
| 126 | + |
| 127 | + return result; |
| 128 | + } |
| 129 | +} |
0 commit comments