/** * Method copies the slave jar to the remote system using scp. * * @param listener The listener. * @param workingDirectory The directory into which the slave jar will be copied. * @throws IOException If something goes wrong. * @throws InterruptedException If something goes wrong. */ private void copySlaveJarUsingSCP(TaskListener listener, String workingDirectory) throws IOException, InterruptedException { SCPClient scp = new SCPClient(connection); try { // check if the working directory exists if (connection.exec("test -d " + workingDirectory, listener.getLogger()) != 0) { listener.getLogger().println("Remote filesystem doesn't exist"); // working directory doesn't exist, lets make it. if (connection.exec("mkdir -p " + workingDirectory, listener.getLogger()) != 0) { listener.getLogger().println("Failed to create " + workingDirectory); } } // delete the slave jar as we do with SFTP connection.exec("rm " + workingDirectory + "/slave.jar", new NullStream()); // SCP it to the slave. hudson.Util.ByteArrayOutputStream2 doesn't work for this. It pads the byte array. listener.getLogger().println("Copying slave jar"); scp.put(new Slave.JnlpJar("slave.jar").readFully(), "slave.jar", workingDirectory, "0644"); } catch (IOException e) { throw new IOException2("Error copying slave jar", e); } }
protected Slave getFakeSlave(JenkinsRule r) throws Exception { PretendSlave slave = r.createPretendSlave(p -> { if (p.cmds().get(0).equals(KUBECTL_BINARY)) { String[] maskedCmd = getMaskedCmd(p.cmds(), p.masks()); PrintStream ps = new PrintStream(p.stdout()); ps.println("Call stubbed for: " + String.join(", ", maskedCmd)); return new FakeLauncher.FinishedProc(0); } return r.createLocalLauncher().launch(p); }); slave.setLabelString("mocked-kubectl"); return slave; }
/** * Akin to {@link JenkinsRule#createSlave(String, String, EnvVars)} but allows {@link Computer#getEnvironment} to be controlled rather than directly modifying launchers. * @param env variables to override in {@link Computer#getEnvironment}; null values will get unset even if defined in the test environment * @see <a href="https://github.com/jenkinsci/jenkins/pull/1553/files#r23784822">explanation in core PR 1553</a> */ public static Slave createSpecialEnvSlave(JenkinsRule rule, String nodeName, @CheckForNull String labels, Map<String,String> env) throws Exception { @SuppressWarnings("deprecation") // keep consistency with original signature rather than force the caller to pass in a TemporaryFolder rule File remoteFS = rule.createTmpDir(); SpecialEnvSlave slave = new SpecialEnvSlave(remoteFS, rule.createComputerLauncher(/* yes null */null), nodeName, labels != null ? labels : "", env); rule.jenkins.addNode(slave); return slave; }
public boolean executeCheck(Node item) { try { LOG.log(Level.INFO, "slave " + item.getDisplayName() + " service " + ( (Slave) item).getComputer().getLauncher().toString()); return ((Slave) item).getComputer().getLauncher().toString().contains("ManagedWindowsServiceLauncher"); } catch (NullPointerException npe) { return false; } }
@Override public void afterContainerCreate(DockerClient client, String containerId) throws IOException { // upload archive try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); TarArchiveOutputStream tarOut = new TarArchiveOutputStream(byteArrayOutputStream)) { // @see hudson.model.Slave.JnlpJar.getURL() // byte[] slavejar = IOUtils.toByteArray(Jenkins.getInstance().servletContext.getResourceAsStream("/WEB-INF/slave.jar")); // if (isNull(null)) { // // during the development this path doesn't have the files. // slavejar = Files.readAllBytes(Paths.get("./target/jenkins/WEB-INF/slave.jar")); // } byte[] slaveJar = new Slave.JnlpJar("slave.jar").readFully(); TarArchiveEntry entry = new TarArchiveEntry("slave.jar"); entry.setSize(slaveJar.length); entry.setMode(0664); tarOut.putArchiveEntry(entry); tarOut.write(slaveJar); tarOut.closeArchiveEntry(); tarOut.close(); try (InputStream is = new ByteArrayInputStream(byteArrayOutputStream.toByteArray())) { client.copyArchiveToContainerCmd(containerId) .withTarInputStream(is) .withRemotePath("/tmp") .exec(); } } }
@Override public synchronized long check(final DeployNowComputer c) { if (!c.isIdle()) { return 1; } final Jenkins jenkins = Jenkins.getInstance(); if (jenkins != null) { for (Queue.Item t : jenkins.getQueue().getItems()) { if (t.task instanceof DeployNowTask) { return 1; } } } if (!c.offline) { c.offline = true; try { if (jenkins != null) { Slave node = c.getNode(); if (node != null) { jenkins.removeNode(node); } } } catch (IOException e) { // ignore } } return 1; }
/** * Returns the separator for that node -- if possible to be retrieved. * <p> * Guaranteed to always be either "/", "\\" or null. * <p> * The result is cached, so that repeated lookups are sped-up and * random node offlining does not affect the separator calculation as much. * * @param n the node to check. * @return the separator, one of "/", "\\" or null. */ public String getSepFor(Node n) { if (n == null) { return null; } //Check if a value is cached String c = nodeLookup.getIfPresent(n); if (c != null) { return c; } try { //Check if the node is online and we can determine the separator via //a system property c = this.getRawSepFor(n.toComputer()); if (c != null) { return c; } //Check if the remote root path can tell us something if (n instanceof Slave) { c = this.getRawSepFor((Slave)n); if (c != null) { return c; } } //Last fallback, try to determine based on full file path c = this.getRawSepFor(n.getRootPath()); if (c != null) { return c; } } finally { if (c != null) { nodeLookup.put(n, c); } } //If this point is reached, no path could be found return null; }
public FleetNodeComputer(final Slave slave) { super(slave); }
@Test public void testDefaultSlave() throws Exception { Slave slave = createLinuxSlave("default", "", ""); assertFalse(checker.executeCheck(slave)); }
@Test public void testWindowsSlave() throws Exception { Slave slave = createWindowsSlave("default", "", "somelabel"); assertFalse(checker.executeCheck(slave)); }
@Test public void testControlComment() throws Exception { Slave slave = createLinuxSlave("default", "", ""); assertFalse(checker.isIgnored(slave.getNodeDescription())); slave = createLinuxSlave("default", "#lint:ignore:" + checker.getClass().getSimpleName(), ""); assertTrue(checker.isIgnored(slave.getNodeDescription())); }
private Slave createLinuxSlave(String name, String description, String label) throws Exception { return new DumbSlave(name, description, "/wherever", "1", Node.Mode.NORMAL, label, new JNLPLauncher(), RetentionStrategy.NOOP, Collections.<NodeProperty<?>>emptyList()); }
private Slave createWindowsSlave(String name, String description, String label) throws Exception { return new DumbSlave(name, description, "/wherever", "1", Node.Mode.NORMAL, label, new ManagedWindowsServiceLauncher("user", "pass"), RetentionStrategy.NOOP, Collections.<NodeProperty<?>>emptyList()); }
@Test public void testDefaultSlave() throws Exception { Slave slave = createSlave("default", "", ""); assertFalse(checker.executeCheck(slave)); }
@Test public void testControlComment() throws Exception { Slave slave = createSlave("default", "", ""); assertFalse(checker.isIgnored(slave.getNodeDescription())); slave = createSlave("default", "#lint:ignore:" + checker.getClass().getSimpleName(), ""); assertTrue(checker.isIgnored(slave.getNodeDescription())); }
private Slave createSlave(String name, String description, String label) throws Exception { return new DumbSlave(name, description, "/wherever", "1", Node.Mode.NORMAL, label, new JNLPLauncher(), RetentionStrategy.NOOP, Collections.<NodeProperty<?>>emptyList()); }
@Test public void testDefaultSlave() throws Exception { Slave slave = createSlave("default", "", ""); assertTrue(checker.executeCheck(slave)); }
@Test public void testSlaveLabel() throws Exception { Slave slave = createSlave("default", "", "somelabel"); assertFalse(checker.executeCheck(slave)); }
@Test public void testDefaultSlave() throws Exception { Slave slave = createSlave("default", ""); assertTrue(checker.executeCheck(slave)); }
@Test public void testSlaveDescription() throws Exception { Slave slave = createSlave("default", "somedescription"); assertFalse(checker.executeCheck(slave)); }
@Test public void testControlComment() throws Exception { Slave slave = createSlave("default", ""); assertFalse(checker.isIgnored(slave.getNodeDescription())); slave = createSlave("default", "#lint:ignore:" + checker.getClass().getSimpleName()); assertTrue(checker.isIgnored(slave.getNodeDescription())); }
private Slave createSlave(String name, String description) throws Exception { return new DumbSlave(name, description, "/wherever", "1", Node.Mode.NORMAL, null, new JNLPLauncher(), RetentionStrategy.NOOP, Collections.<NodeProperty<?>>emptyList()); }
private String getRawSepFor(Slave sl) { if (sl == null) { return null; } return getRawSepFor(sl.getRemoteFS()); }
public VirtualMachineSlaveComputer(Slave slave) { super(slave); }
public MesosComputer(Slave slave) { super(slave); }