hudson.slaves.SlaveComputer Java Examples

The following examples show how to use hudson.slaves.SlaveComputer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ECSLauncher.java    From amazon-ecs-plugin with MIT License 6 votes vote down vote up
private Collection<String> getDockerRunCommand(ECSSlave slave, String jenkinsUrl) {
    Collection<String> command = new ArrayList<>();
    command.add("-url");
    command.add(jenkinsUrl);
    if (StringUtils.isNotBlank(tunnel)) {
        command.add("-tunnel");
        command.add(tunnel);
    }
    SlaveComputer agent = slave.getComputer();
    if (agent == null) {
        throw new IllegalStateException("Node was deleted, computer is null");
    }
    command.add(agent.getJnlpMac());
    command.add(agent.getName());
    return command;
}
 
Example #2
Source File: ZAProxy.java    From zaproxy-plugin with MIT License 6 votes vote down vote up
/**
 * Return the ZAProxy program name with separator prefix (\zap.bat or /zap.sh) depending of the build node and the OS.
 * 
 * @param build
 * @return the ZAProxy program name with separator prefix (\zap.bat or /zap.sh)
 * @throws IOException
 * @throws InterruptedException
 */
private String getZAPProgramNameWithSeparator(AbstractBuild<?, ?> build) throws IOException, InterruptedException {
	Node node = build.getBuiltOn();
	String zapProgramName = "";
	
	// Append zap program following Master/Slave and Windows/Unix
	if( "".equals(node.getNodeName())) { // Master
		if( File.pathSeparatorChar == ':' ) { // UNIX
			zapProgramName = "/" + ZAP_PROG_NAME_SH;
		} else { // Windows (pathSeparatorChar == ';')
			zapProgramName = "\\" + ZAP_PROG_NAME_BAT;
		}
	} 
	else { // Slave
		if( "Unix".equals(((SlaveComputer)node.toComputer()).getOSDescription()) ) {
			zapProgramName = "/" + ZAP_PROG_NAME_SH;
		} else {
			zapProgramName = "\\" + ZAP_PROG_NAME_BAT;
		}
	}
	return zapProgramName;
}
 
Example #3
Source File: KubernetesPipelineTest.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Test
@Issue("JENKINS-58602")
public void jenkinsSecretHidden() throws Exception {
    SemaphoreStep.waitForStart("pod/1", b);
    Optional<SlaveComputer> scOptional = Arrays.stream(r.jenkins.getComputers())
            .filter(SlaveComputer.class::isInstance)
            .map(SlaveComputer.class::cast)
            .findAny();
    assertTrue(scOptional.isPresent());
    String jnlpMac = scOptional.get().getJnlpMac();
    SemaphoreStep.success("pod/1", b);
    r.assertBuildStatusSuccess(r.waitForCompletion(b));
    r.assertLogNotContains(jnlpMac, b);
}
 
Example #4
Source File: TestableDockerContainerWatchdog.java    From docker-plugin with MIT License 5 votes vote down vote up
public static DockerTransientNode createMockedDockerTransientNode(String containerId, String nodeName, DockerCloud cloud, boolean offline) {
    DockerTransientNode result = Mockito.mock(DockerTransientNode.class);
    Mockito.when(result.getContainerId()).thenReturn(containerId);
    Mockito.when(result.getNodeName()).thenReturn(nodeName);
    Mockito.when(result.getCloud()).thenReturn(cloud);
    SlaveComputer sc = Mockito.mock(SlaveComputer.class);
    Mockito.when(sc.isOffline()).thenReturn(offline);
    Mockito.when(result.getComputer()).thenReturn(sc);
    return result;
}
 
Example #5
Source File: DockerDelegatingComputerLauncher.java    From docker-plugin with MIT License 5 votes vote down vote up
@Override
public void launch(SlaveComputer computer, TaskListener listener) throws IOException, InterruptedException {
    try(final DockerClient client = api.getClient()) {
        client.inspectContainerCmd(containerId).exec();
    } catch (NotFoundException e) {
        // Container has been removed
        Queue.withLock(() -> {
            DockerTransientNode node = (DockerTransientNode) computer.getNode();
            node.terminate(listener);
        });
        return;
    }
    super.launch(computer, listener);
}
 
Example #6
Source File: DockerComputerSingleJNLPLauncher.java    From yet-another-docker-plugin with MIT License 5 votes vote down vote up
@Override
public void launch(SlaveComputer computer, TaskListener listener) {
    listener.getLogger().println("Launching " + computer.getDisplayName());
    try {
        if (!(computer instanceof DockerComputerSingle)) {
            throw new IllegalStateException(computer.getName() + " not instance of DockerComputerSingle");
        }
        provisionWithWait((DockerComputerSingle) computer, listener);
    } catch (Throwable e) {
        LOG.error("Can't launch ", e);
        listener.error("Can't launch " + e.getMessage());
        Throwables.propagate(e);
    }
}
 
Example #7
Source File: DockerSwarmComputerLauncher.java    From docker-swarm-plugin with MIT License 5 votes vote down vote up
@Override
public void launch(final SlaveComputer computer, final TaskListener listener) {
    if (computer instanceof DockerSwarmComputer) {
        try {
            launch((DockerSwarmComputer) computer, listener);
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "Failed to launch", e);
        }
    } else {
        throw new IllegalArgumentException("This launcher only can handle DockerSwarmComputer");
    }
}
 
Example #8
Source File: KubernetesSlave.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
private String podAsYaml() {
    String x = Serialization.asYaml(pod);
    Computer computer = toComputer();
    if (computer instanceof SlaveComputer) {
        SlaveComputer sc = (SlaveComputer) computer;
        return x.replaceAll(sc.getJnlpMac(),"********");
    }
    return x;
}
 
