|
|
@@ -0,0 +1,106 @@ |
|
|
|
/** |
|
|
|
* Copyright (c) 2020 Bubble, Inc. All rights reserved. |
|
|
|
* For personal (non-commercial) use, see license: https://bubblev.com/bubble-license/ |
|
|
|
*/ |
|
|
|
package bubble.cloud.compute.amazonEC2; |
|
|
|
|
|
|
|
import bubble.cloud.compute.ComputeServiceDriverBase; |
|
|
|
import bubble.model.cloud.BubbleNode; |
|
|
|
import com.amazonaws.auth.AWSCredentials; |
|
|
|
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 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 org.cobbzilla.util.string.StringUtil.urlEncode; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
public class AmazonEC2Driver extends ComputeServiceDriverBase { |
|
|
|
|
|
|
|
private static final AWSCredentials AWS_CREDENTIALS; |
|
|
|
|
|
|
|
static { |
|
|
|
// Your accesskey and secretkey |
|
|
|
AWS_CREDENTIALS = new BasicAWSCredentials( |
|
|
|
"Your-ID", |
|
|
|
"Your secret-key" |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected String readSshKeyId(HttpResponseBean keyResponse) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected HttpRequestBean registerSshKeyRequest(BubbleNode node) { |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<BubbleNode> listNodes() throws IOException { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public BubbleNode start(BubbleNode node) throws Exception { |
|
|
|
AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard() |
|
|
|
.withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS)) |
|
|
|
.withRegion(Regions.US_EAST_1) |
|
|
|
.build(); |
|
|
|
|
|
|
|
ec2Client.importKeyPair(new ImportKeyPairRequest(node.getUuid(), node.getSshKey().getSshPublicKey())); |
|
|
|
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")); |
|
|
|
|
|
|
|
RunInstancesResult runInstancesResult = ec2Client.runInstances(runInstancesRequest); |
|
|
|
|
|
|
|
Instance instance = runInstancesResult.getReservation().getInstances().get(0); |
|
|
|
String instanceId = instance.getInstanceId(); |
|
|
|
System.out.println("EC2 Instance Id: " + instanceId); |
|
|
|
|
|
|
|
// Setting up the tags for the instance |
|
|
|
CreateTagsRequest createTagsRequest = new CreateTagsRequest() |
|
|
|
.withResources(instance.getInstanceId()) |
|
|
|
.withTags(new Tag("Name", "Edurekademo")); |
|
|
|
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 { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public BubbleNode stop(BubbleNode node) throws Exception { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public BubbleNode status(BubbleNode node) throws Exception { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |