Esempio n. 1
0
  void deleteTask(String taskArn) {
    final AmazonECSClient client = getAmazonECSClient();

    LOGGER.log(Level.INFO, "Delete ECS Slave task: {0}", taskArn);
    try {
      client.stopTask(new StopTaskRequest().withTask(taskArn));
    } catch (ClientException e) {
      LOGGER.log(
          Level.SEVERE,
          "Couldn't stop task arn " + taskArn + " caught exception: " + e.getMessage(),
          e);
    }
  }
    public FormValidation doTestConnection(
        @QueryParameter String accessKeyId,
        @QueryParameter String secretAccessKey,
        @QueryParameter String ecsEndPoint) {

      if (accessKeyId.isEmpty() || secretAccessKey.isEmpty()) {
        logger.warning("No Credentials provided, using  DefaultAWSCredentialsProviderChain ");
      }
      logger.warning("ec2EndPoint=" + ecsEndPoint);
      ecsEndPoint = ecsEndPoint == null ? "" : ecsEndPoint;
      AmazonECSClient client = EcsCloud.getEcsClient(accessKeyId, secretAccessKey, ecsEndPoint);
      ListClustersResult clusters = client.listClusters();
      logger.log(
          Level.SEVERE, "Clusters: " + clusters.getClusterArns().size() + " clusters:" + clusters);
      ListContainerInstancesResult result = client.listContainerInstances();

      return FormValidation.ok(
          "Success. Number of container instances: " + result.getContainerInstanceArns().size());
    }
  public AmazonECSClient getEcsClient() {
    AmazonECSClient client =
        getAwsCredentials() == null
            ? new AmazonECSClient()
            : new AmazonECSClient(getAwsCredentials());

    String endpoint =
        Strings.isNullOrEmpty(this.getEcsEndPoint())
            ? System.getenv("AWS_ECS_ENDPOINT")
            : this.getEcsEndPoint();

    if (Strings.isNullOrEmpty(endpoint)) {
      endpoint = Constants.AWS_ECS_ENDPOINT;
    }
    client.setEndpoint(endpoint);
    logger.log(Level.SEVERE, "AWS ECS Endpoint: " + endpoint);

    return client;
  }
  public static AmazonECSClient getEcsClient(
      String accessKeyId, String secretAccessKey, String ecsEndPoint) {
    AmazonECSClient client;
    if (Strings.isNullOrEmpty(accessKeyId) || Strings.isNullOrEmpty(secretAccessKey)) {
      logger.log(Level.SEVERE, "No Access Key provided");
      client = new AmazonECSClient();
    } else {
      client = new AmazonECSClient(new BasicAWSCredentials(accessKeyId, secretAccessKey));
    }
    String endpoint =
        Strings.isNullOrEmpty(ecsEndPoint) ? System.getenv("AWS_ECS_ENDPOINT") : ecsEndPoint;

    if (Strings.isNullOrEmpty(endpoint)) {
      endpoint = Constants.AWS_ECS_ENDPOINT;
    }
    client.setEndpoint(endpoint);
    logger.log(Level.SEVERE, "AWS ECS Endpoint: " + endpoint);

    return client;
  }
Esempio n. 5
0
    public ListBoxModel doFillClusterItems(
        @QueryParameter String credentialsId, @QueryParameter String regionName) {
      try {
        final AmazonECSClient client = getAmazonECSClient(credentialsId, regionName);

        final ListBoxModel options = new ListBoxModel();
        for (String arn : client.listClusters().getClusterArns()) {
          options.add(arn);
        }
        return options;
      } catch (RuntimeException e) {
        // missing credentials will throw an "AmazonClientException: Unable to load AWS credentials
        // from any provider in the chain"
        LOGGER.log(
            Level.INFO,
            "Exception searching clusters for credentials="
                + credentialsId
                + ", regionName="
                + regionName,
            e);
        return new ListBoxModel();
      }
    }