Example #9
Source File: KubernetesSlave.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@CheckForNull
@Override
public FilePath getRootPath() {
    final SlaveComputer computer = getComputer();
    if (computer == null) {
        // if computer is null then channel is null and thus we were going to return null anyway
        return null;
    } else {
        return createPath(StringUtils.defaultString(computer.getAbsoluteRemoteFs(), getRemoteFS()));
    }
}
 
Example #10
Source File: KafkaComputerLauncher.java    From remoting-kafka-plugin with MIT License 5 votes vote down vote up
@Override
public synchronized void launch(SlaveComputer computer, final TaskListener listener) {
    if (computer instanceof KafkaCloudComputer) {
        launchKubernetesPod((KafkaCloudComputer) computer);
    }
    launchAgent(computer, listener);
}
 
Example #11
Source File: KafkaComputerLauncher.java    From remoting-kafka-plugin with MIT License 5 votes vote down vote up
@Override
public void afterDisconnect(SlaveComputer computer, final TaskListener listener) {
    ExecutorService srv = launcherExecutorService;
    if (srv != null) {
        // If the service is still running, shut it down and interrupt the operations if any
        srv.shutdown();
    }
}
 
Example #12
Source File: KafkaComputerLauncher.java    From remoting-kafka-plugin with MIT License 5 votes vote down vote up
private CommandTransport makeTransport(SlaveComputer computer) throws RemotingKafkaException {
    String nodeName = computer.getName();
    URL jenkinsURL = retrieveJenkinsURL(computer);
    String kafkaURL = getKafkaURL();
    String topic = KafkaConfigs.getConnectionTopic(nodeName, jenkinsURL);
    GlobalKafkaConfiguration kafkaConfig = GlobalKafkaConfiguration.get();
    Properties securityProps = null;
    if (kafkaConfig.getEnableSSL()) {
        securityProps = new SecurityPropertiesBuilder()
                .withSSLTruststoreLocation(kafkaConfig.getSSLTruststoreLocation())
                .withSSLTruststorePassword(kafkaConfig.getSSLTruststorePassword())
                .withSSLKeystoreLocation(kafkaConfig.getSSLKeystoreLocation())
                .withSSLKeystorePassword(kafkaConfig.getSSLKeystorePassword())
                .withSSLKeyPassword(kafkaConfig.getSSLKeyPassword())
                .withSASLJassConfig(kafkaConfig.getKafkaUsername(), kafkaConfig.getKafkaPassword())
                .withSecurityProtocol(SecurityProtocol.SASL_SSL)
                .withSASLMechanism("PLAIN")
                .build();
    }
    KafkaClassicCommandTransport transport = new KafkaTransportBuilder()
            .withRemoteCapability(new Capability())
            .withProducerKey(KafkaConfigs.getMasterAgentCommandKey(nodeName, jenkinsURL))
            .withConsumerKey(KafkaConfigs.getAgentMasterCommandKey(nodeName, jenkinsURL))
            .withProducerTopic(topic)
            .withConsumerTopic(topic)
            .withProducerPartition(KafkaConfigs.MASTER_AGENT_CMD_PARTITION)
            .withConsumerPartition(KafkaConfigs.AGENT_MASTER_CMD_PARTITION)
            .withProducer(KafkaUtils.createByteProducer(kafkaURL, securityProps))
            .withConsumer(KafkaUtils.createByteConsumer(kafkaURL,
                    KafkaConfigs.getConsumerGroupID(nodeName, jenkinsURL), securityProps))
            .withPollTimeout(0)
            .build();
    return transport;
}
 
Example #13
Source File: DockerComputerIOLauncher.java    From yet-another-docker-plugin with MIT License 4 votes vote down vote up
@Override
public void beforeDisconnect(SlaveComputer computer, TaskListener listener) {
}
 
Example #14
Source File: DockerComputerAttachConnector.java    From docker-plugin with MIT License 4 votes vote down vote up
@Override
public void launch(final SlaveComputer computer, TaskListener listener) throws IOException, InterruptedException {
    final PrintStream logger = computer.getListener().getLogger();
    final String jenkinsUrl = Jenkins.getInstance().getRootUrl();
    final String effectiveJavaExe = StringUtils.isNotBlank(javaExeOrNull) ? javaExeOrNull : DEFAULT_JAVA_EXE;
    final String effectiveJvmArgs = StringUtils.isNotBlank(jvmArgsOrEmpty) ? jvmArgsOrEmpty : DEFAULT_JVM_ARGS ;
    final EnvVars knownVariables = calculateVariablesForVariableSubstitution(effectiveJavaExe, effectiveJvmArgs, remoting.getName(), remoteFs, jenkinsUrl);
    final String effectiveEntryPointCmdString = StringUtils.isNotBlank(entryPointCmdOrEmpty) ? entryPointCmdOrEmpty : DEFAULT_ENTRY_POINT_CMD_STRING;
    final String resolvedEntryPointCmdString = Util.replaceMacro(effectiveEntryPointCmdString, knownVariables);
    final String[] resolvedEntryPointCmd = splitAndFilterEmpty(resolvedEntryPointCmdString, "\n");
    logger.println("Connecting to docker container " + containerId + ", running command " + Joiner.on(" ").join(resolvedEntryPointCmd));

    final String execId;
    try(final DockerClient client = api.getClient()) {
        final ExecCreateCmd cmd = client.execCreateCmd(containerId)
                .withAttachStdin(true)
                .withAttachStdout(true)
                .withAttachStderr(true)
                .withTty(false)
                .withCmd(resolvedEntryPointCmd);
        if (StringUtils.isNotBlank(userOrNull)) {
            cmd.withUser(userOrNull);
        }
        final ExecCreateCmdResponse exec = cmd.exec();
        execId = exec.getId();
    }
    final String js = "{ \"Detach\": false, \"Tty\": false }";
    final Socket socket = api.getSocket();
    final OutputStream out = socket.getOutputStream();
    final InputStream in = socket.getInputStream();
    final PrintWriter w = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.US_ASCII));
    w.println("POST /v1.32/exec/" + execId + "/start HTTP/1.1");
    w.println("Host: docker.sock");
    w.println("Content-Type: application/json");
    w.println("Upgrade: tcp");
    w.println("Connection: Upgrade");
    w.println("Content-Length: " + js.length());
    w.println();
    w.println(js);
    w.flush();

    // read HTTP response headers
    String line = readLine(in);
    logger.println(line);
    if (! line.startsWith("HTTP/1.1 101 ")) {   // Switching Protocols
        throw new IOException("Unexpected HTTP response status line " + line);
    }

    // Skip HTTP header
    while ((line = readLine(in)).length() > 0) {
        if (line.length() == 0) break; // end of header
        logger.println(line);
    }

    final InputStream demux = new DockerMultiplexedInputStream(in);

    computer.setChannel(demux, out, listener, new Channel.Listener() {
        @Override
        public void onClosed(Channel channel, IOException cause) {
            // Bye!
        }
    });

}
 
