com.amazonaws.services.ec2.model.Instance Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.Instance. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseTest.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Describe instances.
 *
 * @param instanceIds            instances' IDs
 * @param enableLogging            log to standard out
 * @return list of instances
 */
protected final List<Instance> describeInstances(
        final Collection<String> instanceIds, final boolean enableLogging) {
    if (enableLogging) {
        log.info("Describe instances:" + toString(instanceIds));
    }
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.setInstanceIds(instanceIds);
    DescribeInstancesResult result = amazonEC2Client
            .describeInstances(request);
    Assert.assertTrue(result.getReservations().size() > 0);

    List<Instance> instanceList = new ArrayList<Instance>();

    for (Reservation reservation : result.getReservations()) {
        List<Instance> instances = reservation.getInstances();

        if (null != instances) {
            for (Instance i : instances) {
                instanceList.add(i);
            }
        }
    }

    return instanceList;
}
 
Example #2
Source File: AutomationReaperTask.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void doWork() {
    log.info("Running " + AutomationReaperTask.NAME);
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
    Filter filter = new Filter("tag:LaunchSource");
    filter.withValues("SeleniumGridScalerPlugin");
    describeInstancesRequest.withFilters(filter);
    List<Reservation> reservations = ec2.describeInstances(describeInstancesRequest);
    for(Reservation reservation : reservations) {
        for(Instance instance : reservation.getInstances()) {
            // Look for orphaned nodes
            Date threshold = AutomationUtils.modifyDate(new Date(),-30, Calendar.MINUTE);
            String instanceId = instance.getInstanceId();
            // If we found a node old enough AND we're not internally tracking it, this means this is an orphaned node and we should terminate it
            if(threshold.after(instance.getLaunchTime()) && !AutomationContext.getContext().nodeExists(instanceId)) {
                log.info("Terminating orphaned node: " + instanceId);
                ec2.terminateInstance(instanceId);
            }
        }
    }
}
 
Example #3
Source File: AWSProvider.java    From testgrid with Apache License 2.0 6 votes vote down vote up
/**
 *
 * Logs are retrieved via aws api that looks similar to this cli command:
 * aws ec2 get-console-output --instance-id i-123456789abcd --output text
 *
 * This won't contain the entire ec2 instance console (system) log.
 * It'll be truncated to last {@link #EC2_SYSTEM_LOG_NO_OF_LINES} lines.
 *
 * @param instance the ec2 instance object
 * @param amazonEC2 AmazonEC2 command handler
 * @return return string will be of following format:
 * <pre>
 *  ${ec2-nickname} logs {
 *      <br/>
 *      ${truncated-logs}
 *      <br/>
 *  }
 * </pre>
 */
private String getEC2InstanceConsoleLogs(Instance instance, AmazonEC2 amazonEC2) {
    String decodedOutput;
    String instanceName = instance.getTags().stream()
            .filter(t -> t.getKey().equals("Name"))
            .map(com.amazonaws.services.ec2.model.Tag::getValue)
            .findAny().orElse("<name-empty>");
    try {
        GetConsoleOutputRequest consoleOutputRequest = new GetConsoleOutputRequest(instance.getInstanceId());
        final GetConsoleOutputResult consoleOutputResult = amazonEC2.getConsoleOutput(consoleOutputRequest);
        decodedOutput = consoleOutputResult.getDecodedOutput();
        decodedOutput = reduceLogVerbosity(decodedOutput);

    } catch (NullPointerException e) {
        String error = e.getMessage() +
                (e.getStackTrace().length > 0 ? "at " + e.getStackTrace()[0].toString() : "");
        decodedOutput = "Error occurred while retrieving instance console logs for " + instance.getInstanceId() +
                ". Error: " + error;
    }

    return instanceName + " logs {\n" +
            decodedOutput + "\n" +
            "}\n";
}
 
Example #4
Source File: AWSProvider.java    From testgrid with Apache License 2.0 6 votes vote down vote up
/**
 * Get logs of all the ec2 instances created by this #stackName.
 *
 * @param stackName the stack that created the ec2 instances
 * @param region aws region of the stack
 */
