Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the pause, hibernate and resume features between the AWS and F… #759

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/cloud/fogbow/ras/constants/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public static class Exception {
public static final String ERROR_WHILE_CREATING_RESOURCE_S = Log.ERROR_WHILE_CREATING_RESOURCE_S;
public static final String ERROR_WHILE_GETTING_RESOURCE_S_FROM_CLOUD = Log.ERROR_WHILE_GETTING_RESOURCE_S_FROM_CLOUD;
public static final String ERROR_WHILE_GETTING_VOLUME_INSTANCE = Log.ERROR_WHILE_GETTING_VOLUME_INSTANCE;
public static final String ERROR_WHILE_HIBERNATING_S = Log.ERROR_WHILE_HIBERNATING_S;
public static final String ERROR_WHILE_PAUSING_S = Log.ERROR_WHILE_PAUSING_S;
public static final String ERROR_WHILE_PROCESSING_VOLUME_REQUIREMENTS = Log.ERROR_WHILE_PROCESSING_VOLUME_REQUIREMENTS;
public static final String ERROR_WHILE_REMOVING_RESOURCE_S_S = Log.ERROR_WHILE_REMOVING_RESOURCE_S_S;
public static final String ERROR_WHILE_REMOVING_VM_S_S = "Error while removing virtual machine: %s, with response: %s.";
public static final String ERROR_WHILE_REMOVING_VOLUME_IMAGE_S_S = Log.ERROR_WHILE_REMOVING_VOLUME_IMAGE_S_S;
public static final String ERROR_WHILE_RESUMING_S = Log.ERROR_WHILE_RESUMING_S;
public static final String EXTERNAL_NETWORK_NOT_FOUND = "External network not found.";
public static final String FAILED_TO_GET_QUOTA = "Failed to get quota.";
public static final String GENERIC_EXCEPTION_S = Log.GENERIC_EXCEPTION_S;
Expand Down Expand Up @@ -165,12 +168,15 @@ public static class Log {
public static final String ERROR_WHILE_GETTING_USER_S_S = "Error while getting info about user %s: %s.";
public static final String ERROR_WHILE_GETTING_USERS_S = "Error while getting info about users: %s.";
public static final String ERROR_WHILE_GETTING_VOLUME_INSTANCE = "Error while getting volume instance.";
public static final String ERROR_WHILE_HIBERNATING_S = "Error while hibernating the following image: %s";
public static final String ERROR_WHILE_INSTANTIATING_FROM_TEMPLATE_S = "Error while instantiating an instance from template: %s.";
public static final String ERROR_WHILE_LOADING_IMAGE_S = "Error while loading the following image: %s";
public static final String ERROR_WHILE_PAUSING_S = "Error while pausing the following image: %s";
public static final String ERROR_WHILE_PROCESSING_ASYNCHRONOUS_REQUEST_INSTANCE_STEP = "Error while It was trying to pass to the next step in the asynchronous request instance.";
public static final String ERROR_WHILE_PROCESSING_VOLUME_REQUIREMENTS = "Error while processing volume requirements";
public static final String ERROR_WHILE_REMOVING_RESOURCE_S_S = "An error occurred while removing %s: %s.";
public static final String ERROR_WHILE_REMOVING_VOLUME_IMAGE_S_S = "Error while removing volume image: %s, with response: %s.";
public static final String ERROR_WHILE_RESUMING_S = "Error while resuming the following image: %s.";
public static final String ERROR_WHILE_UPDATING_NETWORK_S = "Error while updating a network from template: %s.";
public static final String ERROR_WHILE_UPDATING_SECURITY_GROUPS_S = "Error while updating a security groups from template: %s.";
public static final String FIRST_STEP_CREATE_PUBLIC_IP_ASYNC_BEHAVIOUR = "First step: Public IP Address created and associated with the virtual machine.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class AwsV2StateMapper {
public static final String RUNNING_STATE = "running";
public static final String SHUTTING_DOWN_STATE = "deleting";
public static final String STOPPING_STATE = "stopping";
public static final String STOPPED_STATE = "stopped";
public static final String TERMINATED_STATE = "terminated";
public static final String TRANSIENT_STATE = "transient";
public static final String UNKNOWN_TO_SDK_VERSION_STATE = "unknown_to_sdk_version";
Expand Down Expand Up @@ -69,6 +70,8 @@ public static InstanceState map(ResourceType type, String state) {
case SHUTTING_DOWN_STATE:
case STOPPING_STATE:
return InstanceState.BUSY;
case STOPPED_STATE:
return InstanceState.PAUSED;
default:
LOGGER.error(String.format(Messages.Log.UNDEFINED_INSTANCE_STATE_MAPPING_S_S, state, COMPUTE_PLUGIN));
return InstanceState.INCONSISTENT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,33 +130,45 @@ public void deleteInstance(ComputeOrder computeOrder, AwsV2User cloudUser) throw
}

@Override
public void takeSnapshot(ComputeOrder order, String name, AwsV2User cloudUser) throws FogbowException {
public void takeSnapshot(ComputeOrder computeOrder, String name, AwsV2User cloudUser) throws FogbowException {
// ToDo: implement
}

@Override
public void pauseInstance(ComputeOrder order, AwsV2User cloudUser) throws FogbowException {
// ToDo: implement
public void pauseInstance(ComputeOrder computeOrder, AwsV2User cloudUser) throws FogbowException {
String instanceId = computeOrder.getInstanceId();
LOGGER.info(String.format(Messages.Log.PAUSING_INSTANCE_S, instanceId));
Ec2Client client = AwsV2ClientUtil.createEc2Client(cloudUser.getToken(), this.region);
doPauseInstance(instanceId, client);
}

@Override
public void hibernateInstance(ComputeOrder order, AwsV2User cloudUser) throws FogbowException {
// ToDo: implement
public void hibernateInstance(ComputeOrder computeOrder, AwsV2User cloudUser) throws FogbowException {
String instanceId = computeOrder.getInstanceId();
LOGGER.info(String.format(Messages.Log.HIBERNATING_INSTANCE_S, instanceId));
Ec2Client client = AwsV2ClientUtil.createEc2Client(cloudUser.getToken(), this.region);
doHibernateInstance(instanceId, client);
}

@Override
public void resumeInstance(ComputeOrder order, AwsV2User cloudUser) throws FogbowException {
// ToDo: implement
public void resumeInstance(ComputeOrder computeOrder, AwsV2User cloudUser) throws FogbowException {
String instanceId = computeOrder.getInstanceId();
LOGGER.info(String.format(Messages.Log.RESUMING_INSTANCE_S, instanceId));
Ec2Client client = AwsV2ClientUtil.createEc2Client(cloudUser.getToken(), this.region);
doResumeInstance(instanceId, client);
}

@Override
public boolean isPaused(String cloudState) throws FogbowException {
return false;
return AwsV2StateMapper.map(ResourceType.COMPUTE, cloudState).equals(InstanceState.PAUSED);
}

@Override
public boolean isHibernated(String cloudState) throws FogbowException {
return false;
// AWS API uses the state "stopped" to represent both stopped and hibernated instances.
// Therefore, it is not possible to map correctly the hibernated state and, thus,
// here we use the PAUSED state.
return AwsV2StateMapper.map(ResourceType.COMPUTE, cloudState).equals(InstanceState.PAUSED);
}

@Override
Expand All @@ -181,7 +193,50 @@ void doDeleteInstance(String instanceId, Ec2Client client) throws InternalServer
throw new InternalServerErrorException(String.format(Messages.Exception.ERROR_WHILE_REMOVING_RESOURCE_S_S, RESOURCE_NAME, instanceId));
}
}


@VisibleForTesting
void doPauseInstance(String instanceId, Ec2Client client) throws InternalServerErrorException {
StopInstancesRequest request = StopInstancesRequest.builder()
.instanceIds(instanceId)
.build();

try{
client.stopInstances(request);
} catch (SdkException e) {
LOGGER.error(String.format(Messages.Log.ERROR_WHILE_PAUSING_S, RESOURCE_NAME, instanceId), e);
throw new InternalServerErrorException(String.format(Messages.Exception.ERROR_WHILE_PAUSING_S, RESOURCE_NAME, instanceId));
}
}

@VisibleForTesting
void doHibernateInstance(String instanceId, Ec2Client client) throws InternalServerErrorException {
StopInstancesRequest request = StopInstancesRequest.builder()
.hibernate(true)
.instanceIds(instanceId)
.build();

try{
client.stopInstances(request);
} catch (SdkException e) {
LOGGER.error(String.format(Messages.Log.ERROR_WHILE_HIBERNATING_S, RESOURCE_NAME, instanceId), e);
throw new InternalServerErrorException(String.format(Messages.Exception.ERROR_WHILE_HIBERNATING_S, RESOURCE_NAME, instanceId));
}
}

@VisibleForTesting
void doResumeInstance(String instanceId, Ec2Client client) throws InternalServerErrorException {
StartInstancesRequest request = StartInstancesRequest.builder()
.instanceIds(instanceId)
.build();

try {
client.startInstances(request);
} catch (SdkException e) {
LOGGER.error(String.format(Messages.Log.ERROR_WHILE_RESUMING_S, RESOURCE_NAME, instanceId), e);
throw new InternalServerErrorException(String.format(Messages.Exception.ERROR_WHILE_RESUMING_S, RESOURCE_NAME, instanceId));
}
}

@VisibleForTesting
ComputeInstance doGetInstance(String instanceId, Ec2Client client) throws FogbowException {
DescribeInstancesResponse response = AwsV2CloudUtil.doDescribeInstanceById(instanceId, client);
Expand Down
Loading