orchestrator-lso 1.0.3__tar.gz → 2.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/.bumpversion.cfg +1 -1
  2. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/.github/workflows/run-unit-tests.yaml +0 -2
  3. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/Dockerfile.example +1 -1
  4. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/PKG-INFO +36 -16
  5. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/README.md +26 -7
  6. orchestrator_lso-2.0.1/docs/source/index.rst +34 -0
  7. orchestrator_lso-2.0.1/env.example +23 -0
  8. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/lso/__init__.py +7 -11
  9. orchestrator_lso-2.0.1/lso/config.py +48 -0
  10. orchestrator_lso-2.0.1/lso/playbook.py +76 -0
  11. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/lso/routes/playbook.py +38 -10
  12. orchestrator_lso-2.0.1/lso/tasks.py +64 -0
  13. orchestrator_lso-2.0.1/lso/worker.py +52 -0
  14. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/pyproject.toml +9 -7
  15. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/test/conftest.py +24 -29
  16. orchestrator_lso-2.0.1/test/routes/test_playbook.py +224 -0
  17. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/test/test_ansible.py +0 -2
  18. orchestrator_lso-1.0.3/config.json.example +0 -3
  19. orchestrator_lso-1.0.3/docs/source/index.rst +0 -10
  20. orchestrator_lso-1.0.3/lso/config.py +0 -70
  21. orchestrator_lso-1.0.3/lso/playbook.py +0 -122
  22. orchestrator_lso-1.0.3/test/routes/test_playbook.py +0 -120
  23. orchestrator_lso-1.0.3/test/test_config.py +0 -44
  24. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/.github/dependabot.yml +0 -0
  25. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/.github/styles/config/vocabularies/Sphinx/accept.txt +0 -0
  26. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/.github/styles/config/vocabularies/jargon/accept.txt +0 -0
  27. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/.github/workflows/publish-package.yaml +0 -0
  28. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/.github/workflows/run-linting-tests.yaml +0 -0
  29. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/.github/workflows/sphinx.yaml +0 -0
  30. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/.gitignore +0 -0
  31. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/.vale.ini +0 -0
  32. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/LICENSE +0 -0
  33. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/LSO_banner.jpg +0 -0
  34. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/Makefile +0 -0
  35. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/source/_static/custom.css +0 -0
  36. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/source/_static/lso_logo.png +0 -0
  37. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/source/conf.py +0 -0
  38. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/source/module/config.rst +0 -0
  39. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/source/module/playbook.rst +0 -0
  40. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/source/module/routes/default.rst +0 -0
  41. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/source/module/routes/index.rst +0 -0
  42. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/source/module/routes/playbook.rst +0 -0
  43. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/docs/source/modules.rst +0 -0
  44. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/lso/app.py +0 -0
  45. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/lso/environment.py +0 -0
  46. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/lso/routes/__init__.py +0 -0
  47. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/lso/routes/default.py +0 -0
  48. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/setup.py +0 -0
  49. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/test/__init__.py +0 -0
  50. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/test/routes/__init__.py +0 -0
  51. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/test/routes/test_default.py +0 -0
  52. {orchestrator_lso-1.0.3 → orchestrator_lso-2.0.1}/test/test-playbook.yaml +0 -0
@@ -1,5 +1,5 @@
1
1
  [bumpversion]
2
- current_version = 1.0.3
2
+ current_version = 2.0.1
3
3
  commit = False
4
4
  tag = False
5
5
  parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(rc(?P<build>\d+))?
@@ -26,8 +26,6 @@ jobs:
26
26
  FLIT_ROOT_INSTALL: 1
27
27
  - name: Run Unit tests
28
28
  run: pytest --cov-branch --cov=lso --cov-report=xml
29
- env:
30
- SETTINGS_FILENAME: dummy.json
31
29
  - name: "Upload coverage to Codecov"
32
30
  uses: codecov/codecov-action@v3
33
31
  with:
@@ -11,7 +11,7 @@ COPY ./ansible-galaxy-requirements.yaml ./ansible-galaxy-requirements.yaml
11
11
  RUN apk add --update --no-cache gcc libc-dev libffi-dev openssh
12
12
 
13
13
  # Install the LSO python package, and additional requirements
14
- RUN pip install orchestrator-lso=="1.0.3"
14
+ RUN pip install orchestrator-lso=="2.0.1"
15
15
  RUN pip install -r requirements.txt
16
16
 
17
17
  # Install required Ansible Galaxy roles and collections
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: orchestrator-lso
3
- Version: 1.0.3
3
+ Version: 2.0.1
4
4
  Summary: LSO, an API for remotely running Ansible playbooks.
5
5
  Author-email: GÉANT Orchestration and Automation Team <goat@geant.org>
6
6
  Requires-Python: >=3.11,<3.13
@@ -25,13 +25,15 @@ Classifier: License :: OSI Approved :: Apache Software License
25
25
  Classifier: Programming Language :: Python :: 3 :: Only
26
26
  Classifier: Programming Language :: Python :: 3.11
27
27
  Classifier: Programming Language :: Python :: 3.12
28
- Requires-Dist: ansible-runner~=2.4.0
29
- Requires-Dist: ansible~=10.1.0
30
- Requires-Dist: fastapi~=0.112.1
31
- Requires-Dist: httpx~=0.27.0
32
- Requires-Dist: jsonschema~=4.21.1
33
- Requires-Dist: uvicorn[standard]~=0.30.6
34
- Requires-Dist: requests~=2.31.0
28
+ Requires-Dist: ansible-runner==2.4.0
29
+ Requires-Dist: ansible==10.6.0
30
+ Requires-Dist: fastapi==0.115.5
31
+ Requires-Dist: httpx==0.28.0
32
+ Requires-Dist: uvicorn[standard]==0.32.1
33
+ Requires-Dist: requests==2.32.3
34
+ Requires-Dist: pydantic-settings==2.6.1
35
+ Requires-Dist: celery==5.4.0
36
+ Requires-Dist: redis==5.2.0
35
37
  Requires-Dist: types-setuptools ; extra == "dev"
36
38
  Requires-Dist: types-requests ; extra == "dev"
37
39
  Requires-Dist: toml ; extra == "dev"
@@ -82,11 +84,9 @@ Use the Docker image to then spin up an environment. An example Docker compose f
82
84
  services:
83
85
  lso:
84
86
  image: my-lso:latest
85
- environment:
86
- SETTINGS_FILENAME: /app/config.json
87
- ANSIBLE_ROLES_PATH: /app/lso/ansible_roles
87
+ env_file:
88
+ .env # Load default environment variables from the .env file
88
89
  volumes:
89
- - "/home/user/config.json:/app/config.json:ro"
90
90
  - "/home/user/ansible_inventory:/opt/ansible_inventory:ro"
91
91
  - "~/.ssh/id_ed25519.pub:/root/.ssh/id_ed25519.pub:ro"
92
92
  - "~/.ssh/id_ed25519:/root/.ssh/id_ed25519:ro"
@@ -94,7 +94,8 @@ services:
94
94
 
95
95
  This will expose the API on port 8000. The container requires some more files to be mounted:
96
96
 
97
- * A `config.json` that references to the location where the Ansible playbooks are stored **inside the container**.
97
+ * An .env file: Sets default environment variables, like ANSIBLE_PLAYBOOKS_ROOT_DIR for the location of Ansible playbooks **inside the container**.
98
+ * Environment variables: Specific configurations, such as ANSIBLE_ROLES_PATH, can be directly set in the environment section. This is ideal for values you may want to override without modifying the .env file.
98
99
  * An Ansible inventory for all host and group variables that are used in the playbooks
99
100
  * A public/private key pair for SSH authentication on external machines that are targeted by Ansible playbooks.
100
101
  * Any Ansible-specific configuration (such as `collections_path`, `roles_path`, etc.) should be set using