private void getAllEC2InstanceConsoleLogs(String stackName, String region) {
    try {
        final DescribeInstancesResult result = getDescribeInstancesResult(stackName, region);
        final long instanceCount = result.getReservations().stream()
                .map(Reservation::getInstances)
                .mapToLong(Collection::size)
                .sum();

        logger.info("");
        logger.info("Downloading logs of " + instanceCount + " EC2 instances in this AWS Cloudformation stack: {");
        for (Reservation reservation : result.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                final String logs = getEC2InstanceConsoleLogs(instance, getAmazonEC2(region));
                logger.info(logs);
            }
        }
        logger.info("}");
        logger.info("Further details about this EC2 instance console outputs can be found at: "
                + "https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-console.html");
        logger.info("");
    } catch (RuntimeException e) {
        logger.warn("Error while trying to download the instance console output from ec2 instances of {}@{}. "
                + "Error: ", stackName, region, e.getMessage());
    }
}
 
Example #5
Source File: AWSProvider.java    From testgrid with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a ssh login command for *nix to access the given ec2 instance.
 *
 * @param instance the ec2 instance
 * @return ssh login command.
 */
private String getLoginCommand(Instance instance) {
    final String privateIpAddress = instance.getPrivateIpAddress();
    String publicAddress = instance.getPublicDnsName();
    publicAddress = publicAddress == null ? instance.getPublicIpAddress() : publicAddress;
    final String keyName = instance.getKeyName();
    String instanceName = instance.getTags().stream()
            .filter(t -> t.getKey().equals("Name"))
            .map(com.amazonaws.services.ec2.model.Tag::getValue)
            .findAny().orElse("");
    instanceName = instanceName.isEmpty() ? "" : "# name: " + instanceName;
    String platform = instance.getPlatform();
    platform = platform == null || platform.isEmpty() ? "" : "# platform: " + platform;

    String ip;
    if (publicAddress != null && !publicAddress.isEmpty()) {
        ip = publicAddress;
    } else {
        ip = privateIpAddress;
    }

    //root user is assumed. EC2 instances print the actual user name when tried to log-in as the root user.
    return "ssh -i " + keyName + " root@" + ip + ";   " + instanceName + platform;
}
 
Example #6
Source File: AutomationReaperTaskTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
// Tests that a node that is being tracked internally is not shut down
public void testNoShutdownNodeTracked() {
    MockVmManager ec2 = new MockVmManager();
    Reservation reservation = new Reservation();
    Instance instance = new Instance();
    String instanceId = "foo";
    AutomationContext.getContext().addNode(new AutomationDynamicNode("faky",instanceId,null,null,new Date(),1));
    instance.setInstanceId(instanceId);
    instance.setLaunchTime(AutomationUtils.modifyDate(new Date(),-5,Calendar.HOUR));
    reservation.setInstances(Arrays.asList(instance));
    ec2.setReservations(Arrays.asList(reservation));
    AutomationReaperTask task = new AutomationReaperTask(null,ec2);
    task.run();
    Assert.assertFalse("Node should NOT be terminated as it was tracked internally", ec2.isTerminated());
}
 
Example #7
Source File: GobblinAWSClusterLauncher.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
private String getMasterPublicIp() {
  final long startTime = System.currentTimeMillis();
  final long launchTimeout = TimeUnit.MINUTES.toMillis(10);
  boolean isMasterLaunched = false;
  List<Instance> instanceIds = Collections.emptyList();
  while (!isMasterLaunched && (System.currentTimeMillis() - startTime) < launchTimeout) {
    try {
      Thread.sleep(5000);
    } catch (InterruptedException e) {
      throw new RuntimeException("Interrupted while waiting for cluster master to boot up", e);
    }
    instanceIds = this.awsSdkClient.getInstancesForGroup(this.masterAutoScalingGroupName, "running");
    isMasterLaunched = instanceIds.size() > 0;
  }

  if (!isMasterLaunched) {
    throw new RuntimeException("Timed out while waiting for cluster master. "
        + "Check for issue manually for ASG: " + this.masterAutoScalingGroupName);
  }

  // This will change if cluster master restarts, but that will be handled by Helix events
  // TODO: Add listener to Helix / Zookeeper for master restart and update master public ip
  // .. although we do not use master public ip for anything
  return instanceIds.get(0).getPublicIpAddress();
}
 
