|
|
@@ -0,0 +1,146 @@ |
|
|
|
/** |
|
|
|
* Copyright (c) 2020 Bubble, Inc. All rights reserved. |
|
|
|
* For personal (non-commercial) use, see license: https://bubblev.com/bubble-license/ |
|
|
|
*/ |
|
|
|
package bubble.cloud.compute.ec2; |
|
|
|
|
|
|
|
import bubble.cloud.compute.ComputeServiceDriverBase; |
|
|
|
import bubble.cloud.shared.aws.BubbleAwsCredentialsProvider; |
|
|
|
import bubble.model.cloud.BubbleNode; |
|
|
|
import com.amazonaws.auth.AWSCredentials; |
|
|
|
import com.amazonaws.auth.AWSCredentialsProvider; |
|
|
|
import com.amazonaws.auth.AWSStaticCredentialsProvider; |
|
|
|
import com.amazonaws.auth.BasicAWSCredentials; |
|
|
|
import com.amazonaws.regions.Regions; |
|
|
|
import com.amazonaws.services.ec2.AmazonEC2; |
|
|
|
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; |
|
|
|
import com.amazonaws.services.ec2.model.*; |
|
|
|
import com.amazonaws.services.route53.AmazonRoute53; |
|
|
|
import com.amazonaws.services.route53.AmazonRoute53ClientBuilder; |
|
|
|
import lombok.Getter; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.cobbzilla.util.http.HttpRequestBean; |
|
|
|
import org.cobbzilla.util.http.HttpResponseBean; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
public class AmazonEC2Driver extends ComputeServiceDriverBase { |
|
|
|
|
|
|
|
@Getter(lazy=true) private final AWSCredentialsProvider ec2credentials = new BubbleAwsCredentialsProvider(cloud, getCredentials()); |
|
|
|
|
|
|
|
@Getter(lazy=true) private final AmazonEC2 ec2Client = initEC2Client(); |
|
|
|
private AmazonEC2 initEC2Client() { |
|
|
|
final Regions region; |
|
|
|
final String regionName = config.getRegion(Regions.DEFAULT_REGION.getName()).getName(); |
|
|
|
try { |
|
|
|
region = Regions.valueOf(regionName); |
|
|
|
} catch (Exception e) { |
|
|
|
return die("initEC2Client: invalid region: " + regionName); |
|
|
|
} |
|
|
|
return AmazonEC2ClientBuilder.standard() |
|
|
|
.withRegion(region) |
|
|
|
.withCredentials(getEc2credentials()) |
|
|
|
.build(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override public void postSetup() { |
|
|
|
|
|
|
|
super.postSetup(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override protected String readSshKeyId(HttpResponseBean keyResponse) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override protected HttpRequestBean registerSshKeyRequest(BubbleNode node) { |
|
|
|
final AmazonEC2 ec2Client = getEc2Client(); |
|
|
|
ec2Client.importKeyPair(new ImportKeyPairRequest(node.getUuid(), node.getSshKey().getSshPublicKey())); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override public List<BubbleNode> listNodes() throws IOException { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override public BubbleNode start(BubbleNode node) throws Exception { |
|
|
|
|
|
|
|
// TODO: imageID, dibnetID, Group |
|
|
|
RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-0080e4c5bc078760e") |
|
|
|
.withInstanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html |
|
|
|
.withMinCount(1) |
|
|
|
.withMaxCount(1) |
|
|
|
.withKeyName(node.getUuid()) |
|
|
|
.withNetworkInterfaces(new InstanceNetworkInterfaceSpecification() |
|
|
|
.withAssociatePublicIpAddress(true) |
|
|
|
.withDeviceIndex(0) |
|
|
|
.withSubnetId("subnet-id") |
|
|
|
.withGroups("sg-id")); |
|
|
|
|
|
|
|
final AmazonEC2 ec2Client = getEc2Client(); |
|
|
|
RunInstancesResult runInstancesResult = ec2Client.runInstances(runInstancesRequest); |
|
|
|
|
|
|
|
Instance instance = runInstancesResult.getReservation().getInstances().get(0); |
|
|
|
String instanceId = instance.getInstanceId(); |
|
|
|
|
|
|
|
// TODO: tags |
|
|
|
// Setting up the tags for the instance |
|
|
|
CreateTagsRequest createTagsRequest = new CreateTagsRequest() |
|
|
|
.withResources(instance.getInstanceId()) |
|
|
|
.withTags(new Tag("", "")); |
|
|
|
ec2Client.createTags(createTagsRequest); |
|
|
|
|
|
|
|
// Starting the Instance |
|
|
|
StartInstancesRequest startInstancesRequest = new StartInstancesRequest().withInstanceIds(instanceId); |
|
|
|
|
|
|
|
ec2Client.startInstances(startInstancesRequest); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override public BubbleNode cleanupStart(BubbleNode node) throws Exception { |
|
|
|
deleteEC2KeyPair(node); |
|
|
|
return node; |
|
|
|
} |
|
|
|
|
|
|
|
@Override public BubbleNode stop(BubbleNode node) throws Exception { |
|
|
|
//Stop EC2 Instance |
|
|
|
//TODO: instanceID |
|
|
|
String instanceID = ""; |
|
|
|
StopInstancesRequest stopInstancesRequest = new StopInstancesRequest() |
|
|
|
.withInstanceIds(instanceID); |
|
|
|
|
|
|
|
final AmazonEC2 ec2Client = getEc2Client(); |
|
|
|
ec2Client.stopInstances(stopInstancesRequest) |
|
|
|
.getStoppingInstances() |
|
|
|
.get(0) |
|
|
|
.getPreviousState() |
|
|
|
.getName(); |
|
|
|
return node; |
|
|
|
} |
|
|
|
|
|
|
|
@Override public BubbleNode status(BubbleNode node) throws Exception { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
public void deleteEC2KeyPair(BubbleNode node) throws IOException { |
|
|
|
if (node.hasTag(TAG_SSH_KEY_ID)) { |
|
|
|
DeleteKeyPairRequest request = new DeleteKeyPairRequest() |
|
|
|
.withKeyName(node.getUuid()); |
|
|
|
|
|
|
|
// destroy key, check response |
|
|
|
final AmazonEC2 ec2Client = getEc2Client(); |
|
|
|
DeleteKeyPairResult response = ec2Client.deleteKeyPair(request); |
|
|
|
|
|
|
|
if (response.getSdkHttpMetadata().getHttpStatusCode() != OK) { |
|
|
|
log.warn("deleteEC2KeyPair: error deleting EC2keyPair, node: "+ node.getUuid()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |