-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
302 lines (231 loc) · 11.7 KB
/
generator.py
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
300
301
import shutil
import yaml
import argparse
import os
import logging
import re
import urllib.request
import json
import pygit2
import jinja2
import subprocess
from packaging import version
parser = argparse.ArgumentParser(description="Generate Atlassian based Dockerfiles")
parser.add_argument("--config", help="a YaML file with configuration (default: atlassian.yml)", default="atlassian.yml")
parser.add_argument("--repositories", help="a YaML file with repository configuration (default: repositories.yml)", default="repositories.yml")
parser.add_argument("--workdir",
help="Location where the script should operate (default: work subdirectory)",
default=os.path.join(os.path.dirname(__file__), 'work'))
parser.add_argument("--templatedir",
help="Location that contains all the templates (default: templates subdirectory)",
default=os.path.join(os.path.dirname(__file__), 'templates'))
parser.add_argument("--nosshagent",
help="Don't use SSH agent based authentication.",
action='store_true')
parser.add_argument("--sshpassword", help="Provide the password for your private key. Implies --nosshagent")
parser.add_argument("--commitname",
help="Name of the commit author (default: draca-be/atlassian-generator)",
default="draca-be/atlassian-generator")
parser.add_argument("--commitemail",
help="Email of the commit author (default: [email protected])",
default="[email protected]")
parser.add_argument("--dontpush",
help="Don't push to the remote repository",
action='store_true')
parser.add_argument("templates", metavar="template", nargs="?",
help="Specify the template to parse (default: all)")
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)
# Cache software feeds so we don't make multiple calls in one run
feeds = {}
# We only support key-based authentication
if args.sshpassword:
gitkeypair = pygit2.Keypair("git", os.path.expanduser("~/.ssh/id_rsa.pub"), os.path.expanduser("~/.ssh/id_rsa"), args.sshpassword)
elif not args.nosshagent:
if 'SSH_AUTH_SOCK' not in os.environ:
logging.error("SSH agent selected but no SSH agent is running")
exit(1)
# Check if someone is actually authenticated
if subprocess.run(['ssh-add', '-l']).returncode == 0:
gitkeypair = pygit2.Keypair("git", None, None, "")
else:
logging.error("SSH agent selected but no active sessions")
exit(1)
else:
logging.info("No SSH agent selected but no password provided, trying passwordless authentication.")
gitkeypair = pygit2.Keypair("git", os.path.expanduser("~/.ssh/id_rsa.pub"), os.path.expanduser("~/.ssh/id_rsa"), "")
gitcallbacks = pygit2.RemoteCallbacks(credentials=gitkeypair)
# Commit author
author = pygit2.Signature(args.commitname, args.commitemail)
def processversion(repo, application, versioninfo, suffix):
logging.info(" -> {}{}".format(versioninfo['version'], suffix))
branch = "refs/heads/{}{}".format(versioninfo['version'], suffix)
remotebranch = "refs/remotes/origin/{}{}".format(versioninfo['version'], suffix)
# Make sure we have a branch to work in
if not repo.references.get(branch):
remoteref = repo.references.get(remotebranch)
if not remoteref:
logging.info(" Creating branch")
tree = repo.TreeBuilder().write()
repo.create_commit(branch,
author,
author,
"New branch for {} {}{}".format(application['name'], versioninfo['version'], suffix),
tree,
[]
)
else:
logging.info(" Using remote branch")
repo.create_reference(branch, remoteref.resolve().target)
logging.info(" Switching branch")
repo.checkout(branch)
# Clean the workdir
for entry in os.listdir(repo.workdir):
path = os.path.join(repo.workdir, entry)
if os.path.isfile(path):
os.unlink(path)
else:
if entry != '.git':
shutil.rmtree(path)
# Start processing templates
templatedir = os.path.join(args.templatedir, application['template'])
environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(args.templatedir)
)
# Create the context from template and add version info
context = application.get('context', {})
context['version'] = versioninfo['version']
context['url'] = versioninfo['zipUrl']
logging.info(" Processing templates")
# Walk the template directory
for root, dirs, files in os.walk(templatedir):
for template in files:
outputfile, ext = os.path.splitext(template)
# Only process files that have a .j2 extension
if ext == '.j2':
subpath = os.path.relpath(root, start=templatedir)
outputpath = os.path.join(repo.workdir, subpath)
# Make sure the directories exist
os.makedirs(outputpath, exist_ok=True)
# Process the template
environment.get_template(os.path.join(application['template'], subpath, template)).stream(context).dump(os.path.join(outputpath, outputfile))
# Make sure file permissions match
mode = os.lstat(os.path.join(root, template)).st_mode
os.chmod(os.path.join(outputpath, outputfile), mode)
# If something changed, add files to the index and commit
for path, flags in repo.status().items():
if flags == pygit2.GIT_STATUS_CURRENT or flags == pygit2.GIT_STATUS_IGNORED:
continue
logging.info(" Comitting changes")
repo.index.add_all()
repo.index.write()
tree = repo.index.write_tree()
repo.create_commit(branch, author, author, "Update", tree, [repo.head.target])
if not args.dontpush:
logging.info(" Pushing branch")
repo.remotes['origin'].push(['+' + branch], callbacks=gitcallbacks)
else:
logging.info(" Push disabled")
break
def tagversion(repo, name, target):
branch = "refs/heads/{}".format(name)
remotebranch = "refs/remotes/origin/{}".format(name)
targetbranch = "refs/heads/{}".format(target)
branchref = repo.references.get(branch)
remoteref = repo.references.get(remotebranch)
targetref = repo.references.get(targetbranch)
if branchref and \
targetref and \
remoteref and \
branchref.resolve().target == targetref.resolve().target == remoteref.resolve().target:
logging.info("{} already tagged as {}, skipping".format(target, name))
else:
logging.info("Tagging {} as {}".format(target, name))
branchref = repo.create_reference(branch, targetref.resolve().target, force=True)
# Only push if the remote reference is not the same as the local
if remoteref == None or remoteref.resolve().target != branchref.resolve().target:
if not args.dontpush:
logging.info("Pushing branch")
repo.remotes['origin'].push(['+' + branch], callbacks=gitcallbacks)
else:
logging.info("Push disabled")
def processconfiguration(repo, configuration):
minimumversion = version.parse(configuration.get('minimumVersion', '0.0.1'))
maximumversion = version.parse(configuration.get('maximumVersion', '999.999.999'))
latestversion, latestmajor, latestminor = minimumversion, {}, {}
suffix = configuration.get('suffix', '')
for feed in configuration['feeds']:
# If we don't have a cached version, fetch the .json
if feed not in feeds:
with urllib.request.urlopen(feed) as url:
feeddata = url.read().decode()
feeds[feed] = json.loads(feeddata[10:-1])
# Use the cached version
feeddata = feeds[feed]
# Iterate all the versions
for versioninfo in feeddata:
itemversion = version.parse(versioninfo['version'])
# Only pick the tarballs and filter out versions
if re.match(r".*TAR\.GZ Archive.*", versioninfo['description']) \
and versioninfo['type'] == 'Binary' \
and maximumversion >= itemversion >= minimumversion:
# Process the app version
processversion(repo, configuration, versioninfo, suffix)
# Hocus pocus to save latest major and minor versions
if itemversion > latestversion:
latestversion = itemversion
major = "{}".format(itemversion.release[0])
if itemversion >= latestmajor.get(major, minimumversion):
latestmajor[major] = itemversion
minor = "{}.{}".format(itemversion.release[0], itemversion.release[1])
if itemversion >= latestminor.get(minor, minimumversion):
latestminor[minor] = itemversion
return (latestmajor, latestminor, latestversion)
def processapp(application):
logging.info("Processing {}".format(application['name']))
if 'repository' not in application:
logging.warning("No repository configured, skipping")
return
# Extract directory name from repository
m = re.match(r".*/([^/]*).git", application['repository'])
path = os.path.join(args.workdir, m.group(1))
if not os.path.exists(path):
logging.info("Cloning to {}".format(path))
# Clone the repository
repo = pygit2.clone_repository(application['repository'], path, callbacks=gitcallbacks)
repo = pygit2.Repository(path)
versions = {}
for configuration in application['configurations']:
suffix = configuration.get('suffix', '')
(latestmajor, latestminor, latestversion) = processconfiguration(repo, configuration)
# Make sure that across configurations the major, minor and latest tags are respected
if suffix not in versions:
versions[suffix] = [latestmajor, latestminor, latestversion]
else:
for major, majorversion in latestmajor.items():
if major not in versions[suffix][0] or majorversion > versions[suffix][0][major]:
versions[suffix][0][major] = majorversion
for minor, minorversion in latestminor.items():
if minor not in versions[suffix][1] or minorversion > versions[suffix][1][minor]:
versions[suffix][1][minor] = minorversion
if latestversion > versions[suffix][2]:
versions[suffix][2] = latestversion
# Tag latest major and minor versions
for suffix, (latestmajor, latestminor, latestversion) in versions.items():
for major, majorversion in latestmajor.items():
tagversion(repo, major + suffix, str(majorversion) + suffix)
for minor, minorversion in latestminor.items():
tagversion(repo, minor + suffix, str(minorversion) + suffix)
tagversion(repo, "latest" + suffix, str(latestversion) + suffix)
if __name__ == '__main__':
with open(args.repositories, 'r') as stream:
repos = yaml.load(stream)
with open(args.config, 'r') as stream:
data = yaml.load(stream)
for item in data:
if item['name'] in repos:
item['repository'] = repos[item['name']]
if not args.templates or item['template'] in args.templates:
processapp(item)
else:
logging.info("No repository configured for {}".format(item['name']))