2020-04-24 23:36:52 +00:00
|
|
|
#!/usr/bin/env nix-shell
|
2023-07-15 17:15:38 +00:00
|
|
|
#! nix-shell -I nixpkgs=../../../.. -i python3 -p bundix bundler nix-update nix nix-universal-prefetch python3 python3Packages.requests python3Packages.click python3Packages.click-log python3Packages.packaging prefetch-yarn-deps git
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
import click
|
|
|
|
import click_log
|
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
import pathlib
|
2021-10-17 09:34:42 +00:00
|
|
|
import tempfile
|
2022-08-12 12:06:08 +00:00
|
|
|
from packaging.version import Version
|
2020-04-24 23:36:52 +00:00
|
|
|
from typing import Iterable
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
NIXPKGS_PATH = pathlib.Path(__file__).parent / "../../../../"
|
|
|
|
GITLAB_DIR = pathlib.Path(__file__).parent
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2023-07-15 17:15:38 +00:00
|
|
|
click_log.basic_config(logger)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GitLabRepo:
|
2023-07-15 17:15:38 +00:00
|
|
|
version_regex = re.compile(r"^v\d+\.\d+\.\d+(\-rc\d+)?(\-ee)?(\-gitlab)?")
|
|
|
|
|
|
|
|
def __init__(self, owner: str = "gitlab-org", repo: str = "gitlab"):
|
2020-04-24 23:36:52 +00:00
|
|
|
self.owner = owner
|
|
|
|
self.repo = repo
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return f"https://gitlab.com/{self.owner}/{self.repo}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tags(self) -> Iterable[str]:
|
2023-07-15 17:15:38 +00:00
|
|
|
"""Returns a sorted list of repository tags"""
|
2020-05-03 17:38:23 +00:00
|
|
|
r = requests.get(self.url + "/refs?sort=updated_desc&ref=master").json()
|
|
|
|
tags = r.get("Tags", [])
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# filter out versions not matching version_regex
|
2020-05-03 17:38:23 +00:00
|
|
|
versions = list(filter(self.version_regex.match, tags))
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
# sort, but ignore v, -ee and -gitlab for sorting comparisons
|
|
|
|
versions.sort(
|
|
|
|
key=lambda x: Version(
|
|
|
|
x.replace("v", "").replace("-ee", "").replace("-gitlab", "")
|
|
|
|
),
|
|
|
|
reverse=True,
|
|
|
|
)
|
2020-04-24 23:36:52 +00:00
|
|
|
return versions
|
|
|
|
|
|
|
|
def get_git_hash(self, rev: str):
|
2023-07-15 17:15:38 +00:00
|
|
|
return (
|
|
|
|
subprocess.check_output(
|
|
|
|
[
|
|
|
|
"nix-universal-prefetch",
|
|
|
|
"fetchFromGitLab",
|
|
|
|
"--owner",
|
|
|
|
self.owner,
|
|
|
|
"--repo",
|
|
|
|
self.repo,
|
|
|
|
"--rev",
|
|
|
|
rev,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.decode("utf-8")
|
|
|
|
.strip()
|
|
|
|
)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-10-17 09:34:42 +00:00
|
|
|
def get_yarn_hash(self, rev: str):
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
2023-07-15 17:15:38 +00:00
|
|
|
with open(tmp_dir + "/yarn.lock", "w") as f:
|
|
|
|
f.write(self.get_file("yarn.lock", rev))
|
|
|
|
return (
|
|
|
|
subprocess.check_output(["prefetch-yarn-deps", tmp_dir + "/yarn.lock"])
|
|
|
|
.decode("utf-8")
|
|
|
|
.strip()
|
|
|
|
)
|
2021-10-17 09:34:42 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
@staticmethod
|
|
|
|
def rev2version(tag: str) -> str:
|
|
|
|
"""
|
|
|
|
normalize a tag to a version number.
|
|
|
|
This obviously isn't very smart if we don't pass something that looks like a tag
|
|
|
|
:param tag: the tag to normalize
|
|
|
|
:return: a normalized version number
|
|
|
|
"""
|
|
|
|
# strip v prefix
|
2023-07-15 17:15:38 +00:00
|
|
|
version = re.sub(r"^v", "", tag)
|
|
|
|
# strip -ee and -gitlab suffixes
|
|
|
|
return re.sub(r"-(ee|gitlab)$", "", version)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
def get_file(self, filepath, rev):
|
|
|
|
"""
|
|
|
|
returns file contents at a given rev
|
|
|
|
:param filepath: the path to the file, relative to the repo root
|
|
|
|
:param rev: the rev to fetch at
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return requests.get(self.url + f"/raw/{rev}/{filepath}").text
|
|
|
|
|
|
|
|
def get_data(self, rev):
|
|
|
|
version = self.rev2version(rev)
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
passthru = {
|
|
|
|
v: self.get_file(v, rev).strip()
|
|
|
|
for v in [
|
|
|
|
"GITALY_SERVER_VERSION",
|
|
|
|
"GITLAB_PAGES_VERSION",
|
|
|
|
"GITLAB_SHELL_VERSION",
|
|
|
|
"GITLAB_ELASTICSEARCH_INDEXER_VERSION",
|
|
|
|
]
|
|
|
|
}
|
2021-04-08 16:26:57 +00:00
|
|
|
passthru["GITLAB_WORKHORSE_VERSION"] = version
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
return dict(
|
|
|
|
version=self.rev2version(rev),
|
|
|
|
repo_hash=self.get_git_hash(rev),
|
|
|
|
yarn_hash=self.get_yarn_hash(rev),
|
|
|
|
owner=self.owner,
|
|
|
|
repo=self.repo,
|
|
|
|
rev=rev,
|
|
|
|
passthru=passthru,
|
|
|
|
)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _get_data_json():
|
2023-07-15 17:15:38 +00:00
|
|
|
data_file_path = pathlib.Path(__file__).parent / "data.json"
|
|
|
|
with open(data_file_path, "r") as f:
|
2020-04-24 23:36:52 +00:00
|
|
|
return json.load(f)
|
|
|
|
|
|
|
|
|
2020-12-27 14:24:52 +00:00
|
|
|
def _call_nix_update(pkg, version):
|
|
|
|
"""calls nix-update from nixpkgs root dir"""
|
2023-07-15 17:15:38 +00:00
|
|
|
return subprocess.check_output(
|
|
|
|
["nix-update", pkg, "--version", version], cwd=NIXPKGS_PATH
|
|
|
|
)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click_log.simple_verbosity_option(logger)
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
@cli.command("update-data")
|
|
|
|
@click.option("--rev", default="latest", help="The rev to use (vX.Y.Z-ee), or 'latest'")
|
2020-04-24 23:36:52 +00:00
|
|
|
def update_data(rev: str):
|
2023-07-15 17:15:38 +00:00
|
|
|
"""Update data.json"""
|
|
|
|
logger.info("Updating data.json")
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
repo = GitLabRepo()
|
|
|
|
if rev == "latest":
|
|
|
|
# filter out pre and rc releases
|
|
|
|
rev = next(filter(lambda x: not ("rc" in x or x.endswith("pre")), repo.tags))
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
data_file_path = pathlib.Path(__file__).parent / "data.json"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
data = repo.get_data(rev)
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
with open(data_file_path.as_posix(), "w") as f:
|
2020-04-24 23:36:52 +00:00
|
|
|
json.dump(data, f, indent=2)
|
2020-11-24 20:58:05 +00:00
|
|
|
f.write("\n")
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
@cli.command("update-rubyenv")
|
2020-04-24 23:36:52 +00:00
|
|
|
def update_rubyenv():
|
|
|
|
"""Update rubyEnv"""
|
2023-07-15 17:15:38 +00:00
|
|
|
logger.info("Updating gitlab")
|
2020-04-24 23:36:52 +00:00
|
|
|
repo = GitLabRepo()
|
2023-07-15 17:15:38 +00:00
|
|
|
rubyenv_dir = pathlib.Path(__file__).parent / "rubyEnv"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# load rev from data.json
|
|
|
|
data = _get_data_json()
|
2023-07-15 17:15:38 +00:00
|
|
|
rev = data["rev"]
|
|
|
|
version = data["version"]
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
for fn in ["Gemfile.lock", "Gemfile"]:
|
|
|
|
with open(rubyenv_dir / fn, "w") as f:
|
2023-01-11 07:51:40 +00:00
|
|
|
f.write(repo.get_file(fn, rev))
|
2022-08-12 12:06:08 +00:00
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
# patch for openssl 3.x support
|
2023-07-15 17:15:38 +00:00
|
|
|
subprocess.check_output(
|
|
|
|
["sed", "-i", "s:'openssl', '2.*':'openssl', '3.0.2':g", "Gemfile"],
|
|
|
|
cwd=rubyenv_dir,
|
|
|
|
)
|
2023-05-24 13:37:59 +00:00
|
|
|
|
2023-01-11 07:51:40 +00:00
|
|
|
# Fetch vendored dependencies temporarily in order to build the gemset.nix
|
2023-10-09 19:29:22 +00:00
|
|
|
subprocess.check_output(["mkdir", "-p", "vendor/gems", "gems"], cwd=rubyenv_dir)
|
2023-07-15 17:15:38 +00:00
|
|
|
subprocess.check_output(
|
|
|
|
[
|
|
|
|
"sh",
|
|
|
|
"-c",
|
|
|
|
f"curl -L https://gitlab.com/gitlab-org/gitlab/-/archive/v{version}-ee/gitlab-v{version}-ee.tar.bz2?path=vendor/gems | tar -xj --strip-components=3",
|
|
|
|
],
|
|
|
|
cwd=f"{rubyenv_dir}/vendor/gems",
|
|
|
|
)
|
2023-10-09 19:29:22 +00:00
|
|
|
subprocess.check_output(
|
|
|
|
[
|
|
|
|
"sh",
|
|
|
|
"-c",
|
|
|
|
f"curl -L https://gitlab.com/gitlab-org/gitlab/-/archive/v{version}-ee/gitlab-v{version}-ee.tar.bz2?path=gems | tar -xj --strip-components=3",
|
|
|
|
],
|
|
|
|
cwd=f"{rubyenv_dir}/gems",
|
|
|
|
)
|
2022-08-12 12:06:08 +00:00
|
|
|
|
2023-01-11 07:51:40 +00:00
|
|
|
# Undo our gemset.nix patches so that bundix runs through
|
2023-07-15 17:15:38 +00:00
|
|
|
subprocess.check_output(
|
|
|
|
["sed", "-i", "-e", "1d", "-e", "s:\\${src}/::g", "gemset.nix"], cwd=rubyenv_dir
|
|
|
|
)
|
|
|
|
|
|
|
|
subprocess.check_output(["bundle", "lock"], cwd=rubyenv_dir)
|
|
|
|
subprocess.check_output(["bundix"], cwd=rubyenv_dir)
|
|
|
|
|
|
|
|
subprocess.check_output(
|
|
|
|
[
|
|
|
|
"sed",
|
|
|
|
"-i",
|
|
|
|
"-e",
|
|
|
|
"1i\\src:",
|
|
|
|
"-e",
|
|
|
|
's:path = \\(vendor/[^;]*\\);:path = "${src}/\\1";:g',
|
2023-10-09 19:29:22 +00:00
|
|
|
"-e",
|
|
|
|
's:path = \\(gems/[^;]*\\);:path = "${src}/\\1";:g',
|
2023-07-15 17:15:38 +00:00
|
|
|
"gemset.nix",
|
|
|
|
],
|
|
|
|
cwd=rubyenv_dir,
|
|
|
|
)
|
2023-10-09 19:29:22 +00:00
|
|
|
subprocess.check_output(["rm", "-rf", "vendor", "gems"], cwd=rubyenv_dir)
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("update-gitaly")
|
2020-04-24 23:36:52 +00:00
|
|
|
def update_gitaly():
|
|
|
|
"""Update gitaly"""
|
2023-07-15 17:15:38 +00:00
|
|
|
logger.info("Updating gitaly")
|
2020-04-24 23:36:52 +00:00
|
|
|
data = _get_data_json()
|
|
|
|
gitaly_server_version = data['passthru']['GITALY_SERVER_VERSION']
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
_call_nix_update("gitaly", gitaly_server_version)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
@cli.command("update-gitlab-pages")
|
2023-03-15 16:39:30 +00:00
|
|
|
def update_gitlab_pages():
|
2023-07-15 17:15:38 +00:00
|
|
|
"""Update gitlab-pages"""
|
|
|
|
logger.info("Updating gitlab-pages")
|
2023-03-15 16:39:30 +00:00
|
|
|
data = _get_data_json()
|
2023-07-15 17:15:38 +00:00
|
|
|
gitlab_pages_version = data["passthru"]["GITLAB_PAGES_VERSION"]
|
|
|
|
_call_nix_update("gitlab-pages", gitlab_pages_version)
|
|
|
|
|
|
|
|
|
|
|
|
def get_container_registry_version() -> str:
|
|
|
|
"""Returns the version attribute of gitlab-container-registry"""
|
|
|
|
return subprocess.check_output(
|
|
|
|
[
|
|
|
|
"nix",
|
|
|
|
"--experimental-features",
|
|
|
|
"nix-command",
|
|
|
|
"eval",
|
|
|
|
"-f",
|
|
|
|
".",
|
|
|
|
"--raw",
|
|
|
|
"gitlab-container-registry.version",
|
|
|
|
],
|
|
|
|
cwd=NIXPKGS_PATH,
|
|
|
|
).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command("update-gitlab-shell")
|
2020-04-24 23:36:52 +00:00
|
|
|
def update_gitlab_shell():
|
|
|
|
"""Update gitlab-shell"""
|
2023-07-15 17:15:38 +00:00
|
|
|
logger.info("Updating gitlab-shell")
|
2020-04-24 23:36:52 +00:00
|
|
|
data = _get_data_json()
|
2023-07-15 17:15:38 +00:00
|
|
|
gitlab_shell_version = data["passthru"]["GITLAB_SHELL_VERSION"]
|
|
|
|
_call_nix_update("gitlab-shell", gitlab_shell_version)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
@cli.command("update-gitlab-workhorse")
|
2020-04-24 23:36:52 +00:00
|
|
|
def update_gitlab_workhorse():
|
|
|
|
"""Update gitlab-workhorse"""
|
2023-07-15 17:15:38 +00:00
|
|
|
logger.info("Updating gitlab-workhorse")
|
2020-04-24 23:36:52 +00:00
|
|
|
data = _get_data_json()
|
2023-07-15 17:15:38 +00:00
|
|
|
gitlab_workhorse_version = data["passthru"]["GITLAB_WORKHORSE_VERSION"]
|
|
|
|
_call_nix_update("gitlab-workhorse", gitlab_workhorse_version)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command("update-gitlab-container-registry")
|
|
|
|
@click.option("--rev", default="latest", help="The rev to use (vX.Y.Z-ee), or 'latest'")
|
|
|
|
@click.option(
|
|
|
|
"--commit", is_flag=True, default=False, help="Commit the changes for you"
|
|
|
|
)
|
|
|
|
def update_gitlab_container_registry(rev: str, commit: bool):
|
|
|
|
"""Update gitlab-container-registry"""
|
|
|
|
logger.info("Updading gitlab-container-registry")
|
|
|
|
repo = GitLabRepo(repo="container-registry")
|
|
|
|
old_container_registry_version = get_container_registry_version()
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
if rev == "latest":
|
|
|
|
rev = next(filter(lambda x: not ("rc" in x or x.endswith("pre")), repo.tags))
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
version = repo.rev2version(rev)
|
|
|
|
_call_nix_update("gitlab-container-registry", version)
|
|
|
|
if commit:
|
|
|
|
new_container_registry_version = get_container_registry_version()
|
|
|
|
commit_container_registry(
|
|
|
|
old_container_registry_version, new_container_registry_version
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command('update-gitlab-elasticsearch-indexer')
|
|
|
|
def update_gitlab_elasticsearch_indexer():
|
|
|
|
"""Update gitlab-elasticsearch-indexer"""
|
|
|
|
data = _get_data_json()
|
|
|
|
gitlab_elasticsearch_indexer_version = data['passthru']['GITLAB_ELASTICSEARCH_INDEXER_VERSION']
|
|
|
|
_call_nix_update('gitlab-elasticsearch-indexer', gitlab_elasticsearch_indexer_version)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command("update-all")
|
|
|
|
@click.option("--rev", default="latest", help="The rev to use (vX.Y.Z-ee), or 'latest'")
|
|
|
|
@click.option(
|
|
|
|
"--commit", is_flag=True, default=False, help="Commit the changes for you"
|
|
|
|
)
|
2020-04-24 23:36:52 +00:00
|
|
|
@click.pass_context
|
2023-07-15 17:15:38 +00:00
|
|
|
def update_all(ctx, rev: str, commit: bool):
|
2020-04-24 23:36:52 +00:00
|
|
|
"""Update all gitlab components to the latest stable release"""
|
2023-07-15 17:15:38 +00:00
|
|
|
old_data_json = _get_data_json()
|
|
|
|
old_container_registry_version = get_container_registry_version()
|
|
|
|
|
2020-05-03 17:38:23 +00:00
|
|
|
ctx.invoke(update_data, rev=rev)
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
new_data_json = _get_data_json()
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
ctx.invoke(update_rubyenv)
|
|
|
|
ctx.invoke(update_gitaly)
|
2023-03-15 16:39:30 +00:00
|
|
|
ctx.invoke(update_gitlab_pages)
|
2020-04-24 23:36:52 +00:00
|
|
|
ctx.invoke(update_gitlab_shell)
|
|
|
|
ctx.invoke(update_gitlab_workhorse)
|
2023-07-15 17:15:38 +00:00
|
|
|
ctx.invoke(update_gitlab_elasticsearch_indexer)
|
|
|
|
if commit:
|
|
|
|
commit_gitlab(
|
|
|
|
old_data_json["version"], new_data_json["version"], new_data_json["rev"]
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx.invoke(update_gitlab_container_registry)
|
|
|
|
if commit:
|
|
|
|
new_container_registry_version = get_container_registry_version()
|
|
|
|
commit_container_registry(
|
|
|
|
old_container_registry_version, new_container_registry_version
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def commit_gitlab(old_version: str, new_version: str, new_rev: str) -> None:
|
|
|
|
"""Commits the gitlab changes for you"""
|
|
|
|
subprocess.run(
|
|
|
|
[
|
|
|
|
"git",
|
|
|
|
"add",
|
|
|
|
"data.json",
|
|
|
|
"rubyEnv",
|
|
|
|
"gitaly",
|
|
|
|
"gitlab-pages",
|
|
|
|
"gitlab-shell",
|
|
|
|
"gitlab-workhorse",
|
|
|
|
"gitlab-elasticsearch-indexer",
|
|
|
|
],
|
|
|
|
cwd=GITLAB_DIR,
|
|
|
|
)
|
|
|
|
subprocess.run(
|
|
|
|
[
|
|
|
|
"git",
|
|
|
|
"commit",
|
|
|
|
"--message",
|
|
|
|
f"""gitlab: {old_version} -> {new_version}\n\nhttps://gitlab.com/gitlab-org/gitlab/-/blob/{new_rev}/CHANGELOG.md""",
|
|
|
|
],
|
|
|
|
cwd=GITLAB_DIR,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def commit_container_registry(old_version: str, new_version: str) -> None:
|
|
|
|
"""Commits the gitlab-container-registry changes for you"""
|
|
|
|
subprocess.run(["git", "add", "gitlab-container-registry"], cwd=GITLAB_DIR)
|
|
|
|
subprocess.run(
|
|
|
|
[
|
|
|
|
"git",
|
|
|
|
"commit",
|
|
|
|
"--message",
|
|
|
|
f"gitlab-container-registry: {old_version} -> {new_version}\n\nhttps://gitlab.com/gitlab-org/container-registry/-/blob/v{new_version}-gitlab/CHANGELOG.md",
|
|
|
|
],
|
|
|
|
cwd=GITLAB_DIR,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-04-24 23:36:52 +00:00
|
|
|
cli()
|