Python / Fabric误解

时间:2013-10-23 22:20:19

标签: python fabric

我在使用Fabric时正在削减Python。看起来我对Python和/或Fabric的工作方式有一个基本的误解。看看我的2个脚本

AppDeploy.py

from fabric.api import *

class AppDeploy:
    # Environment configuration, all in a dictionary
    environments = { 
       'dev' : { 
           'hosts' : ['localhost'],
       },
    }

    # Fabric environment
    env = None

    # Take the fabric environment as a constructor argument
    def __init__(self, env):
        self.env = env 

    # Configure the fabric environment
    def configure_env(self, environment):
        self.env.hosts.extend(self.environments[environment]['hosts'])

fabfile.py

from fabric.api import *
from AppDeploy import AppDeploy

# Instantiate the backend class with
# all the real configuration and logic
deployer = AppDeploy(env)

# Wrapper functions to select an environment
@task
def env_dev():
    deployer.configure_env('dev')

@task
def hello():
    run('echo hello')

@task
def dev_hello():
    deployer.configure_env('dev')
    run('echo hello')

将前两个任务联系起来

$ fab env_dev hello
[localhost] Executing task 'hello'
[localhost] run: echo hello
[localhost] out: hello


Done.
Disconnecting from localhost... done.

然而,运行最后一项任务,其目的是配置环境并在单个任务中执行某些操作,看起来结构没有配置环境

$ fab dev_hello
No hosts found. Please specify (single) host string for connection: 

我很遗憾,因为如果我像这样调整那种方法

@task
def dev_hello():
    deployer.configure_env('dev')
    print(env.hosts)
    run('echo hello')

看起来env.hosts 设置,但是,结构仍然表现得不像:

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

这里发生了什么?

1 个答案:

答案 0 :(得分:1)

我不确定你要做什么,但是......

如果您丢失了有关shell / environment的信息 - Fabric会在单独的shell语句中运行每个命令,因此您需要手动链接命令或使用prefix上下文管理器。

请参阅http://docs.fabfile.org/en/1.8/faq.html#my-cd-workon-export-etc-calls-don-t-seem-to-work

如果你在“python”中丢失了信息,它可能与我最近遇到的这个错误/行为有关[https://github.com/fabric/fabric/issues/1004],其中我进入Fabric的shell似乎已被删除。