Example #15
Source File: DockerContainerWatchdog.java    From docker-plugin with MIT License 4 votes vote down vote up
private void cleanUpSuperfluousComputer(Map<String, Node> nodeMap, ContainerNodeNameMap csmMerged, Instant snapshotInstant) {
    for (Node node : nodeMap.values()) {
        if (! (node instanceof DockerTransientNode)) {
            // this node does not belong to us
            continue;
        }

        checkForTimeout(snapshotInstant);

        DockerTransientNode dtn = (DockerTransientNode) node;

        /*
         * Important note!
         * 
         * We cannot be sure that DockerTransientNode really knows the right getCloudId() (or even 
         * have the right getCloud() instance). This is due to the fact that the - for example - the 
         * node might be left-over from a previous configuration, which no longer is valid (e.g. the
         * slave was created with a DockerCloud configuration which did not work; that is why the admin
         * deleted that DockerCloud configuration while still the node was running; to clean up the mess,
         * he manually force-removed the containers from the docker instance). 
         * 
         * At the end, this means that we cannot verify strictly that the containerId stored with this
         * node, really exists on (any of the) DockerCloud instances or not. 
         * The only option is that, as long as we see "some container having an id like this" on "some of
         * our DockerCloud instances", then we should be careful with it, and do not delete the node (yet).
         * Even if the containerId originates from a collision, then we would just postpone the deletion
         * of the node up to that point in time, when that container gets deleted from the DockerContainer
         * instance (on the next recurrence of this watchdog, we then would detect that situation and the
         * node gets deleted automatically, as no container with the appropriate identifier could be detected
         * anymore). 
         * 
         * It is to be hoped that collision on container identifiers are happening rarely enough to such that
         * we do not run into troubles here. Otherwise, still a larger set of broken nodes could pile up...
         */

        boolean seenOnDockerInstance = csmMerged.isContainerIdRegistered(dtn.getContainerId());
        if (seenOnDockerInstance) {
            // the node and the container still might have a proper mapping => ignore
            continue;
        }

        SlaveComputer computer = dtn.getComputer();
        if (computer == null) {
            // Probably the node is being closed down right now, so we shouldn't touch it.
            continue;
        }

        if (!computer.isOffline()) {
            // the node is still running; we should not touch it.
            continue;
        }

        // the container is already gone for the node, but the node did not notice it yet properly
        LOGGER.info("{} has container ID {}, but the container does not exist in any docker cloud. Will remove node.", dtn, dtn.getContainerId());

        try {
            removeNode(dtn);
            executionStatistics.addNodeRemoved();
        } catch (IOException e) {
            LOGGER.warn("Failed to remove orphaned node {}", dtn.toString(), e);
            executionStatistics.addNodeRemovedFailed();
        }
    }
}
 
