2013-10-23 50 views
1

我在与织物一起工作时正在削减我的牙齿。看起来我对Python和/或Fabric的工作原理有一个基本的误解。我的2个脚本Python /织物误解

看看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') 

链接第2个任务作品

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


Done. 
Disconnecting from localhost... done. 

然而,运行最后的任务,whi CH旨在配置环境,做在一个任务中的东西,看来布没有配置

$ 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: 

什么在这里呢?

+0

我能够使用'@ hosts'和'@ with_settings'装饰器来解决这个问题。 – quickshiftin

回答

1

我不知道你想要做什么,但是......

如果你的shell /环境信息丢失 - 织物运行在一个单独的外壳语句中的每个命令,所以你需要手动链接命令或使用prefix上下文管理器。

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

如果你内“巨蟒”的信息丢失,它可能会捆绑到这个bug /行为,我跑进最近[https://github.com/fabric/fabric/issues/1004]在这里我开始与面料外壳似乎抹杀。

+0

绝对看起来像正确的方向,但我试图在'AppDeploy'中设置'env'结构,所以不确定如何在调用'前缀(...):'from * fabfile.py * 。我想将电话保留在不同的地点,或者根本不可能? – quickshiftin