Skip to content
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

Stop gap to add Workflow Job Template to Job Template Runner #542

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ansible_rulebook/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ async def run_job_template(
ruleset: str,
name: str,
organization: str,
workflow: Optional[bool] = False,
job_args: Optional[dict] = None,
set_facts: Optional[bool] = None,
post_events: Optional[bool] = None,
Expand Down Expand Up @@ -795,6 +796,7 @@ async def run_job_template(
controller_job = await job_template_runner.run_job_template(
name,
organization,
workflow,
job_args,
)
if controller_job["status"] != "failed":
Expand Down
22 changes: 14 additions & 8 deletions ansible_rulebook/job_template_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@


class JobTemplateRunner:
JOB_TEMPLATE_SLUG = "/api/v2/job_templates"
CONFIG_SLUG = "/api/v2/config"
JOB_COMPLETION_STATUSES = ["successful", "failed", "error", "canceled"]

def __init__(
self, host: str = "", token: str = "", verify_ssl: str = "yes"
self, host: str = "", token: str = "", verify_ssl: str = "yes",
workflow: bool = False
):
self.token = token
self.host = host
self.verify_ssl = verify_ssl
self.workflow = workflow
self.refresh_delay = int(
os.environ.get("EDA_JOB_TEMPLATE_REFRESH_DELAY", 10)
)
Expand Down Expand Up @@ -89,9 +90,11 @@ def _sslcontext(self) -> Union[bool, ssl.SSLContext]:
return ssl.create_default_context(cafile=self.verify_ssl)
return False

async def _get_job_template_id(self, name: str, organization: str) -> int:
slug = f"{self.JOB_TEMPLATE_SLUG}/"
async def _get_job_template_id(self, name: str, workflow: bool,
organization: str) -> int:
params = {"name": name}
slug = ("/api/v2/workflow_job_templates" if workflow else
"/api/v2/job_templates")

while True:
json_body = await self._get_page(slug, params)
Expand All @@ -118,10 +121,11 @@ async def _get_job_template_id(self, name: str, organization: str) -> int:
async def run_job_template(
self,
name: str,
workflow: bool,
organization: str,
job_params: dict,
) -> dict:
job = await self.launch(name, organization, job_params)
job = await self.launch(name, workflow, organization, job_params)

url = job["url"]
params = {}
Expand All @@ -136,10 +140,12 @@ async def run_job_template(
await asyncio.sleep(self.refresh_delay)

async def launch(
self, name: str, organization: str, job_params: dict
self, name: str, organization: str, workflow: bool, job_params: dict
) -> dict:
jt_id = await self._get_job_template_id(name, organization)
url = urljoin(self.host, f"{self.JOB_TEMPLATE_SLUG}/{jt_id}/launch/")
jt_id = await self._get_job_template_id(name, workflow, organization)
slug = ("/api/v2/workflow_job_templates" if workflow else
"/api/v2/job_templates")
url = urljoin(self.host, f"{slug}/{jt_id}/launch/")

try:
async with self._session.post(
Expand Down
3 changes: 3 additions & 0 deletions ansible_rulebook/schema/ruleset_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@
"organization": {
"type": "string"
},
"workflow": {
"type": "boolean"
},
"job_args": {
"type": "object"
},
Expand Down
3 changes: 3 additions & 0 deletions docs/actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ Run a job template.
* - organization
- The name of the organization
- Yes
* - workflow
- Launch a workflow job template instead of a normal job template.
- No
* - set_facts
- The artifacts from the playbook execution are inserted back into the rule set as facts
- No
Expand Down