Skip to content

Commit ceade8a

Browse files
committed
Add support to read existing flavors.yaml with gl-flavors-parse
Signed-off-by: Tobias Wolf <[email protected]>
1 parent c635266 commit ceade8a

File tree

1 file changed

+79
-100
lines changed

1 file changed

+79
-100
lines changed

src/python_gardenlinux_lib/flavors/parse_flavors.py

Lines changed: 79 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ def find_repo_root():
2929

3030
return Git(".").rev_parse("--show-superproject-working-tree")
3131

32+
def _get_flavors_from_github(commit):
33+
"""Returns the flavors.yaml from GitHub if readable."""
34+
35+
# Try flavors.yaml first
36+
api_path = "/repos/gardenlinux/gardenlinux/contents/flavors.yaml"
37+
if commit != "latest":
38+
api_path = f"{api_path}?ref={commit}"
39+
command = ["gh", "api", api_path]
40+
logger.debug(f"Fetching flavors.yaml from GitHub for commit {commit_short}")
41+
result = subprocess.run(
42+
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
43+
)
44+
45+
if result.returncode == 0:
46+
content_data = json.loads(result.stdout)
47+
return base64.b64decode(content_data["content"]).decode("utf-8")
48+
else:
49+
raise RuntimeError("Failed receiving result from GitHub: {0}".format(result.stderr))
50+
51+
3252
def parse_flavors_commit(
3353
commit=None,
3454
version=None,
@@ -65,13 +85,6 @@ def parse_flavors_commit(
6585
Returns:
6686
list: List of flavor strings, or empty list if no flavors found
6787
"""
68-
try:
69-
version_info = (
70-
f"{version['major']}.{version.get('minor', 0)}" if version else "unknown"
71-
)
72-
if commit is None:
73-
commit = "latest"
74-
commit_short = commit[:8]
7588

7689
if logger is None:
7790
logger = logging.getLogger("gardenlinux.lib.flavors")
@@ -113,99 +126,65 @@ def parse_flavors_commit(
113126
exclude_categories=exclude_categories or [],
114127
)
115128

116-
# Try flavors.yaml first
117-
api_path = "/repos/gardenlinux/gardenlinux/contents/flavors.yaml"
118-
if commit != "latest":
119-
api_path = f"{api_path}?ref={commit}"
120-
command = ["gh", "api", api_path]
121-
logger.debug(f"Fetching flavors.yaml from GitHub for commit {commit_short}")
122-
result = subprocess.run(
123-
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
124-
)
125-
126-
if result.returncode == 0:
127-
content_data = json.loads(result.stdout)
128-
yaml_content = base64.b64decode(content_data["content"]).decode("utf-8")
129-
flavors_data = yaml.safe_load(yaml_content)
130-
131-
# Parse flavors with all filters
132-
combinations = parse_flavors_data(
133-
flavors_data,
134-
include_only_patterns=include_only_patterns or [],
135-
wildcard_excludes=wildcard_excludes or [],
136-
only_build=only_build,
137-
only_test=only_test,
138-
only_test_platform=only_test_platform,
139-
only_publish=only_publish,
140-
filter_categories=filter_categories or [],
141-
exclude_categories=exclude_categories or [],
129+
all_flavors = set()
130+
for _, combination in combinations:
131+
all_flavors.add(combination)
132+
133+
if all_flavors:
134+
logger.info(f"Found {len(all_flavors)} flavors in flavors.yaml")
135+
return sorted(all_flavors)
136+
else:
137+
logger.info("No flavors found in flavors.yaml")
138+
elif query_s3 and s3_objects and isinstance(s3_objects, dict):
139+
logger.debug("Checking S3 artifacts")
140+
index = s3_objects.get("index", {})
141+
artifacts = s3_objects.get("artifacts", [])
142+
143+
# Try index lookup first
144+
search_key = f"{version_info}-{commit_short}"
145+
if search_key in index:
146+
flavors = index[search_key]
147+
logger.debug(f"Found flavors in S3 index for {search_key}")
148+
else:
149+
# If no index match, search through artifacts
150+
found_flavors = set()
151+
152+
# Search for artifacts matching version and commit
153+
for key in artifacts:
154+
if version_info in key and commit_short in key:
155+
try:
156+
parts = key.split("/")
157+
if len(parts) >= 2:
158+
flavor_with_version = parts[1]
159+
flavor = flavor_with_version.rsplit(
160+
"-" + version_info, 1
161+
)[0]
162+
if flavor:
163+
found_flavors.add(flavor)
164+
except Exception as e:
165+
logger.debug(f"Error parsing artifact key {key}: {e}")
166+
continue
167+
168+
flavors = list(found_flavors)
169+
170+
# Apply filters to S3 flavors
171+
filtered_flavors = []
172+
for flavor in flavors:
173+
# Create a dummy combination with amd64 architecture for filtering
174+
combination = ("amd64", flavor)
175+
if should_include_only(
176+
flavor, include_only_patterns or []
177+
) and not should_exclude(flavor, wildcard_excludes or [], []):
178+
filtered_flavors.append(flavor)
179+
180+
if filtered_flavors:
181+
logger.info(
182+
f"Found {len(filtered_flavors)} flavors in S3 artifacts after filtering"
183+
)
184+
return sorted(filtered_flavors)
185+
else:
186+
logger.info(
187+
f"No flavors found in S3 for version {version_info} and commit {commit_short} after filtering"
142188
)
143189

144-
all_flavors = set()
145-
for _, combination in combinations:
146-
all_flavors.add(combination)
147-
148-
if all_flavors:
149-
logger.info(f"Found {len(all_flavors)} flavors in flavors.yaml")
150-
return sorted(all_flavors)
151-
else:
152-
logger.info("No flavors found in flavors.yaml")
153-
154-
# If no flavors.yaml found and query_s3 is enabled, try S3 artifacts
155-
if query_s3 and s3_objects and isinstance(s3_objects, dict):
156-
logger.debug("Checking S3 artifacts")
157-
index = s3_objects.get("index", {})
158-
artifacts = s3_objects.get("artifacts", [])
159-
160-
# Try index lookup first
161-
search_key = f"{version_info}-{commit_short}"
162-
if search_key in index:
163-
flavors = index[search_key]
164-
logger.debug(f"Found flavors in S3 index for {search_key}")
165-
else:
166-
# If no index match, search through artifacts
167-
found_flavors = set()
168-
169-
# Search for artifacts matching version and commit
170-
for key in artifacts:
171-
if version_info in key and commit_short in key:
172-
try:
173-
parts = key.split("/")
174-
if len(parts) >= 2:
175-
flavor_with_version = parts[1]
176-
flavor = flavor_with_version.rsplit(
177-
"-" + version_info, 1
178-
)[0]
179-
if flavor:
180-
found_flavors.add(flavor)
181-
except Exception as e:
182-
logger.debug(f"Error parsing artifact key {key}: {e}")
183-
continue
184-
185-
flavors = list(found_flavors)
186-
187-
# Apply filters to S3 flavors
188-
filtered_flavors = []
189-
for flavor in flavors:
190-
# Create a dummy combination with amd64 architecture for filtering
191-
combination = ("amd64", flavor)
192-
if should_include_only(
193-
flavor, include_only_patterns or []
194-
) and not should_exclude(flavor, wildcard_excludes or [], []):
195-
filtered_flavors.append(flavor)
196-
197-
if filtered_flavors:
198-
logger.info(
199-
f"Found {len(filtered_flavors)} flavors in S3 artifacts after filtering"
200-
)
201-
return sorted(filtered_flavors)
202-
else:
203-
logger.info(
204-
f"No flavors found in S3 for version {version_info} and commit {commit_short} after filtering"
205-
)
206-
207-
return []
208-
209-
except Exception as e:
210-
logger.error(f"Error parsing flavors for commit {commit_short}: {e}")
211190
return []

0 commit comments

Comments
 (0)