0
votes

I have following conditions 1. stackCreate 2. stackUpdate 3. stackCreate

What I am trying to do is, while the stackCreate/Update/Delete is triggered, I need to check on the progress. How can I do that? I know of 2 wayts 1. openstack stack event list . 2. I have below python code.

    stack_id = str(hc.stacks.get(stack_name).id)
                    hc.stacks.delete(stack_id=stack_id)
                    try:
                        evntsdata = hc.events.list(stack_name)[0].to_dict()
                        event_handle = evntsdata['resource_status']
                        if event_handle == 'DELETE_IN_PROGRESS':
                            loopcontinue = True
                            while loopcontinue:
                                evntsdata = hc.events.list(stack_name)[0].to_dict()
                                event_handle = evntsdata['resource_status']

                                if event_handle == 'DELETE_COMPLETE':
                                    loopcontinue = False
                                    print(str(timestamp()) + " " + "Delete is Completed!")
                                elif event_handle == 'DELETE_FAILED':
                                    print("Failed")  # this needs a proper error msg
                                    sys.exit(0)
                                else:
                                    print(str(timestamp()) + " " + "Delete in Progress!")
                                    time.sleep(5)
                        elif event_handle == 'DELETE_COMPLETE':
                            print(str(timestamp()) + " " + "Delete is Completed!")
                            sys.exit(0)
                        elif event_handle == 'DELETE_FAILED':
                            print("Failed")
                            sys.exit(0)
                    except AttributeError as e:
                        print(str(timestamp()) + " " + "ERROR: Stack Delete Failure")
                        raise
                except (RuntimeError, heatclient.exc.NotFound):
                    print("Stack doesnt exist:", stack_name)

The first method is shell command in which I am not very good. (or lets say I dont know how to best integrate the shell command in python) The problem with both the methods is that I am putting these many steps to identify whether the stack delete is successful. And I am repeating the same for stackupdate and create which is not best practice I am thinking. Anyone has any idea how I can minimize this logic? Any help is greatly appreciated.

2

2 Answers

0
votes

You can write simple functions to create/update/delete stack and also to check the status of stack.

Please check below sample code to create a stack and poll the status of the stack.

from keystoneauth1 import loading
from keystoneauth1 import session
from heatclient import client


tenant_id = 'ab3fd9ca29e149acb25161ec8053da9c'
heat_url = 'http://10.26.12.31:8004/v1/%s' % tenant_id
auth_token = 'gAAAAABZYxfjz88XNXnfoCPkNLVeVtqtJ9o8qEtgFhI2GJ-ewSCuiypdwt3K5evgQeICVRqMa2jXgzVlENAUB19ZNyQfVCxSX4_lMBKyChM76SGuQUP8U-xJ9EKIfFaVwRGBkk4Ow9OO-iNINfMs0B5-LzJvxTFybi8yZw4EiagQpNpfu1onYfc'
heat = client.Client('1', endpoint=heat_url, token=auth_token)


def create_stack(stack_file_path, stack_name, parameters=None):
    template = open(stack_file_path)
    if parameters:
        stack = heat.stacks.create(stack_name=stack_name, template=template.read(), parameters=parameters)
    else:
        stack = heat.stacks.create(stack_name=stack_name, template=template.read())
    template.close()
    return stack


def get_stack_status(stack_id):
    stack = heat.stacks.get(stack_id)
    return stack.stack_status


def poll_stack_status(stack_id, poll_time=5):
    stack_status = get_stack_status(stack_id)
    while stack_status != 'CREATE_COMPLETE':
        if stack_status == 'CREATE_FAILED':
            return 1
        time.sleep(poll_time)
        stack_status = get_stack_status(stack_id)
    return 0    
0
votes

I worked it with below for now. It's not the best I think but satisfies what I need to do.

def stackStatus(status):
    evntsdata = hc.events.list(stack_name)[0].to_dict()
    event_handle = evntsdata['resource_status'].split("_")
    event_handle = '_'.join(event_handle[1:])
    if event_handle == 'IN_PROGRESS':
        loopcontinue = True
        while loopcontinue:
            evntsdata = hc.events.list(stack_name)[0].to_dict()
            event_handle = evntsdata['resource_status'].split("_")
            event_handle = '_'.join(event_handle[1:])
            if event_handle == 'COMPLETE':
                loopcontinue = False
                print(str(timestamp()) + status + " IS COMPLETED!")
            elif event_handle == 'FAILED':
                print("Failed")
                exit(1)
            else:
                print(str(timestamp()) + status + " IN PROGRESS!")
                time.sleep(5)

Call this function

stackStatus("DELETE")
stackStatus("CREATE")
stackStatus("UPDATE")