Skip to content

Commit

Permalink
gh issue #16: use python stdlib logger instead of oras logger
Browse files Browse the repository at this point in the history
* this allows us to log other libraries such as messages from requests
  • Loading branch information
Vincinator committed Aug 1, 2024
1 parent fa13d47 commit 354f680
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
16 changes: 12 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ install_deps: ## Install dependencies.
$(VENV)/bin/pip install -r requirements.txt

example:
@echo "Push first image..."
@echo "==== DEMO ===="
@echo "=== Push dummy container 1 arm64"
$(PYTHON) -m gloci.cli image push --container localhost:8081/$(EXAMPLECONTAINERNAME):latest --architecture arm64 --cname yolo-example_dev --info_yaml example-data/info_1.yaml
@echo "=== Push dummy container 1 amd64"
$(PYTHON) -m gloci.cli image push --container localhost:8081/$(EXAMPLECONTAINERNAME):latest --architecture amd64 --cname yolo-example_dev --info_yaml example-data/info_1.yaml
@echo "=== Push dummy container 2 arm64"
$(PYTHON) -m gloci.cli image push --container localhost:8081/$(EXAMPLECONTAINERNAME):latest --architecture arm64 --cname yolo2-example_dev --info_yaml example-data/info_2.yaml
@echo "=== Push dummy container 2 amd64"
$(PYTHON) -m gloci.cli image push --container localhost:8081/$(EXAMPLECONTAINERNAME):latest --architecture amd64 --cname yolo2-example_dev --info_yaml example-data/info_2.yaml
@echo "Attach some file to image..."
@echo "=== Attach an Extra file to dummy container 2 arm64"
$(PYTHON) -m gloci.cli image attach --container localhost:8081/$(EXAMPLECONTAINERNAME):latest --cname yolo-example_dev --architecture arm64 --file_path example-data/extras --media_type application/vnd.oci.image.layer.v1.tar
@echo "Inspect final oci image"
@echo "\n"
@echo ""
@echo ""
@echo ""
@echo "=== Inspect oci-index"
$(PYTHON) -m gloci.cli image inspect-index --container localhost:8081/$(EXAMPLECONTAINERNAME):latest
@echo "=== Inspect single manigest"
$(PYTHON) -m gloci.cli image inspect --container localhost:8081/$(EXAMPLECONTAINERNAME):latest --cname yolo-example_dev --architecture arm64
@echo "=== Inspect single manigest"
$(PYTHON) -m gloci.cli image inspect --container localhost:8081/$(EXAMPLECONTAINERNAME):latest --cname yolo-example_dev --architecture amd64
21 changes: 8 additions & 13 deletions src/gloci/commands/image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# my_project/commands/image.py
import json
import click
import oras.client
import oras.container
Expand Down Expand Up @@ -33,18 +34,13 @@ def image():
"--info_yaml",
required=True,
type=click.Path(),
help="info.yaml file of the Garden Linux flavor. The info.yaml specifies the data (layers) to expect to "
"be attached later.",
help="info.yaml file of the Garden Linux flavor. The info.yaml specifies the data (layers)",
)
def push(container_name, architecture, cname, info_yaml):
container = oras.container.Container(container_name)
registry = GlociRegistry(container.registry)

# TODO: cname can be computed from info_yaml content,
# but for consistency we let the caller who knows the cname already provide the cname

# Create and Push image manifest
registry.push_image_manifest(container_name, architecture, cname, info_yaml)
click.echo(f"Pushed {container_name}")


@image.command()
Expand Down Expand Up @@ -87,7 +83,6 @@ def remove():
@click.option("--container", required=True, help="oci image reference")
def status(container):
"""Get status of image"""
click.echo(f"Requesting status of {container}")
container = oras.container.Container(container)
registry = GlociRegistry(container.registry)
registry.status_all(container)
Expand All @@ -101,9 +96,9 @@ def inspect(container, cname, architecture):
"""inspect container"""
container = oras.container.Container(container)
registry = GlociRegistry(container.registry)
pprint.pprint(
registry.get_manifest_by_cname(container, cname, architecture), compact=True
)
print(json.dumps(
registry.get_manifest_by_cname(container, cname, architecture), indent=4
))


