Skip to content

Commit

Permalink
✨ deal with inactive domain
Browse files Browse the repository at this point in the history
skip inactive domains from backup and raise exceptions
  • Loading branch information
bunop committed Feb 7, 2022
1 parent b6c9727 commit d3c8afd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
46 changes: 20 additions & 26 deletions Lib/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,31 +176,34 @@ def getDomain(self):

return self.conn.lookupByName(self.domain_name)

@property
def domain(self):
return self.getDomain()

def domainIsActive(self):
"""Return true if domain is active (VM up and running)"""

if self.domain.isActive():
return True

return False

def getDisks(self):
"""Call getDisk on my instance"""

# get my domain
domain = self.getDomain()

# call getDisk to get the disks to do snapshot
return getDisks(domain)
return getDisks(self.domain)

def dumpXML(self, path):
"""Call dumpXML on my instance"""

# get my domain
domain = self.getDomain()

# call getDisk to get the disks to do snapshot
return dumpXML(domain, path)
return dumpXML(self.domain, path)

def hasCurrentSnapshot(self):
"""call hasCurrentSnapshot on domain class attribute"""

# get my domain
domain = self.getDomain()

if domain.hasCurrentSnapshot() == 0:
if self.domain.hasCurrentSnapshot() == 0:
return False

else:
Expand All @@ -210,9 +213,6 @@ def getSnapshotXML(self):
"""Since I need to do a Snapshot with a XML file, I will create an XML
to call the appropriate libvirt method"""

# get my domain
domain = self.getDomain()

# call getDisk to get the disks to do snapshot
self.disks = self.getDisks()

Expand All @@ -231,7 +231,7 @@ def getSnapshotXML(self):
my_cmd = (
"virsh snapshot-create-as --domain {domain_name} {snapshotId} "
"{diskspecs} --disk-only --atomic --quiesce --print-xml").format(
domain_name=domain.name(),
domain_name=self.domain.name(),
snapshotId=self.snapshotId,
diskspecs=" ".join(diskspecs))

Expand Down Expand Up @@ -281,13 +281,10 @@ def callSnapshot(self):
libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC,
libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE]

# get a domain
domain = self.getDomain()

# do a snapshot
logger.info("Creating snapshot %s for %s" %
(self.snapshotId, self.domain_name))
self.snapshot = domain.snapshotCreateXML(
self.snapshot = self.domain.snapshotCreateXML(
self.snapshot_xml, flags=sum([disk_only, atomic, quiesce]))

# Once i've created a snapshot, I can read disks to have snapshot
Expand All @@ -298,17 +295,14 @@ def callSnapshot(self):
for disk, top in iter(self.snapshot_disk.items()):
logger.debug(
"Created top image {top} for {domain_name} {disk}".format(
top=top, domain_name=domain.name(), disk=disk))
top=top, domain_name=self.domain.name(), disk=disk))

return self.snapshot

def doBlockCommit(self):
"""Do a blockcommit for every disks shapshotted"""

# get a domain
domain = self.getDomain()

logger.info("Blockcommitting %s" % (domain.name()))
logger.info("Blockcommitting %s" % (self.domain.name()))

# A blockcommit for every disks. Using names like libvirt variables.
# Base is the original image file
Expand All @@ -317,7 +311,7 @@ def doBlockCommit(self):
my_cmd = (
"virsh blockcommit {domain_name} {disk} --active "
"--verbose --pivot").format(
domain_name=domain.name(), disk=disk)
domain_name=self.domain.name(), disk=disk)
logger.debug("Executing: %s" % (my_cmd))

# split the executable
Expand Down
14 changes: 10 additions & 4 deletions kvmBackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
"""

# A function to open a config file


def loadConf(file_conf):
"""A function to open a config file"""

config = yaml.load(open(file_conf))

# read my defined domains
Expand All @@ -77,10 +77,10 @@ def loadConf(file_conf):

return mydomains, backupdir, config

# a function to check current day of the week


def checkDay(day):
"""A function to check current day of the week"""

now = datetime.datetime.now()
today = now.strftime("%a")

Expand Down Expand Up @@ -115,6 +115,12 @@ def backup(domain, parameters, backupdir):
# create a snapshot instance
snapshot = helper.Snapshot(domain)

# check if domain is active
if not snapshot.domainIsActive():
logger.error(
"domain '%s' is not Active: is VM up and running?" % domain)
raise NotImplementedError("Cannot backup an inactive domain!")

# check if no snapshot are defined
if snapshot.hasCurrentSnapshot() is True:
raise Exception("Domain '%s' has already a snapshot" % (domain))
Expand Down

0 comments on commit d3c8afd

Please sign in to comment.