kubreretes && docker 模块

kubreretes

https://www.cnblogs.com/zhangb8042/p/11444756.html

docker

https://docker-py.readthedocs.io/en/stable/images.html#docker.models.images.Image

#!/usr/bin/env python
import docker
import os
import argparse
import logging

parser = argparse.ArgumentParser(description="遍历目录下所有.tar.gz文件Load镜像")
parser.add_argument("--path", type=str, default="./", required=True, help="指定镜像文件存储路径")
parser.add_argument("--label", type=str, nargs="*", default=[], action="store", help="指定需要删除的标签")
parser.add_argument("--registry", type=str, default="lanceyuan", required=True, help="指定仓库地址")
args = parser.parse_args()

PATH = args.path
REGISTRY = args.registry
DEL_FLAG = args.label
DOCKER_SOCK = "unix://var/run/docker.sock"
VERSION = "1.38"
LOGFILE = ""

def logs():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    fh = logging.FileHandler(LOGFILE, mode="a")
    ch = logging.StreamHandler()
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    fh.setFormatter(fmt=formatter)
    ch.setFormatter(fmt=formatter)
    logger.addHandler(fh)
    logger.addHandler(ch)
    return logger

client = docker.DockerClient(base_url=DOCKER_SOCK, version=VERSION)
files_list = os.listdir(PATH or "/home")
for item in files_list:
    if item.endswith(".tar.gz"):
        with open("{}".format(os.path.join(PATH, item.strip())), "rb") as f:
            try:
                image = client.images.load(f.read())
            except Exception as e:
                print(e)
        if image[0].tags:
            old_image_name = image[0].tags[0]
            _, image_name = old_image_name.split("/")
            for flag in DEL_FLAG:
                image_name = image_name.replace(flag.lower(), "")
            new_image_name = "{}/{}".format(REGISTRY, image_name)
            image[0].tag(new_image_name)
            print("\033[1;32;40m{}\033[0m".format(new_image_name.ljust(30)), end="    ")
            print(image[0].id)
            try:
                client.images.remove(old_image_name)
                client.images.push(new_image_name)
            except Exception as e:
                print(e)
        else:
            old_image_name = image[0].short_id
            new_image_name = item.replace(".tar.gz", "")
            image[0].tag("{}/{}".fromat(REGISTRY, new_image_name))
            try:
                client.images.push("{}/{}".fromat(REGISTRY, new_image_name))
                print("\033[1;32;40m{}\033[0m".format(new_image_name.ljust(30)), end="    ")
                print(image[0].id)
            except Exception as e:
                print(e)
                print("\033[1;31;40m{}\033[0m".format(old_image_name.ljust(30)), end="    ")
                print(image[0].id)
docker push image
venv/bin/python3.5 loadImages.py --path /opt/Congo_hotfix14/images/ --label test-nb --registry 172.16.99.59:5000
readme

 

posted @ 2021-01-04 12:26  ༺༽秋水.无痕༼༻  阅读(55)  评论(0编辑  收藏  举报
……