Commit 6cee6076 by tingweiwang

add

0 parents
Showing with 139 additions and 0 deletions
hub.kce.ksyun.com/aivc-kpl/kpl-examples/miniconda-ubuntu18.04-gpu-cuda10.2:latest
# -*- coding:UTF-8 -*-
import os
import sys
docker_hub_prefix = sys.argv[1] # 请在执行脚本时传入私有仓地址参数例如:192.168.1.47:5000/kpl_k8s/
url = "./"
image_list = "imagelist.txt"
def dockerPull(imagelist):
"""
如果未下载公有仓镜像根据镜像列表中的内容进行pull获取公有仓镜像
"""
n = 0
pull_image = {}
image_name = []
save_dir = []
with open(imagelist, "r") as fi:
for line in fi:
line = line.strip()
if len(line) == 0: continue
os.system("docker pull {0}".format(line))
image_path = line.split("/") # 将镜像文件名称提取用于后面制作tar文件的目录和文件
image_value = image_path[-1].replace(":", "-")
pull_image[line] = image_value
image_name.append(image_value)
save_dir.append(image_value.split("-")[0])
print("pulled image files".center(50, "*"))
for k, v in pull_image.items():
n += 1
print("第{0}个镜像文件是{1}:pulled successfully".format(n, k))
return pull_image, image_name, save_dir
def dockerSave(pullimage, imagename, savedir):
"""
docker save -o *.tar.gz 将pull的镜像制作成.tar文件,
传入格式化后的imagename
"""
save_dir = set(savedir)
n = 0
saved_image = []
# 创建目录文件
for new_dir in set(save_dir):
if os.path.exists("./{0}".format(new_dir.strip())):
continue
else:
os.mkdir("./{0}".format(new_dir.strip())) # 创建保存pull镜像的目录
# 在生成的目录下保存镜像.tar文件
for s_dir in set(save_dir):
for k, v in pullimage.items():
if s_dir != v.split("-")[0]:
continue
else:
os.system("docker save -o ./{0}/{1}.tar {2}".format(s_dir, v, k))
saved_image.append(v)
print("saved images".center(50, "*"))
for m in saved_image:
n += 1
print("第{0}个镜像文件是{1}:saved successfuly".format(n, m))
def getFile(url):
"""
获取当前目录及其包含子目录中的所有文件
url使用“./”即可
"""
all_files = []
for path, dirs, files in os.walk(url):
for file in files:
file = "./" + file.split("-")[0] + "/" + file
all_files.append(file)
return all_files
def dockerLoad(imagetar):
"""
docker load -i *.tar 将tar文件解压成docker镜像文件
imagetar为目录中包含的所有文件列表
"""
n = 0
load_images = []
for file in imagetar:
if ".tar" in file:
image_path = os.path.abspath(file)
if os.system("docker load -i {0}".format(image_path)) !=0 :continue
load_images.append(file)
else:continue
assert (load_images != []), "当前目录及其所有子目录下不包含.tar或.tar.gz文件"
print("loaded imagefiles".center(50, "*"))
for image_file in load_images:
n += 1
print("第{0}个镜像文件是{1}:loaded successfuly".format(n, image_file))
def dockerPush(imageprefix, imagelist):
"""
将docker load 解压后的镜像push到私有仓地址
"""
n = 0
image_push = []
with open(imagelist, "r") as fi:
for line in fi:
line = line.strip()
if len(line) == 0: continue
image_path = line.split("/")[-1]
docker_name = imageprefix + image_path
if os.system("docker tag {0} {1}".format(line, docker_name)) != 0 : continue
if os.system("docker push {0}".format(docker_name)) != 0 :continue
image_push.append(docker_name)
print("pushed imagefiles".center(50, "*"))
for image in image_push:
n += 1
print("第{0}个镜像文件是{1}:pushed successfuly".format(n, image))
def pullRun():
"""
当未从公有云下载镜像时执行
"""
pull_image, image_name, save_dir = dockerPull(image_list)
dockerSave(pull_image, image_name, save_dir)
allfiles = getFile(url)
dockerLoad(allfiles)
dockerPush(docker_hub_prefix,image_list)
def pushRun():
"""
当已经将各个镜像文件单独保存到本地后执行
"""
allfiles = getFile(url)
print(allfiles)
dockerLoad(allfiles)
dockerPush(docker_hub_prefix,image_list)
pullRun()
# pushRun()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!