Search in sources :

Example 1 with LINUX

use of com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.LINUX 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);
    }
}
Also used : Optional.empty(java.util.Optional.empty) com.amazonaws.services.ecs.model(com.amazonaws.services.ecs.model) LimitExceededException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.LimitExceededException) java.util(java.util) ContainerInstanceFailedToRegisterException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerInstanceFailedToRegisterException) InstanceSelectionStrategyFactory(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategyFactory) Collectors(java.util.stream.Collectors) MessageFormat(java.text.MessageFormat) Constants(com.thoughtworks.gocd.elasticagent.ecs.Constants) ECSElasticPlugin.getServerId(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.getServerId) MessageFormat.format(java.text.MessageFormat.format) com.thoughtworks.gocd.elasticagent.ecs.domain(com.thoughtworks.gocd.elasticagent.ecs.domain) ContainerFailedToRegisterException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerFailedToRegisterException) CreateAgentRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) LOG(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG) AmazonECS(com.amazonaws.services.ecs.AmazonECS) StringUtils.equalsIgnoreCase(org.apache.commons.lang3.StringUtils.equalsIgnoreCase) LINUX(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.LINUX) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) ContainerFailedToRegisterException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerFailedToRegisterException)

Aggregations

AmazonECS (com.amazonaws.services.ecs.AmazonECS)1 com.amazonaws.services.ecs.model (com.amazonaws.services.ecs.model)1 Constants (com.thoughtworks.gocd.elasticagent.ecs.Constants)1 LOG (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG)1 ECSElasticPlugin.getServerId (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.getServerId)1 ECSTask (com.thoughtworks.gocd.elasticagent.ecs.ECSTask)1 InstanceSelectionStrategyFactory (com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategyFactory)1 com.thoughtworks.gocd.elasticagent.ecs.domain (com.thoughtworks.gocd.elasticagent.ecs.domain)1 LINUX (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.LINUX)1 ContainerFailedToRegisterException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerFailedToRegisterException)1 ContainerInstanceFailedToRegisterException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerInstanceFailedToRegisterException)1 LimitExceededException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.LimitExceededException)1 CreateAgentRequest (com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest)1 MessageFormat (java.text.MessageFormat)1 MessageFormat.format (java.text.MessageFormat.format)1 java.util (java.util)1 Optional.empty (java.util.Optional.empty)1 Collectors (java.util.stream.Collectors)1 StringUtils.equalsIgnoreCase (org.apache.commons.lang3.StringUtils.equalsIgnoreCase)1