112 lines
4.0 KiB
Python
112 lines
4.0 KiB
Python
"""
|
|
Einfache Proxmox Task-Funktionen mit TaskLogger-Integration
|
|
"""
|
|
|
|
import logging
|
|
|
|
from lib.proxmox import Proxmox
|
|
from tasklogger.models import TaskFactory
|
|
|
|
|
|
def start_container_with_task(task_uuid: str, vmid: int, request=None) -> bool:
|
|
"""Container starten mit TaskLogger-Monitoring"""
|
|
task = TaskFactory(task_uuid=task_uuid, request=request)
|
|
|
|
task.add_entry(f"Starting container {vmid}...")
|
|
|
|
def _start_container():
|
|
with Proxmox() as pm:
|
|
return pm.lxc_post(f'{vmid}/status/start')
|
|
|
|
# Wrap the proxmox function - this handles UPID monitoring synchronously
|
|
task.wrap_proxmox_function(_start_container)
|
|
task.add_entry(f"Container {vmid} started successfully")
|
|
task.unset_as_current()
|
|
return True
|
|
|
|
|
|
def stop_container_with_task(task_uuid: str, vmid: int, request=None) -> bool:
|
|
"""Container stoppen mit TaskLogger-Monitoring"""
|
|
task = TaskFactory(task_uuid=task_uuid, request=request)
|
|
task.add_entry(f"Stopping container {vmid}...")
|
|
|
|
def _stop_container():
|
|
with Proxmox() as pm:
|
|
return pm.lxc_post(f'{vmid}/status/stop')
|
|
|
|
# Wrap the proxmox function - this handles UPID monitoring synchronously
|
|
task.wrap_proxmox_function(_stop_container)
|
|
task.add_entry(f"Container {vmid} stopped successfully")
|
|
task.unset_as_current()
|
|
return True
|
|
|
|
|
|
def resize_container_disk_with_task(task_uuid: str, vmid: int, disk_size: int, request=None) -> bool:
|
|
"""Container-Disk vergrößern mit TaskLogger-Monitoring"""
|
|
|
|
task = TaskFactory(task_uuid=task_uuid, request=request)
|
|
task.add_entry(f"Resizing disk for container {vmid} to {disk_size}GB...")
|
|
|
|
def _resize_container_disk():
|
|
with Proxmox() as pm:
|
|
return pm.lxc_put(f'{vmid}/resize', disk='rootfs', size=f'{disk_size}G')
|
|
|
|
# Wrap the proxmox function - this handles UPID monitoring synchronously
|
|
task.wrap_proxmox_function(_resize_container_disk)
|
|
task.add_entry(f"Container {vmid} disk resized to {disk_size}GB successfully")
|
|
task.unset_as_current()
|
|
return True
|
|
|
|
|
|
def create_container_with_task(task_uuid: str, clone_container, request=None) -> bool:
|
|
"""Container erstellen mit TaskLogger-Monitoring"""
|
|
try:
|
|
# CloneContainer.execute() uses the tasklogger directly now
|
|
clone_container.execute(task_uuid_override=task_uuid, request=request)
|
|
return True
|
|
except Exception as e:
|
|
logging.exception(f"Container creation failed: {e}")
|
|
return False
|
|
|
|
|
|
def delete_container_with_task(task_uuid: str, container, request=None) -> bool:
|
|
"""Container löschen mit TaskLogger-Monitoring"""
|
|
task = TaskFactory(task_uuid=task_uuid, request=request)
|
|
task.add_entry(f"Deleting container {container.name}...")
|
|
|
|
try:
|
|
# Delete Proxmox LXC (if needed)
|
|
task.add_entry(f"Deleting Proxmox container {container.lxc.vmid}...")
|
|
# Wrap the proxmox function - this handles UPID monitoring synchronously
|
|
task.wrap_proxmox_function(container.delete, task=task)
|
|
task.add_entry("Container deleted successfully!")
|
|
|
|
task.status = 'completed'
|
|
task.save()
|
|
return True
|
|
|
|
except Exception as e:
|
|
task.add_entry(f"Error deleting container: {str(e)}")
|
|
task.status = 'error'
|
|
task.save()
|
|
logging.exception(f"Container deletion failed: {e}")
|
|
return False
|
|
finally:
|
|
task.unset_as_current()
|
|
|
|
|
|
def update_container_config_sync(vmid: int, task=None,**config_params) -> bool:
|
|
"""Synchrone Container-Config-Updates (Memory, Cores) - kein Task-Monitoring nötig"""
|
|
try:
|
|
with Proxmox() as pm:
|
|
result = pm.lxc_put(f'{vmid}/config', **config_params)
|
|
logging.info(f"Updated container {vmid} config: {config_params}")
|
|
if task:
|
|
task.add_entry(f"Updated container {vmid} config: {config_params}")
|
|
return True
|
|
except Exception as e:
|
|
logging.error(f"Failed to update container {vmid} config: {e}")
|
|
if task:
|
|
task.add_entry(f"Failed to update container {vmid} config: {e}")
|
|
return False
|