@@ -132,11 +133,30 @@ As an alternative, below are a set of instructions for installing and running LS
132
133
 
133
134
  ### Running the app
134
135
 
135
- * Create a settings file, see `config.json.example` for an example.
136
+ * Set required environment variables; see `env.example` for reference.
136
137
  * If necessary, set the environment variable `ANSIBLE_HOME` to a custom path.
137
138
  * Run the app like this (`app.py` starts the server on port 44444):
138
139
 
139
140
  ```bash
140
- SETTINGS_FILENAME=/absolute/path/to/config.json python -m lso.app
141
+ source .env && python -m lso.app
141
142
  ```
142
143
 
144
+ ### Task Execution Options
145
+ 1. Celery (Distributed Execution)
146
+
147
+ - For distributed task execution, set `EXECUTOR=celery`.
148
+ - Add Celery config in your environment variables:
149
+
150
+ ```bash
151
+ CELERY_BROKER_URL=redis://localhost:6379/0
152
+ CELERY_RESULT_BACKEND=redis://localhost:6379/0
153
+ WORKER_QUEUE_NAME=lso-worker-queue # default value is None so you don't need this by default.
154
+ ```
155
+ - Start a Celery worker:
156
+
157
+ ```bash
158
+ celery -A lso.worker worker --loglevel=info -Q lso-worker-queue
159
+ ```
160
+ 2. ThreadPoolExecutor (Local Execution)
161
+
162
+ For local concurrent tasks, set `EXECUTOR=threadpool` and configure `MAX_THREAD_POOL_WORKERS`.
@@ -25,11 +25,9 @@ Use the Docker image to then spin up an environment. An example Docker compose f
25
25
  services:
26
26
  lso:
27
27
  image: my-lso:latest
28
- environment:
29
- SETTINGS_FILENAME: /app/config.json
30
- ANSIBLE_ROLES_PATH: /app/lso/ansible_roles
28
+ env_file:
29
+ .env # Load default environment variables from the .env file
31
30
  volumes:
32
- - "/home/user/config.json:/app/config.json:ro"
33
31
  - "/home/user/ansible_inventory:/opt/ansible_inventory:ro"
34
32
  - "~/.ssh/id_ed25519.pub:/root/.ssh/id_ed25519.pub:ro"
35
33
  - "~/.ssh/id_ed25519:/root/.ssh/id_ed25519:ro"
@@ -37,7 +35,8 @@ services:
37
35
 
38
36
  This will expose the API on port 8000. The container requires some more files to be mounted:
39
37
 
40
- * A `config.json` that references to the location where the Ansible playbooks are stored **inside the container**.
38
+ * An .env file: Sets default environment variables, like ANSIBLE_PLAYBOOKS_ROOT_DIR for the location of Ansible playbooks **inside the container**.
39
+ * Environment variables: Specific configurations, such as ANSIBLE_ROLES_PATH, can be directly set in the environment section. This is ideal for values you may want to override without modifying the .env file.
41
40
  * An Ansible inventory for all host and group variables that are used in the playbooks
42
41
  * A public/private key pair for SSH authentication on external machines that are targeted by Ansible playbooks.
43
42
  * Any Ansible-specific configuration (such as `collections_path`, `roles_path`, etc.) should be set using
@@ -75,10 +74,30 @@ As an alternative, below are a set of instructions for installing and running LS
75
74
 
76
75
  ### Running the app
77
76
 
78
- * Create a settings file, see `config.json.example` for an example.
77
+ * Set required environment variables; see `env.example` for reference.
79
78
  * If necessary, set the environment variable `ANSIBLE_HOME` to a custom path.
80
79
  * Run the app like this (`app.py` starts the server on port 44444):
81
80
 
82
81
  ```bash
83
- SETTINGS_FILENAME=/absolute/path/to/config.json python -m lso.app
82
+ source .env && python -m lso.app
84
83
  ```
