-
Notifications
You must be signed in to change notification settings - Fork 160
/
make-repo.py
executable file
·448 lines (367 loc) · 15.2 KB
/
make-repo.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python3
import argparse
import glob
import os
import re
import shutil
import sys
from util.buildversions import BuildVersions
from util.utils import fail, shellcomm, yes_or_no
# List of stable versions. Keep the newest version first.
#
# Note, if you update this, --repo-only doesn't currently handle
# the .htacess updating. Do it by hand or fix this script :)
STABLE_RPMS = [
"0.1.262-2", # RHEL9.5.0
"0.1.240-1", # RHEL9.3.0 and RHEL8.9
"0.1.229-1", # RHEL9.1.0.z and RHEL8.7
"0.1.225-2", # RHEL9.1.0
"0.1.221-1", # RHEL9.0.0.z
"0.1.217-2", # RHEL9.0.0
"0.1.215-2", # RHEL8.4.0.z and RHEL8.5.0.z
"0.1.208-1", # RHEL8.5.0
"0.1.196-1", # RHEL8.4.0
"0.1.185-2", # RHEL8.2.1 and RHEL7.9
"0.1.171-1", # RHEL8.0.1
"0.1.160-1", # RHEL7.7ish
"0.1.141-1", # RHEL7.4 zstream
"0.1.126-2", # RHEL7.3 and RHEL6.9
"0.1.110-1", # RHEL7.2 and RHEL6.8
"0.1.102-1", # RHEL6.7 version
"0.1.96-1", # RHEL7.1 version
]
def _glob(pattern, recursive=False):
ret = list(glob.glob(pattern, recursive=recursive))
if not ret:
fail("Didn't find any matching files: %s" % pattern)
return ret
def _get_fas_username():
"""
Get fedora username. Uses FAS_USERNAME environment variable which is
used by some other fedora tools
"""
ret = os.environ.get("FAS_USERNAME")
if not ret:
fail("You must set FAS_USERNAME environment variable to your "
"fedorapeople account name")
return ret
def _get_local_dir():
"""
Directory on the local machine we are using as the virtio-win mirror.
We will update this locally and then rsync it to fedorapeople
"""
ret = os.path.expanduser("~/src/fedora/virt-group-repos/virtio-win")
if not os.path.exists(ret):
fail("Expected local virtio-win mirror does not exist: %s" % ret)
return ret
##############################
# Local repo tree populating #
##############################
def _make_redirect(root, old, new):
return "redirect permanent %s/%s %s/%s\n" % (root, old, root, new)
def _add_relative_link(topdir, srcname, linkname):
"""
Create symlink for passed paths, but using relative path resolution
"""
srcpath = os.path.join(topdir, srcname)
linkpath = os.path.join(topdir, linkname)
if not os.path.exists(srcpath):
fail("Nonexistent link src=%s for target=%s" %
(srcpath, linkpath))
srcrelpath = os.path.relpath(srcname, os.path.dirname(linkname))
if os.path.exists(linkpath):
if (os.path.islink(linkpath) and
os.readlink(linkpath) == srcrelpath):
print("link path=%s already points to src=%s, nothing to do" %
(linkpath, srcrelpath))
return
os.unlink(linkpath)
shellcomm("ln -s %s %s" % (srcrelpath, linkpath))
class LocalRepo():
"""
Class representing the virtio-win tree locally on the system.
Helps contain the various repo tweaking logic
"""
HOSTED_USERNAME = _get_fas_username()
LOCAL_ROOT_DIR = _get_local_dir()
LOCAL_REPO_DIR = os.path.join(LOCAL_ROOT_DIR, "repo")
LOCAL_DIRECT_DIR = os.path.join(LOCAL_ROOT_DIR, "direct-downloads")
HTTP_DIRECT_DIR = "/groups/virt/virtio-win/direct-downloads"
def __init__(self, virtio_version_str, virtio_release_str,
qemuga_release_str):
"""
Init with the new versions we are adding to the tree
:param virtio_version_str: Ex: virtio-win-0.1-150
:param virtio_release_str: Ex: virtio-win-0.1-150-3
:param qemuga_release_str: Ex: qemu-ga-win-100.0.0.0-3.el7ev
"""
self.virtio_version_str = virtio_version_str
self.virtio_release_str = virtio_release_str
self.qemuga_release_str = qemuga_release_str
self.qemuga_basedir = os.path.join(
"archive-qemu-ga", self.qemuga_release_str)
self.virtio_basedir = os.path.join(
"archive-virtio", self.virtio_release_str)
def add_rpms(self, src_rpmpath, src_srpmpath):
"""
Add the build RPM to the local tree
"""
def addpath(srcpath, repodir):
dstpath = os.path.join(self.LOCAL_REPO_DIR, repodir,
os.path.basename(srcpath))
shellcomm("cp %s %s" % (srcpath, dstpath))
return dstpath
dst_rpmpath = addpath(src_rpmpath, "rpms")
dst_srpmpath = addpath(src_srpmpath, "srpms")
return dst_rpmpath, dst_srpmpath
def add_qemuga(self, paths):
"""
Move qemuga msis into the local tree
"""
qemugadir = os.path.join(
self.LOCAL_DIRECT_DIR, self.qemuga_basedir)
if os.path.exists(qemugadir):
print("qemuga has already been uploaded, skipping: %s" %
os.path.basename(qemugadir))
return
os.mkdir(qemugadir)
for path in paths:
shellcomm("cp %s %s" % (path, qemugadir))
def add_virtiogt(self, paths):
"""
Move virtiogt msis into the local virtio-win direct tree
"""
virtiodir = os.path.join(
self.LOCAL_DIRECT_DIR, self.virtio_basedir)
for path in paths:
shellcomm("cp %s %s" % (path, virtiodir))
def add_virtiowin_media(self, isopath, rpmpath, srpmpath):
"""
Move iso media to the local tree. Set up symlinks and
htaccess magic for the non-versioned links
"""
virtiodir = os.path.join(
self.LOCAL_DIRECT_DIR, self.virtio_basedir)
if os.path.exists(virtiodir):
fail("dir=%s already exists? Make sure we aren't "
"overwriting anything." % virtiodir)
os.mkdir(virtiodir)
htaccess = ""
def add_stable_path(path, stablename):
versionname = os.path.basename(path)
_add_relative_link(virtiodir, versionname, stablename)
nonlocal htaccess
htaccess += _make_redirect(
os.path.join(self.HTTP_DIRECT_DIR, self.virtio_basedir),
stablename, versionname)
def add_rpm(path, stablename):
# RPMs are already in the repo tree, so symlink the full path
_add_relative_link(virtiodir,
os.path.relpath(path, virtiodir),
os.path.basename(path))
add_stable_path(path, stablename)
add_rpm(rpmpath, "virtio-win.noarch.rpm")
add_rpm(srpmpath, "virtio-win.src.rpm")
shellcomm("cp %s %s" % (isopath, virtiodir))
add_stable_path(isopath, "virtio-win.iso")
# Write .htaccess, redirecting symlinks to versioned files, so
# nobody ends up with unversioned files locally, since that
# will make for crappy bug reports
open(os.path.join(virtiodir, ".htaccess"), "w").write(htaccess)
def add_htaccess_stable_links(self):
# Make latest-qemu-ga, latest-virtio, and stable-virtio links
def add_link(src, link):
topdir = self.LOCAL_DIRECT_DIR
_add_relative_link(topdir, src, link)
return _make_redirect(self.HTTP_DIRECT_DIR, link, src)
htaccess = ""
htaccess += add_link(self.qemuga_basedir, "latest-qemu-ga")
htaccess += add_link(self.virtio_basedir, "latest-virtio")
htaccess += add_link(
"archive-virtio/virtio-win-%s" % STABLE_RPMS[0],
"stable-virtio")
open(os.path.join(
self.LOCAL_DIRECT_DIR, ".htaccess"), "w").write(htaccess)
def add_pkg_build_input(self, buildversions):
"""
Upload the NEW_BUILDS_DIR content we used, so people can
reproduce the build if they need to
"""
pkg_input_topdir = os.path.join(self.LOCAL_DIRECT_DIR,
"virtio-win-pkg-scripts-input")
pkg_input_dir = os.path.join(pkg_input_topdir, self.virtio_release_str)
if os.path.exists(pkg_input_dir):
print("%s exists, not changing content." % pkg_input_dir)
else:
os.mkdir(pkg_input_dir)
for filename in glob.glob(buildversions.NEW_BUILDS_DIR + "/*"):
shellcomm("cp %s %s" % (filename, pkg_input_dir))
_add_relative_link(pkg_input_topdir,
os.path.basename(pkg_input_dir), "latest-build")
def _populate_local_tree(buildversions, rpm_output, rpm_buildroot):
"""
Copy all the built bits into our local repo tree to get it
ready for syncing: iso, unpacked qemu-ga msis, etc.
Also generate root dir .htaccess redirects
"""
rpm_output = os.path.realpath(rpm_output)
rpm_buildroot = os.path.realpath(rpm_buildroot)
extract_dir = _glob(rpm_buildroot + "/virtio-win*.x86_64")[0]
sharedir = extract_dir + "/usr/share/virtio-win/"
assert os.path.exists(sharedir)
# filename will be like .../virtio-win-0.1.171-6.x86_64
# extract the RPM version and release
virtio_release_str = os.path.basename(extract_dir).rsplit(".", 1)[0]
virtio_version_str = virtio_release_str.rsplit("-", 1)[0]
assert re.match(r"virtio-win-[\d\.]+", virtio_version_str)
assert re.match(r"virtio-win-[\d\.]+-\d+", virtio_release_str)
# there should be a directory like
# $rpm_buildroot/virtio-win-$version/qemu-ga-win-100.0.0.0-3.el7ev/
# Get the basename of that
qemuga_release_str = os.path.basename(
_glob(rpm_buildroot + "/*/qemu-ga-win*")[0])
localrepo = LocalRepo(virtio_version_str,
virtio_release_str, qemuga_release_str)
# Move qemu-ga .msis into our local mirror
qemugapaths = _glob(os.path.join(sharedir, "guest-agent", "*"))
localrepo.add_qemuga(qemugapaths)
# Copy RPMs to the repo/ tree
rpms = _glob(rpm_output + "/**/*.rpm", recursive=True)
assert len(rpms) == 2
src_rpmpath = [rpm for rpm in rpms if rpm.endswith(".noarch.rpm")][0]
src_srpmpath = [rpm for rpm in rpms if rpm.endswith(".src.rpm")][0]
dst_rpmpath, dst_srpmpath = localrepo.add_rpms(src_rpmpath, src_srpmpath)
# Move virtio .iso and RPMs to stable locations
virtiowinpath = os.path.realpath(os.path.join(sharedir, "virtio-win.iso"))
localrepo.add_virtiowin_media(virtiowinpath, dst_rpmpath, dst_srpmpath)
# Add virtio-win-gt .msis into the virtio iso dir
virtiogtpaths = _glob(os.path.join(sharedir, "installer", "*"))
localrepo.add_virtiogt(virtiogtpaths)
# Link htaccess latest-X/stable-X to latest media
localrepo.add_htaccess_stable_links()
# Copy build input content to the tree
localrepo.add_pkg_build_input(buildversions)
########################
# Repo generate + push #
########################
def _add_misc_data():
"""
Add tree stable links, and misc data
"""
LOCAL_REPO_DIR = LocalRepo.LOCAL_REPO_DIR
# Generate stable symlinks
for stablever in STABLE_RPMS:
filename = "virtio-win-%s.noarch.rpm" % stablever
_add_relative_link(LOCAL_REPO_DIR,
"rpms/%s" % filename,
"stable/%s" % filename)
# Generate latest symlinks
for fullpath in glob.glob(os.path.join(LOCAL_REPO_DIR, "rpms", "*.rpm")):
filename = os.path.basename(fullpath)
_add_relative_link(LOCAL_REPO_DIR,
"rpms/%s" % filename,
"latest/%s" % filename)
def cp(srcpath, dstpath):
# Copy, but not if content is unchanged
if (os.path.exists(dstpath) and
open(srcpath).read() == open(dstpath).read()):
print("%s is up to date, skipping." % dstpath)
return
shutil.copy(srcpath, dstpath)
# Put the repo file in place
cp("data/virtio-win.repo",
os.path.join(LocalRepo.LOCAL_ROOT_DIR, "virtio-win.repo"))
# Use the RPM changelog as a changelog file for the whole tree
cp("data/rpm_changelog",
os.path.join(LocalRepo.LOCAL_ROOT_DIR, "CHANGELOG"))
def _run_createrepo():
"""
Run yum createrepo
"""
for rpmdir in ["latest", "stable", "srpms"]:
#shellcomm("rm -rf %s" %
# os.path.join(LOCAL_REPO_DIR, rpmdir, "repodata"))
shellcomm("createrepo_c %s --update > /dev/null" %
os.path.join(LocalRepo.LOCAL_REPO_DIR, rpmdir))
def _run_rsync(reverse, dry):
def _cmd(opts, src, dst):
rsync = "rsync "
rsync += "--archive --verbose --compress --progress --omit-link-times --omit-dir-times "
if not reverse:
# There is no virtmaint-sig user, so we use our user
rsync += "--chown=%s:virtmaint-sig " % LocalRepo.HOSTED_USERNAME
# Set dirs to 775 and files to 664
rsync += "--chmod=D775,F664 "
if dry:
rsync += "--dry-run "
rsync += "%s %s/ %s" % (opts, src, dst)
if dry:
# Filter out uninteresting repoadata updates
rsync += " | grep -Ev 'repodata/.+'"
return rsync
remote = ("%[email protected]:/srv/groups/virt/virtio-win" %
LocalRepo.HOSTED_USERNAME)
local = LocalRepo.LOCAL_ROOT_DIR
if reverse:
src = remote
dst = local
else:
src = local
dst = remote
# Put the RPMs in place. Skip yum repodata until RPMs
# are inplace, to prevent users seeing an inconsistent repo
shellcomm(_cmd("--exclude repodata", src, dst))
# Overwrite the repodata and remove stale files
args = ""
# This says we only want to sync repodata/* and below, so we
# avoid possibly deleting anything else
args += '--include "*/" --include "repodata/*" --exclude "*" '
args += "--delete"
shellcomm(_cmd(args, src, dst))
def _push_repos(reverse):
"""
rsync the changes to fedorapeople.org
"""
print()
print()
_run_rsync(reverse=reverse, dry=True)
print()
print()
if not yes_or_no("Review the --dry-run changes. "
"Do you want to push? (y/n): "):
sys.exit(1)
_run_rsync(reverse=reverse, dry=False)
###################
# main() handling #
###################
def parse_args():
desc = ("Populate the local repo with content from the RPM "
"build process, regenerate the repo data, and rsync it")
parser = argparse.ArgumentParser(description=desc)
parser.add_argument("--rpm-output",
help="Directory containing built virtio-win* RPMs")
parser.add_argument("--rpm-buildroot",
help="Directory containing RPM buildroot content")
parser.add_argument("--regenerate-only", action="store_true",
help="Only regenerate and push the repo contents")
parser.add_argument("--resync", action="store_true",
help="rsync fedorapeople contents back to the local machine,"
"to reset the local mirror.")
return parser.parse_args()
def main():
options = parse_args()
if not options.regenerate_only and not options.resync:
if not options.rpm_output or not options.rpm_buildroot:
fail("--rpm-output and --rpm-buildroot must both "
"be specified, or pass --regenerate-only to "
"regen just the repo.")
buildversions = BuildVersions()
_populate_local_tree(buildversions,
options.rpm_output, options.rpm_buildroot)
if not options.resync:
_add_misc_data()
_run_createrepo()
_push_repos(reverse=options.resync)
return 0
if __name__ == '__main__':
sys.exit(main())