2016-12-27 43 views
0

我剛開始使用結構並運行多個任務正在執行的問題。我只打算讓任務運行。python結構錯誤的任務重複執行

這裏是我的fabfile(節點和樞紐的名單。節點有多個主機。集線器只有一個主機)從fabric.api導入CD,ENV,執行,角色,運行,設置 導入請求 進口JSON ,任務,runs_once

buildconfig_location = 'http://10.102.0.69' 

def get_environment_configuration(environment_name): 
    request = requests.get("{}/api/v1/environments/{}".format(buildconfig_location, environment_name)) 
    environment_configuration = json.loads(request.text) 

    return environment_configuration 

def get_hub_servers(server_list): 
    hub = [ 
     server['ip_address'] 
     for server in server_list 
     if server['active'] and server['unit_tests']] 

    return hub 

def get_node_servers(server_list): 
    nodes = [ 
     server['ip_address'] 
     for server in server_list 
     if server['active'] and server['unit_tests'] is False] 

    return nodes 

def set_hosts(environment_configuration): 
    environment_configuration['servers'] 

    env.roledefs = { 
     'hub': [ 
      server['ip_address'] 
      for server in environment_configuration['servers'] 
      if server['active'] and server['unit_tests']], 
     'node': [ 
      server['ip_address'] 
      for server in environment_configuration['servers'] 
      if server['active'] and server['unit_tests'] is False], 
    } 

def start_node(): 
    with settings(user="automation1"): 
     with cd('/home/automation1/inventory.robot/grid'): 
      output = run('nohup ./start_node.sh > node_out 2>&1 &') 

def start_hub(): 
    with settings(user="automation1"): 
     with cd('/home/automation1/inventory.robot/grid'): 
      run('nohup ./start_hub.sh > hub_out 2>&1 &') 

def robot_test(): 
    with settings(user="automation1"): 
     with cd('/home/automation1/inventory.robot/grid'): 
      run('python grid.py') 

def kill_node(): 
    with settings(user="automation1"): 
     with cd('/home/automation1/inventory.robot/grid'): 
      run('./kill_node.pl') 

@task 
@runs_once 
def robot_test(): 
    environment_configuration = get_environment_configuration('Selenium') 
    #set_hosts(environment_configuration) 
    nodes = get_node_servers(environment_configuration['servers']) 
    hubs = get_hub_servers(environment_configuration['servers']) 

    execute(start_hub, hosts=hubs) 
    execute(start_node, hosts=nodes) 
    execute(robot_test, hosts=hubs) 

    all = hubs + nodes 
    execute(kill_node, hosts=(hubs + nodes)) 

下面是輸出:

