2017-07-08 84 views
0

堆疊狀態我有以下條件 1. stackCreate 2. stackUpdate 3. stackCreate如何捕捉在OpenStack的

我所試圖做的是,雖然stackCreate /更新/刪除被觸發,我需要檢查進度。我怎樣才能做到這一點?我知道2條路線 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命令,其中我不是很好。 (或者讓我說我不知道​​如何最好地集成在python中的shell命令) 這兩種方法的問題是,我正在執行這些很多步驟來確定堆棧刪除是否成功。我正在重複stackupdate和創建這不是我正在考慮的最佳實踐。任何人都有任何想法如何我可以最小化這種邏輯?任何幫助是極大的讚賞。

回答

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

您可以編寫簡單的函數來創建/更新/刪除堆棧以及檢查堆棧的狀態。

請檢查下面的示例代碼來創建堆棧並輪詢堆棧的狀態。

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

我仍然覺得這個漫長。我正在考慮循環check_output([「openstack」,「stack」,「event」,「list」,stack_name]) –

+0

您可以使用「openstack stack show -f value -c stack_status 」 – Tejaswi

+0

這仍然不行因爲它沒有通過不斷變化的狀態循環。 –