Parcourir la source

Create stop method

pull/4/head
Svitlana il y a 4 ans
Parent
révision
27b3286ae0
3 fichiers modifiés avec 53 ajouts et 14 suppressions
  1. +34
    -13
      bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java
  2. +16
    -0
      bubble-server/src/main/resources/models/defaults/cloudService.json
  3. +3
    -1
      bubble-server/src/test/resources/models/system/cloudService.json

+ 34
- 13
bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java Voir le fichier

@@ -7,10 +7,8 @@ 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.AmazonServiceException;
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;
@@ -23,13 +21,19 @@ import org.cobbzilla.util.http.HttpResponseBean;
import java.io.IOException;
import java.util.List;

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.wizard.resources.ResourceUtil.invalidEx;
import static org.cobbzilla.wizard.resources.ResourceUtil.notFoundEx;

@Slf4j
public class AmazonEC2Driver extends ComputeServiceDriverBase {

public static final String TAG_CLOUD_UUID = "cloudUUID";
public static final String TAG_NODE_UUID = "nodeUUID";

@Getter(lazy=true) private final AWSCredentialsProvider ec2credentials = new BubbleAwsCredentialsProvider(cloud, getCredentials());

@Getter(lazy=true) private final AmazonEC2 ec2Client = initEC2Client();
@@ -85,15 +89,26 @@ public class AmazonEC2Driver extends ComputeServiceDriverBase {
final AmazonEC2 ec2Client = getEc2Client();
RunInstancesResult runInstancesResult = ec2Client.runInstances(runInstancesRequest);

if (runInstancesResult.getSdkHttpMetadata().getHttpStatusCode() != OK) return die("start: error running instance: "
+ runInstancesResult.getSdkHttpMetadata().getAllHttpHeaders());

Instance instance = runInstancesResult.getReservation().getInstances().get(0);
String instanceId = instance.getInstanceId();

// TODO: tags
node.setTag(TAG_INSTANCE_ID, instanceId);
nodeDAO.update(node);

// Setting up the tags for the instance
CreateTagsRequest createTagsRequest = new CreateTagsRequest()
.withResources(instance.getInstanceId())
.withTags(new Tag("", ""));
ec2Client.createTags(createTagsRequest);
.withTags(new Tag(TAG_NODE_UUID, node.getUuid()),
new Tag(TAG_CLOUD_UUID, cloud.getUuid()));

try {
ec2Client.createTags(createTagsRequest);
} catch (AmazonServiceException ase) {
log.warn("start: error creating tags instance: " + instanceId + ase.getErrorMessage() + ase.getErrorCode()););
}

// Starting the Instance
StartInstancesRequest startInstancesRequest = new StartInstancesRequest().withInstanceIds(instanceId);
@@ -104,22 +119,28 @@ public class AmazonEC2Driver extends ComputeServiceDriverBase {

@Override public BubbleNode cleanupStart(BubbleNode node) throws Exception {
deleteEC2KeyPair(node);
// TODO do we need delete tags?
return node;
}

@Override public BubbleNode stop(BubbleNode node) throws Exception {
if (!node.hasTag(TAG_INSTANCE_ID)) {
throw notFoundEx(node.id());
}
final String instanceID = node.getTag(TAG_INSTANCE_ID);

//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();

try {
ec2Client.stopInstances(stopInstancesRequest);
} catch (AmazonServiceException ase) {
log.warn("stop: error stopping instance: " + instanceID + ase.getErrorMessage() + ase.getErrorCode());
}
// TODO do we need terminate instances?
return node;
}



+ 16
- 0
bubble-server/src/main/resources/models/defaults/cloudService.json Voir le fichier

@@ -264,5 +264,21 @@
]
},
"template": true
},

{
"name": "AmazonEC2Driver",
"type": "compute",
"driverClass": "bubble.cloud.compute.ec2.AmazonEC2Driver",
"driverConfig": {
"region": "{{#exists BUBBLE_EC2_REGION}}{{BUBBLE_EC2_REGION}}{{else}}US_EAST_1{{/exists}}"
},
"credentials": {
"params": [
{"name": "AWS_ACCESS_KEY_ID", "value": "{{AWS_ACCESS_KEY_ID}}"},
{"name": "AWS_SECRET_KEY", "value": "{{AWS_SECRET_KEY}}"}
]
},
"template": true
}
]

+ 3
- 1
bubble-server/src/test/resources/models/system/cloudService.json Voir le fichier

@@ -238,7 +238,9 @@
"name": "AmazonEC2Driver",
"type": "compute",
"driverClass": "bubble.cloud.compute.ec2.AmazonEC2Driver",
"driverConfig": {},
"driverConfig": {
"region": "US_EAST_1"
},
"credentials": {
"params": [
{"name": "AWS_ACCESS_KEY_ID", "value": "{{AWS_ACCESS_KEY_ID}}"},


Chargement…
Annuler
Enregistrer