use of com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest in project gocd-ecs-elastic-agent by gocd.
the class ClusterProfilePropertiesTest method shouldGenerateSameUUIDAcrossRequests.
@Test
void shouldGenerateSameUUIDAcrossRequests() {
String createAgentRequestJSON = "{\n" + " \"auto_register_key\": \"secret-key\",\n" + " \"elastic_agent_profile_properties\": {\n" + " \"Image\": \"value1\",\n" + " \"MaxMemory\": \"2G\",\n" + " \"ReservedMemory\": \"150M\",\n" + " \"TerminationPolicy\": \"\"\n" + " },\n" + " \"cluster_profile_properties\": {\n" + " \"GoServerUrl\": \"https://cd.server.com/go\", \n" + " \"ClusterName\": \"deployment-cluster\"\n" + " },\n" + " \"environment\": \"prod\"\n" + "}";
String jobCompletionRequestJSON = "{\n" + " \"elastic_agent_id\": \"ea1\",\n" + " \"job_identifier\": {\n" + " \"pipeline_name\": \"test-pipeline\",\n" + " \"pipeline_counter\": 1,\n" + " \"pipeline_label\": \"Test Pipeline\",\n" + " \"stage_name\": \"test-stage\",\n" + " \"stage_counter\": \"1\",\n" + " \"job_name\": \"test-job\",\n" + " \"job_id\": 100\n" + " },\n" + " \"elastic_agent_profile_properties\": {\n" + " \"Image\": \"value1\",\n" + " \"MaxMemory\": \"2G\",\n" + " \"ReservedMemory\": \"150M\",\n" + " \"TerminationPolicy\": \"\"\n" + " },\n" + " \"cluster_profile_properties\": {\n" + " \"GoServerUrl\": \"https://cd.server.com/go\", \n" + " \"ClusterName\": \"deployment-cluster\"\n" + " }\n" + "}";
CreateAgentRequest createAgentRequest = CreateAgentRequest.fromJSON(createAgentRequestJSON);
String createAgentRequestUUID = createAgentRequest.clusterProfileProperties().uuid();
JobCompletionRequest jobCompletionRequest = JobCompletionRequest.fromJSON(jobCompletionRequestJSON);
String jobCompletionRequestUUID = jobCompletionRequest.clusterProfileProperties().uuid();
assertThat(createAgentRequestUUID).isEqualTo(jobCompletionRequestUUID);
}
use of com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest in project gocd-ecs-elastic-agent by gocd.
the class TaskHelper method create.
public Optional<ECSTask> create(CreateAgentRequest createAgentRequest, PluginSettings pluginSettings, ConsoleLogAppender consoleLogAppender) throws ContainerInstanceFailedToRegisterException, LimitExceededException, ContainerFailedToRegisterException {
final String taskName = "GoCD" + UUID.randomUUID().toString().replaceAll("-", "");
final ElasticAgentProfileProperties elasticAgentProfileProperties = createAgentRequest.elasticProfile();
ContainerDefinition containerDefinition = new ContainerDefinitionBuilder().withName(taskName).pluginSettings(pluginSettings).createAgentRequest(createAgentRequest).withServerId(getServerId()).build();
StopPolicy stopPolicy = elasticAgentProfileProperties.platform() == LINUX ? pluginSettings.getLinuxStopPolicy() : pluginSettings.getWindowsStopPolicy();
Optional<ContainerInstance> containerInstance = instanceSelectionStrategyFactory.strategyFor(stopPolicy).instanceForScheduling(pluginSettings, elasticAgentProfileProperties, containerDefinition);
if (!containerInstance.isPresent()) {
consoleLogAppender.accept("No running instance(s) found to build the ECS Task to perform current job.");
LOG.info(format("[create-agent] No running instances found to build container with profile {0}", createAgentRequest.elasticProfile().toJson()));
if (elasticAgentProfileProperties.runAsSpotInstance()) {
spotInstanceService.create(pluginSettings, elasticAgentProfileProperties, consoleLogAppender);
return Optional.empty();
} else {
containerInstance = containerInstanceHelper.startOrCreateOneInstance(pluginSettings, elasticAgentProfileProperties, consoleLogAppender);
}
} else {
consoleLogAppender.accept("Found existing running container instance platform matching ECS Task instance configuration. Not starting a new EC2 instance...");
}
final RegisterTaskDefinitionRequest registerTaskDefinitionRequest = registerTaskDefinitionRequestBuilder.build(pluginSettings, elasticAgentProfileProperties, containerDefinition).withFamily(taskName);
consoleLogAppender.accept("Registering ECS Task definition with cluster...");
LOG.debug(format("[create-agent] Registering task definition: {0} ", registerTaskDefinitionRequest.toString()));
RegisterTaskDefinitionResult taskDefinitionResult = pluginSettings.ecsClient().registerTaskDefinition(registerTaskDefinitionRequest);
consoleLogAppender.accept("Done registering ECS Task definition with cluster.");
LOG.debug("[create-agent] Done registering task definition");
TaskDefinition taskDefinitionFromNewTask = taskDefinitionResult.getTaskDefinition();
StartTaskRequest startTaskRequest = new StartTaskRequest().withTaskDefinition(taskDefinitionFromNewTask.getTaskDefinitionArn()).withContainerInstances(containerInstance.get().getContainerInstanceArn()).withCluster(pluginSettings.getClusterName());
consoleLogAppender.accept("Starting ECS Task to perform current job...");
LOG.debug(format("[create-agent] Starting task : {0} ", startTaskRequest.toString()));
StartTaskResult startTaskResult = pluginSettings.ecsClient().startTask(startTaskRequest);
LOG.debug("[create-agent] Done executing start task request.");
if (isStarted(startTaskResult)) {
String message = elasticAgentProfileProperties.runAsSpotInstance() ? "[WARNING] The ECS task is scheduled on a Spot Instance. A spot instance termination would re-schedule the job." : String.format("ECS Task %s scheduled on container instance %s.", taskName, containerInstance.get().getEc2InstanceId());
consoleLogAppender.accept(message);
LOG.info(format("[create-agent] Task {0} scheduled on container instance {1}", taskName, containerInstance.get().getEc2InstanceId()));
return Optional.of(new ECSTask(startTaskResult.getTasks().get(0), taskDefinitionFromNewTask, elasticAgentProfileProperties, createAgentRequest.getJobIdentifier(), createAgentRequest.environment(), containerInstance.get().getEc2InstanceId()));
} else {
deregisterTaskDefinition(pluginSettings, taskDefinitionFromNewTask.getTaskDefinitionArn());
String errors = startTaskResult.getFailures().stream().map(failure -> " " + failure.getArn() + " failed with reason :" + failure.getReason()).collect(Collectors.joining("\n"));
throw new ContainerFailedToRegisterException("Fail to start task " + taskName + ":\n" + errors);
}
}
use of com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest in project gocd-ecs-elastic-agent by gocd.
the class CreateAgentRequestExecutorTest method shouldAskECSTaskToCreateAnAgent.
@Test
void shouldAskECSTaskToCreateAnAgent() throws Exception {
ClusterProfileProperties settings = mock(ClusterProfileProperties.class);
CreateAgentRequest request = mock(CreateAgentRequest.class);
when(request.clusterProfileProperties()).thenReturn(settings);
when(request.getJobIdentifier()).thenReturn(new JobIdentifier("test-pipeline", 1L, "Test Pipeline", "test-stage", "1", "test-job", 100L));
when(request.elasticProfile()).thenReturn(new ElasticAgentProfileProperties());
ECSTasks agentInstances = mock(ECSTasks.class);
PluginRequest pluginRequest = mock(PluginRequest.class);
final EventStream eventStream = mock(EventStream.class);
new CreateAgentRequestExecutor(request, agentInstances, pluginRequest, eventStream).execute();
verify(agentInstances).create(eq(request), eq(settings), any(ConsoleLogAppender.class));
}
use of com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest in project gocd-ecs-elastic-agent by gocd.
the class ServerPingRequestExecutorTest method testShouldTerminateAgentsThatNeverAutoRegistered.
@Test
void testShouldTerminateAgentsThatNeverAutoRegistered() throws Exception {
final ECSTask task = mock(ECSTask.class);
when(task.name()).thenReturn("task-name");
when(pluginRequest.listAgents()).thenReturn(new Agents(new ArrayList<>()));
verifyNoMoreInteractions(pluginRequest);
when(taskHelper.create(any(CreateAgentRequest.class), eq(clusterProfileProperties), any(ConsoleLogAppender.class))).thenReturn(Optional.of(task));
agentInstances.clock = new Clock.TestClock().forward(Period.minutes(11));
Optional<ECSTask> container = agentInstances.create(new CreateAgentRequest(null, elasticAgentProfileProperties, null, null), clusterProfileProperties, consoleLogAppender);
executor.execute();
assertThat(agentInstances.hasInstance(container.get().name())).isFalse();
}
Aggregations