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

The following examples show how to use com.amazonaws.services.ec2.model.Instance#getKeyName() . 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
/**
 * 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 2
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());
    }
}