You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.6 KiB
110 lines
3.6 KiB
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
import os
|
|
import docker
|
|
from docker import errors
|
|
from kubernetes import client, config
|
|
import requests
|
|
import time
|
|
import logging
|
|
from logging import handlers
|
|
from datetime import datetime
|
|
|
|
config.load_kube_config("/app/config")
|
|
apps_v1 = client.AppsV1Api()
|
|
client = None
|
|
|
|
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 = []
|
|
deploy_list = apps_v1.list_namespaced_deployment(namespace=ns).items
|
|
for item in deploy_list:
|
|
if item.spec.template.spec.containers[0].image.startswith('repository.anxinyun.cn'):
|
|
project_image = get_project(item.spec.template.spec.containers[0].image)
|
|
image_tag = get_image_tag(project_image[2])
|
|
image_list.append({'project': project_image[1], 'name': image_tag[0], 'tag': image_tag[1]})
|
|
return image_list
|
|
|
|
|
|
def get_project(image_url):
|
|
return image_url.split("/")
|
|
|
|
|
|
def get_image_tag(image):
|
|
return image.split(":")
|
|
|
|
|
|
def get_image_latest_tag(project, image):
|
|
url = "https://repository.anxinyun.cn/api/repositories/{}/{}/tags?detail=1".format(project, image)
|
|
r = requests.get(url)
|
|
if r.status_code == 200:
|
|
tags = list(map(lambda t: {'created': t['created'], 'tag': t['name']}, r.json()))
|
|
tags.sort(key=lambda x: x['created'], reverse=True)
|
|
return tags[0]['tag'] if len(tags) == 1 else tags[0]['tag'] if tags[0]['tag'] != 'latest' else tags[1]['tag']
|
|
# if len(tags) >= 2:
|
|
# return tags[0]['tag'] if tags[0]['tag'] != 'latest' else tags[1]['tag']
|
|
# else:
|
|
# return tags[0]['tag']
|
|
|
|
|
|
def exist_images(name):
|
|
try:
|
|
client.images.get(name)
|
|
return True
|
|
except errors.ImageNotFound:
|
|
return False
|
|
except errors.APIError as err:
|
|
logger.error(err)
|
|
return False
|
|
except Exception as ex:
|
|
logger.error(ex)
|
|
return False
|
|
|
|
|
|
def pull_images():
|
|
for ns in ns_list:
|
|
image_list = list_deployment(ns)
|
|
for image in image_list:
|
|
image_url = "repository.anxinyun.cn/{}/{}".format(image['project'], image['name'])
|
|
image_full_name = '{}:{}'.format(image_url,image['tag'])
|
|
if not exist_images(image_full_name):
|
|
logger.info("下载镜像 {} ... ...".format(image_full_name))
|
|
client.images.pull(image_url, image['tag'])
|
|
logger.info("镜像 {} 下载完成。".format(image_full_name))
|
|
|
|
|
|
def write_out_2_file(line):
|
|
with open("/app/log2.log", "a") as file:
|
|
file.writelines(line)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
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.")
|
|
|