Skip to content

Commit 281a7b4

Browse files
committed
ScenarioCloneTemplateVm cloned first VM
1 parent e3b910a commit 281a7b4

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/cluster_tasks/scenarios/scenario_base.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
1+
import asyncio
2+
import logging
3+
import time
14
from abc import ABC, abstractmethod
25

36
from ext_api.proxmox_api import ProxmoxAPI
47

8+
logger = logging.getLogger("CT.{__name__}")
9+
510

611
class ScenarioBase(ABC):
712
def __init__(self, api: ProxmoxAPI):
813
self.name = self.__class__.__name__
914
self.api: ProxmoxAPI = api
1015

16+
@staticmethod
17+
def get_status(api: ProxmoxAPI, node: str, upid: str):
18+
result = api.nodes(node).tasks(upid).status.get(filter_keys="status")
19+
return result
20+
21+
def wait_task_done(self, api: ProxmoxAPI, node: str, upid: str):
22+
while self.get_status(api, node, upid) != "stopped":
23+
logger.info("Waiting for task to finish...")
24+
time.sleep(2)
25+
26+
@staticmethod
27+
async def async_get_status(api: ProxmoxAPI, node: str, upid: str):
28+
result = await api.nodes(node).tasks(upid).status.get(filter_keys="status")
29+
return result
30+
31+
async def async_wait_task_done(self, api: ProxmoxAPI, node: str, upid: str):
32+
while await self.async_get_status(api, node, upid) != "stopped":
33+
logger.info("Waiting for task to finish...")
34+
await asyncio.sleep(2)
35+
1136
@abstractmethod
1237
def run(self):
1338
"""Method to execute the scenario"""

src/cluster_tasks/scenarios/scenario_clone_template_vm.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,29 @@ def __init__(self, api: ProxmoxAPI):
1616
def configure(self, config):
1717
self.node = config.get("node")
1818
self.vmid = config.get("vmid")
19-
self.newid = config.get("name")
20-
self.full = config.get("full", True)
19+
self.newid = config.get("newid")
20+
self.name = config.get("name")
21+
self.full = int(config.get("full", True))
2122

2223
def run(self):
2324
print(f"Running Scenario Template: {self.template_name} at {self.source_node}")
2425
# Perform the specific API logic for this scenario
2526

26-
data = {"newid": self.newid, "name": self.name}
27+
# data = {"newid": int(self.newid), "name": self.name, "full": self.full}
2728
with self.api as api:
28-
result = api.nodes(self.node).qemu(self.vmid).clone.create(data=data)
29-
logger.info(result)
29+
present = (
30+
api.nodes(self.node)
31+
.qemu(self.newid)
32+
.status.current.get(filter_keys="vmid")
33+
)
34+
logger.info(f"result: {present} {self.newid} {self.newid==present}")
35+
if present and int(present) == int(self.newid):
36+
logger.info(f"VM {self.newid} already exists - Deleteting...")
37+
upid = api.nodes(self.node).qemu(self.newid).delete()
38+
logger.info(f"result: {upid}")
39+
self.wait_task_done(api, self.node, upid)
40+
data = {"newid": int(self.newid), "name": self.name, "full": self.full}
41+
upid = api.nodes(self.node).qemu(self.vmid).clone.create(data=data)
42+
logger.info(f"result: {upid}, data: {data}")
43+
self.wait_task_done(api, self.node, upid)
44+
logger.info(f"VM {self.newid} created")

src/cluster_tasks/scenarios_configs.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Scenarios:
33
file: "clone_template_vm"
44
config:
55
node: "c01"
6-
vmid: "1004"
7-
newid: "201"
6+
vmid: 1004
7+
newid: 201
88
name: "Cloned01"
9-
full: True
9+
full: 1

0 commit comments

Comments
 (0)