decorator = new ContainerExecDecorator(); decorator.setClient(client); decorator.setPodName(nodeContext.getPodName()); decorator.setContainerName(containerName); decorator.setNamespace(nodeContext.getNamespace()); decorator.setEnvironmentExpander(env); decorator.setWs(getContext().get(FilePath.class)); decorator.setGlobalVars(globalVars); decorator.setRunContextEnvVars(rcEnvVars); decorator.setShell(shell); getContext().newBodyInvoker() .withContext(BodyInvoker
@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)); }
int p = readPidFromPsCommand(commands); watch = execable.exec(getShell()); } catch (KubernetesClientException e) { if (e.getCause() instanceof InterruptedException) {
@Test @Issue("JENKINS-46719") public void testContainerDoesNotExist() throws Exception { decorator = new ContainerExecDecorator(client, pod.getMetadata().getName(), "doesNotExist", client.getNamespace()); exception.expect(IOException.class); exception.expectMessage(containsString("container [doesNotExist] does not exist in pod [")); execCommand(false, "nohup", "sh", "-c", "sleep 5; return 127"); }
private synchronized int readPidFromPidFile(String... commands) throws IOException, InterruptedException { int pid = -1; String pidFilePath = readPidFile(commands); if (pidFilePath == null) { return pid; } FilePath pidFile = ws.child(pidFilePath); for (int w = 0; w < 10 && !pidFile.exists(); w++) { try { wait(1000); } catch (InterruptedException e) { break; } } if (pidFile.exists()) { try { pid = Integer.parseInt(pidFile.readToString().trim()); } catch (NumberFormatException x) { throw new IOException("corrupted content in " + pidFile + ": " + x, x); } } return pid; }
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()); }
@Before public void configureCloud() throws Exception { client = setupCloud(this).connect(); deletePods(client, getLabels(this), false); String image = "busybox"; Container c = new ContainerBuilder().withName(image).withImagePullPolicy("IfNotPresent").withImage(image) .withCommand("cat").withTty(true).build(); String podName = "test-command-execution-" + RandomStringUtils.random(5, "bcdfghjklmnpqrstvwxz0123456789"); pod = client.pods().create(new PodBuilder().withNewMetadata().withName(podName).withLabels(getLabels(this)) .endMetadata().withNewSpec().withContainers(c).endSpec().build()); System.out.println("Created pod: " + pod.getMetadata().getName()); decorator = new ContainerExecDecorator(client, pod.getMetadata().getName(), image, client.getNamespace()); }
@Test public void commandsEscaping() { ProcStarter procStarter = new DummyLauncher(null).launch(); procStarter = procStarter.cmds("$$$$", "$$?"); String[] commands = ContainerExecDecorator.getCommands(procStarter); assertArrayEquals(new String[] { "\\$\\$", "\\$?" }, commands); }