-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: reduce duplicate code in run actions #603
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1ce76ab
refactor: reduce duplicate run template action code
jshimkus-rh 30bb0c7
refactor: reduce duplicate run action code
jshimkus-rh 7d35428
refactor: rename _template_id as _action_name
jshimkus-rh f21c224
refactor: abstract core __call__ processing
jshimkus-rh acb6534
refactor: override _run in RunPlaybook
jshimkus-rh 42a4735
refactor: use StopAsyncIteration to stop retries
jshimkus-rh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
# Copyright 2023 Red Hat, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import asyncio | ||
import logging | ||
import uuid | ||
from typing import Callable | ||
from urllib.parse import urljoin | ||
|
||
import drools | ||
|
||
from ansible_rulebook.conf import settings | ||
from ansible_rulebook.exception import ControllerApiException | ||
from ansible_rulebook.job_template_runner import job_template_runner | ||
from ansible_rulebook.util import run_at | ||
|
||
from .control import Control | ||
from .helper import Helper | ||
from .metadata import Metadata | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RunBase: | ||
"""Common superclass for "run" actions.""" | ||
|
||
@property | ||
def _job_data(self) -> dict: | ||
data = { | ||
"run_at": run_at(), | ||
"matching_events": self.helper.get_events(), | ||
"action": self.helper.action, | ||
"name": self.name, | ||
"job_id": self.job_id, | ||
"ansible_rulebook_id": settings.identifier, | ||
} | ||
return data | ||
|
||
@property | ||
def _action_name(self) -> str: | ||
raise NotImplementedError | ||
|
||
def __init__(self, metadata: Metadata, control: Control, **action_args): | ||
self.helper = Helper(metadata, control, self._action_name) | ||
self.action_args = action_args | ||
self.job_id = str(uuid.uuid4()) | ||
self.name = self.action_args["name"] | ||
|
||
async def __call__(self): | ||
await self._pre_process() | ||
await self._job_start_event() | ||
await self._run() | ||
await self._post_process() | ||
|
||
async def _do_run(self): | ||
"""Performs a standalone single attempt to effect the requested | ||
operation. In the event of success or non-retriable error raises | ||
StopAsyncIteration exception. | ||
""" | ||
raise NotImplementedError | ||
|
||
async def _run(self): | ||
retries = self.action_args.get("retries", 0) | ||
if self.action_args.get("retry", False): | ||
retries = max(self.action_args.get("retries", 0), 1) | ||
delay = self.action_args.get("delay", 0) | ||
|
||
for i in range(retries + 1): | ||
if i > 0: | ||
if delay > 0: | ||
await asyncio.sleep(delay) | ||
logger.info( | ||
"Previous %s failed. Retry %d of %d", | ||
self._action_name, | ||
i, | ||
retries, | ||
) | ||
|
||
try: | ||
await self._do_run() | ||
except StopAsyncIteration: | ||
break | ||
|
||
async def _pre_process(self) -> None: | ||
pass | ||
|
||
async def _post_process(self) -> None: | ||
pass | ||
|
||
async def _job_start_event(self): | ||
await self.helper.send_status( | ||
self._job_data, | ||
obj_type="Job", | ||
) | ||
|
||
|
||
class RunTemplate(RunBase): | ||
"""Superclass for template-based run actions. Launches the appropriate | ||
specified template on the controller. It waits for the job to be complete. | ||
""" | ||
|
||
@property | ||
def _exceptions(self) -> tuple: | ||
return (ControllerApiException,) | ||
|
||
@property | ||
def _job_data(self) -> dict: | ||
data = super()._job_data | ||
data["hosts"] = ",".join(self.helper.control.hosts) | ||
return data | ||
|
||
@property | ||
def _run_job(self) -> Callable: | ||
raise NotImplementedError | ||
|
||
@property | ||
def _template_name(self) -> str: | ||
raise NotImplementedError | ||
|
||
@property | ||
def _url_path(self) -> str: | ||
return self._url_prefix + f"{self.controller_job['id']}/" + "details" | ||
|
||
@property | ||
def _url_prefix(self) -> str: | ||
return "/#/" | ||
|
||
def _make_log(self) -> dict: | ||
log = { | ||
"organization": self.organization, | ||
"job_id": self.job_id, | ||
"status": self.controller_job["status"], | ||
"run_at": self.controller_job["created"], | ||
"url": self._controller_job_url(), | ||
"matching_events": self.helper.get_events(), | ||
} | ||
if "error" in self.controller_job: | ||
log["message"] = self.controller_job["error"] | ||
log["reason"] = {"error": self.controller_job["error"]} | ||
return log | ||
|
||
def __init__(self, metadata: Metadata, control: Control, **action_args): | ||
super().__init__(metadata, control, **action_args) | ||
self.organization = self.action_args["organization"] | ||
self.job_args = self.action_args.get("job_args", {}) | ||
self.job_args["limit"] = ",".join(self.helper.control.hosts) | ||
self.controller_job = {} | ||
|
||
async def __call__(self): | ||
logger.info( | ||
"running %s: %s, organization: %s", | ||
self._template_name, | ||
self.name, | ||
self.organization, | ||
) | ||
logger.info( | ||
"ruleset: %s, rule %s", | ||
self.helper.metadata.rule_set, | ||
self.helper.metadata.rule, | ||
) | ||
|
||
self.job_args["extra_vars"] = self.helper.collect_extra_vars( | ||
self.job_args.get("extra_vars", {}) | ||
) | ||
await super().__call__() | ||
|
||
async def _do_run(self): | ||
exception = False | ||
try: | ||
controller_job = await self._run_job( | ||
self.name, | ||
self.organization, | ||
self.job_args, | ||
) | ||
except self._exceptions as ex: | ||
exception = True | ||
logger.error(ex) | ||
controller_job = {} | ||
controller_job["status"] = "failed" | ||
controller_job["created"] = run_at() | ||
controller_job["error"] = str(ex) | ||
|
||
self.controller_job = controller_job | ||
|
||
if exception or (self.controller_job["status"] != "failed"): | ||
raise StopAsyncIteration | ||
|
||
async def _post_process(self) -> None: | ||
a_log = self._make_log() | ||
|
||
await self.helper.send_status(a_log) | ||
set_facts = self.action_args.get("set_facts", False) | ||
post_events = self.action_args.get("post_events", False) | ||
|
||
if set_facts or post_events: | ||
ruleset = self.action_args.get( | ||
"ruleset", self.helper.metadata.rule_set | ||
) | ||
logger.debug("set_facts") | ||
facts = self.controller_job.get("artifacts", {}) | ||
if facts: | ||
facts = self.helper.embellish_internal_event(facts) | ||
logger.debug("facts %s", facts) | ||
if set_facts: | ||
drools.ruleset.assert_fact(ruleset, facts) | ||
if post_events: | ||
drools.ruleset.post(ruleset, facts) | ||
else: | ||
logger.debug("Empty facts are not set") | ||
|
||
await super()._post_process() | ||
|
||
def _controller_job_url(self) -> str: | ||
if "id" in self.controller_job: | ||
return urljoin( | ||
job_template_runner.host, | ||
self._url_path, | ||
) | ||
return "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jshimkus-rh Do we need to import specific things here maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jshimkus-rh The earlier implementation tried using a hierarchical model which @benthomasson didn't want to use and wanted to use composition instead of inheritance, this PR seems to be going using inheritance. So we would need approval from @benthomasson if we wants to use inheritance here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming such an objection from @benthomasson I'd like to hear what he considers to be compositional as there's nothing in the previous code (being changed with this PR) that adheres to the conventional definition of composition; there's no facility being re-used across the various classes, rather there's largely duplicated code implemented separately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could do that. I chose not to in order to limit possible confusion between drools and EDA's concept of rulesets.
It was motivated by consistency with eliminating the transitive overriding in tests of the drools apis which would have to be specified against run_base.py file rather than the run_job_template file, as an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My two cents: From what I understand, composition, whether in contrast to inheritance or not, is a pattern used to decouple pieces of logic or distinct responsibilities. In this scenario, where we have shared logic across similar actions, inheritance seems like a fitting approach to me.