Example #8
Source File: AbstractEsInstanceFactory.java    From soundwave with Apache License 2.0 6 votes vote down vote up
protected HashMap getAwsInstanceProperties(Instance awsInstance) throws Exception {
  HashMap map = mapper.readValue(mapper.writeValueAsString(awsInstance), HashMap.class);

  if (awsInstance.getMonitoring() != null && awsInstance.getMonitoring().getState() != null) {
    //Have to comply with the current AWS_V1 schema
    map.put("monitoring", awsInstance.getMonitoring().getState().toString());
  }

  if (awsInstance.getPlacement() != null
      && awsInstance.getPlacement().getAvailabilityZone() != null) {
    //Be backward compatible for tools
    Map placement = (Map) map.get("placement");
    if (placement != null) {
      placement.put("availability_zone", awsInstance.getPlacement().getAvailabilityZone());
    }
  }
  return map;
}
 
Example #9
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 6 votes vote down vote up
public List<ServerEnvironment> getEnvironmentsByTag(Region region, String searchTag) {
    LOGGER.info("getEnvironmentsByTag " + region + " tag: " + searchTag);
    List<ServerEnvironment> environments = new ArrayList<ServerEnvironment>();

    ec2.setRegion(region);
    DescribeInstancesResult instances = ec2.describeInstances();
    for (Reservation reservation : instances.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            for (Tag tag : instance.getTags()) {
                if (tag.getValue().equalsIgnoreCase(searchTag)) {
                    environments.add(getEnvironmentFromInstance(instance));
                }
            }
        }
    }
    return environments;
}
 
Example #10
Source File: ReconcileWithAwsJob.java    From soundwave with Apache License 2.0 6 votes vote down vote up
private void logReconcileStats(Collection<Instance> ec2Instances,
                               Collection<EsInstance> cmdbInstances) {

  int ec2RunningCount = 0;
  for (Instance inst : ec2Instances) {
    if (inst.getState().getCode() == 16) {
      //EC2 API may return some terminated instances. Only get the running count
      ec2RunningCount++;
    }
  }
  Stats.setGauge(StatsUtil.getStatsName("awsreconcile", "ec2TotalRunningCount"), ec2RunningCount);
  Stats.setGauge(StatsUtil.getStatsName("awsreconcile", "cmdbTotalRunningCount"),
      cmdbInstances.size());
  Stats.setGauge(StatsUtil.getStatsName("awsreconcile", "diffcount"),
      Math.abs(ec2RunningCount - cmdbInstances.size()));

  int serviceMappingMissingCount = 0;
  for (EsInstance instance : cmdbInstances) {
    /*if (instance.getServiceMappings() == null || instance.getServiceMappings().length == 0) {
      serviceMappingMissingCount++;
    }*/
  }
  Stats.setGauge(StatsUtil.getStatsName("awsreconcile", "servicemappingmissingcount"),
      serviceMappingMissingCount);

}
 
Example #11
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 6 votes vote down vote up
@Override
public boolean tagEnvironmentWithVersion(Region region, DeployJobVariables jobVariables) {
    String searchTag = jobVariables.getEnvironment();
    String version = jobVariables.getVersion();
    LOGGER.info("tagEnvironmentWithVersion " + region + " Tag " + searchTag + " version " + version);

    boolean environmentSuccessfulTagged = false;
    ec2.setRegion(region);
    DescribeInstancesResult instances = ec2.describeInstances();
    for (Reservation reservation : instances.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            for (Tag tag : instance.getTags()) {
                if (tag.getValue().equalsIgnoreCase(searchTag)) {
                    CreateTagsRequest createTagsRequest = new CreateTagsRequest();
                    createTagsRequest.withResources(instance.getInstanceId()).withTags(new Tag(VERSION_TAG, version));
                    LOGGER.info("Create Tag " + version + " for instance " + instance.getInstanceId());
                    ec2.createTags(createTagsRequest);
                    environmentSuccessfulTagged = true;
                }
            }
        }
    }
    return environmentSuccessfulTagged;
}
 
Example #12
Source File: UploadTagsGenerator.java    From soundwave with Apache License 2.0 6 votes vote down vote up
/**
 * Return a list of tags that need to be updated to the Ec2Instance.
 * The tags are either not in Ec2Instance tags or having different
 * values
 * @param ec2Instance
 * @param esInstance
 * @return A list of tags
 */
