docker镜像更新自动化脚本

18 篇文章 1 订阅
12 篇文章 1 订阅

一般情况下,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下发脚本

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值