一般情况下,docker镜像更新需要好几个步骤,乏味而且繁琐,一不小心还容易出错,如果写一个自动化的脚本直接执行,不仅效率可以大大提升,而且还可以避免出错,下面是我写的一个参考脚本。
#! /usr/bin/python2.7
import os
import re
output_path = './python_update_log/'
os.popen('mkdir -p ' % output_path)
# 加载镜像后的输出结果
load_output_file = 'load_output.txt'
load_output_path = output_path + load_output_file
# tag记录保存文件
tag_output_file = 'tag_output.txt'
tag_output_path = output_path + tag_output_file
# push记录保存文件
push_output_file = 'push_output.txt'
push_output_path = output_path + push_output_file
# docker远程更新仓库(Harbor)地址
harbor_ip = '100.100.100.100'
# 镜像源地址(即该镜像文件的来源地址)
eccom_host = 'registry.xxxx.com.cn'
latest_tag = 'latest'
# 输入更新tar包的具体文件名(或者具体路径)
tar_name = raw_input('input the tar name please: ')
load_cmd = 'docker load < %s' % tar_name
print load_cmd
load_output = os.popen(load_cmd)
images_list = list()
re_str = r'Loaded image: (.+):(.+)'
with open(load_output_path, 'w') as fw:
for line in load_output.readlines():
fw.write(line)
line = line.replace('\r', '').replace('\n', '')
re_match = re.match(re_str, line)
try:
image = re_match.group(1)
tag = re_match.group(2)
images_list.append((image, tag))
except:
continue
with open(tag_output_path, 'w') as ft:
with open(push_output_path, 'w') as fp:
fp.write('')
for image,tag in images_list:
harbor_image = image.replace(eccom_host, harbor_ip)
tag_cmd_1 = 'docker tag %s:%s %s:%s' % (image, tag, harbor_image, tag)
tag_cmd_2 = 'docker tag %s:%s %s:%s' % (image, tag, harbor_image, latest_tag)
print tag_cmd_1
print tag_cmd_2
ft.write('======================tag command=======================\n')
ft.write(tag_cmd_1 + '\n')
ft.write(tag_cmd_2 + '\n')
ft.write('======================tag result===========================\n')
tag_result_1 = os.popen(tag_cmd_1).readlines()
print tag_result_1
tag_result_2 = os.popen(tag_cmd_2).readlines()
print tag_result_2
ft.write(str(tag_result_1) + '\n')
ft.write(str(tag_result_2) + '\n')
# push
push_cmd_1 = 'docker push %s:%s' % (harbor_image, tag)
push_cmd_2 = 'docker push %s:%s' % (harbor_image, latest_tag)
with open(push_output_path, 'a') as fp:
fp.write('=====================push command=============================\n')
print push_cmd_1
fp.write(push_cmd_1 + '\n')
push_result_1 = os.popen(push_cmd_1).readlines()
print push_cmd_2
push_result_2 = os.popen(push_cmd_2).readlines()
fp.write('=====================push result==============================\n')
fp.write(str(push_result_1) + '\n')
fp.write(str(push_result_2) + '\n')
# 生成ansible下发命令
update_re = r'.*eccom/(.+)'
update_tags = ''
for image,tag in images_list:
print image
tag_match = re.match(update_re, image)
tag = tag_match.group(1).replace('-', '_')
update_tags = update_tags + tag + ','
update_tags = update_tags[:-1]
update_cmd = 'ansible-playbook -i hosts -e state=restarted --tags "%s" 4-install-cloudnet-service.yml' % update_tags
update_cmd_log = output_path + 'update_cmd_log.log'
with open(update_cmd_log, 'w') as fu:
fu.write(update_cmd + '\n')
exec_command = 'cd /opt/devops-master/ansible/ && ' + update_cmd
os.system(exec_command)
直接将更新包(如果需要解压则要提前解压)拷贝到该脚本的同一目录,运行该脚本,输入更新包的名称,即可实现自动上传到Harbor远程仓库,并且自动生成ansible下发脚本