forked from virtio-win/virtio-win-pkg-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-repo.py
executable file
·350 lines (281 loc) · 11.8 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
#!/usr/bin/env python3
import argparse
import glob
import os
import re
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.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
##############################
# Local repo tree populating #
##############################
def _make_redirect(root, old, new):
return "redirect permanent %s/%s %s/%s\n" % (root, old, root, new)
class LocalRepo():
"""
Class representing the virtio-win tree locally on the system.
Helps contain the various repo tweaking logic
"""
LOCAL_ROOT_DIR = os.path.expanduser(
"~/src/fedora/virt-group-repos/virtio-win")
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"
HOSTED_USERNAME = "crobinso"
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, paths):
"""
Add the build RPM to the local tree
"""
for path in paths:
dest = "rpms"
if path.endswith(".src.rpm"):
dest = "srpms"
shellcomm("cp %s %s/%s" % (path, self.LOCAL_REPO_DIR, dest))
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_virtiowin_media(self, paths):
"""
Move iso/vfd 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 = ""
for versionfile, symlink in paths:
shellcomm("cp %s %s" % (versionfile, virtiodir))
shellcomm("cp %s %s" % (symlink, virtiodir))
htaccess += _make_redirect(
os.path.join(self.HTTP_DIRECT_DIR, self.virtio_basedir),
os.path.basename(symlink), os.path.basename(versionfile))
# 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):
fullsrc = os.path.join(self.LOCAL_DIRECT_DIR, src)
linkpath = os.path.join(self.LOCAL_DIRECT_DIR, link)
if not os.path.exists(fullsrc):
fail("Nonexistent link src=%s for target=%s" %
(fullsrc, linkpath))
if os.path.exists(linkpath):
os.unlink(linkpath)
shellcomm("ln -s %s %s" % (src, linkpath))
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)
return
os.mkdir(pkg_input_dir)
for filename in glob.glob(buildversions.NEW_BUILDS_DIR + "/*"):
shellcomm("cp %s %s" % (filename, pkg_input_dir))
shellcomm("ln -sf %s %s/latest-build " % (
os.path.basename(pkg_input_dir), pkg_input_topdir))
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: vfd, 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)
# Move virtio .iso and .vfds
virtiopaths = []
for basename in ["virtio-win_x86.vfd",
"virtio-win_amd64.vfd",
"virtio-win.iso"]:
symlink = os.path.join(sharedir, basename)
assert os.path.exists(symlink)
assert os.path.islink(symlink)
versionfile = os.path.realpath(symlink)
virtiopaths.append((versionfile, symlink))
localrepo.add_virtiowin_media(virtiopaths)
# 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)
# Copy RPMs to the tree
rpms = _glob(rpm_output + "/*.rpm", recursive=True)
localrepo.add_rpms(rpms)
########################
# Repo generate + push #
########################
def _generate_repos():
"""
Create repo trees, run createrepo_c
"""
LOCAL_REPO_DIR = LocalRepo.LOCAL_REPO_DIR
# Generate stable symlinks
shellcomm("rm -rf %s/*" % os.path.join(LOCAL_REPO_DIR, "stable"))
for stablever in STABLE_RPMS:
filename = "virtio-win-%s.noarch.rpm" % stablever
fullpath = os.path.join(LOCAL_REPO_DIR, "rpms", filename)
if not os.path.exists(fullpath):
fail("Didn't find stable RPM path %s" % fullpath)
shellcomm("ln -s ../rpms/%s %s" % (filename,
os.path.join(LOCAL_REPO_DIR, "stable",
os.path.basename(fullpath))))
# Generate latest symlinks
shellcomm("rm -rf %s/*" % os.path.join(LOCAL_REPO_DIR, "latest"))
for fullpath in glob.glob(os.path.join(LOCAL_REPO_DIR, "rpms", "*.rpm")):
filename = os.path.basename(fullpath)
shellcomm("ln -s ../rpms/%s %s" % (filename,
os.path.join(LOCAL_REPO_DIR, "latest", os.path.basename(fullpath))))
# Generate repodata
for rpmdir in ["latest", "stable", "srpms"]:
shellcomm("rm -rf %s" %
os.path.join(LOCAL_REPO_DIR, rpmdir, "repodata"))
shellcomm("createrepo_c %s > /dev/null" %
os.path.join(LOCAL_REPO_DIR, rpmdir))
# Put the repo file in place
shellcomm("cp -f data/virtio-win.repo %s" %
LocalRepo.LOCAL_ROOT_DIR)
# Use the RPM changelog as a changelog file for the whole tree
shellcomm("cp -f data/rpm_changelog %s/CHANGELOG" %
LocalRepo.LOCAL_ROOT_DIR)
def _run_rsync(reverse, dry):
rsync = "rsync --archive --verbose --compress --progress "
if dry:
rsync += "--dry-run "
remote = ("%[email protected]:~/virtgroup/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
shellcomm("%s --exclude repodata %s/ %s""" % (rsync, src, dst))
# Overwrite the repodata and remove stale files
shellcomm("%s --delete %s/ %s" % (rsync, 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:
_generate_repos()
_push_repos(reverse=options.resync)
return 0
if __name__ == '__main__':
sys.exit(main())