/** * Copies a {@link ProcStarter}. */ @Nonnull public ProcStarter copy() { ProcStarter rhs = new ProcStarter().cmds(commands).pwd(pwd).masks(masks).stdin(stdin).stdout(stdout).stderr(stderr).envs(envs).quiet(quiet); rhs.stdoutListener = stdoutListener; rhs.reverseStdin = this.reverseStdin; rhs.reverseStderr = this.reverseStderr; rhs.reverseStdout = this.reverseStdout; return rhs; } }
public RemoteProcess call() throws IOException { final Channel channel = getOpenChannelOrFail(); Launcher.ProcStarter ps = new LocalLauncher(listener).launch(); ps.cmds(cmd).masks(masks).envs(env).stdin(in).stderr(err).quiet(quiet); if (stdoutListener != null) { ps.stdout(stdoutListener.getLogger());
@Override public void kill(Map<String,String> modelEnvVars) throws IOException, InterruptedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); String executable = getExecutable(); if (getInner().launch().cmds(executable, "exec", container, "ps", "-A", "-o", "pid,command", "e").stdout(baos).quiet(true).start().joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, listener) != 0) { throw new IOException("failed to run ps"); List<String> cmds = new ArrayList<>(Arrays.asList(executable, "exec", container, "kill")); cmds.addAll(pids); if (getInner().launch().cmds(cmds).quiet(true).start().joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, listener) != 0) { throw new IOException("failed to run kill");
Launcher.ProcStarter ps = launcher.launch().cmds(Arrays.asList(command)).envs(envVars).pwd(filePath).quiet(true).stdout(bos).stderr(bos); proc = ps.start();
Launcher.ProcStarter ps = launcher.launch().cmds(Arrays.asList(command)).envs(envVars).pwd(filePath).quiet(true).stdout(outBuf).stderr(errBuf); proc = ps.start(); long reCheckSleep = 250;
.envs(getEnvVars()) .cmds(args) .stdout(out).quiet(!verbose).stderr(listener.getLogger()).join();
public RemoteProcess call() throws IOException { Launcher.ProcStarter ps = new LocalLauncher(listener).launch(); ps.cmds(cmd).masks(masks).envs(env).stdin(in).stdout(out).stderr(err).quiet(quiet); if(workDir!=null) ps.pwd(workDir); if (reverseStdin) ps.writeStdin();
@Override public Proc launch(ProcStarter starter) throws IOException { LOGGER.log(Level.FINEST, "Launch proc with environment: {0}", Arrays.toString(starter.envs())); String[] envVars = starter.envs(); if (node != null) { // It seems this is possible despite the method javadoc saying it is non-null final Computer computer = node.toComputer(); if (computer != null) { List<String> resultEnvVar = new ArrayList<>(); try { EnvVars environment = computer.getEnvironment(); String[] envs = starter.envs(); for (String keyValue : envs) { String[] split = keyValue.split("=", 2); if (!split[1].equals(environment.get(split[0]))) { // Only keep environment variables that differ from Computer's environment resultEnvVar.add(keyValue); } } envVars = resultEnvVar.toArray(new String[resultEnvVar.size()]); } catch (InterruptedException e) { throw new IOException("Unable to retrieve environment variables", e); } } } return doLaunch(starter.quiet(), envVars, starter.stdout(), starter.pwd(), starter.masks(), getCommands(starter)); }
public void kill(String container) throws IOException, InterruptedException { ArgumentListBuilder args = dockerCommand() .add("kill", container); listener.getLogger().println("Stopping Docker container after build completion"); OutputStream out = verbose ? listener.getLogger() : new ByteArrayOutputStream(); OutputStream err = verbose ? listener.getLogger() : new ByteArrayOutputStream(); int status = launcher.launch() .envs(getEnvVars()) .cmds(args) .stdout(out).stderr(err).quiet(!verbose).join(); if (status != 0) throw new RuntimeException("Failed to stop docker container "+container); args = new ArgumentListBuilder() .add(dockerExecutable) .add("rm", "--force", container); status = launcher.launch() .envs(getEnvVars()) .cmds(args) .stdout(out).stderr(err).quiet(!verbose).join(); if (status != 0) listener.getLogger().println("Failed to remove docker container "+container); }
public EnvVars getEnv(String container, Launcher launcher) throws IOException, InterruptedException { final ArgumentListBuilder args = dockerCommand() .add("exec") .add("--tty") .add(container) .add("env"); final ByteArrayOutputStream out = new ByteArrayOutputStream(); int status = launcher.launch() .envs(getEnvVars()) .cmds(args) .stdout(out).quiet(!verbose).stderr(listener.getLogger()).join(); if (status != 0) { throw new RuntimeException("Failed to retrieve container's environment"); } EnvVars env = new EnvVars(); LineIterator it = new LineIterator(new StringReader(out.toString())); while (it.hasNext()) { env.addLine(it.nextLine()); } return env; }
private LaunchResult launch(@CheckForNull @Nonnull EnvVars launchEnv, boolean quiet, FilePath pwd, @Nonnull ArgumentListBuilder args) throws IOException, InterruptedException { // Prepend the docker command args.prepend(DockerTool.getExecutable(toolName, node, launcher.getListener(), launchEnv)); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "Executing docker command {0}", args.toString()); } Launcher.ProcStarter procStarter = launcher.launch(); if (pwd != null) { procStarter.pwd(pwd); } LaunchResult result = new LaunchResult(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); result.setStatus(procStarter.quiet(quiet).cmds(args).envs(launchEnv).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; }
private ProcReturn execCommand(boolean quiet, String... cmd) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); Launcher launcher = decorator .decorate(new DummyLauncher(new StreamTaskListener(new TeeOutputStream(out, System.out))), null); ContainerExecProc proc = (ContainerExecProc) launcher .launch(launcher.new ProcStarter().pwd("/tmp").cmds(cmd).quiet(quiet)); // wait for proc to finish (shouldn't take long) while (proc.isAlive()) { Thread.sleep(100); } assertFalse("proc is alive", proc.isAlive()); int exitCode = proc.joinWithTimeout(10, TimeUnit.SECONDS, StreamTaskListener.fromStderr()); return new ProcReturn(proc, exitCode, out.toString()); }
/** * Who is executing this {@link DockerClient} instance. * * @return a {@link String} containing the <strong>uid:gid</strong>. */ public String whoAmI() throws IOException, InterruptedException { ByteArrayOutputStream userId = new ByteArrayOutputStream(); launcher.launch().cmds("id", "-u").quiet(true).stdout(userId).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener()); ByteArrayOutputStream groupId = new ByteArrayOutputStream(); launcher.launch().cmds("id", "-g").quiet(true).stdout(groupId).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener()); final String charsetName = Charset.defaultCharset().name(); return String.format("%s:%s", userId.toString(charsetName).trim(), groupId.toString(charsetName).trim()); }
public boolean hasImage(String image) throws IOException, InterruptedException { ArgumentListBuilder args = dockerCommand() .add("inspect", image); OutputStream out = verbose ? listener.getLogger() : new ByteArrayOutputStream(); OutputStream err = verbose ? listener.getLogger() : new ByteArrayOutputStream(); int status = launcher.launch() .envs(getEnvVars()) .cmds(args) .stdout(out).stderr(err).quiet(!verbose).join(); return status == 0; }
private String whoAmI(Launcher launcher) throws IOException, InterruptedException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); launcher.launch().cmds("id", "-u").stdout(bos).quiet(true).join(); String uid = bos.toString().trim(); String gid = group; if (isEmpty(group)) { ByteArrayOutputStream bos2 = new ByteArrayOutputStream(); launcher.launch().cmds("id", "-g").stdout(bos2).quiet(true).join(); gid = bos2.toString().trim(); } return uid+":"+gid; }
private Launcher.ProcStarter launchDockerCLI(Launcher launcher, ArgumentListBuilder args) { prependArgs(args); return launcher.launch() .envs(dockerHost.getEnvironment()) .cmds(args) .quiet(!verbose); }
private Launcher.ProcStarter launchDockerCLI(Launcher launcher, ArgumentListBuilder args) { prependArgs(args); return launcher.launch() .envs(dockerHost.getEnvironment()) .cmds(args) .quiet(!verbose); }
/** * Copies a {@link ProcStarter}. */ @Nonnull public ProcStarter copy() { ProcStarter rhs = new ProcStarter().cmds(commands).pwd(pwd).masks(masks).stdin(stdin).stdout(stdout).stderr(stderr).envs(envs).quiet(quiet); rhs.reverseStdin = this.reverseStdin; rhs.reverseStderr = this.reverseStderr; rhs.reverseStdout = this.reverseStdout; return rhs; } }