Skip to content

Commit ad9f1ba

Browse files
committed
Implement gl-metadata to generate release files
Signed-off-by: Tobias Wolf <[email protected]>
1 parent 760562a commit ad9f1ba

File tree

3 files changed

+125
-29
lines changed

3 files changed

+125
-29
lines changed

src/gardenlinux/constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@
141141
"secureboot.aws-efivars": "application/io.gardenlinux.cert.secureboot.aws-efivars",
142142
}
143143

144+
GL_BUG_REPORT_URL = "https://github.com/gardenlinux/gardenlinux/issues"
145+
GL_COMMIT_SPECIAL_VALUES = ("local",)
146+
GL_DISTRIBUTION_NAME = "Garden Linux"
147+
GL_HOME_URL = "https://gardenlinux.io"
148+
GL_RELEASE_ID = "gardenlinux"
144149
GL_REPOSITORY_URL = "https://github.com/gardenlinux/gardenlinux"
150+
GL_SUPPORT_URL = "https://github.com/gardenlinux/gardenlinux"
145151

146152
OCI_ANNOTATION_SIGNATURE_KEY = "io.gardenlinux.oci.signature"
147153
OCI_ANNOTATION_SIGNED_STRING_KEY = "io.gardenlinux.oci.signed-string"

src/gardenlinux/features/cname.py

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@
1010
from os import PathLike
1111
import re
1212

13-
from ..constants import ARCHS
13+
from ..constants import (
14+
ARCHS,
15+
GL_BUG_REPORT_URL,
16+
GL_COMMIT_SPECIAL_VALUES,
17+
GL_DISTRIBUTION_NAME,
18+
GL_HOME_URL,
19+
GL_RELEASE_ID,
20+
GL_SUPPORT_URL,
21+
)
22+
1423
from .parser import Parser
1524

1625

@@ -86,7 +95,7 @@ def __init__(self, cname, arch=None, commit_hash=None, version=None):
8695
if commit_id_or_hash is not None:
8796
self._commit_id = commit_id_or_hash[:8]
8897

89-
if len(commit_id_or_hash) == 40: # sha1 hex
98+
if len(commit_id_or_hash) == 40: # sha1 hex
9099
self._commit_hash = commit_id_or_hash
91100

92101
@property
@@ -129,7 +138,9 @@ def commit_hash(self) -> str:
129138
"""
130139

131140
if self._commit_hash is None:
132-
raise RuntimeError("GardenLinux canonical name given does not contain the commit hash")
141+
raise RuntimeError(
142+
"GardenLinux canonical name given does not contain the commit hash"
143+
)
133144

134145
return self._commit_hash
135146

@@ -185,6 +196,42 @@ def feature_set(self) -> str:
185196

186197
return Parser().filter_as_string(self.flavor)
187198

199+
@property
200+
def metadata_string(self) -> str:
201+
"""
202+
Returns the metadata describing the given CName instance.
203+
204+
:return: (str) Metadata describing the given CName instance
205+
:since: 0.9.2
206+
"""
207+
208+
features = Parser().filter_as_dict(self.flavor)
209+
210+
elements = ",".join(features["element"])
211+
flags = ",".join(features["flag"])
212+
platforms = ",".join(features["platform"])
213+
214+
metadata = f"""
215+
ID={GL_RELEASE_ID}
216+
NAME="{GL_DISTRIBUTION_NAME}"
217+
PRETTY_NAME="{GL_DISTRIBUTION_NAME} {self.version}"
218+
IMAGE_VERSION={self.version}
219+
VARIANT_ID="{self.flavor}-{self.arch}"
220+
HOME_URL="{GL_HOME_URL}"
221+
SUPPORT_URL="{GL_SUPPORT_URL}"
222+
BUG_REPORT_URL="{GL_BUG_REPORT_URL}"
223+
GARDENLINUX_CNAME="{self.cname}"
224+
GARDENLINUX_FEATURES="{self.feature_set}"
225+
GARDENLINUX_FEATURES_PLATFORMS="{platforms}"
226+
GARDENLINUX_FEATURES_ELEMENTS="{elements}"
227+
GARDENLINUX_FEATURES_FLAGS="{flags}"
228+
GARDENLINUX_VERSION="{self.version}"
229+
GARDENLINUX_COMMIT_ID="{self.commit_id}"
230+
GARDENLINUX_COMMIT_ID_LONG="{self.commit_hash}"
231+
""".strip()
232+
233+
return metadata
234+
188235
@property
189236
def platform(self) -> str:
190237
"""
@@ -320,30 +367,5 @@ def save_to_metadata_file(
320367
f"Refused to overwrite existing metadata file: {metadata_file}"
321368
)
322369

