传递一个Fabric env.hosts sting作为变量在函数中不起作用 [英] Passing a Fabric env.hosts sting as a variable is not work in function
问题描述
demo.py
#。/ usr / bin / env python
from fabric.api import env,运行
def deploy(hosts, )
打印主机
env.hosts =主机
运行(命令)
main.py
#!/ usr / bin / env python
导入部署
hosts = ['localhost']
command ='hostname'
deploy(hosts,command)
python main.py
['localhost']
找不到主机。请指定(单个)连接的主机字符串:
但是env.host_string工程!
demo.py
#!/ usr / bin / env python
从fabric.api import env,运行
def deploy(host,command):
打印主机
env.host_string =主机
运行)
main.py
#/ usr / bin / env python
从演示导入部署
主机='本地主机'
command ='主机名'
deploy(host,command)
python main.py
localhost
[localhost]运行:hostname
[localhost] out:heydevops-workspace
但是env.host_string对我们来说还不够,它是一个单一的主机。
也许我们可以在循环中使用env.host_string,但这不是很好。
因为我们还想设置并发任务编号并并行运行。
现在在ddep(我的部署引擎)中,我只使用MySQLdb来获取参数然后执行fab命令,如:
os.system(fab -f service /%s.py -H%s -P -z%s%s%(项目,主机,号码,任务)
是一个简单的方法,但不好。
因为如果我使用fab命令,我无法捕获Python中的结果的异常和失败,使我的ddep可以重试失败的主机。
如果我使用从演示导入部署,我可以通过Python中的一些代码来控制和获取它们。
所以现在env.host是麻烦。有人可以给我一个解决方案吗?
非常感谢。
这是我的见解。
根据文档,如果您从python调用fabric任务脚本 - 您应该使用 fabric.tasks.execute 。
应该是这样的:
-
demo.py
from fabric.api import运行
from fabric.tasks import execute
def deploy(hosts,command):
execute(execute_deploy,command = command,hosts = hosts)
def execute_deploy(command):
run(command)
-
main.py
从demo import deploy
hosts = ['localhost']
command ='hostname'
部署(主机,命令)
然后,只需运行 python main.py
。希望有帮助。
Passing a Fabric env.hosts sting as a variable is not work in function.
demo.py
#!/usr/bin/env python
from fabric.api import env, run
def deploy(hosts, command):
print hosts
env.hosts = hosts
run(command)
main.py
#!/usr/bin/env python
from demo import deploy
hosts = ['localhost']
command = 'hostname'
deploy(hosts, command)
python main.py
['localhost']
No hosts found. Please specify (single) host string for connection:
But env.host_string works!
demo.py
#!/usr/bin/env python
from fabric.api import env, run
def deploy(host, command):
print host
env.host_string = host
run(command)
main.py
#!/usr/bin/env python
from demo import deploy
host = 'localhost'
command = 'hostname'
deploy(host, command)
python main.py
localhost
[localhost] run: hostname
[localhost] out: heydevops-workspace
But the env.host_string is not enough for us, it's a single host. Maybe we can use env.host_string within a loop, but that's not good. Because we also want to set the concurrent tasks number and run them parallelly.
Now in ddep(my deployment engine), I only use MySQLdb to get the parameters then execute the fab command like:
os.system("fab -f service/%s.py -H %s -P -z %s %s" % (project,host,number,task))
This is a simple way but not good. Because if I use the fab command, I can't catch the exceptions and failures of the results in Python, to make my ddep can "retry" the failed hosts. If I use the "from demo import deploy", I can control and get them by some codes in Python.
So now "env.host " is the trouble. Can somebody give me a solution? Thanks a lot.
Here's my insight.
According to docs, if you're calling fabric tasks from python scripts - you should use fabric.tasks.execute.
Should be smth like this:
demo.py
from fabric.api import run from fabric.tasks import execute def deploy(hosts, command): execute(execute_deploy, command=command, hosts=hosts) def execute_deploy(command): run(command)
main.py
from demo import deploy hosts = ['localhost'] command = 'hostname' deploy(hosts, command)
Then, just run python main.py
. Hope that helps.
这篇关于传递一个Fabric env.hosts sting作为变量在函数中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!