public List<Tag> getUpdateTags(Instance ec2Instance, EsInstance esInstance) {
  Preconditions.checkNotNull(ec2Instance);
  Preconditions.checkNotNull(esInstance);
  List<Tag> updateTags = new ArrayList<>();

  List<Tag> currentEc2Tag = ec2Instance.getTags();
  List<Tag> esUploadTags = getUpdateTags(esInstance);

  for (Tag tag : esUploadTags) {
    boolean shouldUpdate = true;
    for (Tag ec2Tag : currentEc2Tag) {
      if (ec2Tag.getKey().equals(tag.getKey()) && ec2Tag.getValue().equals(tag.getValue())) {
        shouldUpdate = false;
        break;
      }
    }

    if (shouldUpdate) {
      updateTags.add(tag);
    }
  }

  return updateTags;

}
 
Example #13
Source File: EC2Communication.java    From development with Apache License 2.0 6 votes vote down vote up
public String getInstanceState(String instanceId) {
    LOGGER.debug("getInstanceState('{}') entered", instanceId);
    DescribeInstancesResult result = getEC2().describeInstances(
            new DescribeInstancesRequest().withInstanceIds(instanceId));
    List<Reservation> reservations = result.getReservations();
    Set<Instance> instances = new HashSet<Instance>();

    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
        if (instances.size() > 0) {
            String state = instances.iterator().next().getState().getName();
            LOGGER.debug("  InstanceState: {}", state);
            return state;
        }
    }
    LOGGER.debug("getInstanceState('{}') left", instanceId);
    return null;
}
 
Example #14
Source File: EC2FleetCloudTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void update_shouldAddNodeWithScaledNumExecutors_whenWeightPresentAndEnabled() throws IOException {
    // given
    when(ec2Api.connect(any(String.class), any(String.class), anyString())).thenReturn(amazonEC2);

    final String instanceType = "t";
    final String instanceId = "i-0";
    final Instance instance = new Instance()
            .withPublicIpAddress("p-ip")
            .withInstanceType(instanceType)
            .withInstanceId(instanceId);

    when(ec2Api.describeInstances(any(AmazonEC2.class), any(Set.class))).thenReturn(
            ImmutableMap.of(instanceId, instance));

    PowerMockito.when(FleetStateStats.readClusterState(any(AmazonEC2.class), anyString(), anyString()))
            .thenReturn(new FleetStateStats("fleetId", 0, "active",
                    ImmutableSet.of(instanceId),
                    ImmutableMap.of(instanceType, 2.0)));

    mockNodeCreatingPart();

    EC2FleetCloud fleetCloud = new EC2FleetCloud(null, null, "credId", null, "region",
            "", "fleetId", "", null, PowerMockito.mock(ComputerConnector.class), false,
            false, 0, 0, 1, 1, false,
            true, false,
            0, 0, true, 10, false);

    ArgumentCaptor<Node> nodeCaptor = ArgumentCaptor.forClass(Node.class);
    doNothing().when(jenkins).addNode(nodeCaptor.capture());

    // when
    fleetCloud.update();

    // then
    Node actualFleetNode = nodeCaptor.getValue();
    assertEquals(2, actualFleetNode.getNumExecutors());
}
 
Example #15
Source File: EC2ConnectorTest.java    From jenkins-deployment-dashboard-plugin with MIT License 5 votes vote down vote up
@Test
public void testGetEnvironmentFromInstance() throws Exception {
    final Date launchTime = new Date();
    final Instance instance = new Instance().withInstanceId("instance").withInstanceType(InstanceType.C1Xlarge).withTags(new Tag(EC2Connector.DEFAULT_INSTANCE_NAME_TAG, "unknown"))
            .withState(new InstanceState().withName(InstanceStateName.Running)).withLaunchTime(launchTime).withPublicIpAddress("127.0.0.1");
    ServerEnvironment serverEnv = Whitebox.<ServerEnvironment> invokeMethod(env, "getEnvironmentFromInstance", instance);
    assertThat(serverEnv, notNullValue());
    assertThat(serverEnv.getInstanceId(), is("instance"));
    assertThat(serverEnv.getInstanceType(), is(InstanceType.C1Xlarge.toString()));
    assertThat(serverEnv.getType(), is(ENVIRONMENT_TYPES.TEST));
    assertThat(serverEnv.getEnvironmentTag(), is("unknown"));
    assertThat(serverEnv.getState().getName(), is(InstanceStateName.Running.toString()));
    assertThat(serverEnv.getLaunchTime(), is(launchTime));
    assertThat(serverEnv.getPublicIpAddress(), is("127.0.0.1"));
}
 