Esempio n. 6
0
 private static AmazonECSClient getAmazonECSClient(String credentialsId, String regionName) {
   final AmazonECSClient client;
   AmazonWebServicesCredentials credentials = getCredentials(credentialsId);
   if (credentials == null) {
     // no credentials provided, rely on com.amazonaws.auth.DefaultAWSCredentialsProviderChain
     // to use IAM Role define at the EC2 instance level ...
     client = new AmazonECSClient();
   } else {
     if (LOGGER.isLoggable(Level.FINE)) {
       String awsAccessKeyId = credentials.getCredentials().getAWSAccessKeyId();
       String obfuscatedAccessKeyId =
           StringUtils.left(awsAccessKeyId, 4)
               + StringUtils.repeat("*", awsAccessKeyId.length() - (2 * 4))
               + StringUtils.right(awsAccessKeyId, 4);
       LOGGER.log(
           Level.FINE,
           "Connect to Amazon ECS with IAM Access Key {1}",
           new Object[] {obfuscatedAccessKeyId});
     }
     client = new AmazonECSClient(credentials);
   }
   client.setRegion(getRegion(regionName));
   return client;
 }
Esempio n. 7
0
    public Node call() throws Exception {

      String uniq = Long.toHexString(System.nanoTime());
      ECSSlave slave =
          new ECSSlave(
              ECSCloud.this,
              name + "-" + uniq,
              template.getRemoteFSRoot(),
              label == null ? null : label.toString(),
              new JNLPLauncher());
      Jenkins.getInstance().addNode(slave);
      LOGGER.log(Level.INFO, "Created Slave: {0}", slave.getNodeName());

      final AmazonECSClient client = getAmazonECSClient();
      LOGGER.log(Level.INFO, "Selected Region: {0}", getRegionName());
      client.setRegion(getRegion(getRegionName()));

      Collection<String> command = getDockerRunCommand(slave);
      String definitionArn = template.getTaskDefinitionArn();
      slave.setTaskDefinitonArn(definitionArn);

      final RunTaskResult runTaskResult =
          client.runTask(
              new RunTaskRequest()
                  .withTaskDefinition(definitionArn)
                  .withOverrides(
                      new TaskOverride()
                          .withContainerOverrides(
                              new ContainerOverride()
                                  .withName("jenkins-slave")
                                  .withCommand(command)))
                  .withCluster(cluster));

      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, cluster});
        for (Failure failure : runTaskResult.getFailures()) {
          LOGGER.log(
              Level.WARNING,
              "Slave {0} - Failure reason={1}, arn={2}",
              new Object[] {slave.getNodeName(), failure.getReason(), failure.getArn()});
        }
        throw new AbortException("Failed to run slave container " + slave.getNodeName());
      }

      String taskArn = runTaskResult.getTasks().get(0).getTaskArn();
      LOGGER.log(
          Level.INFO,
          "Slave {0} - Slave Task Started : {1}",
          new Object[] {slave.getNodeName(), taskArn});
      slave.setTaskArn(taskArn);

      int i = 0;
      int j = 100; // wait 100 seconds

      // now wait for slave to be online
      for (; i < j; i++) {
        if (slave.getComputer() == null) {
          throw new IllegalStateException(
              "Slave " + slave.getNodeName() + " - Node was deleted, computer is null");
        }
        if (slave.getComputer().isOnline()) {
          break;
        }
        LOGGER.log(
            Level.FINE,
            "Waiting for slave {0} (ecs task {1}) to connect ({2}/{3}).",
            new Object[] {slave.getNodeName(), taskArn, i, j});
        Thread.sleep(1000);
      }
      if (!slave.getComputer().isOnline()) {
        throw new IllegalStateException(
            "ECS Slave "
                + slave.getNodeName()
                + " (ecs task "
                + taskArn
                + ") is not connected after "
                + j
                + " seconds");
      }

      LOGGER.log(
          Level.INFO, "ECS Slave " + slave.getNodeName() + " (ecs task {0}) connected", taskArn);
      return slave;
    }