Search in sources :

Example 1 with JobPlan

use of com.thoughtworks.go.domain.JobPlan in project gocd by gocd.

the class LongWorkCreator method getWork.

public Work getWork() {
    try {
        CruiseConfig config = GoConfigMother.pipelineHavingJob(PIPELINE_NAME, STAGE_NAME, JOB_PLAN_NAME, ARTIFACT_FILE.getAbsolutePath(), ARTIFACT_FOLDER.getAbsolutePath());
        BuildCause buildCause = BuildCause.createWithEmptyModifications();
        builder = new ArrayList<>();
        builder.add(new SleepBuilder());
        JobPlan instance = toBuildInstance(config);
        BuildAssignment buildAssignment = BuildAssignment.create(instance, buildCause, builder, new File(CruiseConfig.WORKING_BASE_DIR + PIPELINE_NAME));
        return new BuildWork(buildAssignment);
    } catch (Exception e) {
        throw bomb(e);
    }
}
Also used : JobPlan(com.thoughtworks.go.domain.JobPlan) BuildAssignment(com.thoughtworks.go.remote.work.BuildAssignment) File(java.io.File) BuildWork(com.thoughtworks.go.remote.work.BuildWork) CruiseConfig(com.thoughtworks.go.config.CruiseConfig) IOException(java.io.IOException) BuildCause(com.thoughtworks.go.domain.buildcause.BuildCause)

Example 2 with JobPlan

use of com.thoughtworks.go.domain.JobPlan in project gocd by gocd.

the class JobPlanXmlRepresenter method populateJob.

public void populateJob(ElementBuilder builder, XmlWriterContext ctx, WaitingJobPlan waitingJobPlan) {
    JobPlan jobPlan = waitingJobPlan.jobPlan();
    builder.attr("name", jobPlan.getName()).link(ctx.jobXmlLink(jobPlan.getIdentifier()), "self").link(ctx.jobDetailsLink(jobPlan.getIdentifier()), "alternate", jobPlan.getName() + " Job Detail", "text/html").textNode("buildLocator", jobPlan.getIdentifier().buildLocator());
    if (isNotBlank(waitingJobPlan.envName())) {
        builder.textNode("environment", waitingJobPlan.envName());
    }
    if (!jobPlan.getResources().isEmpty()) {
        builder.node("resources", rb -> jobPlan.getResources().forEach(resource -> {
            rb.cdataNode("resource", resource.getName());
        }));
    }
    if (!jobPlan.getVariables().isEmpty()) {
        builder.node("environmentVariables", eb -> jobPlan.getVariables().forEach(variable -> {
            eb.node("variable", vb -> vb.attr("name", variable.getName()).text(variable.getDisplayValue()));
        }));
    }
}
Also used : Document(org.dom4j.Document) List(java.util.List) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) XmlWriterContext(com.thoughtworks.go.domain.XmlWriterContext) ElementBuilder(com.thoughtworks.go.server.domain.xml.builder.ElementBuilder) DocumentBuilder(com.thoughtworks.go.server.domain.xml.builder.DocumentBuilder) JobPlan(com.thoughtworks.go.domain.JobPlan) WaitingJobPlan(com.thoughtworks.go.domain.WaitingJobPlan) XmlRepresentable(com.thoughtworks.go.domain.XmlRepresentable) JobPlan(com.thoughtworks.go.domain.JobPlan) WaitingJobPlan(com.thoughtworks.go.domain.WaitingJobPlan)

Example 3 with JobPlan

use of com.thoughtworks.go.domain.JobPlan in project gocd by gocd.

the class ElasticAgentPluginService method getAgentStatusReport.