84
+
85
+ ### Task Execution Options
86
+ 1. Celery (Distributed Execution)
87
+
88
+ - For distributed task execution, set `EXECUTOR=celery`.
89
+ - Add Celery config in your environment variables:
90
+
91
+ ```bash
92
+ CELERY_BROKER_URL=redis://localhost:6379/0
93
+ CELERY_RESULT_BACKEND=redis://localhost:6379/0
94
+ WORKER_QUEUE_NAME=lso-worker-queue # default value is None so you don't need this by default.
95
+ ```
96
+ - Start a Celery worker:
97
+
98
+ ```bash
99
+ celery -A lso.worker worker --loglevel=info -Q lso-worker-queue
100
+ ```
101
+ 2. ThreadPoolExecutor (Local Execution)
102
+
103
+ For local concurrent tasks, set `EXECUTOR=threadpool` and configure `MAX_THREAD_POOL_WORKERS`.
@@ -0,0 +1,34 @@
1
+ Lightweight Service Orchestrator (LSO) Documentation
2
+ =====================================================
3
+
4
+ Introduction
5
+ ------------
6
+
7
+ The Lightweight Service Orchestrator (LSO) is a simple tool designed to run Ansible playbooks remotely.
8
+ It provides a straightforward way to send instructions, like inventory and variables, to Ansible through
9
+ a REST API, making automation easier and more flexible.
10
+
11
+ Why LSO?
12
+ --------
13
+
14
+ LSO was built to solve a common problem: running Ansible playbooks from a remote machine without setting
15
+ up a complicated system. Many tools, like AWX, are powerful but require complex setups, like Kubernetes,
16
+ and are tied to specific ecosystems.
17
+
18
+ We wanted a lightweight, easy-to-use solution that works without extra layers. That’s why we created LSO.
19
+
20
+ What LSO Does
21
+ -------------
22
+
23
+ LSO is a small FastAPI server that receives requests from remote services and uses `ansible-runner` to execute playbooks.
24
+
25
+ It:
26
+ - Accepts the playbook name, inventory details, and extra variables as input.
27
+ - Runs the playbook on Ansible using this information.
28
+ - Sends the results back, including the output and execution status.
29
+
30
+ .. toctree::
31
+ :maxdepth: 1
32
+ :caption: Contents:
33
+
34
+ modules
@@ -0,0 +1,23 @@
1
+ # Environment configuration for LSO application
2
+
3
+ # Ansible configuration
4
+ ANSIBLE_PLAYBOOKS_ROOT_DIR="/path/to/ansible/playbooks"
5
+ ANSIBLE_ROLES_PATH="/app/lso/ansible_roles" # Set specific Ansible roles path
6
+
7
+ # Executor configuration
8
+ EXECUTOR="threadpool" # Options: "threadpool", "celery"
9
+ MAX_THREAD_POOL_WORKERS=10
10
+
11
+ # Request settings
12
+ REQUEST_TIMEOUT_SEC=10
13
+
14
+ # Celery configuration
15
+ CELERY_BROKER_URL="redis://localhost:6379/0"
16
+ CELERY_RESULT_BACKEND="redis://localhost:6379/0"
17
+ CELERY_TIMEZONE="Europe/Amsterdam"
18
+ CELERY_ENABLE_UTC=True
19
+ CELERY_RESULT_EXPIRES=3600
20
+ WORKER_QUEUE_NAME="lso-worker-queue"
21
+
22
+ # Debug/Testing
23
+ TESTING=False
@@ -13,24 +13,23 @@
13
13
 
14
14
  """LSO, an API for remotely running Ansible playbooks."""
15
15
 
16
- __version__ = "1.0.3"
16
+ __version__ = "2.0.1"
17
17
 
18
18
  import logging
19
19
 
20
20
  from fastapi import FastAPI
21
21
  from fastapi.middleware.cors import CORSMiddleware
22
22
 
23
- from lso import config, environment
23
+ from lso import environment
24
24
  from lso.routes.default import router as default_router
