Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and jajreidy committed Oct 30, 2024
1 parent a065198 commit 3c1a735
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 53 deletions.
5 changes: 2 additions & 3 deletions docs/sources/staged.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,15 @@ Will yield instances of :class:`~pushsource.AmiPushItem`.
root/destination/CLOUD_IMAGES/\*
................................

The ``CLOUD_IMAGES`` directory should contain the VMI(s) plus a ``resources.yaml``.
Each directory within ``CLOUD_IMAGES`` should contain one or more VMI(s) plus a
``resources.yaml`` .

The ``resources.yaml`` contains all the information needed for the
images in that folder.

:ref:`cloud_schema` provides a complete reference of the fields which can be set by this
file.

A blank ``staged.yaml`` must be included in the root directory.

Will yield instances of either :class:`~pushsource.AmiPushItem` or
:class:`~pushsource.VHDPushItem`.

Expand Down
14 changes: 7 additions & 7 deletions src/pushsource/_impl/backend/staged/staged_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class TypeHandler(object):
# Decorator for handling specific file directories (e.g. "FILES", "ISOS" etc)
HANDLERS = {}

def __init__(self, type_name):
def __init__(self, type_name, accepts = lambda entry: entry.is_file()):
self.type_name = type_name
self.accepts = accepts

def __call__(self, fn):
TypeHandler.HANDLERS[self.type_name] = fn
TypeHandler.HANDLERS[self.type_name] = (fn, self.accepts)
return fn


Expand All @@ -29,19 +30,18 @@ def __init__(self, *args, **kwargs):
super(StagedBaseMixin, self).__init__(*args, **kwargs)
self._FILE_TYPES = self._FILE_TYPES.copy()
for typename in TypeHandler.HANDLERS:
fn = TypeHandler.HANDLERS[typename]
fn, accepts = TypeHandler.HANDLERS[typename]
bound_fn = partial(fn, self)
self._FILE_TYPES[typename] = partial(
self.__mixin_push_items, delegate=bound_fn
)
self.__mixin_push_items, delegate=bound_fn, accepts=accepts)

def __mixin_push_items(self, leafdir, metadata, delegate):
def __mixin_push_items(self, leafdir, metadata, delegate, accepts):
out = []

LOG.debug("Looking for files in %s", leafdir)

for entry in scandir(leafdir.path):
if entry.is_file():
if accepts(entry):
item = delegate(leafdir, metadata, entry)
if item:
out.append(item)
Expand Down
77 changes: 39 additions & 38 deletions src/pushsource/_impl/backend/staged/staged_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@


class StagedCloudMixin(StagedBaseMixin):
def __build_ami_push_item(self, resources, src, origin, name):
def __build_ami_push_item(self, resources, origin, name, dest):
build_resources = resources.get("build")
release_resources = resources.get("release", {})
release_resources = resources.get("release") or {}
src = os.path.join(origin, name)
build_info = KojiBuildInfo(
name=build_resources.get("name"),
version=build_resources.get("version"),
Expand All @@ -22,6 +23,7 @@ def __build_ami_push_item(self, resources, src, origin, name):
"src": src,
"build_info": build_info,
"origin": origin,
"dest": [dest],
}

