Skip to content

Commit

Permalink
WIP Converge for content
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Aug 15, 2024
1 parent 8339aad commit 123c9ac
Show file tree
Hide file tree
Showing 16 changed files with 54,636 additions and 33,002 deletions.
8 changes: 7 additions & 1 deletion meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,11 @@ action_groups:
- status
- task
- x509_cert_guard
requires_ansible: '>=2.15.0,<2.18'
plugin_routing:
modules:
file_repository_content:
deprecation:
removal_version: "0.1.0"
warning_text: "Use pulp.squeezer.file_content instead."
requires_ansible: ">=2.15.0,<2.18"
...
30 changes: 12 additions & 18 deletions plugins/module_utils/pulp_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
from packaging.requirements import SpecifierSet
from pulp_glue.common import __version__ as pulp_glue_version
from pulp_glue.common.context import PulpContext, PulpException, PulpNoWait
from pulp_glue.common.openapi import BasicAuthProvider

GLUE_VERSION_SPEC = ">=0.27.0dev0,<0.28"
GLUE_VERSION_SPEC = ">=0.28.0.dev,<0.29.0"
if not SpecifierSet(GLUE_VERSION_SPEC, prereleases=True).contains(pulp_glue_version):
raise ImportError(
f"Installed 'pulp-glue' version '{pulp_glue_version}' is not in '{GLUE_VERSION_SPEC}'."
Expand Down Expand Up @@ -86,17 +87,11 @@ def __init__(self, **kwargs):
self.fail_json(msg=missing_required_lib(import_error[0]), exception=import_error[1])

auth_args = {}
if SpecifierSet(">=0.24.0").contains(pulp_glue_version):
if self.params["username"]:
from pulp_glue.common.openapi import BasicAuthProvider

auth_args["auth_provider"] = BasicAuthProvider(
username=self.params["username"],
password=self.params["password"],
)
else:
auth_args["username"] = self.params["username"]
auth_args["password"] = self.params["password"]
if self.params["username"]:
auth_args["auth_provider"] = BasicAuthProvider(
username=self.params["username"],
password=self.params["password"],
)

self.pulp_ctx = PulpSqueezerContext(
api_root="/pulp/",
Expand All @@ -106,13 +101,12 @@ def __init__(self, **kwargs):
key=self.params["user_key"],
validate_certs=self.params["validate_certs"],
refresh_cache=self.params["refresh_api_cache"],
safe_calls_only=self.check_mode,
user_agent=f"Squeezer/{__VERSION__}",
**auth_args,
),
background_tasks=False,
timeout=self.params["timeout"],
fake_mode=self.check_mode, # This should be the same as api_kwargs["safe_calls_only"]
fake_mode=self.check_mode, # This sets api_kwargs["safe_calls_only"] for us.
)

def __enter__(self):
Expand Down Expand Up @@ -173,7 +167,7 @@ def represent(self, entity):
for key, value in entity.items()
}

def process(self, natural_key, desired_attributes):
def process(self, natural_key, desired_attributes, defaults=None):
if self.state is None:
return self.process_info(natural_key, desired_attributes)

Expand All @@ -191,10 +185,10 @@ def process(self, natural_key, desired_attributes):
else:
self.set_result(
self.entity_singular,
self.process_special(self.context.entity, natural_key, desired_attributes),
self.process_special(desired_attributes, defaults=defaults),
)
return
changed, before, after = self.context.converge(desired_entity)
changed, before, after = self.context.converge(desired_entity, defaults=defaults)
if before is not None:
before = self.represent(before)
if after is not None:
Expand Down Expand Up @@ -222,7 +216,7 @@ def process_info(self, natural_key, desired_attributes):
self.context.entity = natural_key
self.set_result(self.entity_singular, self.represent(self.context.entity))

def process_special(self, entity, natural_key, desired_attributes):
def process_special(self, entity, natural_key, desired_attributes, defaults=None):
raise SqueezerException(f"Invalid state '{self.state}'.")


Expand Down
66 changes: 48 additions & 18 deletions plugins/modules/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,56 @@
)

try:
from pulp_glue.core.context import PulpArtifactContext
from pulp_glue.common.context import PreprocessedEntityDefinition, PulpEntityNotFound
from pulp_glue.core.context import PulpArtifactContext as _PulpArtifactContext

PULP_CLI_IMPORT_ERR = None

# Patch the Context to make converge call upload
# It's a case study at this point. Eventually glue should handle this.
class PulpArtifactContext(_PulpArtifactContext):
def converge(self, desired_attributes, defaults=None):
"""
Converge an entity to have a set of desired attributes.
This will look for the entity, and depending on what it found and what should be, create,
delete or update the entity.
Parameters:
desired_attributes: Dictionary of attributes the entity should have.
Returns:
Tuple of (changed, before, after)
"""
try:
entity = self.entity
except PulpEntityNotFound:
entity = None

