stop/restart on clone running container
This commit is contained in:
@@ -19,6 +19,7 @@ from lib.db import (
|
|||||||
)
|
)
|
||||||
from lib.proxmox import Proxmox
|
from lib.proxmox import Proxmox
|
||||||
from mikrotik.models import DNSStatic, IPAddress, IPDHCPLease
|
from mikrotik.models import DNSStatic, IPAddress, IPDHCPLease
|
||||||
|
from proxmox.models import Lxc
|
||||||
|
|
||||||
|
|
||||||
class MinValueValidatorExtended(MinValueValidator):
|
class MinValueValidatorExtended(MinValueValidator):
|
||||||
@@ -87,6 +88,11 @@ class CloneContainer(CloneAbstract):
|
|||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self._old_active = self.is_active
|
self._old_active = self.is_active
|
||||||
|
|
||||||
|
def get_clone_machine_status(self):
|
||||||
|
self.vm.sync_from_proxmox()
|
||||||
|
return self.vm.status
|
||||||
|
|
||||||
|
|
||||||
def execute(self, task_uuid_override=None, *args, **kwargs):
|
def execute(self, task_uuid_override=None, *args, **kwargs):
|
||||||
from proxmox.models import Lxc
|
from proxmox.models import Lxc
|
||||||
from tasklogger.models import TaskFactory
|
from tasklogger.models import TaskFactory
|
||||||
@@ -99,6 +105,12 @@ class CloneContainer(CloneAbstract):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
data = self.proxmox_data
|
data = self.proxmox_data
|
||||||
|
old_status = None
|
||||||
|
if self.vm:
|
||||||
|
old_status = self.get_clone_machine_status()
|
||||||
|
if old_status == 'running':
|
||||||
|
task.add_entry(f"Stopping container '{self.vm.name}'")
|
||||||
|
self.vm.stop()
|
||||||
pm = Proxmox()
|
pm = Proxmox()
|
||||||
route = data.pop('route')
|
route = data.pop('route')
|
||||||
vmdata = data.pop('vm')
|
vmdata = data.pop('vm')
|
||||||
@@ -128,6 +140,7 @@ class CloneContainer(CloneAbstract):
|
|||||||
}
|
}
|
||||||
task.add_entry(f"Cloning container {self.vm.vmid}")
|
task.add_entry(f"Cloning container {self.vm.vmid}")
|
||||||
|
|
||||||
|
|
||||||
success = False
|
success = False
|
||||||
lxc = None
|
lxc = None
|
||||||
lease = None
|
lease = None
|
||||||
@@ -141,9 +154,13 @@ class CloneContainer(CloneAbstract):
|
|||||||
|
|
||||||
def _lxc_post():
|
def _lxc_post():
|
||||||
return pm.lxc_post(route, **vmparams)
|
return pm.lxc_post(route, **vmparams)
|
||||||
|
def _restart_cloned():
|
||||||
|
return self.vm.start()
|
||||||
|
|
||||||
# Wrap the proxmox function - this handles UPID monitoring synchronously
|
# Wrap the proxmox function - this handles UPID monitoring synchronously
|
||||||
task.wrap_proxmox_function(_lxc_post)
|
task.wrap_proxmox_function(_lxc_post)
|
||||||
|
if old_status == 'running':
|
||||||
|
task.wrap_proxmox_function(_restart_cloned)
|
||||||
task.add_entry("Container creation completed")
|
task.add_entry("Container creation completed")
|
||||||
success = True
|
success = True
|
||||||
|
|
||||||
|
|||||||
@@ -58,9 +58,10 @@ class ProxmoxAbstractModel(BaseModel, TaskAwareModelMixin):
|
|||||||
def int_fields(self):
|
def int_fields(self):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def from_proxmox(self, **kwargs):
|
def from_proxmox(self, write_to_db=True, **kwargs):
|
||||||
"""Sets the values in DB
|
"""Sets the values in DB
|
||||||
Extracts csv Values into self, if given"""
|
Extracts csv Values into self, if given"""
|
||||||
|
|
||||||
logging.debug(f"Got {kwargs} from proxmox")
|
logging.debug(f"Got {kwargs} from proxmox")
|
||||||
kwkeys = list(kwargs.keys())
|
kwkeys = list(kwargs.keys())
|
||||||
params = {}
|
params = {}
|
||||||
@@ -92,7 +93,10 @@ class ProxmoxAbstractModel(BaseModel, TaskAwareModelMixin):
|
|||||||
v = no_int_re.sub('', _v) or 0
|
v = no_int_re.sub('', _v) or 0
|
||||||
params[k] = v
|
params[k] = v
|
||||||
logging.debug(f"No CSValues for {self}")
|
logging.debug(f"No CSValues for {self}")
|
||||||
|
if write_to_db:
|
||||||
return self.write(**params)
|
return self.write(**params)
|
||||||
|
else:
|
||||||
|
return params
|
||||||
|
|
||||||
|
|
||||||
class Lxc(ProxmoxAbstractModel):
|
class Lxc(ProxmoxAbstractModel):
|
||||||
@@ -205,8 +209,9 @@ class Lxc(ProxmoxAbstractModel):
|
|||||||
if not data:
|
if not data:
|
||||||
logging.warning(f'Could not find {self.vmid} in proxmox - deleting from local database!')
|
logging.warning(f'Could not find {self.vmid} in proxmox - deleting from local database!')
|
||||||
return self.delete()
|
return self.delete()
|
||||||
self.from_proxmox(**data)
|
statusdata = pm.lxc_get(f'{self.vmid}/status/current')
|
||||||
super().save()
|
data |= statusdata
|
||||||
|
self.from_proxmox(write_to_db=True, **data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Could not get config for {self.vmid} - {e}")
|
logging.error(f"Could not get config for {self.vmid} - {e}")
|
||||||
return self
|
return self
|
||||||
|
|||||||
Reference in New Issue
Block a user