From 7f503e5703702962119d3e79039d5c45291768fa Mon Sep 17 00:00:00 2001 From: Svitlana Date: Mon, 27 Apr 2020 19:50:25 +0200 Subject: [PATCH] Add ssh key --- .../cloud/compute/ec2/AmazonEC2Driver.java | 24 +++++++++++++++---- .../models/defaults/cloudService.json | 4 ++-- .../resources/models/system/cloudService.json | 8 +++---- config/activation.json | 10 ++++++++ 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java b/bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java index 7dfda1d5..533c32d2 100644 --- a/bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java +++ b/bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java @@ -24,11 +24,13 @@ import org.cobbzilla.util.http.HttpResponseBean; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.NoSuchElementException; import static bubble.model.cloud.BubbleNode.TAG_INSTANCE_ID; import static bubble.model.cloud.BubbleNode.TAG_SSH_KEY_ID; import static org.cobbzilla.util.daemon.ZillaRuntime.die; import static org.cobbzilla.util.http.HttpStatusCodes.OK; +import static org.cobbzilla.util.security.RsaKeyPair.newRsaKeyPair; import static org.cobbzilla.wizard.resources.ResourceUtil.notFoundEx; @Slf4j @@ -93,17 +95,24 @@ public class AmazonEC2Driver extends ComputeServiceDriverBase { @Override public BubbleNode start(@NonNull BubbleNode node) throws Exception { final ComputeNodeSize size = config.getSize(node.getSize()); + final AmazonEC2 ec2Client = getEc2Client(); + + DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest(); + + String subnetId = ec2Client.describeSubnets(describeSubnetsRequest).getSubnets().stream() + .filter(s -> s.getAvailableIpAddressCount() != 0) + .findAny().orElseThrow(() -> new NoSuchElementException("Subnet not found")).getSubnetId(); + RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId(config.getConfig("imageId")) .withInstanceType(size.getInternalName()) .withMinCount(MIN_COUNT) .withMaxCount(MAX_COUNT) - .withKeyName(node.getUuid()) .withNetworkInterfaces(new InstanceNetworkInterfaceSpecification() .withAssociatePublicIpAddress(true) - .withSubnetId(SUBNET_ID_PREFIX + node.getUuid()) + .withDeviceIndex(0) + .withSubnetId(subnetId) .withGroups(config.getConfig("group"))); - final AmazonEC2 ec2Client = getEc2Client(); RunInstancesResult runInstancesResult = ec2Client.runInstances(runInstancesRequest); if (runInstancesResult.getSdkHttpMetadata().getHttpStatusCode() != OK) @@ -112,10 +121,12 @@ public class AmazonEC2Driver extends ComputeServiceDriverBase { Instance instance = runInstancesResult.getReservation().getInstances().get(0); final String instanceId = instance.getInstanceId(); + final String ip4 = instance.getPrivateIpAddress(); + final String ip6 = instance.getPublicIpAddress(); final String keyId = registerSshKey(node); - node.setTag(TAG_INSTANCE_ID, instanceId); - node.setTag(TAG_SSH_KEY_ID, keyId); + instance.setKeyName(keyId); + node.setIp4(ip4).setIp6(ip6).setTag(TAG_INSTANCE_ID, instanceId).setTag(TAG_SSH_KEY_ID, keyId); nodeDAO.update(node); @@ -199,6 +210,9 @@ public class AmazonEC2Driver extends ComputeServiceDriverBase { } @Override public String registerSshKey(BubbleNode node) { + if (node.hasSshKey()) return die("registerSshKey: node already has a key: "+node.getUuid()); + node.setSshKey(newRsaKeyPair()); + final AmazonEC2 ec2Client = getEc2Client(); ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(node.getUuid(), node.getSshKey().getSshPublicKey()); ImportKeyPairResult importKeyPairResult = ec2Client.importKeyPair(importKeyPairRequest); diff --git a/bubble-server/src/main/resources/models/defaults/cloudService.json b/bubble-server/src/main/resources/models/defaults/cloudService.json index 37994b1f..0c1905b7 100644 --- a/bubble-server/src/main/resources/models/defaults/cloudService.json +++ b/bubble-server/src/main/resources/models/defaults/cloudService.json @@ -272,8 +272,8 @@ "driverClass": "bubble.cloud.compute.ec2.AmazonEC2Driver", "driverConfig": { "regions": [{ - "name": "us-east-1", "description": "US East (N. Virginia)", - "location": {"country": "US", "lat": "37.926868", "lon": "-78.024902"} + "name": "US_WEST_2", "description": "US West (Oregon)", + "location": {"country": "US", "lat": "43.8041", "lon": "120.5542"} }], "sizes": [ {"name": "small", "type": "small", "internalName": "t2.micro", "vcpu": 1, "memoryMB": 1024, "ssdGB": 0}, diff --git a/bubble-server/src/test/resources/models/system/cloudService.json b/bubble-server/src/test/resources/models/system/cloudService.json index fcd53cd6..a34bd5b0 100644 --- a/bubble-server/src/test/resources/models/system/cloudService.json +++ b/bubble-server/src/test/resources/models/system/cloudService.json @@ -240,8 +240,8 @@ "driverClass": "bubble.cloud.compute.ec2.AmazonEC2Driver", "driverConfig": { "regions": [{ - "name": "us-east-1", "description": "US East (N. Virginia)", - "location": {"country": "US", "lat": "37.926868", "lon": "-78.024902"} + "name": "US_WEST_2", "description": "US West (Oregon)", + "location": {"country": "US", "lat": "43.8041", "lon": "120.5542"} }], "sizes": [ {"name": "small", "type": "small", "internalName": "t2.micro", "vcpu": 1, "memoryMB": 1024, "ssdGB": 0}, @@ -249,8 +249,8 @@ {"name": "large", "type": "large", "internalName": "t2.medium", "vcpu": 2, "memoryMB": 4096, "ssdGB": 80} ], "config": [ - {"name": "imageId", "value": "ami-0080e4c5bc078760e"}, - {"name": "group", "value": "sg-id"} + {"name": "imageId", "value": "ami-09396c35da02d69d3"}, + {"name": "group", "value": "sg-d7ed5bb3"} ]}, "credentials": { "params": [ diff --git a/config/activation.json b/config/activation.json index a4fe7a90..05c1a6bd 100644 --- a/config/activation.json +++ b/config/activation.json @@ -28,6 +28,16 @@ "DigitalOceanCompute": { "credentials": {"apiKey": "your_digitalocean_api_key"} }, + "AmazonEC2Driver" : { + "config": { + // region must be a valid value from the Regions enum: https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/regions/Regions.java + "region": "US_EAST_1" + }, + "credentials" : { + "AWS_ACCESS_KEY_ID": "your_aws_access_key_id", + "AWS_SECRET_KEY": "your_aws_secret_key" + } + }, // You must configure the AWS S3 Storage service in order to launch new Bubbles "S3Storage" : {