-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathupdate-gitlab-versions
executable file
·299 lines (211 loc) · 8.71 KB
/
update-gitlab-versions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python3
"""Update combined GitLab and SubGit Docker images.
This program is used to update the combined GitLab and SubGit Docker images on
Docker Hub, based on official GitLab Docker releases. For this, the program
downloads a list of release tags from Docker Hub, and compares
References:
The code accessing Docker Hub repositories is based on https://github.com/al4/docker-registry-list.
"""
#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------
import argparse
import git
import json
import logging
import operator
import re
import requests
#------------------------------------------------------------------------------
# Utilities
#------------------------------------------------------------------------------
docker_tag = re.compile(r"^(\d{1,2})\.(\d{1,2})\.(\d{1,2})-(ee|ce)\.(\d{1})")
def get_docker_hub_token(auth_url, image_name):
params = {
"service": "registry.docker.io",
"scope": "repository:{image}:pull".format(image = image_name)
}
r = requests.get(auth_url + "/token", params = params)
if not r.status_code == 200:
logging.error("Error status {}".format(r.status_code))
raise RuntimeError("Cannot get Docker Hub authentification token")
j = r.json()
return j["token"]
def get_docker_tags(index_url, token, image_name):
h = {"Authorization": "Bearer {}".format(token)}
r = requests.get("{}/v2/{}/tags/list".format(index_url, image_name), headers = h)
j = r.json()
try:
tags = j["tags"]
except KeyError:
logging.error("Cannot extract Docker Hub version tags:")
print(json.dumps(j, indent = 2))
raise RuntimeError("Cannot extract Docker Hub version tags.")
return tags
def get_git_branches_and_tags(path):
repo = git.Repo(path)
_tags = repo.tags
gtags = [] ; tags = []
for t in _tags:
tag = str(t.name)
gtags.append(tag)
if docker_tag.match(tag):
tags.append(DockerTag(tag))
last = sorted(tags, key = operator.attrgetter("major", "minor", "patch", "sp"))[-1]
rbranches = []
for ref in repo.git.branch("-r").split("\n"):
rbranches.append(ref.strip())
return repo.heads, rbranches, gtags, last
def tag2branch(tag):
return "release/%d.%d" % (tag.major, tag.minor)
def tag2rbranch(tag):
return "origin/release/%d.%d" % (tag.major, tag.minor)
class DockerTag(object):
def __init__(self, tagstring):
m = docker_tag.match(tagstring)
self.tag = tagstring
self.major = int(m.group(1))
self.minor = int(m.group(2))
self.patch = int(m.group(3))
self.kind = m.group(4)
self.sp = int(m.group(5))
def __eq__(self, other):
return self.tag == other.tag
def __ne__(self, other):
result = self.__eq__(other)
if result is not NotImplemented:
return not result
return NotImplemented
def __lt__(self, other):
return NotImplemented
def __gt__(self, other):
if self.major < other.major:
result = False
elif self.major == other.major:
if self.minor < other.minor:
result = False
elif self.minor == other.minor:
if self.patch < other.patch:
result = False
elif self.patch == other.patch:
if self.sp <= other.sp:
result = False
else: # self.sp > other.sp
result = True
else: # self.patch > other.patch
result = True
else: # self.minor > other.minor
result = True
else: # self.major > other.major
result = True
return result
def __le__(self, other):
return NotImplemented
def __ge__(self, other):
return NotImplemented
def __repr__(self):
return self.tag
dockerfile_template = """
FROM {name}:{tag}
MAINTAINER Christian Marquardt
# Subgit version
ENV SUBGIT_VERSION 3.3.6
# Install Java
RUN apt-get update && \\
apt-get install -y openjdk-8-jre-headless && \\
apt-get clean && \\
rm -rf /var/lib/apt/lists/* /tmp/*
# Download subgit from official website and install
RUN curl -o subgit.deb -q https://subgit.com/files/subgit_${{SUBGIT_VERSION}}_all.deb && \\
dpkg -i subgit.deb && \\
rm -fr subgit.deb
# Fix SNI error with Java
RUN sed -i '/^EXTRA_JVM_ARGUMENTS.*/a EXTRA_JVM_ARGUMENTS="$EXTRA_JVM_ARGUMENTS -Djsse.enableSNIExtension=false"' /usr/bin/subgit
# Our wrapper script (enabling cron, and then launching GitLab's wrapper)
COPY assets/outerwrapper /assets/
# Define data volumes
VOLUME ["/etc/gitlab", "/etc/subgit", "/etc/cron.d", "/var/opt/gitlab", "/var/log/gitlab"]
# Wrapper to handle signal, trigger runit and reconfigure GitLab
CMD ["/assets/outerwrapper"]
"""
#------------------------------------------------------------------------------
# Main program
#------------------------------------------------------------------------------
if __name__ == "__main__":
# Command line arguments
p = argparse.ArgumentParser()
p.add_argument("-m", "--max", metavar = "<max-number-of-releases>", help = "Number of releases to process", default = 0, type = int)
p.add_argument("-l", "--list", help = "Only show which versions / imags would / could be built", action = "store_true", default = False)
p.add_argument("-n", "--dry-run", help = "Only show what would be done; do not change anything", action = "store_true", default = False)
p.add_argument('-t', '--token', metavar = "<token>",
help = 'Auth token to use (automatically fetched if not specified)')
p.add_argument('-i', '--index-url', metavar = "<index-url>", default = 'https://index.docker.io')
p.add_argument('-a', '--auth-url', metavar = "<auth-url>", default = 'https://auth.docker.io')
args = p.parse_args()
# Get git tags and branches
branches, rbranches, gtags, last = get_git_branches_and_tags(".")
# Determine if we're dealing with CE or EE versions
name = "gitlab/gitlab-%s" % last.kind
# Get Docker Hub tags
token = args.token or get_docker_hub_token(auth_url = args.auth_url, image_name = name)
dtags = get_docker_tags(args.index_url, token, name)
# Extract the tags we're interested in
tags = []
for tag in dtags:
if docker_tag.match(tag) and tag not in gtags:
t = DockerTag(tag)
if t > last or t.major >= last.major - 1:
tags.append(t)
# Check is there's something to update
if len(tags) == 0:
print("There are no new releases.")
exit(-1)
# Sort releases to be updated
tags = sorted(tags, key = operator.attrgetter("major", "minor", "patch", "sp"))
# Provide a list of images that need to be built
if args.list:
for tag in tags:
print(" %s" % tag)
exit(0)
# Shorten list of releases to be build (if desired)
if args.max > 0 and len(tags) > args.max:
tags = tags[0:args.max]
# Loop over all new releases
repo = git.Repo(".")
for tag in tags:
# Switch into or create corresponding branch
branch = tag2branch(tag)
rbranch = tag2rbranch(tag)
if branch in branches:
print("Switching to branch %s" % branch)
if not args.dry_run:
repo.git.checkout(branch)
elif rbranch in rbranches:
print("Checking out remote branch %s" % rbranch)
if not args.dry_run:
repo.git.checkout("--track", rbranch)
branches = repo.heads
else:
print("Creating new branch %s and pushing to origin..." % branch)
if not args.dry_run:
repo.git.checkout("master")
repo.git.checkout("-b", branch)
repo.git.push(branch, u = "origin")
branches = repo.heads
# Creating new Dockerfile
print("Creating Dockerfile for GitLab v%s..." % tag)
if args.dry_run:
print(dockerfile_template.format(name = name, tag = tag))
else:
with open("Dockerfile", "w") as f:
print(dockerfile_template.format(name = name, tag = tag), file = f)
f.close()
# Committing, tagging, and pushing
print("Committing, tagging and pushing v%s..." % tag)
if not args.dry_run:
repo.git.add("Dockerfile")
repo.git.commit(m = "Update to GitLab v%s." % tag)
repo.git.tag(tag, m = "Tagged GitLab v%s." % tag)
repo.git.push()
repo.git.push(tags = True)
print("\n")