1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-10-31 19:44:16 +00:00

refresh instance pools implementation

This commit is contained in:
cserma 2023-04-27 16:04:43 +03:00
parent 407605761f
commit da9fbeaff4
2 changed files with 24 additions and 13 deletions

View file

@ -2,7 +2,6 @@ import contextlib
import json import json
import os.path import os.path
import time import time
import subprocess
try: try:
from libvirt import ( from libvirt import (
@ -1255,6 +1254,7 @@ class wvmInstance(wvmConnect):
return iso return iso
def delete_all_disks(self): def delete_all_disks(self):
self.refresh_instance_pools()
disks = self.get_disk_devices() disks = self.get_disk_devices()
for disk in disks: for disk in disks:
vol = self.get_volume_by_path(disk.get("path")) vol = self.get_volume_by_path(disk.get("path"))
@ -1318,8 +1318,8 @@ class wvmInstance(wvmConnect):
</domainsnapshot>""" </domainsnapshot>"""
self._snapshotCreateXML(xml, VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY) self._snapshotCreateXML(xml, VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY)
self.refresh_instance_pools()
def get_external_snapshots(self): def get_external_snapshots(self):
return self.get_snapshot(VIR_DOMAIN_SNAPSHOT_LIST_EXTERNAL) return self.get_snapshot(VIR_DOMAIN_SNAPSHOT_LIST_EXTERNAL)
@ -1350,29 +1350,23 @@ class wvmInstance(wvmConnect):
def revert_external_snapshot(self, name, date, desc): def revert_external_snapshot(self, name, date, desc):
pool = None
snap = self.instance.snapshotLookupByName(name, 0) snap = self.instance.snapshotLookupByName(name, 0)
snap_xml = snap.getXMLDesc(0) snap_xml = snap.getXMLDesc(0)
snapXML = ElementTree.fromstring(snap_xml) snapXML = ElementTree.fromstring(snap_xml)
disks = snapXML.findall('inactiveDomain/devices/disk')
if not disks: disks = snapXML.findall('domain/devices/disk')
self.start(flags=VIR_DOMAIN_START_PAUSED) if self.get_status() == 5 else None self.start(flags=VIR_DOMAIN_START_PAUSED) if self.get_status() == 5 else None
self.delete_all_disks()
disk_info = self.get_disk_devices()
for disk in disk_info:
vol_snap = self.get_volume_by_path(disk["path"])
pool = vol_snap.storagePoolLookupByVolume()
pool.refresh(0)
vol_snap.delete(0)
self.force_shutdown() self.force_shutdown()
snap.delete(VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY) snap.delete(VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY)
disks = snapXML.findall('inactiveDomain/devices/disk')
if not disks: disks = snapXML.findall('domain/devices/disk')
for disk in disks: for disk in disks:
self.instance.updateDeviceFlags(ElementTree.tostring(disk).decode("UTF-8")) self.instance.updateDeviceFlags(ElementTree.tostring(disk).decode("UTF-8"))
name = name.replace("s1", "s2") name = name.replace("s1", "s2")
self.create_external_snapshot(name, date, desc) self.create_external_snapshot(name, date, desc)
pool.refresh() if pool else None
def get_snapshot(self, flag=VIR_DOMAIN_SNAPSHOT_LIST_INTERNAL): def get_snapshot(self, flag=VIR_DOMAIN_SNAPSHOT_LIST_INTERNAL):
snapshots = [] snapshots = []
@ -1413,6 +1407,15 @@ class wvmInstance(wvmConnect):
def get_wvmStorages(self): def get_wvmStorages(self):
return wvmStorages(self.host, self.login, self.passwd, self.conn) return wvmStorages(self.host, self.login, self.passwd, self.conn)
def refresh_instance_pools(self):
disks = self.get_disk_devices()
target_paths = set()
for disk in disks:
disk_path = disk.get("path")
target_paths.add(os.path.dirname(disk_path))
for target_path in target_paths:
self.get_wvmStorages().get_pool_by_target(target_path).refresh(0)
def fix_mac(self, mac): def fix_mac(self, mac):
if ":" in mac: if ":" in mac:
return mac return mac

View file

@ -28,6 +28,14 @@ class wvmStorages(wvmConnect):
def define_storage(self, xml, flag): def define_storage(self, xml, flag):
self.wvm.storagePoolDefineXML(xml, flag) self.wvm.storagePoolDefineXML(xml, flag)
def get_pool_by_target(self, target):
pool_names = self.get_storages()
for pool_name in pool_names:
stg = wvmStorage(self.host, self.login, self.passwd, self.conn, pool_name)
if stg.get_target_path() == target:
return self.get_storage(pool_name)
return None
def create_storage(self, stg_type, name, source, target): def create_storage(self, stg_type, name, source, target):
xml = f"""<pool type='{stg_type}'> xml = f"""<pool type='{stg_type}'>
<name>{name}</name>""" <name>{name}</name>"""