From aef3985762249d35436c890e49e11de98852ad01 Mon Sep 17 00:00:00 2001 From: winloong <21272660+vinloong@users.noreply.github.com> Date: Tue, 26 Apr 2022 15:35:34 +0800 Subject: [PATCH] add . --- syncimages/app/Dockerfile | 18 ++++++++------ syncimages/app/entrypoint.sh | 4 +++ syncimages/app/syncimages.py | 48 +++++++++++++++++++++++++----------- 3 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 syncimages/app/entrypoint.sh diff --git a/syncimages/app/Dockerfile b/syncimages/app/Dockerfile index a462755..23bdab3 100644 --- a/syncimages/app/Dockerfile +++ b/syncimages/app/Dockerfile @@ -1,22 +1,26 @@ FROM docker:20-dind-rootless USER root + RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \ - && apk add --no-cache python3 py3-pip shadow \ + && apk add --no-cache python3 py3-pip shadow su-exec \ && ln -sf python3 /usr/bin/python \ && python3 -m ensurepip \ && pip3 install --no-cache --upgrade pip setuptools -i https://pypi.tuna.tsinghua.edu.cn/simple \ && pip install --no-cache kubernetes requests docker -i https://pypi.tuna.tsinghua.edu.cn/simple \ - && rm -rf /var/cache/apk/* \ && usermod -aG ping rootless \ - && echo "#!/bin/sh \n\ - python syncimages.py " >> /etc/periodic/daily/sync-images \ - && chmod +x /etc/periodic/daily/sync-images + && echo $'#!/bin/sh \n\ +python /app/syncimages.py' >> /etc/periodic/daily/sync-images \ + && chmod +x /etc/periodic/daily/sync-images \ + && apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata shadow \ + && rm -rf /var/cache/apk/* WORKDIR /app COPY . . -RUN chown -R rootless:rootless /app +RUN chown -R rootless /app -USER rootless +ENTRYPOINT ["./entrypoint.sh"] diff --git a/syncimages/app/entrypoint.sh b/syncimages/app/entrypoint.sh new file mode 100644 index 0000000..8f08790 --- /dev/null +++ b/syncimages/app/entrypoint.sh @@ -0,0 +1,4 @@ +#!/bin/sh +crond; +su rootless -c 'dockerd-entrypoint.sh' + diff --git a/syncimages/app/syncimages.py b/syncimages/app/syncimages.py index 1094da3..0187412 100644 --- a/syncimages/app/syncimages.py +++ b/syncimages/app/syncimages.py @@ -6,16 +6,37 @@ from docker import errors from kubernetes import client, config import requests import time -import logging.config import logging +from logging import handlers +from datetime import datetime config.load_kube_config("/app/config") apps_v1 = client.AppsV1Api() client = None -logging.config.fileConfig("/app/logging.ini") -logger = logging.getLogger('sync-images') + ns_list = ['anxincloud', 'environment', 'smart-city', 'smart-xxx', 'free-sun', 'ops'] +logger = logging.getLogger('sync-images') +logger.setLevel(logging.DEBUG) +log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + +file_handler = logging.FileHandler('/app/log.log') +file_handler.setLevel(logging.INFO) +file_handler.setFormatter(log_formatter) + +stream_handler = logging.StreamHandler() +stream_handler.setLevel(logging.INFO) +stream_handler.setFormatter(log_formatter) + +logger.addHandler(file_handler) +logger.addHandler(stream_handler) + + +# time_rotating_file_handler = handlers.TimedRotatingFileHandler(filename='/app/log.log', when='w') +# time_rotating_file_handler.setLevel(logging.INFO) +# time_rotating_file_handler.setFormatter(log_formatter) +# logger.addHandler(time_rotating_file_handler) + def list_deployment(ns): image_list = [] @@ -50,17 +71,16 @@ def get_image_latest_tag(project, image): def exist_images(name): - # image = client.images.get('hello-world') try: client.images.get(name) return True except errors.ImageNotFound: return False except errors.APIError as err: - print(err) + logger.error(err) return False except Exception as ex: - print(ex) + logger.error(ex) return False @@ -75,17 +95,15 @@ def pull_images(): logger.info("镜像 {}:{} 下载完成。".format(image_url, image['tag'])) +def write_out_2_file(line): + with open("/app/log2.log", "a") as file: + file.writelines(line) + + if __name__ == '__main__': - count = 0 - while count < 10: - print("wait ... ...") - time.sleep(5) - count += 1 - while not os.path.exists('/var/run/docker.sock'): - print("wait ... ...") - time.sleep(5) - print("init complete.") client = docker.DockerClient(base_url='unix://var/run/docker.sock') + write_out_2_file("{} - start sync images ... ...\n".format(datetime.now())) logger.info("start sync images ....") pull_images() + write_out_2_file("{} - images sync finished.\n".format(datetime.now())) logger.info("images sync finished.")