25
25
  from lso.routes.playbook import router as playbook_router
26
26
 
27
+ logger = logging.getLogger(__name__)
27
28
 
28
- def create_app() -> FastAPI:
29
- """Override default settings with those found in the file read from environment variable `SETTINGS_FILENAME`.
30
29
 
31
- :return: a new flask app instance
32
- """
33
- app = FastAPI()
30
+ def create_app() -> FastAPI:
31
+ """Initialise the :term:`LSO` app."""
32
+ app = FastAPI(docs_url="/api/doc", redoc_url="/api/redoc", openapi_url="/api/openapi.json")
34
33
 
35
34
  app.add_middleware(
36
35
  CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"]
@@ -39,11 +38,8 @@ def create_app() -> FastAPI:
39
38
  app.include_router(default_router, prefix="/api")
40
39
  app.include_router(playbook_router, prefix="/api/playbook")
41
40
 
42
- # test that configuration parameters are loaded and available
43
- config.load()
44
-
45
41
  environment.setup_logging()
46
42
 
47
- logging.info("FastAPI app initialized")
43
+ logger.info("FastAPI app initialized")
48
44
 
49
45
  return app
@@ -0,0 +1,48 @@
1
+ # Copyright 2023-2024 GÉANT Vereniging.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+ """Module for loading and managing configuration settings for the LSO app.
15
+
16
+ Uses `pydantic`'s `BaseSettings` to load settings from environment variables.
17
+ """
18
+
19
+ import os
20
+ from enum import Enum
21
+
22
+ from pydantic_settings import BaseSettings
23
+
24
+
25
+ class ExecutorType(Enum):
26
+ """Enum representing the types of executors available for task execution."""
27
+
28
+ WORKER = "celery"
29
+ THREADPOOL = "threadpool"
30
+
31
+
32
+ class Config(BaseSettings):
33
+ """The set of parameters required for running :term:`LSO`."""
34
+
35
+ TESTING: bool = False
36
+ ANSIBLE_PLAYBOOKS_ROOT_DIR: str = "/path/to/ansible/playbooks"
37
+ EXECUTOR: ExecutorType = ExecutorType.THREADPOOL
38
+ MAX_THREAD_POOL_WORKERS: int = min(32, (os.cpu_count() or 1) + 4)
39
+ REQUEST_TIMEOUT_SEC: int = 10
40
+ CELERY_BROKER_URL: str = "redis://localhost:6379/0"
41
+ CELERY_RESULT_BACKEND: str = "redis://localhost:6379/0"
42
+ CELERY_TIMEZONE: str = "Europe/Amsterdam"
43
+ CELERY_ENABLE_UTC: bool = True
44
+ CELERY_RESULT_EXPIRES: int = 3600
45
+ WORKER_QUEUE_NAME: str | None = None
46
+
47
+
48
+ settings = Config()
@@ -0,0 +1,76 @@
1
+ # Copyright 2023-2024 GÉANT Vereniging.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+ """Module that gathers common API responses and data models."""
15
+
16
+ import uuid
17
+ from concurrent.futures import ThreadPoolExecutor
18
+ from pathlib import Path
19
+ from typing import Any
20
+
21
+ from pydantic import HttpUrl
22
+
23
+ from lso.config import ExecutorType, settings
24
+ from lso.tasks import run_playbook_proc_task
25
+
26
+ _executor = None
27
+
28
+
29
+ def get_thread_pool() -> ThreadPoolExecutor:
30
+ """Get and optionally initialise a ThreadPoolExecutor.
31
+
32
+ Returns:
33
+ ThreadPoolExecutor
34
+
35
+ """
36
+ global _executor # noqa: PLW0603
37
+ if _executor is None:
38
+ _executor = ThreadPoolExecutor(max_workers=settings.MAX_THREAD_POOL_WORKERS)
39
+
40
+ return _executor
41
+
42
+
43
+ def get_playbook_path(playbook_name: Path) -> Path:
44
+ """Get the path of a playbook on the local filesystem."""
45
+ return Path(settings.ANSIBLE_PLAYBOOKS_ROOT_DIR) / playbook_name
46
+
47
+
48
+ def run_playbook(
49
+ playbook_path: Path,
50
+ extra_vars: dict[str, Any],
51
+ inventory: dict[str, Any] | str,
52
+ callback: HttpUrl,
53
+ ) -> uuid.UUID:
54
+ """Run an Ansible playbook against a specified inventory.
55
+
56
+ :param Path playbook_path: playbook to be executed.
57
+ :param dict[str, Any] extra_vars: Any extra vars needed for the playbook to run.
58
+ :param dict[str, Any] | str inventory: The inventory that the playbook is executed against.
59
+ :param HttpUrl callback: Callback URL where the playbook should send a status update when execution is completed.
60
+ This is used for workflow-orchestrator to continue with the next step in a workflow.
61
+ :return: Result of playbook launch, this could either be successful or unsuccessful.
62
+ :rtype: :class:`fastapi.responses.JSONResponse`
63
+ """
64
+ job_id = uuid.uuid4()
65
+ if settings.EXECUTOR == ExecutorType.THREADPOOL:
66
+ executor = get_thread_pool()
67
+ executor_handle = executor.submit(
68
+ run_playbook_proc_task, str(job_id), str(playbook_path), extra_vars, inventory, str(callback)
69
+ )
70
+ if settings.TESTING:
71
+ executor_handle.result()
72
+
73
+ elif settings.EXECUTOR == ExecutorType.WORKER:
74
+ run_playbook_proc_task.delay(str(job_id), str(playbook_path), extra_vars, inventory, str(callback))
75
+
76
+ return job_id
@@ -15,14 +15,16 @@
15
15
 
16
16
  import json
17
17
  import tempfile
18
+ import uuid
18
19
  from contextlib import redirect_stderr
19
20
  from io import StringIO
21
+ from pathlib import Path
20
22
  from typing import Annotated, Any
21
23
 
24
+ import ansible_runner
22
25
  from ansible.inventory.manager import InventoryManager
23
26
  from ansible.parsing.dataloader import DataLoader
24
27
  from fastapi import APIRouter, HTTPException, status
25
- from fastapi.responses import JSONResponse
26
28
  from pydantic import AfterValidator, BaseModel, HttpUrl
27
29
 
28
30
  from lso.playbook import get_playbook_path, run_playbook
@@ -31,11 +33,19 @@ router = APIRouter()
31
33
 
32
34
 
33
35
  def _inventory_validator(inventory: dict[str, Any] | str) -> dict[str, Any] | str:
34
- """Validate the format of the provided inventory by trying to parse it.
36
+ """Validate the provided inventory format.
35
37
 
36
- If an inventory can't be parsed without warnings or errors, these are returned to the user by means of an HTTP
37
- status 422 for 'unprocessable entity'.
38
+ Attempts to parse the inventory to verify its validity. If the inventory cannot be parsed or the inventory
39
+ format is incorrect an HTTP 422 error is raised.
40
+
41
+ :param inventory: The inventory to validate, can be a dictionary or a string.
42
+ :return: The validated inventory if no errors are found.
43
+ :raises HTTPException: If parsing fails or the format is incorrect.
38
44
  """
45
+ if not ansible_runner.utils.isinventory(inventory):
46
+ detail = "Invalid inventory provided. Should be a string, or JSON object."
47
+ raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=detail)
48
+
39
49
  loader = DataLoader()