[10.102.0.101] Executing task 'start_hub' 
[10.102.0.101] run: nohup ./start_hub.sh > hub_out 2>&1 & 
[10.102.0.102] Executing task 'start_node' 
[10.102.0.102] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.103] Executing task 'start_node' 
[10.102.0.103] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.104] Executing task 'start_node' 
[10.102.0.104] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.105] Executing task 'start_node' 
[10.102.0.105] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.106] Executing task 'start_node' 
[10.102.0.106] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.107] Executing task 'start_node' 
[10.102.0.107] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.108] Executing task 'start_node' 
[10.102.0.108] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.109] Executing task 'start_node' 
[10.102.0.109] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.110] Executing task 'start_node' 
[10.102.0.110] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.101] Executing task 'robot_test' 
[10.102.0.101] Executing task 'start_hub' 
[10.102.0.101] run: nohup ./start_hub.sh > hub_out 2>&1 & 
[10.102.0.102] Executing task 'start_node' 
[10.102.0.102] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.103] Executing task 'start_node' 
[10.102.0.103] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.104] Executing task 'start_node' 
[10.102.0.104] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.105] Executing task 'start_node' 
[10.102.0.105] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.106] Executing task 'start_node' 
[10.102.0.106] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.107] Executing task 'start_node' 
[10.102.0.107] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.108] Executing task 'start_node' 
[10.102.0.108] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.109] Executing task 'start_node' 
[10.102.0.109] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.110] Executing task 'start_node' 
[10.102.0.110] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.101] Executing task 'robot_test' 
[10.102.0.101] Executing task 'start_hub' 
[10.102.0.101] run: nohup ./start_hub.sh > hub_out 2>&1 & 
[10.102.0.102] Executing task 'start_node' 
[10.102.0.102] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.103] Executing task 'start_node' 
[10.102.0.103] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.104] Executing task 'start_node' 
[10.102.0.104] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.105] Executing task 'start_node' 
[10.102.0.105] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.106] Executing task 'start_node' 
[10.102.0.106] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.107] Executing task 'start_node' 
[10.102.0.107] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.108] Executing task 'start_node' 
[10.102.0.108] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.109] Executing task 'start_node' 
[10.102.0.109] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.110] Executing task 'start_node' 
[10.102.0.110] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.101] Executing task 'robot_test' 
[10.102.0.101] Executing task 'start_hub' 
[10.102.0.101] run: nohup ./start_hub.sh > hub_out 2>&1 & 
[10.102.0.102] Executing task 'start_node' 
[10.102.0.102] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.103] Executing task 'start_node' 
[10.102.0.103] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.104] Executing task 'start_node' 
[10.102.0.104] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.105] Executing task 'start_node' 
[10.102.0.105] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.106] Executing task 'start_node' 
[10.102.0.106] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.107] Executing task 'start_node' 
[10.102.0.107] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.108] Executing task 'start_node' 
[10.102.0.108] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.109] Executing task 'start_node' 
[10.102.0.109] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.110] Executing task 'start_node' 
[10.102.0.110] run: nohup ./start_node.sh > node_out 2>&1 & 
[10.102.0.101] Executing task 'robot_test' 
^C 
Stopped. 
Disconnecting from [email protected] done. 
Disconnecting from [email protected] done. 
Disconnecting from [email protected] done. 
Disconnecting from [email protected] done. 
Disconnecting from [email protected] done. 
Disconnecting from [email protected] done. 
Disconnecting from [email protected] done. 
Disconnecting from [email protected] done. 
Disconnecting from [email protected] done. 
Disconnecting from [email protected] done. 

如果我註釋掉 '執行(robot_test,主機=集線器)' 我得到的輸出更接近我的期望。我知道這與execute和該頁面上的警告有關,但我不明白問題在這裏。

回答

0

robot_test()是遞歸的,它自稱在一個無限循環,這就是爲什麼它的執行,直到你取消。

刪除在robot_test()內部對robot_test()的呼叫。你打算拿這個電話怎麼樣?因爲我猜它有一個目的,它不是一個錯字。

@task 
@runs_once 
def robot_test(): 
    environment_configuration = get_environment_configuration('Selenium') 
    #set_hosts(environment_configuration) 
    nodes = get_node_servers(environment_configuration['servers']) 
    hubs = get_hub_servers(environment_configuration['servers']) 

    execute(start_hub, hosts=hubs) 
    execute(start_node, hosts=nodes) 

    all = hubs + nodes 
    execute(kill_node, hosts=(hubs + nodes)) 

如果你真的想遞歸執行它,你需要一個參數來知道遞歸應該停止。

+0

你釘了它。我處於其他事情的中間,並意外地使用了robot_test這個名稱來提供兩種不同的方法。 – user7344353

0

您只需將runs_once修飾器添加到應該只運行一次的每個函數中。例如: -

@runs_once 
def start_node(): 
    with settings(user="automation1"): 
     with cd('/home/automation1/inventory.robot/grid'): 
      output = run('nohup ./start_node.sh > node_out 2>&1 &') 

@runs_once 
def start_hub(): 
    with settings(user="automation1"): 
     with cd('/home/automation1/inventory.robot/grid'): 
      run('nohup ./start_hub.sh > hub_out 2>&1 &') 

@runs_once 
def robot_test(): 
    with settings(user="automation1"): 
     with cd('/home/automation1/inventory.robot/grid'): 
      run('python grid.py') 

沒有runs_once裝飾,每個功能將一次每臺主機不是一次每個主機設置運行。


對於每個主機一次,你可以做這樣的事情:

from fabric.context_managers import env 

env.hosts = hubs 
execute(start_hub) 
env.hosts = nodes 
execute(start_node) 
env.hosts = hubs 
execute(robot_test) 
+0

start_node,start_hub,robot_test和kill_node應該爲每個主機運行一次。我的問題是這些每個主機運行多次*而不是每個主機運行一次。 – user7344353

相關問題