当前位置: 首页 > news >正文

docker 更新layer

docker 更新layer

  • 代码
  • 使用

代码

import os
import subprocess
import multiprocessingdef run_command(run_cmd, shell=True, executable='/bin/bash', echo=True):"""run input command through subprocess.check_call.Args:- run_cmd (string): command to be performedReturns:int, program exit codeRaises:raise exception if failed to run the command"""if echo:print("CMD: %s"%run_cmd)res = subprocess.check_call(run_cmd, shell=shell, executable=executable)return resdef get_command_output(run_cmd):"""run input command through subprocess.check_output.Args:- run_cmd (string): command to be performedReturns:str, command outputRaises:raise exception if failed to run the command"""print("CMD: %s"%run_cmd)return_info = subprocess.check_output(run_cmd, shell=True)return bytes.decode(return_info).rstrip('\n')def run_command_parallel(cmd_list, parallel_job=2):"""run input command through Pool & subprocess.check_call to parallel.Args:- run_cmd (list): command to be performedNotes:If failed, call sys.exit with the exitcode same as the running command."""pool = multiprocessing.Pool(parallel_job)pool.map(run_command, cmd_list)pool.close()pool.join()def create_dir(folder, *subfolders):"""to create folder and any given subfolders under `folder`Args:- folder (string): folder to be created- \*subfolders (zero or more string): subfolders under ``folder`` to be createdReturns:- list of stringeach element will be the "folder/subfolder" being created.empty list if `subfolders` are not given."""if not os.path.exists(folder):os.makedirs(folder)ret = []for subfolder in subfolders:d = os.path.join(folder, subfolder)ret.append(d)if not os.path.exists(d):os.makedirs(d)return retclass DockerCmd:def __init__(self, repository, tag):self.repository = repositoryself.tag = tagdef docker_images():docker_images_cmd = f"docker images"def docker_save(self, save_tar_path):docker_save_cmd = f"docker save -o {save_tar_path} {self.repository}:{self.tag}"return docker_save_cmddef docker_load(self, in_tar_path):docker_save_cmd = f"docker load -i {in_tar_path} "return docker_save_cmdclass TarCMD:def compress(source_path, tar_path, file_str="*"):cmd = f"cd {source_path}; tar -cvf {tar_path} {file_str}"return cmddef uncompress(target_path, tar_path):create_dir(target_path)cmd = f"cd {target_path}; tar xvf {tar_path}"return cmddef dir_diff(old_dir, new_dir):old_dir_list = os.listdir(old_dir)new_dir_list = os.listdir(new_dir)# 相同名称,但需要替换的diff_list = [f"{new_dir}/manifest.json", f"{new_dir}/repositories"]# 把old中多余的删除掉# rm_list = [f"{old_dir}/{i}" for i in old_dir_list if i.endswith('.json')]rm_list = []for old_file in old_dir_list:if old_file not in new_dir_list:rm_list.append(f"{old_dir}/{old_file}")# 把new中的添加到old中for new_file in new_dir_list:if new_file not in old_dir_list:diff_list.append(f"{new_dir}/{new_file}")# print(diff_list, rm_list)return diff_list, rm_listdef update_shell_script():uncompress_cmd = TarCMD.uncompress(update_path, update_tar)if __name__ == "__main__":old_rep = "software"old_tag = "v1.4.0"new_rep = "software"new_tag = "v1.4.0"target_path = "/home/software/dlgrab/tmp"# 把这个脚本放到update和cupcake相同层目录下sh_script = f"{target_path}/{old_tag}_{new_tag}_update.sh"f_shell = open(sh_script, "w")f_shell.write("#!/usr/bin/bash" + "\n")f_shell.write("set -ex" + "\n")f_shell.write("update_path=$1" + "\n")f_shell.write("target_path=$2" + "\n")update_tar = f"{target_path}/{old_tag}_{new_tag}.tar"update_path = f"{target_path}/{old_tag}_{new_tag}"new_tar = f"{target_path}/{old_rep}_{new_tag}.tar"old_tar = f"{target_path}/{new_rep}_{old_tag}.tar"new_path = f"{target_path}/{new_tag}"old_path = f"{target_path}/{old_tag}"create_dir(old_path)create_dir(new_path)create_dir(update_path)# # 从docker 中下载 tar 文件# new_docker = DockerCmd(new_rep, new_tag)# old_docker = DockerCmd(old_rep, old_tag)# old_save_cmd = old_docker.docker_save(old_tar)# new_save_cmd = new_docker.docker_save(new_tar)# run_command_parallel([old_save_cmd, new_save_cmd])# # 解压 tar 文件old_uncompress_cmd = TarCMD.uncompress(old_path, old_tar)new_uncompress_cmd = TarCMD.uncompress(new_path, new_tar)run_command(old_uncompress_cmd)run_command(new_uncompress_cmd)# 更新 image diff_list, rm_list = dir_diff(old_path, new_path)# 删除old中不需要的for rm_file in rm_list:# run_command(f"rm -rf {rm_file}")        f_shell.write(f"rm -rf $target_path/{os.path.basename(rm_file)}" + "\n")# 将更新的文件,打包成一个tar 文件for diff_file in diff_list:# run_command(f"rsync -azvP {diff_file} {old_path}")run_command(f"rsync -azvP {diff_file} {update_path}")f_shell.write(f"rsync -azvP $update_path/{os.path.basename(diff_file)} $target_path" + "\n")# 打包成一个 tar 文件new_compress_cmd = TarCMD.compress(new_path, new_tar)old_compress_cmd = TarCMD.compress(old_path, old_tar.replace('old', 'old_update'))update_compress_cmd = TarCMD.compress(update_path, update_tar)# run_command(new_uncompress_cmd)# run_command(old_compress_cmd)run_command(update_compress_cmd)f_shell.write("cd ${target_path}; tar -cvf ${target_path}_updated.tar *" + "\n")f_shell.write("# docker load -i ${target_path}_updated.tar" + "\n")f_shell.close()run_command(f"sh {sh_script} {update_path} {old_path}")