image_kwargs.update(
Expand Down Expand Up @@ -54,7 +56,6 @@ def __build_ami_push_item(self, resources, src, origin, name):
image_kwargs["release"] = AmiRelease(**release_kwargs)

image_attrs = [
"dest",
"type",
"region",
"virtualization",
Expand Down Expand Up @@ -83,8 +84,9 @@ def __build_ami_push_item(self, resources, src, origin, name):

return AmiPushItem(**image_kwargs)

def __build_azure_push_item(self, resources, src, origin, name):
def __build_azure_push_item(self, resources, origin, name, dest):
build_resources = resources.get("build")
src = os.path.join(origin, name)
build_info = KojiBuildInfo(
name=build_resources.get("name"),
version=build_resources.get("version"),
Expand All @@ -96,46 +98,45 @@ def __build_azure_push_item(self, resources, src, origin, name):
"description": resources.get("description"),
"build_info": build_info,
"origin": origin,
"dest": [dest],
}
return VHDPushItem(**image_kwargs)

def __process_builds(self, build_dirs):
out = []
for bd in build_dirs:
yaml_path = os.path.join(bd, "resources.yaml")
try:
with open(yaml_path, "rt") as fh:
raw = yaml.safe_load(fh)
except FileNotFoundError as e:
LOG.warning("No resources.yaml file found at %s (ignored)", yaml_path)
continue
if not raw:
continue
image_type = raw.get("type", "")
images_info = raw.get("images", [])

for image in images_info:
if "/" in image.get("path"):
LOG.warning("Unexpected '/' in %s (ignored)", image_relative_path)
continue
image_relative_path = os.path.join(bd, image.get("path"))
if image_type == "AMI":
out.append(
self.__build_ami_push_item(
raw, image_relative_path, bd, image.get("path")
)
def __process_builds(self, current_dir, leafdir):
yaml_path = os.path.join(current_dir, "resources.yaml")
try:
with open(yaml_path, "rt") as fh:
raw = yaml.safe_load(fh)
except FileNotFoundError:
LOG.warning("No resources.yaml file found at %s (ignored)", yaml_path)
return
if not raw:
LOG.warning("Resources.yaml file at %s is empty (ignored)", yaml_path)
return
image_type = raw.get("type") or ""
images_info = raw.get("images") or []
out = []
for image in images_info:
if "/" in image.get("path"):
LOG.warning("Unexpected '/' in %s (ignored)", image.get("path"))
return
if image_type == "AMI":
out.append(
self.__build_ami_push_item(
raw, current_dir, image.get("path"), leafdir.dest
)
elif image_type == "VHD":
out.append(
self.__build_azure_push_item(
raw, image_relative_path, bd, image.get("path")
)
)
elif image_type == "VHD":
out.append(
self.__build_azure_push_item(
raw, current_dir, image.get("path"), leafdir.dest
)
)
return out

@handles_type("CLOUD_IMAGES")
def __cloud_push_item(self, leafdir, _, entry):
builds = [ f.path for f in os.scandir(leafdir.path) if f.is_dir() ]
out = self.__process_builds(builds)
@handles_type("CLOUD_IMAGES",
accepts=lambda entry: entry.is_dir() and os.path.exists(os.path.join(entry.path, "resources.yaml")))
def __cloud_push_item(self, leafdir, metadata, entry):
out = self.__process_builds(entry.path, leafdir)

return out
15 changes: 10 additions & 5 deletions tests/baseline/cases/staged-simple-cloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ items:
release: '1'
version: '9.4'
description: build2 sample
dest: []
dest:
- starmap
ena_support: null
image_id: null
marketplace_entity_type: null
Expand Down Expand Up @@ -59,7 +60,8 @@ items:
release: '1'
version: '9.4'
description: build2 sample
dest: []
dest:
- starmap
ena_support: null
image_id: null
marketplace_entity_type: null
Expand Down Expand Up @@ -99,7 +101,8 @@ items:
release: '1'
version: '9.4'
description: build3 sample
dest: []
dest:
- starmap
ena_support: null
image_id: null
marketplace_entity_type: null
Expand Down Expand Up @@ -146,7 +149,8 @@ items:
release: '1'
version: '9.4'
description: build1 sample
dest: []
dest:
- starmap
disk_version: null
generation: V2
legacy_sku_id: null
Expand All @@ -173,7 +177,8 @@ items:
release: '1'
version: '9.4'
description: build1 sample
dest: []
dest:
- starmap
disk_version: null
generation: V2
legacy_sku_id: null
Expand Down
Empty file.

0 comments on commit 3c1a735

Please sign in to comment.