Java Code Examples for com.amazonaws.services.ec2.model.Instance#getInstanceId()

The following examples show how to use com.amazonaws.services.ec2.model.Instance#getInstanceId() . 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: 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 2
Source File: EC2Processor.java    From development with Apache License 2.0 6 votes vote down vote up
private List<Server> convertInstanceToServer(List<Instance> instances) {
    List<Server> servers = new ArrayList<>();
    for (Instance instance : instances) {
        Server server = new Server(instance.getInstanceId());
        for (Tag tag : instance.getTags()) {
            if (tag != null && tag.getKey() != null
                    && tag.getKey().equals("Name")) {
                server.setName(tag.getValue());
            }
        }
        server.setStatus(instance.getState().getName());
        server.setType(instance.getInstanceType());
        server.setPublicIP(Arrays.asList(instance.getPublicIpAddress()));
        server.setPrivateIP(Arrays.asList(instance.getPrivateIpAddress()));
        servers.add(server);
    }
    return servers;
}
 
Example 3
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 4
Source File: AwsMetadataCollector.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private void addFromUnknownMap(CloudInstance cloudInstance, Multimap<String, Instance> unknownMap,
        List<CloudVmMetaDataStatus> collectedCloudVmMetaDataStatuses) {
    LOGGER.debug("Collect from unknown map, cloudInstance: {}, unknownMap: {}", cloudInstance.getInstanceId(), unknownMap.keySet());
    String groupName = cloudInstance.getTemplate().getGroupName();
    Collection<Instance> unknownInstancesForGroup = unknownMap.get(groupName);
    if (!unknownInstancesForGroup.isEmpty()) {
        Optional<Instance> found = unknownInstancesForGroup.stream().findFirst();
        Instance foundInstance = found.get();
        CloudInstance newCloudInstance = new CloudInstance(foundInstance.getInstanceId(), cloudInstance.getTemplate(),
                cloudInstance.getAuthentication(), cloudInstance.getParameters());
        CloudInstanceMetaData cloudInstanceMetaData = new CloudInstanceMetaData(
                foundInstance.getPrivateIpAddress(),
                foundInstance.getPublicIpAddress(),
                awsLifeCycleMapper.getLifeCycle(foundInstance));
        CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(newCloudInstance, InstanceStatus.CREATED);
        CloudVmMetaDataStatus newMetadataStatus = new CloudVmMetaDataStatus(cloudVmInstanceStatus, cloudInstanceMetaData);
        collectedCloudVmMetaDataStatuses.add(newMetadataStatus);
        unknownMap.remove(groupName, found.get());
    }
}
 
Example 5
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 5 votes vote down vote up
private ServerEnvironment getEnvironmentFromInstance(Instance instance) {
    ServerEnvironment env = new ServerEnvironment(instance.getInstanceId(), instance.getInstanceType());

    List<EnvironmentTag> tags = new ArrayList<EnvironmentTag>();
    for (Tag tag : instance.getTags()) {
        EnvironmentTag envTag = new EnvironmentTag(tag.getKey(), tag.getValue());
        tags.add(envTag);
        if (tag.getKey().equalsIgnoreCase(DEFAULT_INSTANCE_NAME_TAG)) {
            env.setEnvironmentTag(tag.getValue());
            if (tag.getValue().contains(PROD_VALUE)) {
                env.setType(ENVIRONMENT_TYPES.PRODUCTION);
            } else if (tag.getValue().contains(STAGING_VALUE)) {
                env.setType(ENVIRONMENT_TYPES.STAGING);
            } else if (tag.getValue().contains(JENKINS_VALUE)) {
                env.setType(ENVIRONMENT_TYPES.JENKINS);
            }
        }
        if (tag.getKey().equalsIgnoreCase(VERSION_TAG)) {
            env.setVersion(tag.getValue());
        }
    }
    env.setState(instance.getState());
    env.setLaunchTime(instance.getLaunchTime());
    env.setPublicIpAddress(instance.getPublicIpAddress());
    env.setTags(tags);
    return env;
}
 
Example 6
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 7
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 8
Source File: EC2FleetCloud.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * https://github.com/jenkinsci/ec2-plugin/blob/master/src/main/java/hudson/plugins/ec2/EC2Cloud.java#L640
 *
 * @param ec2      ec2 client
 * @param instance instance
 */
