Last active
March 13, 2020 09:54
-
-
Save Na0ki/1563f7ede03e19a47cd2c8a2d8d544ff to your computer and use it in GitHub Desktop.
check for gitlab container updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import sys | |
import os | |
import json | |
import re | |
import urllib.request | |
def find(iterable, pred): | |
return next(filter(pred, iterable), None) | |
def find_all(iterable, pred): | |
return list(filter(pred, iterable)) | |
def exec_command(command, cwd = None): | |
proc = subprocess.run(command, shell=True, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
if proc.returncode != 0: | |
print(proc.stderr) | |
sys.exit(proc.returncode) | |
return proc.stdout.decode().rstrip() | |
def fetch_json(url): | |
with urllib.request.urlopen(url) as res: | |
return json.loads(res.read()) | |
def need_update(current, latest): | |
current_tuple = tuple(map(int, current.split("."))) | |
latest_tuple = tuple(map(int, latest.split("."))) | |
return latest_tuple > current_tuple | |
manifest = exec_command("docker-compose exec gitlab cat /opt/gitlab/version-manifest.txt", "/srv/gitlab") | |
current_version = re.match(r'^gitlab-ce\s+(?P<version_number>.+)', manifest).group("version_number") | |
latest_tags = fetch_json("https://hub.docker.com/v2/repositories/gitlab/gitlab-ce/tags?name=latest") | |
latest = find(latest_tags.get("results"), lambda x: x.get("name") == "latest") | |
latest_image = find(latest.get("images"), lambda x: x.get("architecture") == "amd64" and x.get("os") == "linux") | |
latest_digest = latest_image.get("digest") | |
tags = fetch_json("https://hub.docker.com/v2/repositories/gitlab/gitlab-ce/tags") | |
result = find_all(tags.get("results"), lambda x: find(x.get("images"), lambda x: x.get("digest") == latest_digest)) | |
target = find(result, lambda x: re.match(r'\d+\.\d+\.\d+-ce\.\d', x.get("name"))) | |
latest_version = re.match(r'(?P<version_number>\d+\.\d+\.\d+)', target.get("name")).group("version_number") | |
print(need_update(current_version, latest_version)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment