use of com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties in project gocd-ecs-elastic-agent by gocd.
the class ClusterStatusReportRequestTest method shouldDeserializeFromJSON.
@Test
void shouldDeserializeFromJSON() {
String json = "{\n" + " \"cluster_profile_properties\": {\n" + " \"GoServerUrl\": \"https://cd.server.com/go\", \n" + " \"ClusterName\": \"deployment-cluster\"\n" + " }\n" + "}";
ClusterStatusReportRequest request = ClusterStatusReportRequest.fromJSON(json);
Map<String, String> clusterProfileConfigurations = new HashMap<>();
clusterProfileConfigurations.put("GoServerUrl", "https://cd.server.com/go");
clusterProfileConfigurations.put("ClusterName", "deployment-cluster");
ClusterProfileProperties expectedClusterProfileProperties = ClusterProfileProperties.fromConfiguration(clusterProfileConfigurations);
assertThat(request.clusterProfileProperties()).isEqualTo(expectedClusterProfileProperties);
}
use of com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties in project gocd-ecs-elastic-agent by gocd.
the class ShouldAssignWorkRequestTest method shouldDeserializeFromJSON.
@Test
void shouldDeserializeFromJSON() {
String json = "{\n" + " \"environment\": \"prod\",\n" + " \"agent\": {\n" + " \"agent_id\": \"42\",\n" + " \"agent_state\": \"Idle\",\n" + " \"build_state\": \"Idle\",\n" + " \"config_state\": \"Enabled\"\n" + " },\n" + " \"elastic_agent_profile_properties\": {\n" + " \"Image\": \"go-agent\"\n" + " },\n" + " \"cluster_profile_properties\": {\n" + " \"GoServerUrl\": \"https://cd.server.com/go\", \n" + " \"ClusterName\": \"deployment-cluster\"\n" + " }\n" + "}";
ShouldAssignWorkRequest request = ShouldAssignWorkRequest.fromJSON(json);
assertThat(request.environment()).isEqualTo("prod");
assertThat(request.agent()).isEqualTo(new Agent("42", Agent.AgentState.Idle, Agent.BuildState.Idle, Agent.ConfigState.Enabled));
assertThat(request.elasticProfile().getImage()).isEqualTo("go-agent");
Map<String, String> clusterProfileConfigurations = new HashMap<>();
clusterProfileConfigurations.put("GoServerUrl", "https://cd.server.com/go");
clusterProfileConfigurations.put("ClusterName", "deployment-cluster");
ClusterProfileProperties expectedClusterProfileProperties = ClusterProfileProperties.fromConfiguration(clusterProfileConfigurations);
assertThat(request.clusterProfileProperties()).isEqualTo(expectedClusterProfileProperties);
}
use of com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties in project gocd-ecs-elastic-agent by gocd.
the class ClusterStatusReportExecutor method execute.
@Override
public GoPluginApiResponse execute() throws IOException, TemplateException {
final Map<String, Object> dataModel = new HashMap<>();
try {
final ClusterProfileProperties clusterProfileProperties = request.clusterProfileProperties();
agentInstances.refreshAll(clusterProfileProperties);
if (clusterProfileProperties == null || !clusterProfileProperties.isConfigured()) {
throw new RuntimeException("Please configure the cluster profile properly before using the plugin.");
}
final Cluster cluster = helper.getCluster(clusterProfileProperties);
final List<ContainerInstance> containerInstances = new ArrayList<>();
final List<Instance> instances = new ArrayList<>();
final List<ECSContainer> ecsContainers = new ArrayList<>();
if (cluster.getRegisteredContainerInstancesCount() != 0) {
containerInstances.addAll(helper.getContainerInstances(clusterProfileProperties));
instances.addAll(helper.ec2InstancesFromContainerInstances(clusterProfileProperties, containerInstances));
ecsContainers.addAll(taskHelper.allRunningContainers(clusterProfileProperties));
}
dataModel.put("region", clusterProfileProperties.getRegion());
final ECSCluster ecsCluster = new ECSCluster(cluster, containerInstances, instances, ecsContainers, clusterProfileProperties.getMaxLinuxInstancesAllowed(), clusterProfileProperties.getMaxWindowsInstancesAllowed(), clusterProfileProperties.getMaxLinuxSpotInstanceAllowed(), clusterProfileProperties.getMaxWindowsSpotInstanceAllowed());
dataModel.put("cluster", ecsCluster);
eventStream.remove(EventFingerprint.forStatusReport());
} catch (Exception e) {
LOG.error("[status report] Error accessing ECS cluster details", e);
final String errorMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
eventStream.update(Event.errorEvent(EventFingerprint.forStatusReport(), "Error accessing ECS cluster details", errorMessage));
}
addPluginVersionInformation(dataModel);
dataModel.put("errors", eventStream.allErrors());
dataModel.put("warnings", eventStream.allWarnings());
final Template template = pluginStatusReportViewBuilder.getTemplate("status-report.template.ftlh");
final String view = pluginStatusReportViewBuilder.build(template, dataModel);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("view", view);
return DefaultGoPluginApiResponse.success(jsonObject.toString());
}
use of com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties in project gocd-ecs-elastic-agent by gocd.
the class JobCompletionRequestExecutorTest method shouldTerminateElasticAgentOnJobCompletion.
@Test
public void shouldTerminateElasticAgentOnJobCompletion() throws Exception {
JobIdentifier jobIdentifier = new JobIdentifier("test", 1L, "test", "test_stage", "1", "test_job", 100L);
String elasticAgentId = "agent-1";
ClusterProfileProperties clusterProfileProperties = new ClusterProfileProperties();
JobCompletionRequest request = new JobCompletionRequest(elasticAgentId, jobIdentifier, new ElasticAgentProfileProperties(), clusterProfileProperties);
JobCompletionRequestExecutor executor = new JobCompletionRequestExecutor(request, mockAgentInstances, mockPluginRequest);
Agents agents = new Agents();
agents.add(new Agent(elasticAgentId));
when(mockPluginRequest.listAgents()).thenReturn(agents);
GoPluginApiResponse response = executor.execute();
InOrder inOrder = inOrder(mockPluginRequest, mockAgentInstances);
inOrder.verify(mockPluginRequest).disableAgents(agentsArgumentCaptor.capture());
inOrder.verify(mockAgentInstances).terminate(elasticAgentId, clusterProfileProperties);
inOrder.verify(mockPluginRequest).deleteAgents(agentsArgumentCaptor.capture());
List<Agent> agentsToDisabled = agentsArgumentCaptor.getValue();
assertThat(1).isEqualTo(agentsToDisabled.size());
assertThat(elasticAgentId).isEqualTo(agentsToDisabled.get(0).elasticAgentId());
List<Agent> agentsToDelete = agentsArgumentCaptor.getValue();
assertThat(agentsToDisabled).isEqualTo(agentsToDelete);
assertThat(200).isEqualTo(response.responseCode());
assertThat(response.responseBody().isEmpty()).isTrue();
}
use of com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties in project gocd-ecs-elastic-agent by gocd.
the class ECSElasticPlugin method handle.
@Override
public GoPluginApiResponse handle(GoPluginApiRequest request) {
ClusterProfileProperties clusterProfileProperties;
try {
fetchServerIdFromServer();
LOG.debug("Request from server: " + request.requestName());
switch(Request.fromString(request.requestName())) {
case PLUGIN_SETTINGS_GET_ICON:
return new GetPluginSettingsIconExecutor().execute();
case REQUEST_GET_ELASTIC_AGENT_PROFILE_METADATA:
return new GetProfileMetadataExecutor().execute();
case REQUEST_GET_ELASTIC_AGENT_PROFILE_VIEW:
return new GetProfileViewExecutor().execute();
case REQUEST_VALIDATE_ELASTIC_AGENT_PROFILE:
return ProfileValidateRequest.fromJSON(request.requestBody()).executor().execute();
case REQUEST_GET_CLUSTER_PROFILE_METADATA:
return new GetClusterProfileMetadataExecutor().execute();
case REQUEST_GET_CLUSTER_PROFILE_VIEW:
return new GetClusterProfileViewRequestExecutor().execute();
case REQUEST_VALIDATE_CLUSTER_PROFILE_CONFIGURATION:
return ValidateClusterProfileRequest.fromJSON(request.requestBody()).executor().execute();
case REQUEST_CREATE_AGENT:
CreateAgentRequest createAgentRequest = CreateAgentRequest.fromJSON(request.requestBody());
clusterProfileProperties = createAgentRequest.clusterProfileProperties();
refreshInstancesForCluster(clusterProfileProperties);
return createAgentRequest.executor(getAgentInstancesFor(clusterProfileProperties), pluginRequest, getEventStreamFor(clusterProfileProperties)).execute();
case REQUEST_SHOULD_ASSIGN_WORK:
ShouldAssignWorkRequest shouldAssignWorkRequest = ShouldAssignWorkRequest.fromJSON(request.requestBody());
clusterProfileProperties = shouldAssignWorkRequest.clusterProfileProperties();
refreshInstancesForCluster(clusterProfileProperties);
return shouldAssignWorkRequest.executor(getAgentInstancesFor(clusterProfileProperties)).execute();
case REQUEST_JOB_COMPLETION:
JobCompletionRequest jobCompletionRequest = JobCompletionRequest.fromJSON(request.requestBody());
clusterProfileProperties = jobCompletionRequest.clusterProfileProperties();
refreshInstancesForCluster(clusterProfileProperties);
return jobCompletionRequest.executor(getAgentInstancesFor(clusterProfileProperties), pluginRequest).execute();
case REQUEST_SERVER_PING:
ServerPingRequest serverPingRequest = ServerPingRequest.fromJSON(request.requestBody());
List<ClusterProfileProperties> listOfClusterProfileProperties = serverPingRequest.allClusterProfileProperties();
refreshInstancesForAllClusters(listOfClusterProfileProperties);
return serverPingRequest.executor(clusterSpecificAgentInstances, pluginRequest).execute();
case REQUEST_CLUSTER_STATUS_REPORT:
ClusterStatusReportRequest clusterStatusReportRequest = ClusterStatusReportRequest.fromJSON(request.requestBody());
clusterProfileProperties = clusterStatusReportRequest.clusterProfileProperties();
refreshInstancesForCluster(clusterProfileProperties);
return clusterStatusReportRequest.executor(getAgentInstancesFor(clusterProfileProperties), getEventStreamFor(clusterProfileProperties)).execute();
case REQUEST_AGENT_STATUS_REPORT:
AgentStatusReportRequest statusReportRequest = AgentStatusReportRequest.fromJSON(request.requestBody());
clusterProfileProperties = statusReportRequest.clusterProfileProperties();
refreshInstancesForCluster(clusterProfileProperties);
return statusReportRequest.executor(getAgentInstancesFor(clusterProfileProperties)).execute();
case REQUEST_GET_CAPABILITIES:
return new GetCapabilitiesExecutor().execute();
case REQUEST_CLUSTER_PROFILE_CHANGED:
return new DefaultGoPluginApiResponse(200);
case REQUEST_MIGRATE_CONFIGURATION:
return MigrateConfigurationRequest.fromJSON(request.requestBody()).executor().execute();
default:
throw new UnhandledRequestTypeException(request.requestName());
}
} catch (AmazonEC2Exception e) {
LOG.error("Failed to handle request " + request.requestName() + " due to:" + e.getMessage());
return DefaultGoPluginApiResponse.error("Failed to handle request " + request.requestName() + " due to:" + e.getMessage());
} catch (Exception e) {
LOG.error("Failed to handle request " + request.requestName() + " due to:", e);
return DefaultGoPluginApiResponse.error("Failed to handle request " + request.requestName() + " due to:" + e.getMessage());
}
}
Aggregations