40
50
  output = StringIO()
41
51
  with tempfile.NamedTemporaryFile(mode="w+") as temp_inv, redirect_stderr(output):
@@ -53,15 +63,31 @@ def _inventory_validator(inventory: dict[str, Any] | str) -> dict[str, Any] | st
53
63
  return inventory
54
64
 
55
65
 
66
+ def _playbook_path_validator(playbook_name: Path) -> Path:
67
+ playbook_path = get_playbook_path(playbook_name)
68
+ if not Path.exists(playbook_path):
69
+ msg = f"Filename '{playbook_path}' does not exist."
70
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=msg)
71
+
72
+ return playbook_path
73
+
74
+
56
75
  PlaybookInventory = Annotated[dict[str, Any] | str, AfterValidator(_inventory_validator)]
76
+ PlaybookName = Annotated[Path, AfterValidator(_playbook_path_validator)]
77
+
78
+
79
+ class PlaybookRunResponse(BaseModel):
80
+ """PlaybookRunResponse domain model schema."""
81
+
82
+ job_id: uuid.UUID
57
83
 
58
84
 
59
85
  class PlaybookRunParams(BaseModel):
60
86
  """Parameters for executing an Ansible playbook."""
61
87
 
62
88
  #: The filename of a playbook that's executed. It should be present inside the directory defined in the
