2016-12-27 5 views
0

Я начинаю работать с тканью и запускаюсь в проблему, когда задачи выполняются несколько раз. Я только планирую, что задача будет запущена.python fabric ошибочные дубликаты выполнения задачи

Вот мой fabfile (оба узел и концентраторы списки. Узлы имеют несколько узлов. Концентраторы имеют только один хост) импорт запросов импорт JSON из fabric.api импорта CD, ENV, выполнять роли, работает, настройки, задача, 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. 

Если я закомментируйте 'Execute (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