概要
Gitlab 側で最新の Prometheus が使われているか定期的にチェックします
環境
-
Gitlab.com 17.4.0-pre* Runner (docker-mahcine executor ruby:3.1)
app.py
-
vim app.py
import re
import sys
import requests
from packaging.version import Version
image = sys.argv[1]
url = f"https://hub.docker.com/v2/repositories/prom/{image}/tags/"
response = requests.get(url)
data = response.json()
# セマンティックバージョンに一致するタグを正規表現でフィルタ
semver_pattern = re.compile(r"^v?(\d+\.\d+\.\d+)$")
# バージョンをリストに格納
versions = []
for tag in data["results"]:
tag_name = tag["name"]
if semver_pattern.match(tag_name): # セマンティックバージョニングに一致
versions.append(tag_name.lstrip("v"))
# バージョンを比較して最新を取得
latest_version = max(versions, key=lambda x: Version(x))
print(latest_version)
.gitlab-ci.yml
Gitlab のバージョンはソースコードから取得し dockerhub にあるタグのバージョンは API を使っています
取得した結果は artifacts を使ってファイルに保存して最後に比較しています
image: python:3.11.9-bullseye
stages:
- fetch_versions
- compare_versions
fetch_dockerhub:
stage: fetch_versions
before_script:
- mkdir -p $CI_PROJECT_DIR/build/artifacts
- pip install requests packaging
script:
- python app.py prometheus > $CI_PROJECT_DIR/build/artifacts/dockerhub_latest_prometheus.txt
- python app.py alertmanager > $CI_PROJECT_DIR/build/artifacts/dockerhub_latest_alertmanager.txt
- python app.py node-exporter > $CI_PROJECT_DIR/build/artifacts/dockerhub_latest_node_exporter.txt
artifacts:
paths:
- $CI_PROJECT_DIR/build/artifacts/*.txt
fetch_gitlab:
stage: fetch_versions
before_script:
- mkdir -p $CI_PROJECT_DIR/build/artifacts
script:
- git clone https://gitlab.com/gitlab-org/omnibus-gitlab.git
- cd omnibus-gitlab
- git checkout $TARGET_TAG
- grep 'Gitlab::Version.new' config/software/prometheus.rb | sed -n "s/.*'\(.*\)'.*/\1/p" > $CI_PROJECT_DIR/build/artifacts/gitlab_tagged_prometheus.txt
- grep 'Gitlab::Version.new' config/software/alertmanager.rb | sed -n "s/.*'\(.*\)'.*/\1/p" > $CI_PROJECT_DIR/build/artifacts/gitlab_tagged_alertmanager.txt
- grep 'Gitlab::Version.new' config/software/node-exporter.rb | sed -n "s/.*'\(.*\)'.*/\1/p" > $CI_PROJECT_DIR/build/artifacts/gitlab_tagged_node_exporter.txt
artifacts:
paths:
- $CI_PROJECT_DIR/build/artifacts/*.txt
rules:
- if: '$TARGET_TAG != null'
compare:
stage: compare_versions
script:
- |
# バージョンの比較
DOCKERHUB_PROMETHEUS_VERSION=$(cat $CI_PROJECT_DIR/build/artifacts/dockerhub_latest_prometheus.txt)
DOCKERHUB_ALERTMANAGER_VERSION=$(cat $CI_PROJECT_DIR/build/artifacts/dockerhub_latest_alertmanager.txt)
DOCKERHUB_NODE_EXPORTER_VERSION=$(cat $CI_PROJECT_DIR/build/artifacts/dockerhub_latest_node_exporter.txt)
GITLAB_TAGGED_PROMETHEUS_VERSION=$(cat $CI_PROJECT_DIR/build/artifacts/gitlab_tagged_prometheus.txt)
GITLAB_TAGGED_ALERTMANAGER_VERSION=$(cat $CI_PROJECT_DIR/build/artifacts/gitlab_tagged_alertmanager.txt)
GITLAB_TAGGED_NODE_EXPORTER_VERSION=$(cat $CI_PROJECT_DIR/build/artifacts/gitlab_tagged_node_exporter.txt)
version_gt() {
[ "$(printf '%s\n' "$1" "$2" | sort -V | head -n1)" != "$1" ]
}
if version_gt "$DOCKERHUB_PROMETHEUS_VERSION" "$GITLAB_TAGGED_PROMETHEUS_VERSION"; then
echo "Version has increased from $GITLAB_TAGGED_PROMETHEUS_VERSION to $DOCKERHUB_PROMETHEUS_VERSION"
else
echo "Prometheus Version is unchanged."
fi
if version_gt "$DOCKERHUB_ALERTMANAGER_VERSION" "$GITLAB_TAGGED_ALERTMANAGER_VERSION"; then
echo "Version has increased from $GITLAB_TAGGED_ALERTMANAGER_VERSION to $DOCKERHUB_ALERTMANAGER_VERSION"
else
echo "Alertmanager Version is unchanged."
fi
if version_gt "$DOCKERHUB_NODE_EXPORTER_VERSION" "$GITLAB_TAGGED_NODE_EXPORTER_VERSION"; then
echo "Version has increased from $GITLAB_TAGGED_NODE_EXPORTER_VERSION to $DOCKERHUB_NODE_EXPORTER_VERSION"
else
echo "Node_Exporter Version is unchanged."
fi
artifacts:
paths:
- $CI_PROJECT_DIR/build/artifacts/*.txt
最後に
Omnibus Gitlab の Prometheus のバージョンはボットが管理しているっぽいのでその仕組を使うのもありなのかもしれない
0 件のコメント:
コメントを投稿