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);
}
}
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.");
}
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.");
}
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);
}
}
}
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);
}
Aggregations