Example #16
Source File: EC2FleetCloudTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void update_shouldAddNodeWithNumExecutors_whenWeightPresentAndEnabledButForDiffType() throws IOException {
    // given
    when(ec2Api.connect(any(String.class), any(String.class), anyString())).thenReturn(amazonEC2);

    final String instanceType = "t";
    final String instanceId = "i-0";
    final Instance instance = new Instance()
            .withPublicIpAddress("p-ip")
            .withInstanceType(instanceType)
            .withInstanceId(instanceId);

    when(ec2Api.describeInstances(any(AmazonEC2.class), any(Set.class))).thenReturn(
            ImmutableMap.of(instanceId, instance));

    PowerMockito.when(FleetStateStats.readClusterState(any(AmazonEC2.class), anyString(), anyString()))
            .thenReturn(new FleetStateStats("fleetId", 0, "active",
                    ImmutableSet.of(instanceId),
                    ImmutableMap.of("diff-t", 2.0)));

    mockNodeCreatingPart();

    EC2FleetCloud fleetCloud = new EC2FleetCloud(null, null, "credId", null, "region",
            "", "fleetId", "", null, PowerMockito.mock(ComputerConnector.class), false,
            false, 0, 0, 1, 1, false,
            true, false,
            0, 0, true, 10, false);

    ArgumentCaptor<Node> nodeCaptor = ArgumentCaptor.forClass(Node.class);
    doNothing().when(jenkins).addNode(nodeCaptor.capture());

    // when
    fleetCloud.update();

    // then
    Node actualFleetNode = nodeCaptor.getValue();
    assertEquals(1, actualFleetNode.getNumExecutors());
}
 
Example #17
Source File: EC2ConnectorTest.java    From jenkins-deployment-dashboard-plugin with MIT License 5 votes vote down vote up
@Test
public void testGetEnvironmentFromInstanceProd() throws Exception {
    final Instance instance = new Instance().withInstanceId("instance").withInstanceType(InstanceType.C1Xlarge).withTags(new Tag(EC2Connector.DEFAULT_INSTANCE_NAME_TAG, EC2Connector.PROD_VALUE));
    ServerEnvironment serverEnv = Whitebox.<ServerEnvironment> invokeMethod(env, "getEnvironmentFromInstance", instance);
    assertThat(serverEnv, notNullValue());
    assertThat(serverEnv.getType(), is(ENVIRONMENT_TYPES.PRODUCTION));
}
 
Example #18
Source File: AwsInstanceConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Collection<String> instanceIdsWhichAreNotInCorrectState(List<CloudInstance> vms, AmazonEC2 amazonEC2Client, String state) {
    Set<String> instances = vms.stream().map(CloudInstance::getInstanceId).collect(Collectors.toCollection(HashSet::new));
    DescribeInstancesResult describeInstances = amazonEC2Client.describeInstances(
            new DescribeInstancesRequest().withInstanceIds(instances));
    for (Reservation reservation : describeInstances.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            if (state.equalsIgnoreCase(instance.getState().getName())) {
                instances.remove(instance.getInstanceId());
            }
        }
    }
    return instances;
}
 
Example #19
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 5 votes vote down vote up
public List<ServerEnvironment> getEnvironments(Region region) {
    List<ServerEnvironment> environments = new ArrayList<ServerEnvironment>();

    ec2.setRegion(region);
    DescribeInstancesResult instances = ec2.describeInstances();
    for (Reservation reservation : instances.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            environments.add(getEnvironmentFromInstance(instance));
        }
    }

    return environments;
}
 