public String getAgentStatusReport(String pluginId, JobIdentifier jobIdentifier, String elasticAgentId) throws Exception {
    final ElasticAgentPluginInfo pluginInfo = elasticAgentMetadataStore.getPluginInfo(pluginId);
    if (pluginInfo == null) {
        throw new RecordNotFoundException(format("Plugin with id: '%s' is not found.", pluginId));
    }
    if (pluginInfo.getCapabilities().supportsAgentStatusReport()) {
        JobPlan jobPlan = jobInstanceSqlMapDao.loadPlan(jobIdentifier.getId());
        if (jobPlan != null) {
            ClusterProfile clusterProfile = jobPlan.getClusterProfile();
            Map<String, String> clusterProfileConfigurations = emptyMap();
            if (clusterProfile != null) {
                secretParamResolver.resolve(clusterProfile);
                clusterProfileConfigurations = clusterProfile.getConfigurationAsMap(true, true);
            }
            return elasticAgentPluginRegistry.getAgentStatusReport(pluginId, jobIdentifier, elasticAgentId, clusterProfileConfigurations);
        }
        throw new Exception(format("Could not fetch agent status report for agent %s as either the job running on the agent has been completed or the agent has been terminated.", elasticAgentId));
    }
    throw new UnsupportedOperationException("Plugin does not support agent status report.");
}
Also used : ElasticAgentPluginInfo(com.thoughtworks.go.plugin.domain.elastic.ElasticAgentPluginInfo) RecordNotFoundException(com.thoughtworks.go.config.exceptions.RecordNotFoundException) JobPlan(com.thoughtworks.go.domain.JobPlan) ClusterProfile(com.thoughtworks.go.config.elastic.ClusterProfile) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) IllegalArtifactLocationException(com.thoughtworks.go.domain.exception.IllegalArtifactLocationException) RecordNotFoundException(com.thoughtworks.go.config.exceptions.RecordNotFoundException) IOException(java.io.IOException) SecretResolutionFailureException(com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException)

Example 4 with JobPlan

use of com.thoughtworks.go.domain.JobPlan in project gocd by gocd.

the class ElasticAgentPluginService method createAgentsFor.

public void createAgentsFor(List<JobPlan> old, List<JobPlan> newPlan) {
    Collection<JobPlan> starvingJobs = new ArrayList<>();
    for (JobPlan jobPlan : newPlan) {
        if (jobPlan.requiresElasticAgent()) {
            if (!jobCreationTimeMap.containsKey(jobPlan.getJobId())) {
                continue;
            }
            long lastTryTime = jobCreationTimeMap.get(jobPlan.getJobId());
            if ((timeProvider.currentTimeMillis() - lastTryTime) >= goConfigService.elasticJobStarvationThreshold()) {
                starvingJobs.add(jobPlan);
            }
        }
    }
    ArrayList<JobPlan> jobsThatRequireAgent = new ArrayList<>();
    jobsThatRequireAgent.addAll(Sets.difference(new HashSet<>(newPlan), new HashSet<>(old)));
    jobsThatRequireAgent.addAll(starvingJobs);
    List<JobPlan> plansThatRequireElasticAgent = jobsThatRequireAgent.stream().filter(isElasticAgent()).collect(toList());
    // messageTimeToLive is lesser than the starvation threshold to ensure there are no duplicate create agent message
    long messageTimeToLive = goConfigService.elasticJobStarvationThreshold() - 10000;
    for (JobPlan plan : plansThatRequireElasticAgent) {
        jobCreationTimeMap.put(plan.getJobId(), timeProvider.currentTimeMillis());
        ElasticProfile elasticProfile = plan.getElasticProfile();
        ClusterProfile clusterProfile = plan.getClusterProfile();
        JobIdentifier jobIdentifier = plan.getIdentifier();
        if (clusterProfile == null) {
            String cancellationMessage = "\nThis job was cancelled by GoCD. The version of your GoCD server requires elastic profiles to be associated with a cluster(required from Version 19.3.0). " + "This job is configured to run on an Elastic Agent, but the associated elastic profile does not have information about the cluster.  \n\n" + "The possible reason for the missing cluster information on the elastic profile could be, an upgrade of the GoCD server to a version >= 19.3.0 before the completion of the job.\n\n" + "A re-run of this job should fix this issue.";
            logToJobConsole(jobIdentifier, cancellationMessage);
            scheduleService.cancelJob(jobIdentifier);
        } else if (elasticAgentPluginRegistry.has(clusterProfile.getPluginId())) {
            String environment = environmentConfigService.envForPipeline(plan.getPipelineName());
            try {
                resolveSecrets(clusterProfile, elasticProfile);
                createAgentQueue.post(new CreateAgentMessage(ephemeralAutoRegisterKeyService.autoRegisterKey(), environment, elasticProfile, clusterProfile, jobIdentifier), messageTimeToLive);
                serverHealthService.removeByScope(scopeForJob(jobIdentifier));
            } catch (RulesViolationException | SecretResolutionFailureException e) {
                JobInstance jobInstance = jobInstanceSqlMapDao.buildById(plan.getJobId());
                String failureMessage = format("\nThis job was failed by GoCD. This job is configured to run on an elastic agent, there were errors while resolving secrets for the the associated elastic configurations.\nReasons: %s", e.getMessage());
                logToJobConsole(jobIdentifier, failureMessage);
                scheduleService.failJob(jobInstance);
                jobStatusTopic.post(new JobStatusMessage(jobIdentifier, jobInstance.getState(), plan.getAgentUuid()));
            }
        } else {
            String jobConfigIdentifier = jobIdentifier.jobConfigIdentifier().toString();
            String description = format("Plugin [%s] associated with %s is missing. Either the plugin is not " + "installed or could not be registered. Please check plugins tab " + "and server logs for more details.", clusterProfile.getPluginId(), jobConfigIdentifier);
            serverHealthService.update(error(format("Unable to find agent for %s", jobConfigIdentifier), description, general(scopeForJob(jobIdentifier))));
            LOGGER.error(description);
        }
    }
}
Also used : CreateAgentMessage(com.thoughtworks.go.server.messaging.elasticagents.CreateAgentMessage) JobPlan(com.thoughtworks.go.domain.JobPlan) JobInstance(com.thoughtworks.go.domain.JobInstance) JobIdentifier(com.thoughtworks.go.domain.JobIdentifier) JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) ElasticProfile(com.thoughtworks.go.config.elastic.ElasticProfile) ClusterProfile(com.thoughtworks.go.config.elastic.ClusterProfile)

