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.
91 lines
2.9 KiB
91 lines
2.9 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.config
|
|
import logging
|
|
|
|
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']
|
|
|
|
|
|
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):
|
|
# 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)
|
|
return False
|
|
except Exception as ex:
|
|
print(ex)
|
|
return False
|
|
|
|
|
|
def pull_images():
|
|
for ns in ns_list:
|
|
image_list = list_deployment(ns)
|
|
for image in image_list:
|
|
if not exist_images(image):
|
|
image_url = "repository.anxinyun.cn/{}/{}".format(image['project'], image['name'])
|
|
logger.info("下载镜像 {}:{} ... ...".format(image_url, image['tag']))
|
|
client.images.pull(image_url, image['tag'])
|
|
logger.info("镜像 {}:{} 下载完成。".format(image_url, image['tag']))
|
|
|
|
|
|
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')
|
|
logger.info("start sync images ....")
|
|
pull_images()
|
|
logger.info("images sync finished.")
|
|
|