Example #20
Source File: InstanceLookupTable.java    From graylog-plugin-aws with Apache License 2.0 5 votes vote down vote up
public DiscoveredInstance findByIp(String ip) {
    try {
        // Let's see if this is an EC2 instance maybe?
        if (ec2Instances.containsKey(ip)) {
            Instance instance = ec2Instances.get(ip);
            LOG.debug("Found IP [{}] in EC2 instance lookup table.", ip);
            return new DiscoveredEC2Instance(instance.getInstanceId(), getNameOfInstance(instance));
        }

        // If it's not an EC2 instance, we might still find it in our list of network interfaces.
        if(networkInterfaces.containsKey(ip)) {
            NetworkInterface iface = networkInterfaces.get(ip);
            switch (determineType(iface)) {
                case RDS:
                    return new DiscoveredRDSInstance(null, null);
                case EC2:
                    // handled directly via separate EC2 table above
                    break;
                case ELB:
                    return new DiscoveredELBInstance(getELBNameFromInterface(iface), null);
                case UNKNOWN:
                    LOG.debug("IP [{}] in table of network interfaces but of unknown instance type.", ip);
                    return DiscoveredInstance.UNDISCOVERED;
            }
        }

        // The IP address is not known to us. This most likely means it is an external public IP.
        return DiscoveredInstance.UNDISCOVERED;
    } catch(Exception e) {
        LOG.error("Error when trying to match IP to AWS instance. Marking as undiscovered.", e);
        return DiscoveredInstance.UNDISCOVERED;
    }
}
 
Example #21
Source File: AmiIdProviderImplTest.java    From fullstop with Apache License 2.0 5 votes vote down vote up
@Test
public void testAmiIdNotFound() throws Exception {

    when(ec2InstanceContextMock.getInstanceJson()).thenReturn("{json here");
    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);

    final DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(INSTANCE_ID);
    when(amazonEC2ClientMock.describeInstances(eq(describeInstancesRequest)))
            .thenReturn(new DescribeInstancesResult()
                    .withReservations(newArrayList(
                            new Reservation().withInstances(newArrayList(
                                    new Instance()
                                            .withInstanceId(INSTANCE_ID)
                                            .withImageId(IMAGE_ID)
                            ))
                    )));

    final Optional<String> result = amiIdProvider.apply(ec2InstanceContextMock);

    assertThat(result).isPresent();

    verify(ec2InstanceContextMock).getInstanceJson();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstances(eq(describeInstancesRequest));
}
 
Example #22
Source File: EC2Communication.java    From development with Apache License 2.0 5 votes vote down vote up
public String getPublicDNS(String instanceId) {
    DescribeInstancesResult result = getEC2().describeInstances(
            new DescribeInstancesRequest().withInstanceIds(instanceId));
    List<Reservation> reservations = result.getReservations();
    Set<Instance> instances = new HashSet<Instance>();

    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
        if (instances.size() > 0) {
            return instances.iterator().next().getPublicDnsName();
        }
    }
    return null;
}
 
Example #23
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
// Test the optional fields for launching a node are indeed optional
public void testLaunchNodesOptionalFieldsSet()  throws NodesCouldNotBeStartedException {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="chrome",os=null;
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    String securityGroup="securityGroup",subnetId="subnetId",keyName="keyName",linuxImage="linuxImage";
    properties.setProperty(region + "_security_group",securityGroup);
    properties.setProperty(region + "_subnet_id",subnetId);
    properties.setProperty(region + "_key_name", keyName);
    properties.setProperty(region + "_linux_node_ami", linuxImage);
    manageEC2.setUserData(userData);
    manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    RunInstancesRequest request = client.getRunInstancesRequest();
    Assert.assertEquals("Min count should match thread count requested",threadCount,request.getMinCount());
    Assert.assertEquals("Max count should match thread count requested",threadCount,request.getMaxCount());
    Assert.assertEquals("User data should match",userData,request.getUserData());
    Assert.assertEquals("Image id should match",linuxImage,request.getImageId());
    List<String> securityGroups = request.getSecurityGroupIds();
    Assert.assertEquals("Only one security group should be set",1,securityGroups.size());
    Assert.assertEquals("Only one security group should be set", securityGroup, securityGroups.get(0));
    Assert.assertEquals("Subnet ids should match", subnetId, request.getSubnetId());
    Assert.assertEquals("Key names should match", keyName, request.getKeyName());
}
 
