Python 如何为fabric local()命令设置docker机器环境
我试图对docker machine云提供程序运行命令,因此我需要获取命令Python 如何为fabric local()命令设置docker机器环境,python,docker,fabric,docker-machine,Python,Docker,Fabric,Docker Machine,我试图对docker machine云提供程序运行命令,因此我需要获取命令docker machine env digitalocean的内容,通常如下所示: export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://1.2.3.4:2376" export DOCKER_CERT_PATH="/Users/danh/.docker/machine/machines/digitalocean" export DOCKER_MACHINE_NAM
docker machine env digitalocean
的内容,通常如下所示:
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://1.2.3.4:2376"
export DOCKER_CERT_PATH="/Users/danh/.docker/machine/machines/digitalocean"
export DOCKER_MACHINE_NAME="digitalocean"
# Run this command to configure your shell:
# eval "$(docker-machine env digitalocean)"
并将上述内容用作shell前缀,例如:
print 'outside with:' + local('echo $DOCKER_HOST')
with prefix(local('docker-machine env digitalocean', capture=True)):
print 'inside with:' + local('echo $DOCKER_HOST')
with prefix('DOCKER_HOST="tcp://1.2.3.4:2376"'):
print 'inside with (manual):' + local('echo $DOCKER_HOST')
然而,这反而返回:
outside with:tcp://192.168.99.100:2376
inside with:
inside with (manual):tcp://1.2.3.4:2376
我能看到的唯一方法就是手动撕开
local('docker-machine env digitalocean')
的结果。当然,还有一种更像织物的方式吗?好吧,到目前为止,我采用了一种解决方案,但感觉有点粗糙:
def dm_env(machine):
"""
Sets the environment to use a given docker machine.
"""
_env = local('docker-machine env {}'.format(machine), capture=True)
# Reorganize into a string that could be used with prefix().
_env = re.sub(r'^#.*$', '', _env, flags=re.MULTILINE) # Remove comments
_env = re.sub(r'^export ', '', _env, flags=re.MULTILINE) # Remove `export `
_env = re.sub(r'\n', ' ', _env, flags=re.MULTILINE) # Merge to a single line
return _env
@task
def blah():
print 'outside with: ' + local('echo $DOCKER_HOST')
with prefix(dm_env('digitalocean')):
print 'inside with: ' + local('echo $DOCKER_HOST')
产出:
outside with: tcp://192.168.99.100:2376
inside with: tcp://1.2.3.4:2376
获取所需信息的另一种方法是对
docker machine inspect
命令的输出使用格式化模板
请注意,Python字符串中的花括号需要通过将它们加倍来转义,因此每次都会有四个打开和关闭括号
machine_name = dev
machine_ip = local("docker-machine inspect --format='{{{{.Driver.IPAddress}}}}' {0}".format(machine_name), capture=True)
machine_port = local("docker-machine inspect --format='{{{{.Driver.EnginePort}}}}' {0}".format(machine_name), capture=True)
machine_cert_path = local("docker-machine inspect --format='{{{{.HostOptions.AuthOptions.StorePath}}}}' {0}".format(machine), capture=True)
现在,您可以使用,通过设置相应的环境变量,将Docker守护程序临时指向远程计算机:
from fabric.api import shell_env
with shell_env(DOCKER_TLS_VERIFY='1',
DOCKER_HOST='tcp://{0}:{1}'.format(machine_ip, machine_port),
DOCKER_CERT_PATH=machine_cert_path,
DOCKER_MACHINE_NAME=machine_name):
# will print containers on the remote machine
local('docker ps -a')
# will print containers on your local machine
# since environment switch is only valid within the context manager
local('docker ps -a')
看起来,带有local的内部结果类似于导出DOCKER\u TLS\u VERIFY=“1”echo$DOCKER\u TLS\u VERIFY,这不是您想要的。我怀疑这会奏效。但是您是否尝试过eval
使用docker machine
命令来设置环境,就像您通常在shell上那样?我猜每个local
都在自己的shell中,所以这可能不起作用。