From b0324b2c5863a4f609449c741b120bd9abd6fcf1 Mon Sep 17 00:00:00 2001 From: sguazt <marco.guazzone@gmail.com> Date: Tue, 5 Jan 2021 11:29:24 +0100 Subject: [PATCH] Ditto --- easycloud/common/aws.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/easycloud/common/aws.py b/easycloud/common/aws.py index 43fa4a0..6a099ce 100644 --- a/easycloud/common/aws.py +++ b/easycloud/common/aws.py @@ -1,4 +1,6 @@ +from botocore.exceptions import WaiterError from easycloud.core.metaconfmanager import MetaConfManager +import math class AWSBaseConfManager(MetaConfManager): @@ -34,3 +36,38 @@ class AWSBaseConfManager(MetaConfManager): # Filters (for images, Amazon has 30.000+ images available) self.images_filters = self.parser.get( "filters", "images_filters").replace(" ", "").replace("\n", "").split(",") + + +class AWSEC2Utils: + @staticmethod + def wait_for_instance_operation(client, operation, instances, timeout=None): + """ Wait for the given operation to complete. + Args: + client (boto.EC2.Client) the AWS EC2 client. + operation (str): the operation to wait for (e.g., 'instance_running'). + instances (list): the list of instance IDs. + timeout (float): an optional value representing the max number of + seconds to wait for; if None, no timeout is enforced. + + 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 = client.get_waiter(operation) + if timeout is not None: + delay = 5 + waiter_config = {'Delay': delay, 'MaxAttempts': math.ceil(timeout/delay)} + ret = True + check = True + while check: + try: + if timeout is None: + waiter.wait(InstanceIds = instances) + else: + waiter.wait(InstanceIds = instances, WaiterConfig = waiter_config) + check = False + except WaiterError: + if timeout is not None: + check = False + ret = False + return ret -- GitLab