소스 검색

Create initEC2Client method

pull/4/head
Svitlana 4 년 전
부모
커밋
8077a205bd
5개의 변경된 파일168개의 추가작업 그리고 106개의 파일을 삭제
  1. +6
    -0
      bubble-server/pom.xml
  2. +0
    -105
      bubble-server/src/main/java/bubble/cloud/compute/amazonEC2/AmazonEC2Driver.java
  3. +146
    -0
      bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java
  4. +15
    -0
      bubble-server/src/test/resources/models/system/cloudService.json
  5. +1
    -1
      utils/cobbzilla-wizard

+ 6
- 0
bubble-server/pom.xml 파일 보기

@@ -106,6 +106,12 @@
<artifactId>jetty-proxy</artifactId>
<version>${jetty.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-ec2 -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ec2</artifactId>
<version>1.11.762</version>
</dependency>

<!--&lt;!&ndash; https://mvnrepository.com/artifact/org.atmosphere/atmosphere-jersey &ndash;&gt;-->
<!--<dependency>-->


+ 0
- 105
bubble-server/src/main/java/bubble/cloud/compute/amazonEC2/AmazonEC2Driver.java 파일 보기

@@ -1,105 +0,0 @@
/**
* 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();

// 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;
}
}

+ 146
- 0
bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java 파일 보기

@@ -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());
}
}
}
}

+ 15
- 0
bubble-server/src/test/resources/models/system/cloudService.json 파일 보기

@@ -233,6 +233,21 @@
"template": true
},

{
"_subst": true,
"name": "AmazonEC2Driver",
"type": "compute",
"driverClass": "bubble.cloud.compute.ec2.AmazonEC2Driver",
"driverConfig": {},
"credentials": {
"params": [
{"name": "AWS_ACCESS_KEY_ID", "value": "{{AWS_ACCESS_KEY_ID}}"},
{"name": "AWS_SECRET_KEY", "value": "{{AWS_SECRET_KEY}}"}
]
},
"template": true
},

{
"_subst": true,
"name": "InviteCode",


+ 1
- 1
utils/cobbzilla-wizard

@@ -1 +1 @@
Subproject commit 073dbbc3f8401e946b731e2269f6e253b04bee68
Subproject commit 23e6ab7399be669a1f9bb9abf64186f3d029eda5

불러오는 중...
취소
저장