323-
features = Parser().filter_as_dict(self.flavor)
324-
325-
elements = ",".join(features["element"])
326-
flags = ",".join(features["flag"])
327-
platforms = ",".join(features["platform"])
328-
329-
metadata = f"""
330-
ID=gardenlinux
331-
NAME="Garden Linux"
332-
PRETTY_NAME="Garden Linux {self.version}"
333-
IMAGE_VERSION={self.version}
334-
VARIANT_ID="{self.flavor}-{self.arch}"
335-
HOME_URL="https://gardenlinux.io"
336-
SUPPORT_URL="https://github.com/gardenlinux/gardenlinux"
337-
BUG_REPORT_URL="https://github.com/gardenlinux/gardenlinux/issues"
338-
GARDENLINUX_CNAME="{self.cname}"
339-
GARDENLINUX_FEATURES="{self.feature_set}"
340-
GARDENLINUX_FEATURES_PLATFORMS="{platforms}"
341-
GARDENLINUX_FEATURES_ELEMENTS="{elements}"
342-
GARDENLINUX_FEATURES_FLAGS="{flags}"
343-
GARDENLINUX_VERSION="{self.version}"
344-
GARDENLINUX_COMMIT_ID="{self.commit_id}"
345-
GARDENLINUX_COMMIT_ID_LONG="{self.commit_hash}"
346-
""".strip()
347-
348370
with metadata_file.open("w") as fp:
349-
fp.write(metadata)
371+
fp.write(self.metadata_string)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
gl-metadata main entrypoint
6+
"""
7+
8+
from functools import reduce
9+
from os.path import basename, dirname
10+
import argparse
11+
import logging
12+
import re
13+
14+
from .cname import CName
15+
from .parser import Parser
16+
17+
from .__main__ import (
18+
get_cname_base,
19+
get_minimal_feature_set,
20+
get_version_and_commit_id_from_files,
21+
sort_subset,
22+
)
23+
24+
25+
_ARGS_ACTION_ALLOWED = [
26+
"output-as-json",
27+
"write",
28+
]
29+
30+
31+
def main():
32+
"""
33+
gl-metadata main()
34+
35+
:since: 0.7.0
36+
"""
37+
38+
parser = argparse.ArgumentParser()
39+
40+
parser.add_argument("--arch", dest="arch")
41+
parser.add_argument("--cname", required=True, dest="cname")
42+
parser.add_argument("--commit-hash", dest="commit_hash")
43+
parser.add_argument("--release-file", dest="release_file")
44+
parser.add_argument("--overwrite-file", type=bool, dest="overwrite_file")
45+
parser.add_argument("--version", dest="version")
46+
47+
parser.add_argument(
48+
"action", nargs="?", choices=_ARGS_ACTION_ALLOWED, default="output-as-json"
49+
)
50+
51+
args = parser.parse_args()
52+
53+
cname = CName(
54+
args.cname, arch=args.arch, commit_hash=args.commit_hash, version=args.version
55+
)
56+
57+
if args.commit_hash is not None:
58+
cname.commit_hash = args.commit_hash
59+
60+
if args.action == "write":
61+
cname.save_to_metadata_file(args.release_file, args.overwrite_file)
62+
else:
63+
cname.load_from_metadata_file(args.release_file)
64+
print(cname.metadata_string)
65+
66+
67+
if __name__ == "__main__":
68+
main()

0 commit comments

Comments
 (0)