Example usage for com.amazonaws.services.ecs AmazonECSClient runTask

List of usage examples for com.amazonaws.services.ecs AmazonECSClient runTask

Introduction

In this page you can find the example usage for com.amazonaws.services.ecs AmazonECSClient runTask.

Prototype

@Override
public RunTaskResult runTask(RunTaskRequest request) 

Source Link

Document

Starts a new task using the specified task definition.

Usage

From source file:com.cloudbees.jenkins.plugins.amazonecs.ECSService.java

License:Open Source License

String runEcsTask(final ECSSlave slave, final ECSTaskTemplate template, String clusterArn,
        Collection<String> command) throws IOException, AbortException {
    AmazonECSClient client = getAmazonECSClient();
    String definitionArn = template.getTaskDefinitionArn();
    slave.setTaskDefinitonArn(definitionArn);

    KeyValuePair envNodeName = new KeyValuePair();
    envNodeName.setName("SLAVE_NODE_NAME");
    envNodeName.setValue(slave.getComputer().getName());

    KeyValuePair envNodeSecret = new KeyValuePair();
    envNodeSecret.setName("SLAVE_NODE_SECRET");
    envNodeSecret.setValue(slave.getComputer().getJnlpMac());

    final RunTaskResult runTaskResult = client
            .runTask(new RunTaskRequest().withTaskDefinition(definitionArn)
                    .withOverrides(new TaskOverride().withContainerOverrides(new ContainerOverride()
                            .withName(template.getFullQualifiedTemplateName(slave.getCloud()))
                            .withCommand(command).withEnvironment(envNodeName).withEnvironment(envNodeSecret)))
                    .withCluster(clusterArn));

    if (!runTaskResult.getFailures().isEmpty()) {
        LOGGER.log(Level.WARNING, "Slave {0} - Failure to run task with definition {1} on ECS cluster {2}",
                new Object[] { slave.getNodeName(), definitionArn, clusterArn });
        for (Failure failure : runTaskResult.getFailures()) {
            LOGGER.log(Level.WARNING, "Slave {0} - Failure reason={1}, arn={2}",
                    new Object[] { slave.getNodeName(), failure.getReason(), failure.getArn() });
        }//w w  w . j  a va2 s.com
        throw new AbortException("Failed to run slave container " + slave.getNodeName());
    }
    return runTaskResult.getTasks().get(0).getTaskArn();
}