From ece1de8d8df0b00a4678d872233d1482ab040d49 Mon Sep 17 00:00:00 2001 From: sguazt <marco.guazzone@gmail.com> Date: Tue, 5 Jan 2021 11:29:11 +0100 Subject: [PATCH] AWS: added class to use for implementing synchronized operations (e.g., for waiting until the starting of an instance is done) --- easycloud/modules/aws_awssdk/manager.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/easycloud/modules/aws_awssdk/manager.py b/easycloud/modules/aws_awssdk/manager.py index 9592d43..0458aa3 100644 --- a/easycloud/modules/aws_awssdk/manager.py +++ b/easycloud/modules/aws_awssdk/manager.py @@ -3,6 +3,7 @@ EasyCloud Amazon Web Services Manager """ import boto3 +#from easycloud.common.aws import AWSEC2Utils from easycloud.core.compute import Instance, InstanceStatus from easycloud.core.actionbinder import bind_action from easycloud.core.metamanager import MetaManager @@ -93,12 +94,30 @@ class AWSInstance(Instance): def start(self): self._ec2_conn.start_instances(InstanceIds=[self._ec2_inst['InstanceId']]) #, DryRun=True) + #TODO: call "AWSEC2Utils.wait_for_operation(self._ec_conn, 'instance_running', [self._ec2_inst['InstanceId'])" to perform a synchronous version of this method return True # Boto3 will raise an exception if an operation fails (see: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/error-handling.html) def stop(self): self._ec2_conn.stop_instances(InstanceIds=[self._ec2_inst['InstanceId']]) #, DryRun=True) + #TODO: call "AWSEC2Utils.wait_for_operation(self._ec_conn, 'instance_running', [self._ec2_inst['InstanceId'])" to perform a synchronous version of this method return True # Boto3 will raise an exception if an operation fails (see: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/error-handling.html) + def _wait_for_operation(self, op): + """ Wait for the given operation to complete. + Args: + operation (str): the operation Id to wait for. + + See: + - https://boto3.amazonaws.com/v1/documentation/api/latest/guide/clients.html + - https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#waiters + """ + # See: + # - https://boto3.amazonaws.com/v1/documentation/api/latest/guide/clients.html + # - https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#waiters + waiter = self.ec2_client.get_waiter(op) + waiter.wait(Filters=[ + {'Name': 'instance-id', 'Values': [each_instance.instance_id]}]) + d # For a list of valid filters to use with the ex_filter parameter of certain methods, # please visit https://docs.aws.amazon.com/en_us/AWSEC2/latest/APIReference/API_Operations.html -- GitLab