1 #!/usr/bin/env nix-shell
2 #! nix-shell -I nixpkgs=../../../.. -i python3 -p bundix bundler nix-update nix python3 python3Packages.requests python3Packages.click python3Packages.click-log python3Packages.packaging prefetch-yarn-deps git
12 from packaging
.version
import Version
13 from typing
import Iterable
17 NIXPKGS_PATH
= pathlib
.Path(__file__
).parent
/ "../../../../"
18 GITLAB_DIR
= pathlib
.Path(__file__
).parent
20 logger
= logging
.getLogger(__name__
)
21 click_log
.basic_config(logger
)
25 version_regex
= re
.compile(r
"^v\d+\.\d+\.\d+(\-rc\d+)?(\-ee)?(\-gitlab)?")
27 def __init__(self
, owner
: str = "gitlab-org", repo
: str = "gitlab"):
33 return f
"https://gitlab.com/{self.owner}/{self.repo}"
36 def tags(self
) -> Iterable
[str]:
37 """Returns a sorted list of repository tags"""
38 r
= requests
.get(self
.url
+ "/refs?sort=updated_desc&ref=master").json()
39 tags
= r
.get("Tags", [])
41 # filter out versions not matching version_regex
42 versions
= list(filter(self
.version_regex
.match
, tags
))
44 # sort, but ignore v, -ee and -gitlab for sorting comparisons
46 key
=lambda x
: Version(
47 x
.replace("v", "").replace("-ee", "").replace("-gitlab", "")
52 def get_git_hash(self
, rev
: str):
54 subprocess
.check_output(
58 f
"https://gitlab.com/{self.owner}/{self.repo}/-/archive/{rev}/{self.repo}-{rev}.tar.gz",
65 def get_yarn_hash(self
, rev
: str):
66 with tempfile
.TemporaryDirectory() as tmp_dir
:
67 with
open(tmp_dir
+ "/yarn.lock", "w") as f
:
68 f
.write(self
.get_file("yarn.lock", rev
))
70 subprocess
.check_output(["prefetch-yarn-deps", tmp_dir
+ "/yarn.lock"])
76 def rev2version(tag
: str) -> str:
78 normalize a tag to a version number.
79 This obviously isn't very smart if we don't pass something that looks like a tag
80 :param tag: the tag to normalize
81 :return: a normalized version number
84 version
= re
.sub(r
"^v", "", tag
)
85 # strip -ee and -gitlab suffixes
86 return re
.sub(r
"-(ee|gitlab)$", "", version
)
88 def get_file(self
, filepath
, rev
):
90 returns file contents at a given rev
91 :param filepath: the path to the file, relative to the repo root
92 :param rev: the rev to fetch at
95 return requests
.get(self
.url
+ f
"/raw/{rev}/{filepath}").text
97 def get_data(self
, rev
):
98 version
= self
.rev2version(rev
)
101 v
: self
.get_file(v
, rev
).strip()
103 "GITALY_SERVER_VERSION",
104 "GITLAB_PAGES_VERSION",
105 "GITLAB_SHELL_VERSION",
106 "GITLAB_ELASTICSEARCH_INDEXER_VERSION",
109 passthru
["GITLAB_WORKHORSE_VERSION"] = version
112 version
=self
.rev2version(rev
),
113 repo_hash
=self
.get_git_hash(rev
),
114 yarn_hash
=self
.get_yarn_hash(rev
),
122 def _get_data_json():
123 data_file_path
= pathlib
.Path(__file__
).parent
/ "data.json"
124 with
open(data_file_path
, "r") as f
:
128 def _call_nix_update(pkg
, version
):
129 """calls nix-update from nixpkgs root dir"""
130 return subprocess
.check_output(
131 ["nix-update", pkg
, "--version", version
], cwd
=NIXPKGS_PATH
135 @click_log.simple_verbosity_option(logger
)
141 @cli.command("update-data")
142 @click.option("--rev", default
="latest", help="The rev to use (vX.Y.Z-ee), or 'latest'")
143 def update_data(rev
: str):
144 """Update data.json"""
145 logger
.info("Updating data.json")
149 # filter out pre and rc releases
150 rev
= next(filter(lambda x
: not ("rc" in x
or x
.endswith("pre")), repo
.tags
))
152 data_file_path
= pathlib
.Path(__file__
).parent
/ "data.json"
154 data
= repo
.get_data(rev
)
156 with
open(data_file_path
.as_posix(), "w") as f
:
157 json
.dump(data
, f
, indent
=2)
161 @cli.command("update-rubyenv")
162 def update_rubyenv():
164 logger
.info("Updating gitlab")
166 rubyenv_dir
= pathlib
.Path(__file__
).parent
/ "rubyEnv"
168 # load rev from data.json
169 data
= _get_data_json()
171 version
= data
["version"]
173 for fn
in ["Gemfile.lock", "Gemfile"]:
174 with
open(rubyenv_dir
/ fn
, "w") as f
:
175 f
.write(repo
.get_file(fn
, rev
))
177 # patch for openssl 3.x support
178 subprocess
.check_output(
179 ["sed", "-i", "s:'openssl', '2.*':'openssl', '3.0.2':g", "Gemfile"],
185 # The sidekiq dependency was vendored to maintain compatibility with Redis 6.0 (as
186 # stated in this [comment]) but unfortunately, it seems to cause a crash in the
187 # application, as noted in this [upstream issue].
189 # We can safely swap out the dependency, as our Redis release in nixpkgs is >= 7.0.
191 # [comment]: https://gitlab.com/gitlab-org/gitlab/-/issues/468435#note_1979750600
192 # [upstream issue]: https://gitlab.com/gitlab-org/gitlab/-/issues/468435
193 subprocess
.check_output(
194 ["sed", "-i", "s|gem 'sidekiq', path: 'vendor/gems/sidekiq-7.1.6', require: 'sidekiq'|gem 'sidekiq', '~> 7.1.6'|g", "Gemfile"],
198 # Fetch vendored dependencies temporarily in order to build the gemset.nix
199 subprocess
.check_output(["mkdir", "-p", "vendor/gems", "gems"], cwd
=rubyenv_dir
)
200 subprocess
.check_output(
204 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",
206 cwd
=f
"{rubyenv_dir}/vendor/gems",
208 subprocess
.check_output(
212 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=2",
214 cwd
=f
"{rubyenv_dir}/gems",
217 # Undo our gemset.nix patches so that bundix runs through
218 subprocess
.check_output(
219 ["sed", "-i", "-e", "1d", "-e", "s:\\${src}/::g", "gemset.nix"], cwd
=rubyenv_dir
222 subprocess
.check_output(["bundle", "lock"], cwd
=rubyenv_dir
)
223 subprocess
.check_output(["bundix"], cwd
=rubyenv_dir
)
225 subprocess
.check_output(
232 's:path = \\(vendor/[^;]*\\);:path = "${src}/\\1";:g',
234 's:path = \\(gems/[^;]*\\);:path = "${src}/\\1";:g',
239 subprocess
.check_output(["rm", "-rf", "vendor", "gems"], cwd
=rubyenv_dir
)
242 @cli.command("update-gitaly")
245 logger
.info("Updating gitaly")
246 data
= _get_data_json()
247 gitaly_server_version
= data
['passthru']['GITALY_SERVER_VERSION']
248 repo
= GitLabRepo(repo
="gitaly")
249 gitaly_dir
= pathlib
.Path(__file__
).parent
/ 'gitaly'
251 makefile
= repo
.get_file("Makefile", f
"v{gitaly_server_version}")
252 makefile
+= "\nprint-%:;@echo $($*)\n"
254 git_version
= subprocess
.run(["make", "-f", "-", "print-GIT_VERSION"], check
=True, input=makefile
, text
=True, capture_output
=True).stdout
.strip()
256 _call_nix_update("gitaly", gitaly_server_version
)
257 _call_nix_update("gitaly.git", git_version
)
260 @cli.command("update-gitlab-pages")
261 def update_gitlab_pages():
262 """Update gitlab-pages"""
263 logger
.info("Updating gitlab-pages")
264 data
= _get_data_json()
265 gitlab_pages_version
= data
["passthru"]["GITLAB_PAGES_VERSION"]
266 _call_nix_update("gitlab-pages", gitlab_pages_version
)
269 def get_container_registry_version() -> str:
270 """Returns the version attribute of gitlab-container-registry"""
271 return subprocess
.check_output(
274 "--experimental-features",
280 "gitlab-container-registry.version",
286 @cli.command("update-gitlab-shell")
287 def update_gitlab_shell():
288 """Update gitlab-shell"""
289 logger
.info("Updating gitlab-shell")
290 data
= _get_data_json()
291 gitlab_shell_version
= data
["passthru"]["GITLAB_SHELL_VERSION"]
292 _call_nix_update("gitlab-shell", gitlab_shell_version
)
295 @cli.command("update-gitlab-workhorse")
296 def update_gitlab_workhorse():
297 """Update gitlab-workhorse"""
298 logger
.info("Updating gitlab-workhorse")
299 data
= _get_data_json()
300 gitlab_workhorse_version
= data
["passthru"]["GITLAB_WORKHORSE_VERSION"]
301 _call_nix_update("gitlab-workhorse", gitlab_workhorse_version
)
304 @cli.command("update-gitlab-container-registry")
305 @click.option("--rev", default
="latest", help="The rev to use (vX.Y.Z-ee), or 'latest'")
307 "--commit", is_flag
=True, default
=False, help="Commit the changes for you"
309 def update_gitlab_container_registry(rev
: str, commit
: bool):
310 """Update gitlab-container-registry"""
311 logger
.info("Updading gitlab-container-registry")
312 repo
= GitLabRepo(repo
="container-registry")
313 old_container_registry_version
= get_container_registry_version()
316 rev
= next(filter(lambda x
: not ("rc" in x
or x
.endswith("pre")), repo
.tags
))
318 version
= repo
.rev2version(rev
)
319 _call_nix_update("gitlab-container-registry", version
)
321 new_container_registry_version
= get_container_registry_version()
322 commit_container_registry(
323 old_container_registry_version
, new_container_registry_version
327 @cli.command('update-gitlab-elasticsearch-indexer')
328 def update_gitlab_elasticsearch_indexer():
329 """Update gitlab-elasticsearch-indexer"""
330 data
= _get_data_json()
331 gitlab_elasticsearch_indexer_version
= data
['passthru']['GITLAB_ELASTICSEARCH_INDEXER_VERSION']
332 _call_nix_update('gitlab-elasticsearch-indexer', gitlab_elasticsearch_indexer_version
)
335 @cli.command("update-all")
336 @click.option("--rev", default
="latest", help="The rev to use (vX.Y.Z-ee), or 'latest'")
338 "--commit", is_flag
=True, default
=False, help="Commit the changes for you"
341 def update_all(ctx
, rev
: str, commit
: bool):
342 """Update all gitlab components to the latest stable release"""
343 old_data_json
= _get_data_json()
344 old_container_registry_version
= get_container_registry_version()
346 ctx
.invoke(update_data
, rev
=rev
)
348 new_data_json
= _get_data_json()
350 ctx
.invoke(update_rubyenv
)
351 ctx
.invoke(update_gitaly
)
352 ctx
.invoke(update_gitlab_pages
)
353 ctx
.invoke(update_gitlab_shell
)
354 ctx
.invoke(update_gitlab_workhorse
)
355 ctx
.invoke(update_gitlab_elasticsearch_indexer
)
358 old_data_json
["version"], new_data_json
["version"], new_data_json
["rev"]
361 ctx
.invoke(update_gitlab_container_registry
)
363 new_container_registry_version
= get_container_registry_version()
364 commit_container_registry(
365 old_container_registry_version
, new_container_registry_version
369 def commit_gitlab(old_version
: str, new_version
: str, new_rev
: str) -> None:
370 """Commits the gitlab changes for you"""
381 "gitlab-elasticsearch-indexer",
390 f
"""gitlab: {old_version} -> {new_version}\n\nhttps://gitlab.com/gitlab-org/gitlab/-/blob/{new_rev}/CHANGELOG.md""",
396 def commit_container_registry(old_version
: str, new_version
: str) -> None:
397 """Commits the gitlab-container-registry changes for you"""
398 subprocess
.run(["git", "add", "gitlab-container-registry"], cwd
=GITLAB_DIR
)
404 f
"gitlab-container-registry: {old_version} -> {new_version}\n\nhttps://gitlab.com/gitlab-org/container-registry/-/blob/v{new_version}-gitlab/CHANGELOG.md",
410 if __name__
== "__main__":