@image.command()
Expand All @@ -112,10 +107,10 @@ def inspect_index(container):
"""inspects complete index"""
container = oras.container.Container(container)
registry = GlociRegistry(container.registry)
pprint.pprint(registry.get_index(container), compact=True)
json.dumps(registry.get_index(container), indent=4)


@image.command()
def list():
"""List available images with status"""
click.echo(f"Requesting status of all available images")
click.echo("Requesting status of all available images")
20 changes: 3 additions & 17 deletions src/gloci/oras/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import oras.client
import oras.utils
from oras.decorator import ensure_container
from oras.logger import setup_logger, logger

import logging
import jsonschema
import json
import requests
Expand Down Expand Up @@ -48,8 +48,8 @@ class ManifestState(Enum):
Final = auto()


setup_logger(quiet=False, debug=True)

logger = logging.getLogger(__name__)
logging.basicConfig(filename='gl-ci.log', level=logging.DEBUG)

def attach_state(d: dict, state):
d["image_state"] = state
Expand Down Expand Up @@ -242,7 +242,6 @@ def change_state(self, container_name, cname, architecture, new_state):
attach_state(manifest["annotations"], new_state)

def attach_layer(self, container_name, cname, architecture, file_path, media_type):
# File Blobs must exist
if not os.path.exists(file_path):
logger.exit(f"{file_path} does not exist.")

Expand All @@ -257,15 +256,11 @@ def attach_layer(self, container_name, cname, architecture, file_path, media_typ
if cur_state == "SIGNED":
logger.exit("Manifest is already signed. Manifest is read-only now")

# Create a new layer from the blob
layer = oras.oci.NewLayer(file_path, media_type, is_dir=False)
new_layer_size = int(layer["size"])
# annotations = annotations.get_annotations(blob)
layer["annotations"] = {
oras.defaults.annotation_title: os.path.basename(file_path)
}

# update the manifest with the new layer
manifest["layers"].append(layer)

old_manifest_digest = self.get_digest(manifest_container)
Expand All @@ -287,10 +282,6 @@ def attach_layer(self, container_name, cname, architecture, file_path, media_typ
def remove_container(self, container):
self.delete_tag(container.manifest_url())

@ensure_container
def status_manifest(self, container, manifest_id):
pass

@ensure_container
def status_all(self, container):
"""
Expand Down Expand Up @@ -404,7 +395,6 @@ def push_image_manifest(self, container_name, architecture, cname, info_yaml):
checksum_sha1 = calculate_sha1(file_path)
checksum_md5 = calculate_md5(file_path)

# File Blobs must exist
if not os.path.exists(file_path):
logger.error(f"{file_path} does not exist.")
continue
Expand All @@ -414,7 +404,6 @@ def push_image_manifest(self, container_name, architecture, cname, info_yaml):
file_path = oras.utils.make_targz(file_path)
cleanup_blob = True

# Create a new layer from the blob
layer = oras.oci.NewLayer(file_path, media_type, is_dir=cleanup_blob)
total_size += int(layer["size"])

Expand All @@ -427,14 +416,11 @@ def push_image_manifest(self, container_name, architecture, cname, info_yaml):
if annotations:
layer["annotations"].update(annotations)

# update the manifest with the new layer
manifest_image["layers"].append(layer)

# Upload the blob layer
response = self.upload_blob(file_path, container, layer)
self._check_200_response(response)

# Do we need to clean up a temporary targz?
if cleanup_blob and os.path.exists(file_path):
os.remove(file_path)

Expand Down

0 comments on commit 354f680

Please sign in to comment.