Skip to content

Commit da7ced8

Browse files
committed
README ## Scenarios
1 parent 80ae721 commit da7ced8

File tree

5 files changed

+112
-8
lines changed

5 files changed

+112
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ The primary goal of this project is to streamline Proxmox VE cluster management,
3434

3535
- [API Reference](docs/part2-api-reference.md)
3636
- [Setup Instructions](docs/part3-setup.md)
37+
- [Scenarios](docs/part6-scenarios.md)
38+
- NodeTasks Class
3739
- ProxmoxAPI Class
3840
- [Examples](docs/part4-1-proxmoxapi-examples.md)
3941
- [Advanced Concurrent Usage and Low-Level API Requests](docs/part4-2-advanced-examples.md)

docs/part6-scenarios.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
## Scenarios
2+
3+
### Setup
4+
5+
Example:
6+
```python
7+
config_file = Path(__file__).parent / "scenarios_configs.yaml"
8+
scenarios_config = ConfigLoader(file_path=config_file)
9+
backend_name = scenarios_config.get("API.backend", "https")
10+
register_backends(backend_name)
11+
ext_api = ProxmoxAPI(backend_name=backend_name, backend_type="sync")
12+
with ext_api as api:
13+
node_tasks = NodeTasksSync(api=api) # Pass the api instance to NodeTasksAsync
14+
for v in scenarios_config.get("Scenarios").values():
15+
scenario_file = v.get("file")
16+
config = v.get("config")
17+
# Create scenario instance using the factory
18+
scenario = ScenarioFactory.create_scenario(scenario_file, config)
19+
# Run the scenario
20+
scenario.run(node_tasks)
21+
```
22+
23+
### Config
24+
#### Folder tree
25+
26+
```bash
27+
cluster_tasks
28+
├── scenarios_configs.yaml
29+
├── scenarios
30+
│ ├── clone_template_vm_async.py (class ScenarioCloneTemplateVmAsync)
31+
│ ├── clone_template_vm_sync.py (class ScenarioCloneTemplateVmSync)
32+
````
33+
#### Scenarios config `scenarios_configs.yaml`
34+
```yaml
35+
API:
36+
backend: "https"
37+
Scenarios:
38+
CloneTemplateVM:
39+
file: "clone_template_vm"
40+
config:
41+
node: "c01"
42+
vmid: 1004
43+
newid: 201
44+
name: "Cloned01"
45+
full: 1
46+
```
47+
48+
#### Result
49+
<details>
50+
<summary>src/cluster_tasks/controller_sync.py</summary>
51+
52+
``` pycon
53+
python /src/cluster_tasks/controller_sync.py
54+
DEBUG: Scenarios config: <config.config.ConfigLoader object at 0x10fd0b350>
55+
DEBUG: Creating backend: https of type: sync
56+
Running Scenario Template VM Clone: ScenarioCloneTemplateVmSync
57+
DEBUG: Generated a new task ID: 036b0cf8-c506-42ae-bb16-41f79d8708d2
58+
DEBUG: Formatted endpoint: /api2/json/nodes/c01/qemu/201/status/current
59+
INFO: VM 201 already exists - Deleting...
60+
DEBUG: Generated a new task ID: d851e29d-11b9-49f0-ac48-b4be53f66a47
61+
DEBUG: Formatted endpoint: /api2/json/nodes/c01/qemu/201
62+
DEBUG: Generated a new task ID: fb7e60d4-9f45-490b-ba48-c2b7c408f297
63+
DEBUG: Formatted endpoint: /api2/json/nodes/c01/tasks/UPID:c01:002DC36D:045F99E0:677A9183:qmdestroy:201:api_user@pam!cluster_helper:/status
64+
INFO: Waiting for task to finish... [ 0:00:00 / 0:01:00 ]
65+
DEBUG: Generated a new task ID: cb281c02-d5de-4f5e-9362-11f83e261250
66+
DEBUG: Formatted endpoint: /api2/json/nodes/c01/tasks/UPID:c01:002DC36D:045F99E0:677A9183:qmdestroy:201:api_user@pam!cluster_helper:/status
67+
INFO: VM 201 deleted successfully
68+
DEBUG: Generated a new task ID: 1f3ce5b8-2ac1-4889-b43c-5ddfc76b949a
69+
DEBUG: Formatted endpoint: /api2/json/nodes/c01/qemu/1004/clone
70+
DEBUG: Generated a new task ID: 2bb19080-44ab-43ce-9b30-a3d8c4d0c51f
71+
DEBUG: Formatted endpoint: /api2/json/nodes/c01/tasks/UPID:c01:002DC388:045F9AE2:677A9185:qmclone:1004:api_user@pam!cluster_helper:/status
72+
INFO: Waiting for task to finish... [ 0:00:00 / 0:01:00 ]
73+
DEBUG: Generated a new task ID: 3398e65b-ddad-427b-af86-ee2735e54907
74+
DEBUG: Formatted endpoint: /api2/json/nodes/c01/tasks/UPID:c01:002DC388:045F9AE2:677A9185:qmclone:1004:api_user@pam!cluster_helper:/status
75+
INFO: Waiting for task to finish... [ 0:00:02 / 0:01:00 ]
76+
DEBUG: Generated a new task ID: 6cafba81-fa97-48a8-9b51-c11df3bbe1d8
77+
DEBUG: Formatted endpoint: /api2/json/nodes/c01/tasks/UPID:c01:002DC388:045F9AE2:677A9185:qmclone:1004:api_user@pam!cluster_helper:/status
78+
INFO: Waiting for task to finish... [ 0:00:04 / 0:01:00 ]
79+
DEBUG: Generated a new task ID: 507cb3aa-7a72-4b01-b1f0-9a52a8f92b99
80+
DEBUG: Formatted endpoint: /api2/json/nodes/c01/tasks/UPID:c01:002DC388:045F9AE2:677A9185:qmclone:1004:api_user@pam!cluster_helper:/status
81+
INFO: Waiting for task to finish... [ 0:00:06 / 0:01:00 ]
82+
DEBUG: Generated a new task ID: f4cb2983-3d06-49d0-b221-6336da330ce1
83+
DEBUG: Formatted endpoint: /api2/json/nodes/c01/tasks/UPID:c01:002DC388:045F9AE2:677A9185:qmclone:1004:api_user@pam!cluster_helper:/status
84+
INFO: VM 201 cloned successfully
85+
86+
Process finished with exit code 0
87+
```
88+
</details>
89+
90+
### ScenarioFactory
91+
92+
```python
93+
scenario = ScenarioFactory.create_scenario(scenario_file, config)
94+
```
95+
96+
97+
### ScenarioBase
98+
99+
100+
101+
102+
103+
[README](../README.md)

src/cluster_tasks/controller_sync.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@
1616

1717
def main():
1818
config_file = Path(__file__).parent / "scenarios_configs.yaml"
19-
scenarios_config = ConfigLoader(file_path=config_file).settings.copy()
19+
scenarios_config = ConfigLoader(file_path=config_file)
2020
# Iterate over the scenarios and run them
2121
logger.debug(f"Scenarios config: {scenarios_config}")
22-
23-
register_backends(["https"])
24-
ext_api = ProxmoxAPI(backend_name="https", backend_type="sync")
22+
backend_name = scenarios_config.get("API.backend", "https")
23+
register_backends(backend_name)
24+
ext_api = ProxmoxAPI(backend_name=backend_name, backend_type="sync")
2525
with ext_api as api:
2626
node_tasks = NodeTasksSync(api=api) # Pass the api instance to NodeTasksAsync
27-
for k, v in scenarios_config.get("Scenarios").items():
27+
for v in scenarios_config.get("Scenarios").values():
2828
scenario_file = v.get("file")
2929
config = v.get("config")
30-
3130
# Create scenario instance using the factory
3231
scenario = ScenarioFactory.create_scenario(scenario_file, config)
33-
3432
# Run the scenario
3533
scenario.run(node_tasks)
3634

src/cluster_tasks/scenarios_configs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
API:
22
backend: "https"
3-
type: "sync"
43
Scenarios:
54
CloneTemplateVM:
65
file: "clone_template_vm"

src/ext_api/backends/registry.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def register_backends(names: list[str] | str = None):
1717
names = [names]
1818

1919
for name in names:
20+
if name is None:
21+
continue
2022
name = name.strip().lower()
2123
if name not in BACKENDS_NAMES:
2224
logger.error(f"Unsupported backend: {name}")

0 commit comments

Comments
 (0)