-
Notifications
You must be signed in to change notification settings - Fork 5
/
sync-release.py
executable file
·329 lines (306 loc) · 11.6 KB
/
sync-release.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
#!/usr/bin/env python
import argparse
import datetime
import getpass
import logging
import os
from pydpkg import Dpkg
import rpm
import shutil
import subprocess
import sys
import yaml
logger = logging.getLogger()
PROJ_ROOT = os.path.dirname(os.path.realpath(__file__))
DISTS = [
'el8',
'el9',
'amzn2023',
'ubuntu-20.04',
'ubuntu-22.04',
'ubuntu-24.04',
'debian-12'
]
EL_RELEASES = [
'el8',
'el9'
]
BASIC_RELEASES = [
'amzn2023',
]
DEB_CODENAMES = [
'focal',
'jammy',
'noble',
'bookworm'
]
SKIP = [
'bionic'
]
COMPUTE_DISTS = [
'el8',
'el9',
'amzn2023',
]
ARCHES = [
'x86_64',
'aarch64',
'ppc64le',
]
def get_rpm_info(rpm_file):
ts = rpm.ts()
fdno = os.open(rpm_file, os.O_RDONLY)
try:
hdr = ts.hdrFromFdno(fdno)
except rpm.error:
ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES)
hdr = ts.hdrFromFdno(fdno)
os.close(fdno)
info = {
'name': hdr[rpm.RPMTAG_NAME],
'ver': "%s-%s" % (hdr[rpm.RPMTAG_VERSION],hdr[rpm.RPMTAG_RELEASE])
}
return info
def get_deb_info(deb_file):
dp = Dpkg(deb_file)
info = dp.headers
return info
def main():
gpgpass = '/systems/osc_certs/gpg/ondemand/.gpgpass'
usage_examples = """
Usage examples:
Sync given release
%(prog)s --release 1.3
"""
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, epilog=usage_examples)
parser.add_argument('-d', '--debug', action='store_true', default=False)
parser.add_argument('-f', '--force', help='overwrite existing RPMs', action='store_true', default=False)
parser.add_argument('-c', '--clean', help='remove non-manifest RPMs from release repo', action='store_true', default=False)
parser.add_argument('-b', '--repo-base', help="Repo base directory (default: %(default)s)", default='/var/www/repos/public/ondemand')
parser.add_argument('-m', '--manifest', help="Release manifest path (default: %(default)s)", default=os.path.join(os.path.dirname(__file__), 'release-manifest.yaml'))
parser.add_argument('-r', '--release', help="Release version", required=True)
parser.add_argument('-g','--gpgpass', help='GPG passphrase file (default: %(default)s)', default=gpgpass)
args = parser.parse_args()
if args.debug:
log_level = logging.DEBUG
else:
log_level = logging.INFO
logger.setLevel(log_level)
ch = logging.StreamHandler()
formatter = logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
user = getpass.getuser()
if user != 'oodpkg':
logger.error("Only run this script as oodpkg, not %s", user)
sys.exit(1)
latest_dir = os.path.join(args.repo_base, 'latest')
release_dir = os.path.join(args.repo_base, args.release)
rpms = []
debs = []
manifest = {}
manifest_data = {}
copied_manifest = {}
# Read manifest
if os.path.isfile(args.manifest):
with open(args.manifest, 'r') as f:
manifest_data = yaml.load(f)
else:
logger.error("Manifest %s not found", args.manifest)
# Get global values
major = manifest_data.get('major', args.release)
full = manifest_data.get('full', None)
runtime = manifest_data.get('runtime', None)
if full is None:
logger.error("Unable to determine full version of ondemand")
sys.exit(1)
# Build manifest
for k,v in manifest_data.items():
if k in ['major','full','runtime']:
continue
name = k.format(major=major, full=full, runtime=runtime)
if isinstance(v, dict):
for p in v['packages']:
name = p.format(major=major, full=full, runtime=runtime)
manifest[name] = []
for val in v['versions']:
value = val.format(major=major, full=full, runtime=runtime)
manifest[name].append(value)
else:
manifest[name] = []
for val in v:
value = val.format(major=major, full=full, runtime=runtime)
manifest[name].append(value)
# Print manifest
logger.debug("MANIFEST:")
for name, versions in sorted(manifest.items(), key=lambda item: item[0]):
logger.debug("%s: %s", name, ','.join(versions))
logger.debug("END MANIFEST:")
#sys.exit(0)
# Prep release directories
for t in ['compute', 'web']:
for rel in EL_RELEASES:
rel_d = os.path.join(release_dir, t, rel)
if not os.path.isdir(rel_d):
logger.info("mkdir -p %s", rel_d)
os.makedirs(rel_d, 0o755)
rel_l = "%sServer" % rel_d
if not os.path.islink(rel_l):
logger.info("ln -s %s %s", rel, rel_l)
os.symlink(rel, rel_l)
for arch in ['SRPMS', 'x86_64', 'aarch64', 'ppc64le']:
d = os.path.join(release_dir, t, rel, arch)
if not os.path.isdir(d):
logger.info("mkdir -p %s", d)
os.makedirs(d, 0o755)
for rel in BASIC_RELEASES:
rel_d = os.path.join(release_dir, t, rel)
if not os.path.isdir(rel_d):
logger.info("mkdir -p %s", rel_d)
os.makedirs(rel_d, 0o755)
for arch in ['SRPMS', 'x86_64', 'aarch64', 'ppc64le']:
d = os.path.join(release_dir, t, rel, arch)
if not os.path.isdir(d):
logger.info("mkdir -p %s", d)
os.makedirs(d, 0o755)
for rel in DEB_CODENAMES:
pool_d = os.path.join(release_dir, t, 'apt/pool', rel)
if not os.path.isdir(pool_d):
logger.info("mkdir -p %s", pool_d)
os.makedirs(pool_d, 0o755)
for arch in ['binary-amd64', 'binary-arm64', 'binary-ppc64el']:
rel_d = os.path.join(release_dir, t, 'apt/dists', rel, 'main', arch)
if not os.path.isdir(rel_d):
logger.info("mkdir -p %s", rel_d)
os.makedirs(rel_d, 0o755)
if args.release in ['latest','ci','nightly'] or args.release.startswith('build'):
logger.info("Latest release does not require sync, exiting")
sys.exit(0)
# Get lists of all SRPMs and RPMs
for root, dirnames, filenames in os.walk(latest_dir):
skip = False
for skips in SKIP:
if skips in root.split('/'):
skip = True
if skip:
logger.debug("SKIP %s", root)
continue
for filename in filenames:
f = os.path.join(root, filename)
if f.endswith('.rpm'):
rpms.append(f)
elif f.endswith('.deb'):
debs.append(f)
# Determine if SRPM/RPM needs to be copie to release repo
# If a given SRPM/RPM is in the manifest, copy to release repo
for r in sorted(rpms):
copy = True
rpm_info = get_rpm_info(r)
logger.debug("RPM info for %s: %s", r, rpm_info)
name = rpm_info['name']
if name not in manifest:
logger.warning("%s not in manifest", name)
continue
version = rpm_info['ver'].replace('.el8', '').replace('.el9', '').replace('.amzn2023', '')
if version not in manifest[name]:
logger.debug("Skipping %s-%s, not in manifest", name, version)
continue
dest = r.replace('/latest/', "/%s/" % args.release)
if os.path.isfile(dest) and not args.force:
copy = False
logger.debug("%s already exists, skipping", dest)
if name in copied_manifest:
if version not in copied_manifest[name]:
copied_manifest[name].append(version)
else:
copied_manifest[name] = [version]
if not copy:
continue
logger.info("Copy %s -> %s", r, dest)
shutil.copy2(r, dest)
for d in sorted(debs):
copy = True
deb_info = get_deb_info(d)
logger.debug("DEB info for %s: %s", d, deb_info)
name = deb_info['Package']
version = deb_info['Version']
if name not in manifest:
logger.warning("%s not in manifest", name)
continue
if version not in manifest[name]:
logger.debug("Skipping %s-%s, not in manifest", name, version)
continue
dest = d.replace('/latest/', "/%s/" % args.release)
if os.path.isfile(dest) and not args.force:
copy = False
logger.debug("%s already exists, skipping", dest)
if name in copied_manifest:
if version not in copied_manifest[name]:
copied_manifest[name].append(version)
else:
copied_manifest[name] = [version]
if not copy:
continue
logger.info("Copy %s -> %s", d, dest)
shutil.copy2(d, dest)
# Check for files in manifest that were not found in latest repo
for name, versions in sorted(manifest.items(), key=lambda item: item[0]):
if name not in copied_manifest:
logger.error("Manifest item %s not found", name)
continue
for version in versions:
if version not in copied_manifest[name]:
logger.error("Manifest item version %s-%s not found", name, version)
# Look for files in release repo that are not in manifest
for root, dirnames, filenames in os.walk(release_dir):
for filename in filenames:
f = os.path.join(root, filename)
found = False
if filename.endswith('.rpm'):
found = True
rpm_info = get_rpm_info(f)
name = rpm_info['name']
version = rpm_info['ver'].replace('.el8', '').replace('.el9', '').replace('.amzn2023', '')
elif filename.endswith('.deb'):
found = True
deb_info = get_deb_info(f)
name = deb_info['Package']
version = deb_info['Version']
if found and name not in manifest or version not in manifest.get(name, []):
logger.error("Package %s-%s should not be in release repo", name, version)
if args.clean:
logger.debug("rm %s", f)
os.remove(f)
for dist in DISTS:
for arch in ARCHES:
logger.info("repo-update.sh -r %s -d %s -a %s", args.release, dist, arch)
repo_update_cmd = [
os.path.join(PROJ_ROOT, 'repo-update.sh'),
'-r', args.release,
'-d', dist,
'-a', arch,
]
process = subprocess.Popen(repo_update_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
exit_code = process.returncode
if exit_code != 0:
logger.error("OUTPUT: %s", out)
logger.error("ERROR: %s", err)
if dist not in COMPUTE_DISTS:
continue
logger.info("repo-update.sh -r %s -d %s -a %s -t compute", args.release, dist, arch)
repo_update_cmd = [
os.path.join(PROJ_ROOT, 'repo-update.sh'),
'-r', args.release,
'-d', dist,
'-a', arch,
'-t', 'compute',
]
process = subprocess.Popen(repo_update_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
exit_code = process.returncode
if exit_code != 0:
logger.error("OUTPUT: %s", out)
logger.error("ERROR: %s", err)
if __name__ == '__main__':
main()