Skip to content

Commit

Permalink
refactor: add PVEVolume and PVEBackupJob classes
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Dec 20, 2024
1 parent 4a4bbdd commit 6f7f52c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/pvecontrol/backup_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class PVEBackupJob:
"""Proxmox VE Backup Job"""

_default_kwargs = {
"compress": None,
"enabled": None,
"fleecing": None,
"mode": None,
"next-run": None,
"notes-template": None,
"prune-backups": None,
"schedule": None,
"storage": None,
"type": None,
"vmid": None,
}

def __init__(self, backup_id, **kwargs):
self.id = backup_id

for k, v in self._default_kwargs.items():
self.__setattr__(k, kwargs.get(k, v))

def __str__(self):
output = f"Vm(s): {self.vmid}\n" + f"Id: {self.id}\n"
for key in self._default_kwargs:
output += f"{key.capitalize()}: {self.__getattribute__(key)}\n"
return output
11 changes: 9 additions & 2 deletions src/pvecontrol/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pvecontrol.node import PVENode
from pvecontrol.storage import PVEStorage
from pvecontrol.task import PVETask
from pvecontrol.backup_job import PVEBackupJob
from pvecontrol.volume import PVEVolume


class PVECluster:
Expand Down Expand Up @@ -162,11 +164,16 @@ def backups(self):
if self._backups is None:
self._backups = []
for item in PVEStorage.get_grouped_list(self):
self._backups.extend(item["storage"].get_content("backup"))
for backup in item["storage"].get_content("backup"):
self._backups.append(
PVEVolume(backup.pop("volid"), backup.pop("format"), backup.pop("size"), **backup)
)
return self._backups

@property
def backup_jobs(self):
if self._backup_jobs is None:
self._backup_jobs = self.api.cluster.backup.get()
self._backup_jobs = []
for backup_job in self.api.cluster.backup.get():
self._backup_jobs.append(PVEBackupJob(backup_job.pop("id"), **backup_job))
return self._backup_jobs
4 changes: 2 additions & 2 deletions src/pvecontrol/sanitycheck/tests/vm_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def _check_is_backed_up(self):
backuped_vms = []
for vm in self.proxmox.vms():
vm_backup_jobs = vm.get_backup_jobs(self.proxmox)
vm_enabled_backup_ids = [backup["id"] for backup in vm_backup_jobs if backup["enabled"] == 1]
vm_enabled_backup_ids = [backup.id for backup in vm_backup_jobs if backup.enabled == 1]
if len(vm_enabled_backup_ids) > 0:
msg = f"Vm {vm.vmid} ({vm.name}) is associated to {len(vm_enabled_backup_ids)} enabled backup job(s)"
self.add_messages(CheckMessage(CheckCode.OK, msg))
Expand All @@ -33,7 +33,7 @@ def _check_backup_ran_recently(self, vms):

for vm in vms:
last_backup = vm.get_last_backup(self.proxmox)
last_backup_time = datetime.fromtimestamp(last_backup["ctime"])
last_backup_time = datetime.fromtimestamp(last_backup.ctime)
msg_template = f"Vm {vm.vmid} ({vm.name}) has been backed up {{}} than {time_ago} ({last_backup_time.strftime('%Y-%m-%d %H:%M:%S')})"
if last_backup_time > datetime.now() - timedelta(minutes=minutes_ago):
msg = msg_template.format("less")
Expand Down
6 changes: 3 additions & 3 deletions src/pvecontrol/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ def migrate(self, target, online=False):
def get_backup_jobs(self, proxmox):
vm_backups = []
for backup in proxmox.backup_jobs:
if str(self.vmid) in backup["vmid"].split(","):
if str(self.vmid) in backup.vmid.split(","):
vm_backups.append(backup)
return vm_backups

def get_backups(self, proxmox):
return [backup for backup in proxmox.backups if backup["vmid"] == self.vmid]
return [backup for backup in proxmox.backups if backup.vmid == self.vmid]

def get_last_backup(self, proxmox):
backups = sorted(self.get_backups(proxmox), key=lambda x: x["ctime"])
backups = sorted(self.get_backups(proxmox), key=lambda x: x.ctime)
return backups[-1] if len(backups) > 0 else None
24 changes: 24 additions & 0 deletions src/pvecontrol/volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class PVEVolume:
"""Proxmox VE Volume"""

_default_kwargs = {
"content": None,
"ctime": None,
"encrypted": None,
"notes": None,
"parent": None,
"path": None,
"protected": None,
"subtype": None,
"used": None,
"verification": None,
"vmid": None,
}

def __init__(self, volid, volume_format, size, **kwargs):
self.volid = volid
self.format = volume_format
self.size = size

for k, v in self._default_kwargs.items():
self.__setattr__(k, kwargs.get(k, v))

0 comments on commit 6f7f52c

Please sign in to comment.