Search in sources :

Example 26 with ClusterProfile

use of com.thoughtworks.go.config.elastic.ClusterProfile in project gocd by gocd.

the class ElasticAgentPluginService method jobCompleted.

public void jobCompleted(JobInstance job) {
    AgentInstance agentInstance = agentService.findAgent(job.getAgentUuid());
    if (!agentInstance.isElastic()) {
        LOGGER.debug("Agent {} is not elastic. Skipping further execution.", agentInstance.getUuid());
        return;
    }
    if (job.isAssignedToAgent()) {
        jobCreationTimeMap.remove(job.getId());
    }
    String pluginId = agentInstance.elasticAgentMetadata().elasticPluginId();
    String elasticAgentId = agentInstance.elasticAgentMetadata().elasticAgentId();
    JobIdentifier jobIdentifier = job.getIdentifier();
    ElasticProfile elasticProfile = job.getPlan().getElasticProfile();
    ClusterProfile clusterProfile = job.getPlan().getClusterProfile();
    try {
        secretParamResolver.resolve(elasticProfile);
        Map<String, String> elasticProfileConfiguration = elasticProfile.getConfigurationAsMap(true, true);
        Map<String, String> clusterProfileConfiguration = emptyMap();
        if (clusterProfile != null) {
            secretParamResolver.resolve(clusterProfile);
            clusterProfileConfiguration = clusterProfile.getConfigurationAsMap(true, true);
        }
        elasticAgentPluginRegistry.reportJobCompletion(pluginId, elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);
    } catch (RulesViolationException | SecretResolutionFailureException e) {
        String description = format("The job completion call to the plugin for the job identifier [%s] failed for secrets resolution: %s ", jobIdentifier.toString(), e.getMessage());
        ServerHealthState healthState = error("Failed to notify plugin", description, general(scopeForJob(jobIdentifier)));
        healthState.setTimeout(Timeout.FIVE_MINUTES);
        serverHealthService.update(healthState);
        LOGGER.error(description);
    }
}
Also used : AgentInstance(com.thoughtworks.go.domain.AgentInstance) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) SecretResolutionFailureException(com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException) ElasticProfile(com.thoughtworks.go.config.elastic.ElasticProfile) ServerHealthState(com.thoughtworks.go.serverhealth.ServerHealthState) JobIdentifier(com.thoughtworks.go.domain.JobIdentifier) ClusterProfile(com.thoughtworks.go.config.elastic.ClusterProfile)

Example 27 with ClusterProfile

use of com.thoughtworks.go.config.elastic.ClusterProfile in project gocd by gocd.

the class ElasticAgentPluginService method getClusterStatusReport.

public String getClusterStatusReport(String pluginId, String clusterProfileId) {
    final ElasticAgentPluginInfo pluginInfo = elasticAgentMetadataStore.getPluginInfo(pluginId);
    if (pluginInfo == null) {
        throw new RecordNotFoundException(format("Plugin with id: '%s' is not found.", pluginId));
    }
    if (pluginInfo.getCapabilities().supportsClusterStatusReport()) {
        ClusterProfile clusterProfile = clusterProfilesService.getPluginProfiles().findByPluginIdAndProfileId(pluginId, clusterProfileId);
        if (clusterProfile == null) {
            throw new RecordNotFoundException(format("Cluster profile with id: '%s' is not found.", clusterProfileId));
        }
        secretParamResolver.resolve(clusterProfile);
        return elasticAgentPluginRegistry.getClusterStatusReport(pluginId, clusterProfile.getConfigurationAsMap(true, true));
    }
    throw new UnsupportedOperationException("Plugin does not support cluster status report.");
}
Also used : ElasticAgentPluginInfo(com.thoughtworks.go.plugin.domain.elastic.ElasticAgentPluginInfo) RecordNotFoundException(com.thoughtworks.go.config.exceptions.RecordNotFoundException) ClusterProfile(com.thoughtworks.go.config.elastic.ClusterProfile)

Example 28 with ClusterProfile

use of com.thoughtworks.go.config.elastic.ClusterProfile 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 29 with ClusterProfile

use of com.thoughtworks.go.config.elastic.ClusterProfile 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 30 with ClusterProfile

use of com.thoughtworks.go.config.elastic.ClusterProfile in project gocd by gocd.

the class ClusterProfilePermissionTest method shouldReturnEmptyListWhenUserHasNoPermissionsOnClusterProfiles.

@Test
void shouldReturnEmptyListWhenUserHasNoPermissionsOnClusterProfiles() {
    cruiseConfig.getElasticConfig().getClusterProfiles().add(new ClusterProfile("dev_cluster", "ecs"));
    cruiseConfig.getElasticConfig().getClusterProfiles().add(new ClusterProfile("prod_cluster", "ecs"));
    Map<String, Object> permissions = permission.permissions(username);
    Map<String, Object> clusterProfile = new LinkedHashMap<>();
    clusterProfile.put("view", Collections.emptyList());
    clusterProfile.put("administer", Collections.emptyList());
    assertThat(permissions).isEqualTo(clusterProfile);
}
Also used : ClusterProfile(com.thoughtworks.go.config.elastic.ClusterProfile) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Aggregations

ClusterProfile (com.thoughtworks.go.config.elastic.ClusterProfile)69 Test (org.junit.jupiter.api.Test)39 ElasticProfile (com.thoughtworks.go.config.elastic.ElasticProfile)32 ConfigurationProperty (com.thoughtworks.go.domain.config.ConfigurationProperty)14 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)12 ConfigurationKey (com.thoughtworks.go.domain.config.ConfigurationKey)10 ConfigurationValue (com.thoughtworks.go.domain.config.ConfigurationValue)10 ElasticAgentPluginInfo (com.thoughtworks.go.plugin.domain.elastic.ElasticAgentPluginInfo)10 ClusterProfiles (com.thoughtworks.go.config.elastic.ClusterProfiles)8 Username (com.thoughtworks.go.server.domain.Username)8 GoPluginDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ElasticConfig (com.thoughtworks.go.config.elastic.ElasticConfig)6 Capabilities (com.thoughtworks.go.plugin.domain.elastic.Capabilities)6 BasicCruiseConfig (com.thoughtworks.go.config.BasicCruiseConfig)5 RecordNotFoundException (com.thoughtworks.go.config.exceptions.RecordNotFoundException)5 JobAgentMetadata (com.thoughtworks.go.domain.JobAgentMetadata)4 ElasticAgentInformation (com.thoughtworks.go.plugin.access.elastic.models.ElasticAgentInformation)4 PluginConfiguration (com.thoughtworks.go.plugin.domain.common.PluginConfiguration)4 GoCipher (com.thoughtworks.go.security.GoCipher)4