if desired_attributes is None:
if entity is not None:
# raise SqueezerException("Artifacts cannot be deleted")
self.delete()
return True, entity, None
else:
if entity is None:
# This is being quite different:
with open(defaults["file"], "rb") as file:
self.upload(
file=file,
chunk_size=defaults["chunk_size"],
sha256=self._entity_lookup["sha256"],
)
return True, None, self.entity
return False, entity, entity

except ImportError:
PULP_CLI_IMPORT_ERR = traceback.format_exc()
PulpArtifactContext = None


class PulpArtifactAnsibleModule(PulpEntityAnsibleModule):
def process_present(self, entity, natural_key, desired_attributes):
if entity is None:
if self.check_mode:
entity = {**desired_attributes, **natural_key}
else:
with open(self.params["file"], "rb") as infile:
self.context.upload(
infile, sha256=natural_key["sha256"], chunk_size=self.params["chunk_size"]
)
entity = self.context.entity
self.set_changed()
return self.represent(entity)


def main():
with PulpArtifactAnsibleModule(
with PulpEntityAnsibleModule(
context_class=PulpArtifactContext,
entity_singular="artifact",
entity_plural="artifacts",
Expand Down Expand Up @@ -144,8 +169,13 @@ def main():
natural_key = {
"sha256": sha256,
}
desired_attributes = {}
defaults = {
"file": module.params["file"],
"chunk_size": module.params["chunk_size"],
}

module.process(natural_key, {})
module.process(natural_key, desired_attributes, defaults=defaults)


if __name__ == "__main__":
Expand Down
40 changes: 36 additions & 4 deletions plugins/modules/file_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@
"""


import os
import traceback

from ansible_collections.pulp.squeezer.plugins.module_utils.pulp_glue import PulpEntityAnsibleModule

try:
from pulp_glue.common.context import PreprocessedEntityDefinition, PulpEntityNotFound
from pulp_glue.file.context import PulpFileContentContext
from pulp_glue.file.context import PulpFileRepositoryContext

PULP_CLI_IMPORT_ERR = None

except ImportError:
PULP_CLI_IMPORT_ERR = traceback.format_exc()
PulpFileContentContext = None
Expand All @@ -84,19 +88,47 @@ def main():
argument_spec={
"sha256": {"aliases": ["digest"]},
"relative_path": {},
"file": {"type": "path"},
"chunk_size": {"type": "int", "default": 33554432},
"repository": {},
},
required_if=[
("state", "present", ["sha256", "relative_path"]),
("state", "absent", ["sha256", "relative_path"]),
("state", "present", ["file", "relative_path", "repository"]),
("state", "absent", ["relative_path", "repository"]),
],
) as module:
sha256 = module.params["sha256"]
if module.params["file"]:
if not os.path.exists(module.params["file"]):
raise SqueezerException("File not found.")
file_sha256 = module.sha256(module.params["file"])
if sha256:
if sha256 != file_sha256:
raise SqueezerException("File checksum mismatch.")
else:
sha256 = file_sha256

if sha256 is None and module.state == "absent":
raise SqueezerException(
"One of 'file' and 'sha256' is required if 'state' is 'absent'."
)

natural_key = {
"sha256": module.params["sha256"],
"sha256": sha256,
"relative_path": module.params["relative_path"],
}
desired_attributes = {}
defaults = {
"file": module.params["file"],
"chunk_size": module.params["chunk_size"],
}

if module.params["repository"]:
module.context.repository_ctx = PulpFileRepositoryContext(
module.pulp_ctx, entity={"name": module.params["repository"]}
)

module.process(natural_key, desired_attributes)
module.process(natural_key, desired_attributes, defaults=defaults)


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/file_repository_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# copyright (c) 2020, Matthias Dellweg
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)

TODO = """Deprecate this properly"""

DOCUMENTATION = r"""
---
module: file_repository_content
short_description: Manage content in file repositories of a pulp server
short_description: [deprecated] Manage content in file repositories of a pulp server
description:
- "This module adds or removes content to/from file repositories in a pulp server."
options:
Expand Down
9 changes: 6 additions & 3 deletions plugins/modules/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
)

try:
from pulp_glue.common.context import PulpEntityNotFound
from pulp_glue.core.context import PulpTaskContext

PULP_CLI_IMPORT_ERR = None
Expand All @@ -71,10 +72,12 @@


class PulpTaskAnsibleModule(PulpEntityAnsibleModule):
def process_special(self, entity, natural_key, desired_attributes):
def process_special(self, desired_attributes, defaults=None):
if self.state in ["canceled", "completed"]:
if entity is None:
raise SqueezerException("Entity not found.")
try:
entity = self.context.entity
except PulpEntityNotFound:
raise SqueezerException("Task not found.")
if entity["state"] in ["waiting", "running", "canceling"]:
if not self.check_mode:
if self.state == "canceled":
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pulp-glue<0.27,>=0.20.0
pulp-glue<0.28.0.dev99,>=0.27.0

ansible_runner<2.3; python_version < '3.7'
ansible_runner; python_version >= '3.7'
Expand Down
Loading

0 comments on commit 123c9ac

Please sign in to comment.