gpt4 book ai didi

python - 如何为 fabric local() 命令设置 docker-machine env

转载 作者:行者123 更新时间:2023-11-28 17:31:22 25 4
gpt4 key购买 nike

我正在尝试针对 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_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

关于python - 如何为 fabric local() 命令设置 docker-machine env,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com