private void addNewSlave(final AmazonEC2 ec2, final Instance instance, FleetStateStats stats) throws Exception {
    final String instanceId = instance.getInstanceId();

    // instance state check enabled and not running, skip adding
    if (addNodeOnlyIfRunning && InstanceStateName.Running != InstanceStateName.fromValue(instance.getState().getName()))
        return;

    final String address = privateIpUsed ? instance.getPrivateIpAddress() : instance.getPublicIpAddress();
    // Check if we have the address to use. Nodes don't get it immediately.
    if (address == null) {
        if (!privateIpUsed) {
            info("%s instance public IP address not assigned, it could take some time or" +
                    " Spot Request is not configured to assign public IPs", instance.getInstanceId());
        }
        return; // wait more time, probably IP address not yet assigned
    }

    // Generate a random FS root if one isn't specified
    final String effectiveFsRoot;
    if (StringUtils.isBlank(fsRoot)) {
        effectiveFsRoot = "/tmp/jenkins-" + UUID.randomUUID().toString().substring(0, 8);
    } else {
        effectiveFsRoot = fsRoot;
    }

    final Double instanceTypeWeight = stats.getInstanceTypeWeights().get(instance.getInstanceType());
    final int effectiveNumExecutors;
    if (scaleExecutorsByWeight && instanceTypeWeight != null) {
        effectiveNumExecutors = (int) Math.max(Math.round(numExecutors * instanceTypeWeight), 1);
    } else {
        effectiveNumExecutors = numExecutors;
    }

    final EC2FleetAutoResubmitComputerLauncher computerLauncher = new EC2FleetAutoResubmitComputerLauncher(
            computerConnector.launch(address, TaskListener.NULL));
    final Node.Mode nodeMode = restrictUsage ? Node.Mode.EXCLUSIVE : Node.Mode.NORMAL;
    final EC2FleetNode node = new EC2FleetNode(instanceId, "Fleet slave for " + instanceId,
            effectiveFsRoot, effectiveNumExecutors, nodeMode, labelString, new ArrayList<NodeProperty<?>>(),
            this, computerLauncher);

    // Initialize our retention strategy
    node.setRetentionStrategy(new IdleRetentionStrategy());

    final Jenkins jenkins = Jenkins.getInstance();
    // jenkins automatically remove old node with same name if any
    jenkins.addNode(node);

    final SettableFuture<Node> future;
    if (plannedNodesCache.isEmpty()) {
        future = SettableFuture.create();
    } else {
        final NodeProvisioner.PlannedNode plannedNode = plannedNodesCache.iterator().next();
        plannedNodesCache.remove(plannedNode);
        future = ((SettableFuture<Node>) plannedNode.future);
    }

    // use getters for timeout and interval as they provide default value
    // when user just install new version and did't recreate fleet
    EC2FleetOnlineChecker.start(node, future,
            TimeUnit.SECONDS.toMillis(getInitOnlineTimeoutSec()),
            TimeUnit.SECONDS.toMillis(getInitOnlineCheckIntervalSec()));
}
 
Example 9
Source File: EC2Instance.java    From billow with Apache License 2.0 4 votes vote down vote up
public EC2Instance(Instance instance) {
    this.id = instance.getInstanceId();
    this.type = instance.getInstanceType();
    this.lifecycle = instance.getInstanceLifecycle();
    this.hypervisor = instance.getHypervisor();
    this.az = instance.getPlacement().getAvailabilityZone();
    this.group = instance.getPlacement().getGroupName();
    this.tenancy = instance.getPlacement().getTenancy();
    this.vpc = instance.getVpcId();
    this.platform = instance.getPlatform();
    this.kernel = instance.getKernelId();
    this.key = instance.getKeyName();
    this.image = instance.getImageId();
    this.privateIP = instance.getPrivateIpAddress();
    this.publicIP = instance.getPublicIpAddress();
    this.publicHostname = instance.getPublicDnsName();
    this.privateHostname = instance.getPrivateDnsName();
    this.architecture = instance.getArchitecture();
    this.state = instance.getState().getName();
    this.ramdisk = instance.getRamdiskId();
    this.subnet = instance.getSubnetId();
    this.rootDeviceName = instance.getRootDeviceName();
    this.rootDeviceType = instance.getRootDeviceType();
    this.stateTransitionReason = instance.getStateTransitionReason();
    this.spotInstanceRequest = instance.getSpotInstanceRequestId();
    this.virtualizationType = instance.getVirtualizationType();
    this.sourceDestCheck = instance.getSourceDestCheck();
    this.launchTime = new DateTime(instance.getLaunchTime());

    if (instance.getIamInstanceProfile() != null) {
        this.iamInstanceProfile = instance.getIamInstanceProfile().getArn().toString();
    } else {
        this.iamInstanceProfile = null;
    }

    final StateReason stateReason = instance.getStateReason();
    if (stateReason != null)
        this.stateReason = stateReason.getMessage();
    else
        this.stateReason = null;

    this.securityGroups = new ArrayList<>();
    for (GroupIdentifier identifier : instance.getSecurityGroups()) {
        this.securityGroups.add(new SecurityGroup(identifier));
    }

    this.tags = new HashMap<>();
    for (Tag tag : instance.getTags()) {
        this.tags.put(tag.getKey(), tag.getValue());
    }
}