@@ -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, instance Id);
node.setTag(TAG_SSH_KEY_ID, keyId);
instance.setKeyName(key Id);
node.setIp4(ip4).setIp6(ip6).setTag(TAG_INSTANCE_ID, instanceId).set Tag(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);