简体   繁体   English

将Fabric env.hosts sting作为变量传递在函数中不起作用

[英]Passing a Fabric env.hosts sting as a variable is not work in function

Passing a Fabric env.hosts sting as a variable is not work in function. 将Fabric env.hosts sting作为变量传递在函数中不起作用。

demo.py 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 main.py

#!/usr/bin/env python

from demo import deploy

hosts = ['localhost']
command = 'hostname'
deploy(hosts, command)

python main.py python main.py

['localhost']
No hosts found. Please specify (single) host string for connection:

But env.host_string works! 但是env.host_string有效!

demo.py 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 main.py

#!/usr/bin/env python

from demo import deploy

host = 'localhost'
command = 'hostname'
deploy(host, command)

python main.py 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. 但env.host_string对我们来说还不够,它只是一个主机。 Maybe we can use env.host_string within a loop, but that's not good. 也许我们可以在循环中使用env.host_string,但这并不好。 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: 现在在ddep(我的部署引擎)中,我只使用MySQLdb来获取参数然后执行fab命令,如:

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. 因为如果我使用fab命令,我无法捕获Python中结果的异常和失败,以使我的ddep可以“重试”失败的主机。 If I use the "from demo import deploy", I can control and get them by some codes in Python. 如果我使用“来自demo demo deploy”,我可以通过Python中的一些代码来控制和获取它们。

So now "env.host " is the trouble. 所以现在“env.host”就是麻烦。 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 . 根据文档 ,如果你从python脚本调用结构任务 - 你应该使用fabric.tasks.execute

Should be smth like this: 应该是这样的:

  • demo.py 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 main.py

     from demo import deploy hosts = ['localhost'] command = 'hostname' deploy(hosts, command) 

Then, just run python main.py . 然后,运行python main.py Hope that helps. 希望有所帮助。

Finally, I fixed this problem by using execute() and exec. 最后,我使用execute()和exec修复了这个问题。

main.py main.py

#!/usr/bin/env python

from demo import FabricSupport

hosts = ['localhost']

myfab = FabricSupport()
myfab.execute("df",hosts)

demo.py demo.py

#!/usr/bin/env python

from fabric.api import env, run, execute

class FabricSupport:
    def __init__(self):
        pass

    def hostname(self):
        run("hostname")

    def df(self):
        run("df -h")

    def execute(self,task,hosts):
        get_task = "task = self.%s" % task
        exec get_task
        execute(task,hosts=hosts)

python main.py python main.py

[localhost] Executing task 'hostname'
[localhost] run: hostname
[localhost] out: heydevops-workspace

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

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