简体   繁体   English

在Fabric中,如何从另一个python文件执行任务?

[英]In Fabric, how can I execute tasks from another python file?

I have a fabfile (fabfile.py) with some tasks declared: 我有一个fabfile(fabfile.py),声明了一些任务:

# fabfile.py
from fabric.api import *

@task
def start():
  # code

@task
def stop():
  # code

Then when I try to call any of those tasks using the execute function from fabric like this: 然后,当我尝试使用面料中的执行函数调用任何这些任务时,如下所示:

# main.py
from fabric.api import execute
from fabfile import * # I don't really know if this is necessary 
                      # or how should it be done
def main():
  execute('start')

It raises this error: 它引发了这个错误:

Fatal error: None is not callable or a valid task name

My intention is to make a kind of wrapper for some tasks specified in that fabfile that can be called with different arguments, and the task to perform must be taken from the arguments when you make a call to this main program, so I can't explicitly call the function, but use the task names. 我的目的是为fabfile中指定的某些任务创建一个包装器,可以使用不同的参数调用,并且当你调用这个主程序时必须从参数中获取要执行的任务,所以我不能显式调用该函数,但使用任务名称。

How would this be done? 怎么做? Maybe I'm misunderstanding how fabric is supposed to work? 也许我误解了面料应该如何运作?

Thank you 谢谢

Change execute('start') to execute(start) . 更改execute('start')execute(start)

I didn't find out why pass a taskname to execute did not work, but there is a workaround: 我没有找到为什么传递一个taskname执行不起作用,但有一个解决方法:

import fabfile
execute(getattr(fabfile, 'start'))

Update: After reading a bit code and doing some test of fabric, I think execute('task_name') can only be used when fabric tasks are loaded. 更新:在读取位代码并对结构进行一些测试后,我认为execute('task_name')只能在加载结构任务时使用。 By default you can use it in the fabfile.py like this: 默认情况下,您可以在fabfile.py中使用它,如下所示:

@task
def task1():
    #do task1

@task
def task2():
    #do task2

@task
def task3():
    #do task1 and task2
    execute('task1')
    execute('task2')

Then you can use fab task3 to execute task1 and task2 together. 然后你可以使用fab task3执行task1task2在一起。 But till now, I am still using fabric a tool. 但到目前为止,我仍在使用面料工具。

Update again :-) 再次更新:-)

Then I read a bit code of fabric and found that use fabric as a tool will call fabric.main.main which invokes fabric.main.load_fabfile to load tasks from the fabfile. 然后我读了一些结构代码,发现使用fabric作为工具将调用fabric.main.main ,它调用fabric.main.load_fabfile从fabfile加载任务。

Since you use python main.py to run your script, fab tasks are not loaded even if you imported fabfile. 由于您使用python main.py来运行脚本,即使您导入fabfile,也不会加载fab任务。 So I add a bit code to you main.py : 所以我给你main.py添加了一些代码:

docstring, callables, default = load_fabfile('fabfile.py')
state.commands.update(callables)

And now, execute('start') works exactly as you wanted. 现在, execute('start')完全按照您的意愿工作。

There is no need to use fabfile.py always. 总是不需要使用fabfile.py。 We can give any name to fabfile. 我们可以给fabfile任何名字。 Only we need to specify one parameter --fabfile . 只有我们需要指定一个参数--fabfile

Syntax:
fab --fabfile=<Your File Goes Here> function_name

Example:
fab --fabfile=test test_deploy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM