我有一个要求,我需要在本地计算机上运行一个docker命令,并将此列表发送到远程服务器并检查这些图像是否存在。我需要将远程服务器上不存在的映像列表重新运行到本地服务器。我需要用python来做。我已经通过混合shell和python编写了一些代码,如下所示。
List=$(docker images -q | grep "docker pull" | awk '{print $3}') #this command is mandatory to get exact docker name.
fab remote_sync_system_spec_docker_to_aws_artifactory:List -u ${USERNAME} -H 1.2.3.4
我正在通过fab尝试shell命令的传递输出,即List到pyhon函数。这个函数如下所示。
def remote_sync_system_spec_docker_to_aws_artifactory(List):
for line in List:
if( os.popen("docker images -q $line") == none )
List=... #need to prepare list and return back to calling function.
一旦我在远程服务器上获得列表,我需要将其返回给调用函数,我可以在那里进行一些操作。基本上我可以使用shell但问题是在我的项目中不接受使用sshpass连接到远程服务器所以寻找python脚本。
答案 0 :(得分:0)
mod_mam:
default: always
iqdisc: one_queue
request_activates_archiving: true
assume_mam_usage: true
cache_size: 1000
cache_life_time: 3600
db_type: sql
会在内存中返回并反对,你应该做的是
os.popen()
答案 1 :(得分:0)
如果您需要的是从shell命令获取输出,则应该避免os.popen()
甚至更换subprocess.Popen()
。
对于最近的Python 3.x,请使用subprocess.run()
:
import subprocess
List = ()
for result in subprocess.run(["docker", "images", "-q"],
stdout=subprocess.PIPE, universal_newlines=True).stdout.split('\n'):
if 'docker pull' in result:
List.append(result.split()[3])
在Python 2.x中,相应的函数是subprocess.check_output()
。
也许你会想要用更集中的东西取代grep
; 'docker pull' in result
将在该行的任何位置查找字符串,但您可能希望将其限制在特定列中,例如。
答案 2 :(得分:0)
作为传输列表的简单方法,我建议使用管道而不是变量。
docker images -q | awk '/docker pull/ { print $3 }' |
fab remote_sync_system_spec_docker_to_aws_artifactory_stdin -u ${USERNAME} -H 1.2.3.4
其中函数类似于
import sys, subprocess
def remote_sync_system_spec_docker_to_aws_artifactory_stdin (handle=sys.stdin):
"""
Read Docker image identifiers from file handle; check which
ones are available here, and filter those out; return the rest.
"""
missing = ()
for line in handle:
repo = line.rstrip('\n')
if subprocess.run(['docker', 'images', '-q', repo],
stdout=subprocess.PIPE, universal_newlines=True).stdout == "":
missing.append(repo)
return missing