Example #24
Source File: EC2FleetCloudTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void update_shouldAddNodeIfAnyNewDescribed_restrictUsage() throws IOException {
    // given
    when(ec2Api.connect(any(String.class), any(String.class), anyString())).thenReturn(amazonEC2);

    PowerMockito.when(FleetStateStats.readClusterState(any(AmazonEC2.class), anyString(), anyString()))
            .thenReturn(new FleetStateStats("fleetId", 0, "active",
                    ImmutableSet.of("i-0"), Collections.<String, Double>emptyMap()));

    final Instance instance = new Instance()
            .withPublicIpAddress("p-ip")
            .withInstanceId("i-0");

    when(ec2Api.describeInstances(any(AmazonEC2.class), any(Set.class))).thenReturn(
            ImmutableMap.of("i-0", instance));

    mockNodeCreatingPart();

    EC2FleetCloud fleetCloud = new EC2FleetCloud(null, null, "credId", null, "region",
            "", "fleetId", "", null, PowerMockito.mock(ComputerConnector.class), false,
            false, 0, 0, 1, 1, false,
            true, false,
            0, 0, false, 10, false);

    ArgumentCaptor<Node> nodeCaptor = ArgumentCaptor.forClass(Node.class);
    doNothing().when(jenkins).addNode(nodeCaptor.capture());

    // when
    FleetStateStats stats = fleetCloud.update();

    // then
    assertEquals(0, stats.getNumDesired());
    assertEquals(1, stats.getNumActive());
    assertEquals("fleetId", stats.getFleetId());

    // and
    Node actualFleetNode = nodeCaptor.getValue();
    assertEquals(Node.Mode.EXCLUSIVE, actualFleetNode.getMode());
}
 
Example #25
Source File: EC2FleetCloudTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void update_shouldAddNodeIfAnyNewDescribed() throws IOException {
    // given
    when(ec2Api.connect(any(String.class), any(String.class), anyString())).thenReturn(amazonEC2);

    final Instance instance = new Instance()
            .withPublicIpAddress("p-ip")
            .withInstanceId("i-0");

    when(ec2Api.describeInstances(any(AmazonEC2.class), any(Set.class))).thenReturn(
            ImmutableMap.of("i-0", instance));

    PowerMockito.when(FleetStateStats.readClusterState(any(AmazonEC2.class), anyString(), anyString()))
            .thenReturn(new FleetStateStats("fleetId", 0, "active",
                    ImmutableSet.of("i-0"), Collections.<String, Double>emptyMap()));

    mockNodeCreatingPart();

    EC2FleetCloud fleetCloud = new EC2FleetCloud(null, null, "credId", null, "region",
            "", "fleetId", "", null, PowerMockito.mock(ComputerConnector.class), false,
            false, 0, 0, 1, 1,
            false, false, false,
            0, 0, false, 10, false);

    ArgumentCaptor<Node> nodeCaptor = ArgumentCaptor.forClass(Node.class);
    doNothing().when(jenkins).addNode(nodeCaptor.capture());

    // when
    FleetStateStats stats = fleetCloud.update();

    // then
    assertEquals(0, stats.getNumDesired());
    assertEquals(1, stats.getNumActive());
    assertEquals("fleetId", stats.getFleetId());

    // and
    Node actualFleetNode = nodeCaptor.getValue();
    assertEquals(Node.Mode.NORMAL, actualFleetNode.getMode());
}
 
Example #26
Source File: AwsTagReporter.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Associates the correct tags for each instance passed in
 * @param instances
 */
private void associateTags(Collection<Instance> instances) {
    try{
        for(Instance instance : instances) {
            log.info("Associating tags to instance: " + instance.getInstanceId());
            String instanceId = instance.getInstanceId();
            setTagsForInstance(instanceId);
        }
        log.info("Tags added!");
    } catch(IndexOutOfBoundsException | ClassCastException e) {
        log.error("Error adding tags.  Please make sure your tag syntax is correct (refer to the readme)",e);
    }
}
 
Example #27
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
// Test if a bad OS is specified, it is handled correctly
public void testLaunchNodesBadOs()  throws NodesCouldNotBeStartedException{
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="chrome",os="badOs";
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    String securityGroup="securityGroup",subnetId="subnetId",keyName="keyName",windowsImage="windowsImage";
    properties.setProperty(region + "_security_group",securityGroup);
    properties.setProperty(region + "_subnet_id", subnetId);
    properties.setProperty(region + "_key_name", keyName);
    properties.setProperty(region + "_windows_node_ami", windowsImage);
    manageEC2.setUserData(userData);
    try{
        manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    } catch(RuntimeException e) {
        Assert.assertTrue("Failure message should be correct",e.getMessage().contains(os));
        return;
    }
    Assert.fail("Call should fail due to invalid OS");
}
 
