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