码迷,mamicode.com
首页 > 其他好文 > 详细

kubreretes && docker 模块

时间:2021-01-07 11:43:02      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:exce   --   from   end   pat   mha   style   doc   spl   

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

 

kubreretes && docker 模块

标签:exce   --   from   end   pat   mha   style   doc   spl   

原文地址:https://www.cnblogs.com/hanwei999/p/14229090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!