2
2
"""
3
3
The generate_common_versions.py generates all variables for the common_versions.sh script and prints them to stdout.
4
4
"""
5
+
5
6
import requests
6
7
import subprocess
7
8
import os
8
9
import sys
9
10
import argparse
11
+ import logging
10
12
11
13
ARCH = os .uname ().machine
12
14
15
+ # The version of Sonobuoy package used in CNCF tests.
16
+ # See https://github.com/vmware-tanzu/sonobuoy/releases.
13
17
CNCF_SONOBUOY_VERSION = "v0.57.3"
14
18
19
+ logging .basicConfig (
20
+ level = logging .INFO ,
21
+ format = '%(levelname)s: %(message)s' ,
22
+ stream = sys .stderr
23
+ )
24
+
15
25
16
26
def get_candidate_repo_url (minor , dev_preview = False ):
17
27
"""
18
- :param minor: the minor version, e.g. 19 for 4.19
19
- :param dev_preview: if True, returns the engineering candidate repo, otherwise returns the release candidate repo
28
+ Get the URL of the engineering or release candidate repository.
29
+
30
+ Args:
31
+ minor (int): The minor version, e.g., 19 for version 4.19.
32
+ dev_preview (bool): If True, return the engineering candidate repo URL;
33
+ otherwise, return the release candidate repo URL.
20
34
21
- ``get_candidate_repo_url`` returns the URL of the EC/RC repository for the specified minor version.
35
+ Returns:
36
+ str: The URL of the candidate repository.
22
37
"""
23
38
return f"https://mirror.openshift.com/pub/openshift-v4/{ ARCH } /microshift/ocp{ '-dev-preview' if dev_preview else '' } /latest-4.{ minor } /el9/os"
24
39
25
40
26
- def get_dependencies_repo_url (minor , prev = None ):
41
+ def get_dependencies_repo_url (minor , steps_back = 0 ):
27
42
"""
28
- :param minor: the minor version, e.g. 19. for 4.19
29
- :param prev: specifies how many previous minor versions to try if current is unavailable
43
+ Get the URL of the beta repository for the specified minor version.
30
44
31
- ``get_dependencies_repo_url`` returns the URL of the beta repository for the specified
32
- minor version. If the repo for the wanted version does not exist or it does not provide
33
- the necessary packages, it looks for previous releases, for up to `prev` previous minors.
34
- If `prev` is not specified, it only checks the current minor.
35
- """
45
+ This function constructs and checks the beta repository URL for the given
46
+ minor version (e.g., 4.19). If the repository does not exist or does not
47
+ provide the required packages, it searches previous minor versions—up to
48
+ `steps_back` times—until a valid repository is found.
36
49
37
- count = prev if prev is not None else 0
50
+ Args:
51
+ minor (int): The minor version, e.g., 19 for version 4.19.
52
+ steps_back (int, optional): How many previous minor versions to try
53
+ if the current version is unavailable. Defaults to 0.
38
54
39
- print (f"Getting beta repository for 4.{ minor } , max. { count } previous minors" , file = sys .stderr )
55
+ Returns:
56
+ str or None: The URL of a valid beta repository, or None if none are found.
57
+ """
58
+ logging .info (f"Getting beta repository for 4.{ minor } , max. { steps_back } previous minors" )
40
59
41
- for i in range (minor , minor - count - 1 , - 1 ):
60
+ for i in range (minor , minor - steps_back - 1 , - 1 ):
42
61
url = f"https://mirror.openshift.com/pub/openshift-v4/{ ARCH } /dependencies/rpms/4.{ i } -el9-beta"
43
- if mirror_exists (url ) and provides_pkg (url , "cri-o" ):
44
- print (f"Beta repository found for 4.{ i } " , file = sys . stderr )
62
+ if mirror_exists (url ) and repo_provides_pkg (url , "cri-o" ):
63
+ logging . info (f"Beta repository found for 4.{ i } " )
45
64
return url
46
- print (f"Beta repository for 4.{ i } not found{ ', retrying' if i > minor - count else '' } " )
47
-
65
+ logging .info (f"Beta repository for 4.{ i } not found{ ', trying older minor' if i > minor - steps_back else '' } " )
48
66
return None
49
67
50
68
51
- def provides_pkg (repo , pkg ):
69
+ def repo_provides_pkg (repo , pkg ):
52
70
"""
53
- :param repo: the repository to check
54
- :param pkg: the package to look for
71
+ Check if the repository provides the specified package.
72
+
73
+ Args:
74
+ repo (str): The repository URL or name to check.
75
+ pkg (str): The name of the package to look for.
55
76
56
- ``provides_pkg`` checks if the repository provides the package specified by `pkg`.
77
+ Returns:
78
+ bool: True if the package is available in the repository, False otherwise.
57
79
"""
58
80
args = ['dnf' , 'repoquery' , pkg , '--queryformat' , '%{version}-%{release}' ]
59
81
60
82
if repo .startswith ("https" ):
61
- temp = f"this,{ repo } "
62
- args += ['--disablerepo' , '*' , '--repofrompath' , temp ]
83
+ args += ['--disablerepo' , '*' , '--repofrompath' , f"this,{ repo } " ]
63
84
else :
64
85
args += ['--repo' , repo ]
65
86
@@ -72,9 +93,13 @@ def provides_pkg(repo, pkg):
72
93
73
94
def mirror_exists (repo_url ):
74
95
"""
75
- :param repo_url: the URL of a repository
96
+ Check if a URL points to a valid repository.
76
97
77
- ``mirror_exists`` checks if a URL points to a valid repository.
98
+ Args:
99
+ repo_url (str): The URL of the repository to check.
100
+
101
+ Returns:
102
+ bool: True if the repository exists and is accessible, False otherwise.
78
103
"""
79
104
url = repo_url + "/repodata/repomd.xml"
80
105
r = requests .get (url )
@@ -86,56 +111,75 @@ def mirror_exists(repo_url):
86
111
87
112
def get_subscription_repo_name_if_exists (minor ):
88
113
"""
89
- :param minor: the minor version, e.g. 19 for 4.19
114
+ Get the name of the subscription repository for the specified minor version.
115
+
116
+ If the repository provides the microshift package, return the repository name;
117
+ otherwise, return None.
90
118
91
- ``get_subscription_repo_name_if_exists`` returns the name of the subscription repository
92
- for the specified minor version if the repository provides the microshift package,
93
- otherwise returns None.
119
+ Args:
120
+ minor (int): The minor version, e.g., 19 for 4.19.
121
+
122
+ Returns:
123
+ str or None: The name of the subscription repository, or None if not available.
94
124
"""
95
125
repo = f"rhocp-4.{ minor } -for-rhel-9-{ ARCH } -rpms"
96
126
97
- if provides_pkg (repo , "microshift" ):
127
+ if repo_provides_pkg (repo , "microshift" ):
98
128
return repo
99
129
else :
100
130
return None
101
131
102
132
103
133
def get_microshift_repo (minor ):
104
134
"""
105
- :param minor: the minor version, e.g. 19 for 4.19
135
+ Get the repository for the specified minor version.
106
136
107
- ``get_microshift_repo`` returns the repository for the specified minor version.
108
- It looks for the 'rhocp' stream, release candidate and engineering candidate,
109
- in that order, and checks if they provide the microshift package. If none of
110
- these repositories are available, returns empty string.
111
- """
137
+ This function searches for a repository that provides the microshift package
138
+ for the given minor version. It checks the 'rhocp' stream, then release
139
+ candidate (RC), and finally engineering candidate (EC) repositories—in that
140
+ order. If none are available, it returns an empty string.
141
+
142
+ Args:
143
+ minor (int): The minor version, e.g., 19 for 4.19.
112
144
145
+ Returns:
146
+ str: The repository name or URL if found, otherwise an empty string.
147
+ """
113
148
repo = get_subscription_repo_name_if_exists (minor )
114
149
if repo is not None :
115
- print (f"Found subscription repository for 4.{ minor } " , file = sys . stderr )
150
+ logging . info (f"Found subscription repository for 4.{ minor } " )
116
151
return repo
117
152
118
153
rc = get_candidate_repo_url (minor , dev_preview = False )
119
- if mirror_exists (rc ) and provides_pkg (rc , "microshift" ):
120
- print (f"Found release candidate for 4.{ minor } " , file = sys . stderr )
154
+ if mirror_exists (rc ) and repo_provides_pkg (rc , "microshift" ):
155
+ logging . info (f"Found release candidate for 4.{ minor } " )
121
156
return rc
122
157
123
158
ec = get_candidate_repo_url (minor , dev_preview = True )
124
- if mirror_exists (ec ) and provides_pkg (ec , "microshift" ):
125
- print (f"Found engineering candidate for 4.{ minor } " , file = sys . stderr )
159
+ if mirror_exists (ec ) and repo_provides_pkg (ec , "microshift" ):
160
+ logging . info (f"Found engineering candidate for 4.{ minor } " )
126
161
return ec
127
162
128
- print (f"No repository found for 4.{ minor } " , file = sys . stderr )
163
+ logging . info (f"No repository found for 4.{ minor } " )
129
164
return ""
130
165
131
166
132
167
def get_release_version_string (repo , var_name ):
133
168
"""
134
- :param repo: the name or the URL of the repository
169
+ Get a Bash string calling the appropriate `get_vrel_from_*` function.
170
+
171
+ This function returns a string suitable for use as a Bash variable assignment,
172
+ calling either `get_vrel_from_rhsm` or `get_vrel_from_beta` depending on whether
173
+ the `repo` parameter is a repository name or a URL. If `repo` is neither,
174
+ it returns an empty string.
135
175
136
- ``get_release_version_string`` returns a string to be used as a bash variable,
137
- with a call to the right function depending on whether the `repo` param
138
- is a URL or not. if neither, returns empty double quotes.
176
+ Args:
177
+ repo (str): The name or URL of the repository.
178
+ var_name (str): The name of the Bash variable to use in the command.
179
+
180
+ Returns:
181
+ str: A Bash command string using the appropriate `get_vrel_from_*` function,
182
+ or an empty string if the input is invalid.
139
183
"""
140
184
if repo .startswith ("rhocp" ):
141
185
return f'$(get_vrel_from_rhsm "${{{ var_name } }}")'
@@ -153,43 +197,91 @@ def get_release_version_string(repo, var_name):
153
197
minor_version = args .minor
154
198
previous_minor_version = minor_version - 1
155
199
yminus2_minor_version = minor_version - 2
156
- fake_next_minor_version = minor_version + 1
157
200
158
201
# The current release repository comes from the 'rhocp' stream for release
159
202
# branches, or the OpenShift mirror if only a RC or EC is available. It can
160
203
# be empty, if no candidate for the current minor has been built yet.
204
+ logging .info ("Getting CURRENT_RELEASE_REPO" )
161
205
current_release_repo = get_microshift_repo (minor_version )
162
206
current_release_version = get_release_version_string (current_release_repo , "CURRENT_RELEASE_REPO" )
163
207
164
208
# The previous release repository value should either point to the OpenShift
165
209
# mirror URL or the 'rhocp' repository name.
210
+ logging .info ("Getting PREVIOUS_RELEASE_REPO" )
166
211
previous_release_repo = get_microshift_repo (previous_minor_version )
167
212
previous_release_version = get_release_version_string (previous_release_repo , "PREVIOUS_RELEASE_REPO" )
168
213
169
214
# The y-2 release repository value should either point to the OpenShift
170
215
# mirror URL or the 'rhocp' repository name. It should always come from
171
216
# the 'rhocp' stream.
217
+ logging .info ("Getting YMINUS2_RELEASE_REPO" )
172
218
yminus2_release_repo = get_microshift_repo (yminus2_minor_version )
173
219
yminus2_release_version = get_release_version_string (yminus2_release_repo , "YMINUS2_RELEASE_REPO" )
174
220
175
221
# The 'rhocp_minor_y' variable should be the minor version number, if the
176
222
# current release is available through the 'rhocp' stream, otherwise empty.
177
- rhocp_minor_y = minor_version if get_subscription_repo_name_if_exists ( minor_version ) is not None else '""'
223
+ rhocp_minor_y = minor_version if repo_provides_pkg ( f"rhocp-4. { minor_version } -for-rhel-9- { ARCH } -rpms" , "cri-o" ) else '""'
178
224
179
225
# The beta repository, containing dependencies, should point to the
180
- # OpenShift mirror URL. If the repository for current minor is not
226
+ # OpenShift mirror URL. If the mirror for current minor is not
181
227
# available yet, it should point to an older release.
228
+ logging .info ("Getting RHOCP_MINOR_Y_BETA" )
182
229
rhocp_minor_y_beta = get_dependencies_repo_url (minor_version , 3 )
183
230
184
- # The 'rhocp_minor_y1' should always be the y-1 minor version number.
185
- # The repository for y-1 release should always exist.
186
- rhocp_minor_y1 = previous_minor_version
231
+ # The 'rhocp_minor_y' variable should be the previous minor version number, if
232
+ # the previous release is available through the 'rhocp' stream, otherwise empty.
233
+ rhocp_minor_y1 = previous_minor_version if repo_provides_pkg (f"rhocp-4.{ previous_minor_version } -for-rhel-9-{ ARCH } -rpms" , "cri-o" ) else '""'
234
+
235
+ # The beta repository, containing dependencies, should point to the
236
+ # OpenShift mirror URL. The mirror for previous release should always
237
+ # be available.
238
+ logging .info ("Getting RHOCP_MINOR_Y1_BETA" )
187
239
rhocp_minor_y1_beta = get_dependencies_repo_url (previous_minor_version )
188
240
189
241
# The 'rhocp_minor_y2' should always be the y-2 minor version number.
190
242
rhocp_minor_y2 = yminus2_minor_version
191
243
192
- output = f"""
244
+ output = f"""#!/bin/bash
245
+ set -euo pipefail
246
+
247
+ if [[ "${{BASH_SOURCE[0]}}" == "${{0}}" ]]; then
248
+ echo "This script must be sourced, not executed."
249
+ exit 1
250
+ fi
251
+
252
+ get_vrel_from_beta() {{
253
+ local -r beta_repo="$1"
254
+ local -r beta_vrel=$(\\
255
+ dnf repoquery microshift \\
256
+ --quiet \\
257
+ --queryformat '%{{version}}-%{{release}}' \\
258
+ --disablerepo '*' \\
259
+ --repofrompath "this,${{beta_repo}}" \\
260
+ --latest-limit 1 2>/dev/null \\
261
+ )
262
+ if [ -n "${{beta_vrel}}" ]; then
263
+ echo "${{beta_vrel}}"
264
+ return
265
+ fi
266
+ echo ""
267
+ }}
268
+
269
+ get_vrel_from_rhsm() {{
270
+ local -r rhsm_repo="$1"
271
+ local -r rhsm_vrel=$(\\
272
+ dnf repoquery microshift \\
273
+ --quiet \\
274
+ --queryformat '%{{version}}-%{{release}}' \\
275
+ --repo "${{rhsm_repo}}" \\
276
+ --latest-limit 1 2>/dev/null \\
277
+ )
278
+ if [ -n "${{rhsm_vrel}}" ]; then
279
+ echo "${{rhsm_vrel}}"
280
+ return
281
+ fi
282
+ echo ""
283
+ }}
284
+
193
285
export MINOR_VERSION={ minor_version }
194
286
export PREVIOUS_MINOR_VERSION=$(( "${{MINOR_VERSION}}" - 1 ))
195
287
export YMINUS2_MINOR_VERSION=$(( "${{MINOR_VERSION}}" - 2 ))
0 commit comments