Example #16
Source File: ECSService.java    From amazon-ecs-plugin with MIT License 4 votes vote down vote up
RunTaskResult runEcsTask(final ECSSlave agent, final ECSTaskTemplate template, String clusterArn, Collection<String> command, TaskDefinition taskDefinition) throws IOException, AbortException {
    AmazonECS client = clientSupplier.get();
    agent.setTaskDefinitonArn(taskDefinition.getTaskDefinitionArn());

    SlaveComputer agentComputer = agent.getComputer();

    if (agentComputer == null) {
        throw new IllegalStateException("Node was deleted, computer is null");
    }

    KeyValuePair envNodeName = new KeyValuePair();
    envNodeName.setName("SLAVE_NODE_NAME");
    envNodeName.setValue(agentComputer.getName());

    KeyValuePair envNodeSecret = new KeyValuePair();
    envNodeSecret.setName("SLAVE_NODE_SECRET");
    envNodeSecret.setValue(agentComputer.getJnlpMac());

    // by convention, we assume the jenkins agent container is the first container in the task definition. ECS requires
    // all task definitions to contain at least one container, and all containers to have a name, so we do not need
    // to null- or bounds-check for the presence of a container definition.
    String agentContainerName = taskDefinition.getContainerDefinitions().get(0).getName();

    LOGGER.log(Level.FINE, "Found container definition with {0} container(s). Assuming first container is the Jenkins agent: {1}", new Object[]{taskDefinition.getContainerDefinitions().size(), agentContainerName});

    RunTaskRequest req = new RunTaskRequest()
            .withTaskDefinition(taskDefinition.getTaskDefinitionArn())
            .withLaunchType(LaunchType.fromValue(template.getLaunchType()))
            .withOverrides(new TaskOverride()
                    .withContainerOverrides(new ContainerOverride()
                            .withName(agentContainerName)
                            .withCommand(command)
                            .withEnvironment(envNodeName)
                            .withEnvironment(envNodeSecret)))
            .withPlacementStrategy(template.getPlacementStrategyEntries())
            .withCluster(clusterArn);

    if (template.getLaunchType() != null && template.getLaunchType().equals("FARGATE")) {
        req.withPlatformVersion(template.getPlatformVersion());
    }

    if (taskDefinition.getNetworkMode() != null && taskDefinition.getNetworkMode().equals("awsvpc")) {
        AwsVpcConfiguration awsVpcConfiguration = new AwsVpcConfiguration();
        awsVpcConfiguration.setAssignPublicIp(template.getAssignPublicIp() ? "ENABLED" : "DISABLED");
        awsVpcConfiguration.setSecurityGroups(Arrays.asList(template.getSecurityGroups().split(",")));
        awsVpcConfiguration.setSubnets(Arrays.asList(template.getSubnets().split(",")));

        NetworkConfiguration networkConfiguration = new NetworkConfiguration();
        networkConfiguration.withAwsvpcConfiguration(awsVpcConfiguration);

        req.withNetworkConfiguration(networkConfiguration);
    }
    return client.runTask(req);
}
 
Example #17
Source File: DockerComputerJNLPLauncher.java    From yet-another-docker-plugin with MIT License 4 votes vote down vote up
@Override
public void beforeDisconnect(SlaveComputer computer, TaskListener listener) {
}
 
Example #18
Source File: DockerComputerJNLPLauncher.java    From yet-another-docker-plugin with MIT License 4 votes vote down vote up
@Override
public void afterDisconnect(SlaveComputer computer, TaskListener listener) {
}
 
