2018-11-23 15:01:55 +01:00
|
|
|
#!/usr/bin/env nix-shell
|
2020-12-26 22:35:33 +01:00
|
|
|
#! nix-shell -i python3 -p bundix bundler nix-update nix nix-universal-prefetch python3 python3Packages.requests python3Packages.click python3Packages.click-log yarn2nix
|
2018-11-23 15:01:55 +01:00
|
|
|
|
|
|
|
import click
|
|
|
|
import click_log
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
import pathlib
|
2019-05-03 20:18:14 +02:00
|
|
|
from distutils.version import LooseVersion
|
2018-11-23 15:01:55 +01:00
|
|
|
from typing import Iterable
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class GitLabRepo:
|
2019-05-03 20:18:14 +02:00
|
|
|
version_regex = re.compile(r"^v\d+\.\d+\.\d+(\-rc\d+)?(\-ee)?")
|
2019-10-08 15:52:11 +02:00
|
|
|
def __init__(self, owner: str = 'gitlab-org', repo: str = 'gitlab'):
|
2018-11-23 15:01:55 +01: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]:
|
2020-04-30 23:50:28 +02:00
|
|
|
r = requests.get(self.url + "/refs?sort=updated_desc&ref=master").json()
|
|
|
|
tags = r.get("Tags", [])
|
2018-11-23 15:01:55 +01:00
|
|
|
|
2019-05-03 20:18:14 +02:00
|
|
|
# filter out versions not matching version_regex
|
2020-04-30 23:50:28 +02:00
|
|
|
versions = list(filter(self.version_regex.match, tags))
|
2019-07-10 02:09:15 +02:00
|
|
|
|
2019-05-03 20:18:14 +02:00
|
|
|
# sort, but ignore v and -ee for sorting comparisons
|
|
|
|
versions.sort(key=lambda x: LooseVersion(x.replace("v", "").replace("-ee", "")), reverse=True)
|
|
|
|
return versions
|
2018-11-23 15:01:55 +01:00
|
|
|
|
|
|
|
def get_git_hash(self, rev: str):
|
2020-11-20 21:02:55 +01:00
|
|
|
return subprocess.check_output(['nix-universal-prefetch', 'fetchFromGitLab', '--owner', self.owner, '--repo', self.repo, '--rev', rev]).decode('utf-8').strip()
|
2018-11-23 15:01:55 +01: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
|
|
|
|
version = re.sub(r"^v", '', tag)
|
|
|
|
# strip -ee suffix
|
|
|
|
return re.sub(r"-ee$", '', version)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-10-08 15:52:11 +02:00
|
|
|
def get_data(self, rev):
|
2018-11-23 15:01:55 +01:00
|
|
|
version = self.rev2version(rev)
|
|
|
|
|
|
|
|
passthru = {v: self.get_file(v, rev).strip() for v in ['GITALY_SERVER_VERSION', 'GITLAB_PAGES_VERSION',
|
2021-04-07 16:52:15 +02:00
|
|
|
'GITLAB_SHELL_VERSION']}
|
2021-04-08 09:54:56 +02:00
|
|
|
|
|
|
|
passthru["GITLAB_WORKHORSE_VERSION"] = version
|
|
|
|
|
2018-11-23 15:01:55 +01:00
|
|
|
return dict(version=self.rev2version(rev),
|
|
|
|
repo_hash=self.get_git_hash(rev),
|
|
|
|
owner=self.owner,
|
|
|
|
repo=self.repo,
|
|
|
|
rev=rev,
|
|
|
|
passthru=passthru)
|
|
|
|
|
|
|
|
|
|
|
|
def _get_data_json():
|
|
|
|
data_file_path = pathlib.Path(__file__).parent / 'data.json'
|
|
|
|
with open(data_file_path, 'r') as f:
|
|
|
|
return json.load(f)
|
|
|
|
|
|
|
|
|
2020-12-26 22:35:33 +01:00
|
|
|
def _call_nix_update(pkg, version):
|
|
|
|
"""calls nix-update from nixpkgs root dir"""
|
2018-11-23 15:01:55 +01:00
|
|
|
nixpkgs_path = pathlib.Path(__file__).parent / '../../../../'
|
2020-12-26 22:35:33 +01:00
|
|
|
return subprocess.check_output(['nix-update', pkg, '--version', version], cwd=nixpkgs_path)
|
2018-11-23 15:01:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
@click_log.simple_verbosity_option(logger)
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command('update-data')
|
2020-04-20 17:09:02 +02:00
|
|
|
@click.option('--rev', default='latest', help='The rev to use (vX.Y.Z-ee), or \'latest\'')
|
2019-10-08 15:52:11 +02:00
|
|
|
def update_data(rev: str):
|
|
|
|
"""Update data.nix"""
|
|
|
|
repo = GitLabRepo()
|
2018-11-23 15:01:55 +01:00
|
|
|
|
|
|
|
if rev == 'latest':
|
|
|
|
# filter out pre and re releases
|
2019-10-08 15:52:11 +02:00
|
|
|
rev = next(filter(lambda x: not ('rc' in x or x.endswith('pre')), repo.tags))
|
2018-11-23 15:01:55 +01:00
|
|
|
logger.debug(f"Using rev {rev}")
|
|
|
|
|
2019-10-08 15:52:11 +02:00
|
|
|
version = repo.rev2version(rev)
|
2018-11-23 15:01:55 +01:00
|
|
|
logger.debug(f"Using version {version}")
|
|
|
|
|
|
|
|
data_file_path = pathlib.Path(__file__).parent / 'data.json'
|
|
|
|
|
2019-10-08 15:52:11 +02:00
|
|
|
data = repo.get_data(rev)
|
2018-11-23 15:01:55 +01:00
|
|
|
|
2019-10-08 15:52:11 +02:00
|
|
|
with open(data_file_path.as_posix(), 'w') as f:
|
|
|
|
json.dump(data, f, indent=2)
|
2020-09-29 22:39:23 +02:00
|
|
|
f.write("\n")
|
2018-11-23 15:01:55 +01:00
|
|
|
|
|
|
|
|
2019-10-08 15:52:11 +02:00
|
|
|
@cli.command('update-rubyenv')
|
|
|
|
def update_rubyenv():
|
|
|
|
"""Update rubyEnv"""
|
|
|
|
repo = GitLabRepo()
|
|
|
|
rubyenv_dir = pathlib.Path(__file__).parent / f"rubyEnv"
|
2018-11-23 15:01:55 +01:00
|
|
|
|
|
|
|
# load rev from data.json
|
|
|
|
data = _get_data_json()
|
2019-10-08 15:52:11 +02:00
|
|
|
rev = data['rev']
|
2018-11-23 15:01:55 +01:00
|
|
|
|
2021-05-24 21:26:08 +02:00
|
|
|
with open(rubyenv_dir / 'Gemfile.lock', 'w') as f:
|
|
|
|
f.write(repo.get_file('Gemfile.lock', rev))
|
|
|
|
with open(rubyenv_dir / 'Gemfile', 'w') as f:
|
|
|
|
original = repo.get_file('Gemfile', rev)
|
|
|
|
f.write(re.sub(r".*mail-smtp_pool.*", "", original))
|
2018-11-23 15:01:55 +01:00
|
|
|
|
2020-04-21 22:29:01 +02:00
|
|
|
subprocess.check_output(['bundle', 'lock'], cwd=rubyenv_dir)
|
2018-11-23 15:01:55 +01:00
|
|
|
subprocess.check_output(['bundix'], cwd=rubyenv_dir)
|
|
|
|
|
|
|
|
|
2019-10-08 11:24:06 +02:00
|
|
|
@cli.command('update-yarnpkgs')
|
|
|
|
def update_yarnpkgs():
|
|
|
|
"""Update yarnPkgs"""
|
|
|
|
|
2019-10-08 15:52:11 +02:00
|
|
|
repo = GitLabRepo()
|
2019-10-08 11:24:06 +02:00
|
|
|
yarnpkgs_dir = pathlib.Path(__file__).parent
|
|
|
|
|
|
|
|
# load rev from data.json
|
|
|
|
data = _get_data_json()
|
2019-10-08 15:52:11 +02:00
|
|
|
rev = data['rev']
|
2019-10-08 11:24:06 +02:00
|
|
|
|
|
|
|
with open(yarnpkgs_dir / 'yarn.lock', 'w') as f:
|
|
|
|
f.write(repo.get_file('yarn.lock', rev))
|
|
|
|
|
|
|
|
with open(yarnpkgs_dir / 'yarnPkgs.nix', 'w') as f:
|
|
|
|
subprocess.run(['yarn2nix'], cwd=yarnpkgs_dir, check=True, stdout=f)
|
|
|
|
|
|
|
|
os.unlink(yarnpkgs_dir / 'yarn.lock')
|
|
|
|
|
|
|
|
|
2018-11-23 15:01:55 +01:00
|
|
|
@cli.command('update-gitaly')
|
|
|
|
def update_gitaly():
|
|
|
|
"""Update gitaly"""
|
|
|
|
data = _get_data_json()
|
2019-10-08 15:52:11 +02:00
|
|
|
gitaly_server_version = data['passthru']['GITALY_SERVER_VERSION']
|
|
|
|
repo = GitLabRepo(repo='gitaly')
|
2019-07-16 01:18:11 +02:00
|
|
|
gitaly_dir = pathlib.Path(__file__).parent / 'gitaly'
|
2018-11-23 15:01:55 +01:00
|
|
|
|
|
|
|
for fn in ['Gemfile.lock', 'Gemfile']:
|
2019-07-16 01:18:11 +02:00
|
|
|
with open(gitaly_dir / fn, 'w') as f:
|
2019-10-08 15:52:11 +02:00
|
|
|
f.write(repo.get_file(f"ruby/{fn}", f"v{gitaly_server_version}"))
|
2018-11-23 15:01:55 +01:00
|
|
|
|
2020-04-21 22:29:01 +02:00
|
|
|
subprocess.check_output(['bundle', 'lock'], cwd=gitaly_dir)
|
2019-07-16 01:18:11 +02:00
|
|
|
subprocess.check_output(['bundix'], cwd=gitaly_dir)
|
2020-03-12 02:56:48 +01:00
|
|
|
|
2020-12-26 22:35:33 +01:00
|
|
|
_call_nix_update('gitaly', gitaly_server_version)
|
2018-11-23 16:09:13 +01:00
|
|
|
|
2018-11-23 15:01:55 +01:00
|
|
|
|
|
|
|
@cli.command('update-gitlab-shell')
|
|
|
|
def update_gitlab_shell():
|
|
|
|
"""Update gitlab-shell"""
|
|
|
|
data = _get_data_json()
|
2019-10-08 15:52:11 +02:00
|
|
|
gitlab_shell_version = data['passthru']['GITLAB_SHELL_VERSION']
|
2020-12-26 22:35:33 +01:00
|
|
|
_call_nix_update('gitlab-shell', gitlab_shell_version)
|
2018-11-23 15:01:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command('update-gitlab-workhorse')
|
|
|
|
def update_gitlab_workhorse():
|
2019-10-01 15:38:22 +02:00
|
|
|
"""Update gitlab-workhorse"""
|
2018-11-23 15:01:55 +01:00
|
|
|
data = _get_data_json()
|
2021-04-08 09:54:56 +02:00
|
|
|
gitlab_workhorse_version = data['passthru']['GITLAB_WORKHORSE_VERSION']
|
2020-12-26 22:35:33 +01:00
|
|
|
_call_nix_update('gitlab-workhorse', gitlab_workhorse_version)
|
2018-11-23 15:01:55 +01:00
|
|
|
|
2019-10-01 15:38:22 +02:00
|
|
|
|
2018-11-23 15:01:55 +01:00
|
|
|
@cli.command('update-all')
|
2020-04-20 17:09:02 +02:00
|
|
|
@click.option('--rev', default='latest', help='The rev to use (vX.Y.Z-ee), or \'latest\'')
|
2018-11-23 15:01:55 +01:00
|
|
|
@click.pass_context
|
2020-04-20 17:09:02 +02:00
|
|
|
def update_all(ctx, rev: str):
|
2019-10-08 15:52:11 +02:00
|
|
|
"""Update all gitlab components to the latest stable release"""
|
2020-04-20 17:09:02 +02:00
|
|
|
ctx.invoke(update_data, rev=rev)
|
2019-10-08 15:52:11 +02:00
|
|
|
ctx.invoke(update_rubyenv)
|
2019-10-08 11:24:06 +02:00
|
|
|
ctx.invoke(update_yarnpkgs)
|
2018-11-23 15:01:55 +01:00
|
|
|
ctx.invoke(update_gitaly)
|
|
|
|
ctx.invoke(update_gitlab_shell)
|
|
|
|
ctx.invoke(update_gitlab_workhorse)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
cli()
|