使用

tar -cvf ${old_id}.tar
tar -cvf ${old_id}_${new_id}.tar# 升级 docker
sh ${old_id}_${new_id}_update.sh ./${old_id}_${new_id} ./${old_id}
http://www.dtcms.com/a/511110.html

相关文章:

  • 基于卷积神经网络的香蕉成熟度识别系统,resnet50,vgg16,resnet34【pytorch框架,python代码】
  • 深度学习YOLO实战:6、通过视频案例,解析YOLO模型的能力边界与选型策略
  • C# 识别图片中是否有人
  • [Power BI] 漏斗图(Funnel Chart)
  • 做网站优化响应式网站 企业模版
  • 视觉学习篇——图像存储格式
  • GB28181视频服务wvp搭建(二)
  • Spring Boot安全配置全解析
  • EasyGBS如何通过流媒体技术提升安防监控效率?
  • 做展览的网站国家免费职业培训平台
  • 农业技术网站建设原则曲阜网站建设
  • 【python】基于 生活方式与健康数据预测数据集(Lifestyle and Health Risk Prediction)的可视化练习,附数据集源文件。
  • C#WPF如何实现登录页面跳转
  • 健康与生活方式数据库编程手册(Python方向教学2025年4月)
  • HarmonyOS测试与上架:单元测试、UI测试与App Gallery Connect发布实战
  • 以太网学习理解
  • 微算法科技(NASDAQ MLGO)标准化API驱动多联邦学习系统模型迁移技术
  • 【Redis】三种缓存问题(穿透、击穿、双删)的 Golang 实践
  • 第1部分-并发编程基础与线程模型
  • 【含文档+PPT+源码】基于SSM的智能驾校预约管理系统
  • python股票交易数据管理系统 金融数据 分析可视化 Django框架 爬虫技术 大数据技术 Hadoop spark(源码)✅
  • 有哪些网站可以自己做加视频做一个购物网站
  • 佛山建站公司模板开发一个物流app需要多少钱
  • Java——使用Aspose实现docx模板填充与导出word和pdf
  • Cef笔记:VS2019编译cef_109.0.5414
  • 云上极速转码:阿里云ECS+T4 GPU打造高性能FFmpeg视频处理引擎(部署指南)
  • Mysql 数据库迁移
  • 自建webrtc低延时分布式街机游戏直播方案
  • PHP四川文旅服务网站-计算机毕业设计源码76050
  • 从“开源开放”走向“高效智能”:阿里云 EMR 年度重磅发布