Skip to content

Commit 81c122d

Browse files
NotTheEvilOneyeoldegrove
authored andcommitted
Restructure gardenlinux.oci to clean up code and logic
Signed-off-by: Tobias Wolf <[email protected]>
1 parent a765148 commit 81c122d

File tree

14 files changed

+1032
-912
lines changed

14 files changed

+1032
-912
lines changed

src/gardenlinux/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,4 @@
166166

167167
OCI_ANNOTATION_SIGNATURE_KEY = "io.gardenlinux.oci.signature"
168168
OCI_ANNOTATION_SIGNED_STRING_KEY = "io.gardenlinux.oci.signed-string"
169+
OCI_IMAGE_INDEX_MEDIA_TYPE = "application/vnd.oci.image.index.v1+json"

src/gardenlinux/features/cname.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from ..constants import ARCHS
77

8+
from .parser import Parser
9+
810

911
class CName(object):
1012
def __init__(self, cname, arch=None, version=None):
@@ -66,6 +68,10 @@ def commit_id(self) -> Optional[str]:
6668
def flavor(self) -> str:
6769
return self._flavor
6870

71+
@property
72+
def feature_set(self) -> str:
73+
return Parser().filter_as_string(self.flavor)
74+
6975
@property
7076
def version(self) -> Optional[str]:
7177
return self._version

src/gardenlinux/features/parser.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818

1919

2020
class Parser(object):
21+
_GARDENLINUX_ROOT: str = "."
22+
2123
def __init__(
2224
self,
23-
gardenlinux_root: str = ".",
24-
feature_dir_name: str = "features",
25+
gardenlinux_root: Optional[str] = None,
26+
feature_dir_name: Optional[str] = "features",
2527
logger: Optional[logging.Logger] = None,
2628
):
29+
if gardenlinux_root is None:
30+
gardenlinux_root = Parser._GARDENLINUX_ROOT
31+
2732
feature_base_dir = os.path.join(gardenlinux_root, feature_dir_name)
2833

2934
if not os.access(feature_base_dir, os.R_OK):
@@ -248,6 +253,10 @@ def filter_func(a, b):
248253
def _get_graph_node_type(node):
249254
return node.get("content", {}).get("type")
250255

256+
@staticmethod
257+
def set_default_gardenlinux_root_dir(root_dir):
258+
Parser._GARDENLINUX_ROOT = root_dir
259+
251260
@staticmethod
252261
def sort_graph_nodes(graph):
253262
def key_function(node):

src/gardenlinux/git/git.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# -*- coding: utf-8 -*-
22

3+
import sys
4+
from git import Repo
35
from git import Git as _Git
6+
from os import PathLike
47
from pathlib import Path
5-
import sys
68

79
from ..logger import LoggerSetup
810

911

10-
class Git:
12+
class Git(object):
1113
"""Git operations handler."""
1214

13-
def __init__(self, logger=None):
15+
def __init__(self, git_directory=".", logger=None):
1416
"""Initialize Git handler.
1517
1618
Args:
@@ -20,11 +22,31 @@ def __init__(self, logger=None):
2022
if logger is None or not logger.hasHandlers():
2123
logger = LoggerSetup.get_logger("gardenlinux.git")
2224

25+
if not isinstance(git_directory, PathLike):
26+
git_directory = Path(git_directory)
27+
28+
if not git_directory.is_dir():
29+
raise RuntimeError(f"Git directory given is invalid: {git_directory}")
30+
31+
self._git_directory = git_directory
2332
self._logger = logger
2433

25-
def get_root(self):
34+
@property
35+
def commit_id(self):
36+
"""Get the commit ID for Git `HEAD`."""
37+
return str(self.root_repo.head.commit)
38+
39+
@property
40+
def root(self):
2641
"""Get the root directory of the current Git repository."""
27-
root_dir = Git(".").rev_parse("--show-superproject-working-tree")
28-
self.log.debug(f"Git root directory: {root_dir}")
42+
root_dir = _Git(self._git_directory).rev_parse(
43+
"--show-superproject-working-tree"
44+
)
2945

46+
self._logger.debug(f"Git root directory: {root_dir}")
3047
return Path(root_dir)
48+
49+
@property
50+
def root_repo(self):
51+
"""Get the root Git `Repo` instance."""
52+
return Repo(self.root)

src/gardenlinux/oci/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
# -*- coding: utf-8 -*-
2+
3+
from .container import Container
4+
from .index import Index
5+
from .layer import Layer
6+
from .manifest import Manifest
7+
8+
__all__ = ["Container", "Index", "Layer", "Manifest"]

src/gardenlinux/oci/__main__.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from pygments.lexer import default
77

8-
from .registry import GlociRegistry
8+
from .container import Container
99

1010

1111
@click.group()
@@ -26,6 +26,13 @@ def cli():
2626
type=click.Path(),
2727
help="Version of image",
2828
)
29+
@click.option(
30+
"--commit",
31+
required=False,
32+
type=click.Path(),
33+
default=None,
34+
help="Commit of image",
35+
)
2936
@click.option(
3037
"--arch",
3138
required=True,
@@ -60,6 +67,7 @@ def cli():
6067
def push_manifest(
6168
container,
6269
version,
70+
commit,
6371
arch,
6472
cname,
6573
directory,
@@ -69,17 +77,19 @@ def push_manifest(
6977
additional_tag,
7078
):
7179
"""push artifacts from a dir to a registry, get the index-entry for the manifest in return"""
72-
container_name = f"{container}:{version}"
73-
registry = GlociRegistry(
74-
container_name=container_name,
75-
token=os.getenv("GL_CLI_REGISTRY_TOKEN"),
80+
container = Container(
81+
f"{container}:{version}",
7682
insecure=insecure,
7783
)
78-
digest = registry.push_from_dir(
79-
arch, version, cname, directory, manifest_file, additional_tag
84+
85+
manifest = container.read_or_generate_manifest(cname, arch, version, commit)
86+
87+
container.push_manifest_and_artifacts_from_directory(
88+
manifest, directory, manifest_file, additional_tag
8089
)
90+
8191
if cosign_file:
82-
print(digest, file=open(cosign_file, "w"))
92+
print(manifest.digest, file=open(cosign_file, "w"))
8393

8494

8595
@cli.command()
@@ -115,13 +125,12 @@ def push_manifest(
115125
)
116126
def update_index(container, version, manifest_folder, insecure, additional_tag):
117127
"""push a index entry from a list of files to an index"""
118-
container_name = f"{container}:{version}"
119-
registry = GlociRegistry(
120-
container_name=container_name,
121-
token=os.getenv("GL_CLI_REGISTRY_TOKEN"),
128+
container = Container(
129+
f"{container}:{version}",
122130
insecure=insecure,
123131
)
124-
registry.update_index(manifest_folder, additional_tag)
132+
133+
container.push_index_from_directory(manifest_folder, additional_tag)
125134

126135

127136
def main():

0 commit comments

Comments
 (0)