63
- #: configuration option ``ansible_playbooks_root_dir``.
64
- playbook_name: str
89
+ #: configuration option ``ANSIBLE_PLAYBOOKS_ROOT_DIR``.
90
+ playbook_name: PlaybookName
65
91
  #: The address where LSO should call back to upon completion.
66
92
  callback: HttpUrl
67
93
  #: The inventory to run the playbook against. This inventory can also include any host vars, if needed. When
@@ -74,8 +100,8 @@ class PlaybookRunParams(BaseModel):
74
100
  extra_vars: dict[str, Any] = {}
75
101
 
76
102
 
77
- @router.post("/")
78
- def run_playbook_endpoint(params: PlaybookRunParams) -> JSONResponse:
103
+ @router.post("/", response_model=PlaybookRunResponse, status_code=status.HTTP_201_CREATED)
104
+ def run_playbook_endpoint(params: PlaybookRunParams) -> PlaybookRunResponse:
79
105
  """Launch an Ansible playbook to modify or deploy a subscription instance.
80
106
 
81
107
  The response will contain either a job ID, or error information.
@@ -83,9 +109,11 @@ def run_playbook_endpoint(params: PlaybookRunParams) -> JSONResponse:
83
109
  :param PlaybookRunParams params: Parameters for executing a playbook.
84
110
  :return JSONResponse: Response from the Ansible runner, including a run ID.
85
111
  """
