如何在 Openstack 中捕获堆栈状态

How to catch the stack status in Openstack

我有以下条件 1.堆栈创建 2.堆栈更新 3. 堆栈创建

我想做的是,当 stackCreate/Update/Delete 被触发时,我需要检查进度。我怎样才能做到这一点?我知道 2 wayts 1. openstack堆栈事件列表。 2. 我有以下 python 代码。

    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)

第一种方法是shell命令,我不是很擅长。 (或者说我不知道​​如何最好地将 shell 命令集成到 python 中) 这两种方法的问题在于我正在执行这么多步骤来确定堆栈删除是否成功。我对 stackupdate 和 create 重复同样的操作,这不是我认为的最佳实践。任何人都知道如何最小化这种逻辑?任何帮助是极大的赞赏。

您可以将简单的函数写入 create/update/delete 堆栈并检查堆栈的状态。

请检查下面的示例代码以创建堆栈并轮询堆栈的状态。

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    

我现在在下面使用它。这不是我认为最好的,但满足了我需要做的事情。

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)

调用这个函数

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