当我使用 Fabric 时,我正在学习 Python。看起来我对 Python 和/或 Fabric 的工作原理有一个基本的误解。看看我的 2 个脚本
应用部署.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'])
工厂文件.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.
但是,运行最后一个任务,该任务旨在配置环境并在单个任务中执行某些操作,看起来fabric 没有配置环境
$ 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
已经设置好了,但是,fabric 的行为就像它不是:
$ fab dev_hello
['localhost']
No hosts found. Please specify (single) host string for connection:
这里发生了什么?