Example #19
Source File: IdleRetentionStrategy.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Will be called under {@link hudson.model.Queue#withLock(Runnable)}
 *
 * @param computer computer
 * @return delay in min before next run
 */
@Override
public long check(final SlaveComputer computer) {
    final EC2FleetNodeComputer fc = (EC2FleetNodeComputer) computer;
    final EC2FleetCloud cloud = fc.getCloud();

    LOGGER.log(Level.INFO, "Check if node idle " + computer.getName());

    // in some multi-thread edge cases cloud could be null for some time, just be ok with that
    if (cloud == null) {
        LOGGER.warning("Edge case cloud is null for computer " + fc.getDisplayName()
                + " should be autofixed in a few minutes, if no please create issue for plugin");
        return RE_CHECK_IN_MINUTE;
    }

    // Ensure that the EC2FleetCloud cannot be mutated from under us while
    // we're doing this check
    // Ensure nobody provisions onto this node until we've done
    // checking
    boolean shouldAcceptTasks = fc.isAcceptingTasks();
    boolean justTerminated = false;
    fc.setAcceptingTasks(false);
    try {
        if (fc.isIdle() && isIdleForTooLong(cloud, fc)) {
            // Find instance ID
            Node compNode = fc.getNode();
            if (compNode == null) {
                return 0;
            }

            final String instanceId = compNode.getNodeName();
            if (cloud.scheduleToTerminate(instanceId)) {
                // Instance successfully terminated, so no longer accept tasks
                shouldAcceptTasks = false;
                justTerminated = true;
            }
        }

        if (cloud.isAlwaysReconnect() && !justTerminated && fc.isOffline() && !fc.isConnecting() && fc.isLaunchSupported()) {
            LOGGER.log(Level.INFO, "Reconnecting to instance: " + fc.getDisplayName());
            fc.tryReconnect();
        }
    } finally {
        fc.setAcceptingTasks(shouldAcceptTasks);
    }

    return RE_CHECK_IN_MINUTE;
}
 
Example #20
Source File: CucumberTestReportPublisher.java    From bootstraped-multi-test-results-report with MIT License 4 votes vote down vote up
public boolean generateReport(Run<?, ?> build, FilePath workspace, TaskListener listener)
    throws IOException, InterruptedException {

    listener.getLogger().println("[CucumberReportPublisher] Compiling Cucumber Html Reports ...");

    // source directory (possibly on slave)
    FilePath workspaceJsonReportDirectory;
    if (getReportsDirectory().isEmpty()) {
        workspaceJsonReportDirectory = workspace;
    } else {
        workspaceJsonReportDirectory = new FilePath(workspace, getReportsDirectory());
    }

    // target directory (always on master)
    File targetBuildDirectory = new File(build.getRootDir(), "cucumber-reports-with-handlebars");
    if (!targetBuildDirectory.exists()) {
        if (!targetBuildDirectory.mkdirs()) {
            listener.getLogger().println("target dir was not created !!!");
        }
    }

    if (Computer.currentComputer() instanceof SlaveComputer) {
        listener.getLogger()
            .println("[Cucumber test report builder] Copying JSON files from slave: "
                + workspaceJsonReportDirectory.getRemote() + " to master reports directory: "
                + targetBuildDirectory);
    } else {
        listener.getLogger().println("[Cucumber test report builder] Copying JSON files from: "
            + workspaceJsonReportDirectory.getRemote() + " to reports directory: " + targetBuildDirectory);
    }
    File targetBuildJsonDirectory = new File(targetBuildDirectory.getAbsolutePath() + "/jsonData");
    if (!targetBuildJsonDirectory.exists()) {
        targetBuildJsonDirectory.mkdirs();
    }
    String includePattern = (fileIncludePattern == null || fileIncludePattern.isEmpty())
        ? DEFAULT_FILE_INCLUDE_PATTERN : fileIncludePattern;
    workspaceJsonReportDirectory.copyRecursiveTo(includePattern,
        new FilePath(targetBuildJsonDirectory));

    // generate the reports from the targetBuildDirectory
    Result result = Result.NOT_BUILT;
    String[] jsonReportFiles =
        findFiles(targetBuildJsonDirectory, getFileIncludePattern(),
            getFileExcludePattern(), DEFAULT_FILE_INCLUDE_PATTERN);

    if (jsonReportFiles.length > 0) {
        listener.getLogger()
            .println(String.format("[CucumberReportPublisher] Found %d json files.", jsonReportFiles.length));
        int jsonIndex = 0;
        for (String jsonReportFile : jsonReportFiles) {
            listener.getLogger().println(
                "[Cucumber test report builder] " + jsonIndex + ". Found a json file: " + jsonReportFile);
            jsonIndex++;
        }
        listener.getLogger().println("[Cucumber test report builder] Generating HTML reports");

        try {
            for (String ss : fullPathToFiles(jsonReportFiles, targetBuildJsonDirectory)) {
                listener.getLogger().println("processing: " + ss);
            }
            CucumberReportBuilder rep = new CucumberReportBuilder(
                fullPathToFiles(jsonReportFiles, targetBuildJsonDirectory),
                targetBuildDirectory.getAbsolutePath(), props);
            boolean featuresResult = rep.writeReportsOnDisk();
            if (featuresResult) {
                result = Result.SUCCESS;
            } else {
                result = isMarkAsUnstable() ? Result.UNSTABLE : Result.FAILURE;
            }

            // finally copy to workspace, if needed
            if (isCopyHTMLInWorkspace()) {
                FilePath workspaceCopyDirectory =
                    new FilePath(workspace, "cucumber-reports-with-handlebars");
                if (workspaceCopyDirectory.exists()) {
                    workspaceCopyDirectory.deleteRecursive();
                }
                listener.getLogger().println(
                    "[Cucumber test report builder] Copying report to workspace directory: "
                        + workspaceCopyDirectory.toURI());
                new FilePath(targetBuildDirectory).copyRecursiveTo("**/*.html", workspaceCopyDirectory);
            }

        } catch (Exception e) {
            result = Result.FAILURE;
            listener.getLogger()
                .println("[Cucumber test report builder] there was an error generating the reports: " + e);
            for (StackTraceElement error : e.getStackTrace()) {
                listener.getLogger().println(error);
            }
        }
    } else {
        result = Result.SUCCESS;
        listener.getLogger().println(
            "[Cucumber test report builder] json path for the reports might be wrong, " + targetBuildDirectory);
    }
    build.setResult(result);

    return true;
}
 
Example #21
Source File: DockerComputerIOLauncher.java    From yet-another-docker-plugin with MIT License 4 votes vote down vote up
@Override
public void afterDisconnect(SlaveComputer computer, TaskListener listener) {
}
 
Example #22
Source File: DockerComputerIOLauncher.java    From yet-another-docker-plugin with MIT License 4 votes vote down vote up
@Override
public void launch(SlaveComputer computer, TaskListener listener)
        throws IOException, InterruptedException {
    final PrintStream llog = listener.getLogger();
    DockerComputer dockerComputer;
    if (computer instanceof DockerComputer) {
        dockerComputer = (DockerComputer) computer;
    } else {
        listener.error("Docker JNLP Launcher accepts only DockerComputer.class");
        throw new IllegalArgumentException("Docker JNLP Launcher accepts only DockerComputer.class");
    }
    Objects.requireNonNull(dockerComputer);

    final String containerId = dockerComputer.getContainerId();
    final DockerCloud dockerCloud = dockerComputer.getCloud();
    if (isNull(dockerCloud)) {
        listener.error("Cloud not found for computer " + computer.getName());
        throw new NullPointerException("Cloud not found for computer " + computer.getName());
    }
    final DockerClient client = dockerCloud.getClient();
    final DockerSlave node = dockerComputer.getNode();
    if (isNull(node)) {
        throw new NullPointerException("Node can't be null");
    }
    InspectContainerResponse inspect = client.inspectContainerCmd(containerId).exec();
    if (nonNull(inspect) && nonNull(inspect.getState().getRunning()) &&
            !inspect.getState().getRunning()) {
        throw new IllegalStateException("Container is not running!");
    }

    PipedOutputStream out = new PipedOutputStream();
    PipedInputStream inputStream = new PipedInputStream(out, 4096);

    IOCallback callback = new IOCallback(listener, out);

    PipedInputStream pipedInputStream = new PipedInputStream(4096);
    PipedOutputStream outputStream = new PipedOutputStream(pipedInputStream);
    llog.println("Attaching to container...");

    String java = getJavaPath();

    if (StringUtils.isEmpty(java)) {
        java = "java";
    }

    OsType osType = node.getDockerSlaveTemplate().getOsType();

    ExecCreateCmdResponse cmdResponse = client.execCreateCmd(containerId)
            .withAttachStderr(true)
            .withAttachStdin(true)
            .withAttachStdout(true)
            .withTty(false)
            .withCmd(
                    osType == OsType.WINDOWS ? "cmd" : "/bin/sh",
                    osType == OsType.WINDOWS ? "/c" : "-c",
                    java + " " + getJvmOpts() + " -jar " + SLAVE_JAR)
            .exec();

    client.execStartCmd(cmdResponse.getId())
            .withStdIn(pipedInputStream)
            .exec(callback);

    computer.setChannel(inputStream, outputStream, listener.getLogger(), new Channel.Listener() {
        @Override
        public void onClosed(Channel channel, IOException cause) {
            try {
                callback.close();
            } catch (IOException e) {
                Throwables.propagate(e);
            }
        }
    });
}
 
Example #23
Source File: NoOpDelegatingComputerLauncher.java    From yet-another-docker-plugin with MIT License 4 votes vote down vote up
@Override
public void launch(SlaveComputer computer, TaskListener listener) throws IOException, InterruptedException {
    // noop
}
 
Example #24
Source File: IdleRetentionStrategy.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void start(SlaveComputer c) {
    LOGGER.log(Level.INFO, "Connecting to instance: " + c.getDisplayName());
    c.connect(false);
}
 
Example #25
Source File: EC2FleetAutoResubmitComputerLauncher.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * {@link ComputerLauncher#afterDisconnect(SlaveComputer, TaskListener)}
 * <p>
 * EC2 Fleet plugin overrides this method to detect jobs which were failed because of
 * EC2 instance was terminated/stopped. It could be manual stop or because of Spot marked.
 * In all cases as soon as job aborted because of broken connection and slave is offline
 * it will try to resubmit aborted job back to the queue, so user doesn't need to do that manually
 * and another slave could take it.
 * <p>
 * Implementation details
 * <p>
 * There is no official recommendation about way how to resubmit job according to
 * https://issues.jenkins-ci.org/browse/JENKINS-49707 moreover some of Jenkins code says it impossible.
 * <p>
 * method checks {@link SlaveComputer#getOfflineCause()} for disconnect because of EC2 instance termination
 * it returns
 * <code>
 * result = {OfflineCause$ChannelTermination@13708} "Connection was broken: java.io.IOException:
 * Unexpected termination of the channel\n\tat hudson.remoting.SynchronousCommandTransport$ReaderThread...
 * cause = {IOException@13721} "java.io.IOException: Unexpected termination of the channel"
 * timestamp = 1561067177837
 * </code>
 *
 * @param computer computer
 * @param listener listener
 */
@Override
public void afterDisconnect(final SlaveComputer computer, final TaskListener listener) {
    // according to jenkins docs could be null in edge cases, check ComputerLauncher.afterDisconnect
    if (computer == null) return;

    // in some multi-thread edge cases cloud could be null for some time, just be ok with that
    final EC2FleetCloud cloud = ((EC2FleetNodeComputer) computer).getCloud();
    if (cloud == null) {
        LOGGER.warning("Edge case cloud is null for computer " + computer.getDisplayName()
                + " should be autofixed in a few minutes, if no please create issue for plugin");
        return;
    }

    final boolean unexpectedDisconnect = computer.isOffline() && computer.getOfflineCause() instanceof OfflineCause.ChannelTermination;
    if (!cloud.isDisableTaskResubmit() && unexpectedDisconnect) {
        final List<Executor> executors = computer.getExecutors();
        LOGGER.log(LOG_LEVEL, "Unexpected " + computer.getDisplayName()
                + " termination,  resubmit");

        for (Executor executor : executors) {
            if (executor.getCurrentExecutable() != null) {
                executor.interrupt(Result.ABORTED, new EC2TerminationCause(computer.getDisplayName()));

                final Queue.Executable executable = executor.getCurrentExecutable();
                // if executor is not idle
                if (executable != null) {
                    final SubTask subTask = executable.getParent();
                    final Queue.Task task = subTask.getOwnerTask();

                    List<Action> actions = new ArrayList<>();
                    if (executable instanceof Actionable) {
                        actions = ((Actionable) executable).getActions();
                    }

                    Queue.getInstance().schedule2(task, RESCHEDULE_QUIET_PERIOD_SEC, actions);
                    LOGGER.log(LOG_LEVEL, "Unexpected " + computer.getDisplayName()
                            + " termination, resubmit " + task + " with actions " + actions);
                }
            }
        }
        LOGGER.log(LOG_LEVEL, "Unexpected " + computer.getDisplayName()
                + " termination, resubmit finished");
    } else {
        LOGGER.log(LOG_LEVEL, "Unexpected " + computer.getDisplayName()
                + " termination but resubmit disabled, no actions, disableTaskResubmit: "
                + cloud.isDisableTaskResubmit() + ", offline: " + computer.isOffline()
                + ", offlineCause: " + (computer.getOfflineCause() != null ? computer.getOfflineCause().getClass() : "null"));
    }

    // call parent
    super.afterDisconnect(computer, listener);
}
 
Example #26
Source File: PodTemplateBuilder.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
private Container createContainer(ContainerTemplate containerTemplate, Collection<TemplateEnvVar> globalEnvVars,
        Collection<VolumeMount> volumeMounts) {
    Map<String, EnvVar> envVarsMap = new HashMap<>();
    String workingDir = substituteEnv(containerTemplate.getWorkingDir());
    if (JNLP_NAME.equals(containerTemplate.getName())) {
        envVarsMap.putAll(jnlpEnvVars(workingDir));
    }
    envVarsMap.putAll(defaultEnvVars(globalEnvVars));

    if (containerTemplate.getEnvVars() != null) {
        containerTemplate.getEnvVars().forEach(item ->
                envVarsMap.put(item.getKey(), item.buildEnvVar())
        );
    }

    EnvVar[] envVars = envVarsMap.values().stream().toArray(EnvVar[]::new);

    String cmd = containerTemplate.getArgs();
    if (slave != null && cmd != null) {
        SlaveComputer computer = slave.getComputer();
        if (computer != null) {
            cmd = cmd.replaceAll(JNLPMAC_REF, computer.getJnlpMac()) //
                    .replaceAll(NAME_REF, computer.getName());
        }
    }
    List<String> arguments = Strings.isNullOrEmpty(containerTemplate.getArgs()) ? Collections.emptyList()
            : parseDockerCommand(cmd);

    ContainerPort[] ports = containerTemplate.getPorts().stream().map(entry -> entry.toPort()).toArray(size -> new ContainerPort[size]);


    List<VolumeMount> containerMounts = getContainerVolumeMounts(volumeMounts, workingDir);

    ContainerLivenessProbe clp = containerTemplate.getLivenessProbe();
    Probe livenessProbe = null;
    if (clp != null && parseLivenessProbe(clp.getExecArgs()) != null) {
        livenessProbe = new ProbeBuilder()
                .withExec(new ExecAction(parseLivenessProbe(clp.getExecArgs())))
                .withInitialDelaySeconds(clp.getInitialDelaySeconds())
                .withTimeoutSeconds(clp.getTimeoutSeconds())
                .withFailureThreshold(clp.getFailureThreshold())
                .withPeriodSeconds(clp.getPeriodSeconds())
                .withSuccessThreshold(clp.getSuccessThreshold())
                .build();
    }

    return new ContainerBuilder()
            .withName(substituteEnv(containerTemplate.getName()))
            .withImage(substituteEnv(containerTemplate.getImage()))
            .withImagePullPolicy(containerTemplate.isAlwaysPullImage() ? "Always" : "IfNotPresent")
            .withNewSecurityContext()
            .withPrivileged(containerTemplate.isPrivileged())
            .withRunAsUser(containerTemplate.getRunAsUserAsLong())
            .withRunAsGroup(containerTemplate.getRunAsGroupAsLong())
            .endSecurityContext()
            .withWorkingDir(workingDir)
            .withVolumeMounts(containerMounts.toArray(new VolumeMount[containerMounts.size()]))
            .addToEnv(envVars)
            .addToPorts(ports)
            .withCommand(parseDockerCommand(containerTemplate.getCommand()))
            .withArgs(arguments)
            .withLivenessProbe(livenessProbe)
            .withTty(containerTemplate.isTtyEnabled())
            .withNewResources()
            .withRequests(getResourcesMap(containerTemplate.getResourceRequestMemory(), containerTemplate.getResourceRequestCpu()))
            .withLimits(getResourcesMap(containerTemplate.getResourceLimitMemory(), containerTemplate.getResourceLimitCpu()))
            .endResources()
            .build();
}
 
Example #27
Source File: PodTemplateBuilder.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
private Map<String, EnvVar> jnlpEnvVars(String workingDir) {
    if (workingDir == null) {
        workingDir = ContainerTemplate.DEFAULT_WORKING_DIR;
    }
    // Last-write wins map of environment variable names to values
    HashMap<String, String> env = new HashMap<>();

    if (slave != null) {
        SlaveComputer computer = slave.getComputer();
        if (computer != null) {
            // Add some default env vars for Jenkins
            env.put("JENKINS_SECRET", computer.getJnlpMac());
            // JENKINS_AGENT_NAME is default in jnlp-slave
            // JENKINS_NAME only here for backwords compatability
            env.put("JENKINS_NAME", computer.getName());
            env.put("JENKINS_AGENT_NAME", computer.getName());
        } else {
            LOGGER.log(Level.INFO, "Computer is null for agent: {0}", slave.getNodeName());
        }

        env.put("JENKINS_AGENT_WORKDIR", workingDir);

        KubernetesCloud cloud = slave.getKubernetesCloud();

        if (!StringUtils.isBlank(cloud.getJenkinsTunnel())) {
            env.put("JENKINS_TUNNEL", cloud.getJenkinsTunnel());
        }

        if (!cloud.isDirectConnection()) {
            env.put("JENKINS_URL", cloud.getJenkinsUrlOrDie());
            if (cloud.isWebSocket()) {
                env.put("JENKINS_WEB_SOCKET", "true");
            }
        } else {
            TcpSlaveAgentListener tcpSlaveAgentListener = Jenkins.get().getTcpSlaveAgentListener();
            String host = tcpSlaveAgentListener.getAdvertisedHost();
            int port = tcpSlaveAgentListener.getAdvertisedPort();
            env.put("JENKINS_DIRECT_CONNECTION", host + ":" + port);
            env.put("JENKINS_PROTOCOLS", "JNLP4-connect");
            env.put("JENKINS_INSTANCE_IDENTITY", tcpSlaveAgentListener.getIdentityPublicKey());
        }

    }
    Map<String, EnvVar> envVarsMap = new HashMap<>();

    env.entrySet().forEach(item ->
            envVarsMap.put(item.getKey(), new EnvVar(item.getKey(), item.getValue(), null))
    );
    return envVarsMap;
}
 
Example #28
Source File: HudsonTestCaseShutdownSlaveTest.java    From jenkins-test-harness with MIT License 4 votes vote down vote up
public void testShutdownSlave() throws Exception {
    DumbSlave slave1 = createOnlineSlave(); // online, and a build finished.
    DumbSlave slave2 = createOnlineSlave(); // online, and a build finished, and disconnected.
    DumbSlave slave3 = createOnlineSlave(); // online, and a build still running.
    DumbSlave slave4 = createOnlineSlave(); // online, and not used.
    DumbSlave slave5 = createSlave();   // offline.
    
    assertNotNull(slave1);
    assertNotNull(slave2);
    assertNotNull(slave3);
    assertNotNull(slave4);
    assertNotNull(slave5);
    
    // A build runs on slave1 and finishes.
    {
        FreeStyleProject project1 = createFreeStyleProject();
        project1.setAssignedLabel(LabelExpression.parseExpression(slave1.getNodeName()));
        project1.getBuildersList().add(new SleepBuilder(1 * 1000));
        assertBuildStatusSuccess(project1.scheduleBuild2(0));
    }
    
    // A build runs on slave2 and finishes, then disconnect slave2 
    {
        FreeStyleProject project2 = createFreeStyleProject();
        project2.setAssignedLabel(LabelExpression.parseExpression(slave2.getNodeName()));
        project2.getBuildersList().add(new SleepBuilder(1 * 1000));
        assertBuildStatusSuccess(project2.scheduleBuild2(0));
        
        SlaveComputer computer2 = slave2.getComputer();
        computer2.disconnect(null);
        computer2.waitUntilOffline();
    }
    
    // A build runs on slave3 and does not finish.
    // This build will be interrupted in tearDown().
    {
        FreeStyleProject project3 = createFreeStyleProject();
        project3.setAssignedLabel(LabelExpression.parseExpression(slave3.getNodeName()));
        project3.getBuildersList().add(new SleepBuilder(10 * 60 * 1000));
        project3.scheduleBuild2(0);
        FreeStyleBuild build;
        while((build = project3.getLastBuild()) == null) {
            Thread.sleep(500);
        }
        assertTrue(build.isBuilding());
    }
}
 
Example #29
Source File: TestNGTestReportPublisher.java    From bootstraped-multi-test-results-report with MIT License 4 votes vote down vote up
private void generateReports(Run<?, ?> build, FilePath workspace, TaskListener listener)
    throws IOException, InterruptedException {
    // source directory (possibly on slave)
    FilePath workspaceJsonReportDirectory;
    if (getReportsDirectory().isEmpty()) {
        workspaceJsonReportDirectory = workspace;
    } else {
        workspaceJsonReportDirectory = new FilePath(workspace, getReportsDirectory());
    }

    // target directory (always on master)
    File targetBuildDirectory = new File(build.getRootDir(), "testng-reports-with-handlebars");
    if (!targetBuildDirectory.exists()) {
        if (!targetBuildDirectory.mkdirs()) {
            listener.getLogger().println("target dir was not created !!!");
        }
    }

    if (Computer.currentComputer() instanceof SlaveComputer) {
        listener.getLogger().println(
            "[TestNG test report builder] Copying XML files from slave: "
                + workspaceJsonReportDirectory.getRemote() + " to master reports directory: "
                + targetBuildDirectory);
    } else {
        listener.getLogger().println(
            "[TestNG test report builder] Copying XML files from: "
                + workspaceJsonReportDirectory.getRemote()
                + " to reports directory: " + targetBuildDirectory);
    }
    File targetBuildJsonDirectory = new File(targetBuildDirectory.getAbsolutePath() + "/xmlData");
    if (!targetBuildJsonDirectory.exists()) {
        targetBuildJsonDirectory.mkdirs();
    }
    String includePattern = (fileIncludePattern == null || fileIncludePattern.isEmpty()) ?
        DEFAULT_FILE_INCLUDE_PATTERN : fileIncludePattern;
    workspaceJsonReportDirectory.copyRecursiveTo(includePattern, new FilePath(
        targetBuildJsonDirectory));

    // generate the reports from the targetBuildDirectory
    Result result = Result.NOT_BUILT;
    String[] reportFiles =
        findFiles(targetBuildJsonDirectory, getFileIncludePattern(),
            getFileExcludePattern(), DEFAULT_FILE_INCLUDE_PATTERN);

    if (reportFiles.length > 0) {
        listener.getLogger().println(
            String.format("[TestNGReportPublisher] Found %d xml files.", reportFiles.length));
        int jsonIndex = 0;
        for (String reportFile : reportFiles) {
            listener.getLogger().println(
                "[TestNG test report builder] " + jsonIndex + ". Found a xml file: " + reportFile);
            jsonIndex++;
        }
        listener.getLogger().println("[TestNG test report builder] Generating HTML reports");

        try {
            for (String ss : fullPathToFiles(reportFiles, targetBuildJsonDirectory)) {
                listener.getLogger().println("processing: " + ss);
            }
            TestNgReportBuilder rep =
                new TestNgReportBuilder(fullPathToFiles(reportFiles, targetBuildJsonDirectory),
                    targetBuildDirectory.getAbsolutePath());

            boolean featuresResult = rep.writeReportsOnDisk();
            if (featuresResult) {
                result = Result.SUCCESS;
            } else {
                result = isMarkAsUnstable() ? Result.UNSTABLE : Result.FAILURE;
            }

            // finally copy to workspace, if needed
            if (isCopyHTMLInWorkspace()) {
                FilePath workspaceCopyDirectory =
                    new FilePath(workspace, "testng-reports-with-handlebars");
                if (workspaceCopyDirectory.exists()) {
                    workspaceCopyDirectory.deleteRecursive();
                }
                listener.getLogger().println(
                    "[TestNG test report builder] Copying report to workspace directory: " + workspaceCopyDirectory
                        .toURI());
                new FilePath(targetBuildDirectory).copyRecursiveTo("**/*.html", workspaceCopyDirectory);
            }

        } catch (Exception e) {
            result = Result.FAILURE;
            listener.getLogger().println(
                "[TestNG test report builder] there was an error generating the reports: " + e);
            for (StackTraceElement error : e.getStackTrace()) {
                listener.getLogger().println(error);
            }
        }
    } else {
        result = Result.SUCCESS;
        listener.getLogger().println(
            "[TestNG test report builder] xml path for the reports might be wrong, " + targetBuildDirectory);
    }
    build.setResult(result);
}