Example 5 with JobPlan

use of com.thoughtworks.go.domain.JobPlan in project gocd by gocd.

the class JobXmlViewModelTest method setUp.

@Before
public void setUp() {
    xmlWriterContext = mock(XmlWriterContext.class);
    jobPlan = mock(JobPlan.class);
    defaultJob = JobInstanceMother.completed("defaultJob");
    when(xmlWriterContext.planFor(defaultJob.getIdentifier())).thenReturn(jobPlan);
    when(xmlWriterContext.propertiesForJob(defaultJob.getId())).thenReturn(new Properties());
    jobXmlViewModel = new JobXmlViewModel(defaultJob);
}
Also used : JobPlan(com.thoughtworks.go.domain.JobPlan) XmlWriterContext(com.thoughtworks.go.domain.XmlWriterContext) Properties(com.thoughtworks.go.domain.Properties) Before(org.junit.Before)

Aggregations

JobPlan (com.thoughtworks.go.domain.JobPlan)5 ClusterProfile (com.thoughtworks.go.config.elastic.ClusterProfile)2 XmlWriterContext (com.thoughtworks.go.domain.XmlWriterContext)2 IOException (java.io.IOException)2 CruiseConfig (com.thoughtworks.go.config.CruiseConfig)1 ElasticProfile (com.thoughtworks.go.config.elastic.ElasticProfile)1 RecordNotFoundException (com.thoughtworks.go.config.exceptions.RecordNotFoundException)1 JobIdentifier (com.thoughtworks.go.domain.JobIdentifier)1 JobInstance (com.thoughtworks.go.domain.JobInstance)1 Properties (com.thoughtworks.go.domain.Properties)1 WaitingJobPlan (com.thoughtworks.go.domain.WaitingJobPlan)1 XmlRepresentable (com.thoughtworks.go.domain.XmlRepresentable)1 BuildCause (com.thoughtworks.go.domain.buildcause.BuildCause)1 IllegalArtifactLocationException (com.thoughtworks.go.domain.exception.IllegalArtifactLocationException)1 SecretResolutionFailureException (com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException)1 ElasticAgentPluginInfo (com.thoughtworks.go.plugin.domain.elastic.ElasticAgentPluginInfo)1 BuildAssignment (com.thoughtworks.go.remote.work.BuildAssignment)1 BuildWork (com.thoughtworks.go.remote.work.BuildWork)1 DocumentBuilder (com.thoughtworks.go.server.domain.xml.builder.DocumentBuilder)1 ElementBuilder (com.thoughtworks.go.server.domain.xml.builder.ElementBuilder)1