86
- return run_playbook(
87
- playbook_path=get_playbook_path(params.playbook_name),
112
+ job_id = run_playbook(
113
+ playbook_path=params.playbook_name,
88
114
  extra_vars=params.extra_vars,
89
115
  inventory=params.inventory,
90
116
  callback=params.callback,
91
117
  )
118
+
119
+ return PlaybookRunResponse(job_id=job_id)
@@ -0,0 +1,64 @@
1
+ # Copyright 2023-2024 GÉANT Vereniging.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+ """Module defines tasks for executing Ansible playbooks asynchronously using Celery.
15
+
16
+ The primary task, `run_playbook_proc_task`, runs an Ansible playbook and sends a POST request with
17
+ the results to a specified callback URL.
18
+ """
19
+
20
+ import logging
21
+ from typing import Any
22
+
23
+ import ansible_runner
24
+ import requests
25
+ from starlette import status
26
+
27
+ from lso.config import settings
28
+ from lso.worker import RUN_PLAYBOOK, celery
29
+
30
+ logger = logging.getLogger(__name__)
31
+
32
+
33
+ class CallbackFailedError(Exception):
34
+ """Exception raised when a callback url can't be reached."""
35
+
36
+
37
+ @celery.task(name=RUN_PLAYBOOK) # type: ignore[misc]
38
+ def run_playbook_proc_task(
39
+ job_id: str, playbook_path: str, extra_vars: dict[str, Any], inventory: dict[str, Any] | str, callback: str
40
+ ) -> None:
41
+ """Celery task to run a playbook.
42
+
43
+ :param str job_id: Identifier of the job being executed.
44
+ :param str playbook_path: Path to the playbook to be executed.
45
+ :param dict[str, Any] extra_vars: Extra variables to pass to the playbook.
46
+ :param dict[str, Any] | str inventory: Inventory to run the playbook against.
47
+ :param HttpUrl callback: Callback URL for status updates.
48
+ :return: None
49
+ """
50
+ msg = f"playbook_path: {playbook_path}, callback: {callback}"
51
+ logger.info(msg)
52
+ ansible_playbook_run = ansible_runner.run(playbook=playbook_path, inventory=inventory, extravars=extra_vars)
53
+
54
+ payload = {
55
+ "status": ansible_playbook_run.status,
56
+ "job_id": job_id,
57
+ "output": ansible_playbook_run.stdout.readlines(),
58
+ "return_code": int(ansible_playbook_run.rc),
59
+ }
60
+
61
+ request_result = requests.post(str(callback), json=payload, timeout=settings.REQUEST_TIMEOUT_SEC)
62
+ if not status.HTTP_200_OK <= request_result.status_code < status.HTTP_300_MULTIPLE_CHOICES:
63
+ msg = f"Callback failed: {request_result.text}, url: {callback}"
64
+ raise CallbackFailedError(msg)
@@ -0,0 +1,52 @@
1
+ # Copyright 2023-2024 GÉANT Vereniging.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+ """Module that sets up :term:`LSO` as a Celery worker."""
15
+
16
+ from celery import Celery
17
+ from celery.signals import worker_shutting_down
18
+
19
+ from lso.config import settings
20
+
21
+ RUN_PLAYBOOK = "lso.tasks.run_playbook_proc_task"
22
+
23
+ celery = Celery(
24
+ "lso-worker",
25
+ broker=settings.CELERY_BROKER_URL,
26
+ backend=settings.CELERY_RESULT_BACKEND,
27
+ )
28
+
29
+ if settings.TESTING:
30
+ celery.conf.update(backend=settings.CELERY_RESULT_BACKEND, task_ignore_result=False)
31
+ else:
32
+ celery.conf.update(task_ignore_result=True)
33
+
34
+ celery.conf.update(
35
+ result_expires=settings.CELERY_RESULT_EXPIRES,
36
+ worker_prefetch_multiplier=1,
37
+ worker_send_task_event=True,
38
+ task_send_sent_event=True,
39
+ redbeat_redis_url=settings.CELERY_BROKER_URL,
40
+ broker_connection_retry_on_startup=True,
41
+ )
42
+
43
+ if settings.WORKER_QUEUE_NAME:
44
+ celery.conf.task_routes = {
45
+ RUN_PLAYBOOK: {"queue": settings.WORKER_QUEUE_NAME},
46
+ }
47
+
48
+
49
+ @worker_shutting_down.connect # type: ignore[misc]
50
+ def worker_shutting_down_handler(sig, how, exitcode, **kwargs) -> None: # type: ignore[no-untyped-def] # noqa: ARG001
51
+ """Handle the Celery worker shutdown event."""
52
+ celery.close()
@@ -29,13 +29,15 @@ classifiers = [
29
29
  "Programming Language :: Python :: 3.12",
30
30
  ]
31
31
  dependencies = [
32
- "ansible-runner~=2.4.0",
33
- "ansible~=10.1.0",
34
- "fastapi~=0.112.1",
35
- "httpx~=0.27.0",
36
- "jsonschema~=4.21.1",
37
- "uvicorn[standard]~=0.30.6",
38
- "requests~=2.31.0"
32
+ "ansible-runner==2.4.0",
33
+ "ansible==10.6.0",
34
+ "fastapi==0.115.5",
35
+ "httpx==0.28.0",
36
+ "uvicorn[standard]==0.32.1",
37
+ "requests==2.32.3",
38
+ "pydantic-settings==2.6.1",
39
+ "celery==5.4.0",
40
+ "redis==5.2.0",
39
41
  ]
40
42
 
41
43
  readme = "README.md"