Example #28
Source File: EC2ApiTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void describeInstances_shouldSendInOneCallNoMoreThenBatchSizeOfInstance() {
    // given
    Set<String> instanceIds = new HashSet<>();
    instanceIds.add("i1");
    instanceIds.add("i2");
    instanceIds.add("i3");

    DescribeInstancesResult describeInstancesResult1 =
            new DescribeInstancesResult()
                    .withReservations(
                            new Reservation().withInstances(new Instance()
                                            .withInstanceId("stopped")
                                            .withState(new InstanceState().withName(InstanceStateName.Running)),
                                    new Instance()
                                            .withInstanceId("stopping")
                                            .withState(new InstanceState().withName(InstanceStateName.Running))
                            ));

    DescribeInstancesResult describeInstancesResult2 = new DescribeInstancesResult();

    when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class)))
            .thenReturn(describeInstancesResult1)
            .thenReturn(describeInstancesResult2);

    // when
    new EC2Api().describeInstances(amazonEC2, instanceIds, 2);

    // then
    verify(amazonEC2).describeInstances(new DescribeInstancesRequest().withInstanceIds(Arrays.asList("i1", "i2")));
    verify(amazonEC2).describeInstances(new DescribeInstancesRequest().withInstanceIds(Arrays.asList("i3")));
    verifyNoMoreInteractions(amazonEC2);
}
 
Example #29
Source File: EC2ApiTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void describeInstances_shouldNotDescribeMissedInResultInstanceOrTerminatedOrStoppedOrStoppingOrShuttingDownAs() {
    // given
    Set<String> instanceIds = new HashSet<>();
    instanceIds.add("missed");
    instanceIds.add("stopped");
    instanceIds.add("terminated");
    instanceIds.add("stopping");
    instanceIds.add("shutting-down");

    DescribeInstancesResult describeInstancesResult1 =
            new DescribeInstancesResult()
                    .withReservations(
                            new Reservation().withInstances(new Instance()
                                            .withInstanceId("stopped")
                                            .withState(new InstanceState().withName(InstanceStateName.Stopped)),
                                    new Instance()
                                            .withInstanceId("stopping")
                                            .withState(new InstanceState().withName(InstanceStateName.Stopping)),
                                    new Instance()
                                            .withInstanceId("shutting-down")
                                            .withState(new InstanceState().withName(InstanceStateName.ShuttingDown)),
                                    new Instance()
                                            .withInstanceId("terminated")
                                            .withState(new InstanceState().withName(InstanceStateName.Terminated))
                            ));


    when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class)))
            .thenReturn(describeInstancesResult1);

    // when
    Map<String, Instance> described = new EC2Api().describeInstances(amazonEC2, instanceIds);

    // then
    Assert.assertEquals(Collections.<String, Instance>emptyMap(), described);
    verify(amazonEC2, times(1))
            .describeInstances(any(DescribeInstancesRequest.class));
}
 
Example #30
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
// Tests that the built in guard against an infinite loop in the fallback recursive logic has a working safeguard
public void testSubnetInfiniteLoop() throws NodesCouldNotBeStartedException {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    client.setThrowExceptionsInRunInstancesIndefinitely();
    AmazonServiceException exception = new AmazonServiceException("message");
    exception.setErrorCode("InsufficientInstanceCapacity");
    client.setThrowDescribeInstancesError(exception);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="chrome",os="linux";
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    String securityGroup="securityGroup",subnetId="subnetId",keyName="keyName",windowsImage="windowsImage";
    properties.setProperty(region + "_security_group",securityGroup);
    properties.setProperty(region + "_subnet_id", subnetId);
    properties.setProperty(region + "_subnet_fallback_id_1", "foo");
    properties.setProperty(region + "_subnet_fallback_id_2", "foo");
    properties.setProperty(region + "_subnet_fallback_id_3", "foo");
    properties.setProperty(region + "_subnet_fallback_id_4", "foo");
    properties.setProperty(region + "_subnet_fallback_id_5", "foo");
    properties.setProperty(region + "_subnet_fallback_id_6", "foo");
    properties.setProperty(region + "_key_name", keyName);
    properties.setProperty(region + "_windows_node_ami", windowsImage);
    manageEC2.setUserData(userData);
    try{
        manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    } catch(NodesCouldNotBeStartedException e) {
        Assert.assertTrue("Failure message should be correct",e.getMessage().contains("Sufficient resources were not available in any of the availability zones"));
        return;
    }
    Assert.fail("Call should fail due to insufficient resources");
}