private List<BlockDeviceMapping> getNewEphemeralDeviceMapping() {

    final List<BlockDeviceMapping> oldDeviceMapping = getAmiBlockDeviceMappings();

    final Set<String> occupiedDevices = new HashSet<String>();
    for (final BlockDeviceMapping mapping : oldDeviceMapping) {

      occupiedDevices.add(mapping.getDeviceName());
    }

    final List<String> available =
        new ArrayList<String>(
            Arrays.asList("ephemeral0", "ephemeral1", "ephemeral2", "ephemeral3"));

    final List<BlockDeviceMapping> newDeviceMapping = new ArrayList<BlockDeviceMapping>(4);
    for (char suffix = 'b'; suffix <= 'z' && !available.isEmpty(); suffix++) {

      final String deviceName = String.format("/dev/xvd%s", suffix);

      if (occupiedDevices.contains(deviceName)) continue;

      final BlockDeviceMapping newMapping =
          new BlockDeviceMapping().withDeviceName(deviceName).withVirtualName(available.get(0));

      newDeviceMapping.add(newMapping);
      available.remove(0);
    }

    return newDeviceMapping;
  }
 public Set<String> parseSecurityGroups() {
   if (securityGroups == null || "".equals(securityGroups.trim())) {
     return Collections.emptySet();
   } else {
     return new HashSet<String>(Arrays.asList(securityGroups.split("\\s*,\\s*")));
   }
 }
  @Inject
  public AwsEc2UnicastHostsProvider(
      Settings settings,
      TransportService transportService,
      AwsEc2Service awsEc2Service,
      Version version) {
    super(settings);
    this.transportService = transportService;
    this.client = awsEc2Service.client();
    this.version = version;

    this.hostType =
        HostType.valueOf(
            settings.get("discovery.ec2.host_type", "private_ip").toUpperCase(Locale.ROOT));

    this.bindAnyGroup = settings.getAsBoolean("discovery.ec2.any_group", true);
    this.groups = new HashSet<>();
    groups.addAll(Arrays.asList(settings.getAsArray("discovery.ec2.groups")));

    this.tags = settings.getByPrefix("discovery.ec2.tag.").getAsMap();

    Set<String> availabilityZones = new HashSet();
    availabilityZones.addAll(
        Arrays.asList(settings.getAsArray("discovery.ec2.availability_zones")));
    if (settings.get("discovery.ec2.availability_zones") != null) {
      availabilityZones.addAll(
          Strings.commaDelimitedListToSet(settings.get("discovery.ec2.availability_zones")));
    }
    this.availabilityZones = availabilityZones;

    if (logger.isDebugEnabled()) {
      logger.debug(
          "using host_type [{}], tags [{}], groups [{}] with any_group [{}], availability_zones [{}]",
          hostType,
          tags,
          groups,
          bindAnyGroup,
          availabilityZones);
    }
  }
Exemple #4
0
  @Action(
      value = "/manage/importEC2Key",
      results = {
        @Result(name = "input", location = "/manage/view_ec2_keys.jsp"),
        @Result(name = "success", location = "/manage/viewEC2Keys.action", type = "redirect")
      })
  public String importEC2Key() {

    String retVal = SUCCESS;

    try {
      // get AWS credentials from DB
      AWSCred awsCred = AWSCredDB.getAWSCred();

      // set  AWS credentials for service
      BasicAWSCredentials awsCredentials =
          new BasicAWSCredentials(awsCred.getAccessKey(), awsCred.getSecretKey());

      // create service
      AmazonEC2 service = new AmazonEC2Client(awsCredentials);
      service.setEndpoint(ec2Key.getEc2Region());

      // describe key pair request
      DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
      describeKeyPairsRequest.setKeyNames(Arrays.asList(ec2Key.getKeyNm()));

      // call service
      DescribeKeyPairsResult describeKeyPairsResult =
          service.describeKeyPairs(describeKeyPairsRequest);

      if (describeKeyPairsResult != null && describeKeyPairsResult.getKeyPairs().size() > 0) {
        // add to db
        Long keyId = EC2KeyDB.saveEC2Key(ec2Key);
        SSHUtil.storePrivateKey(keyId.toString(), ec2Key.getPrivateKey().trim());
      } else {
        addActionError("Imported key does not exist on AWS");
        retVal = INPUT;
      }

    } catch (AmazonServiceException ex) {
      addActionError(ex.getMessage());
      retVal = INPUT;
    }

    return retVal;
  }