Search code examples
python-2.7automationopenstack

How to catch the stack status in Openstack


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.


Solution

  • 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")