Last active
October 5, 2015 23:38
-
-
Save syndicut/2896404 to your computer and use it in GitHub Desktop.
Replaced check_call with Popen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import contextlib, os, finalize_vm, subprocess | |
backup_path = '/mnt/backup/tmp/backup' | |
@contextlib.contextmanager | |
def flock(path): | |
while True: | |
try: | |
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR) | |
else: | |
break | |
try: | |
yield fd | |
finally: | |
os.close(fd) | |
os.unlink(path) | |
def add_to_backup_store(source): | |
dom_name = os.path.basename(source) | |
store = '%s/%s' % (store_path, dom_name) | |
print subprocess.Popen([ | |
'duplicity', | |
'--name', dom_name, | |
'--no-encryption', | |
'--volsize','1024', | |
'--tempdir','/dev/shm', | |
source, | |
store | |
], stdout=subprocess.PIPE).communicate()[0] | |
if __name__=="__main__": | |
for dom_path in os.listdir(backup_path): | |
dom_name = os.path.basename(dom_path) | |
lock_file = '%s/%s.lock' % (dom_path, dom_name) | |
if not os.path.exists(lock_file): | |
with flock(lock_file): | |
finalize(os.path.dirname(dom_path), dom_name) | |
add_to_backup_store(dom_path) | |
rmtree(dom_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import libvirt | |
import sys | |
import os | |
import time | |
from xml.etree import ElementTree | |
def prepare_xml(xml_file): | |
"""Function that prepares vm xml for starting on backup machine | |
It adds the correct path to disks, changes network to "default" | |
and add '-backup' postfix to machine name""" | |
etree = ElementTree.ElementTree() | |
tree = etree.parse(xml_file) | |
dom_name = tree.find('name').text | |
for disk in tree.findall("devices/disk"): | |
if disk.get('device') == 'disk': | |
dev=disk.find('target').get("dev") | |
disk.find('source').set('dev', "%s/%s_%s.%s" % (os.path.dirname(xml_file), dom_name, dev, 'img') ) | |
for interface in tree.findall('devices/interface'): | |
target = interface.find('target') | |
interface.remove(target) | |
alias = interface.find('alias') | |
interface.remove(alias) | |
source = interface.find('source') | |
source.clear() | |
source.set('network', 'default') | |
name = dom_name+'-backup' | |
tree.find('name').text = name | |
return ElementTree.tostring(tree) | |
def finalize(backup_path, dom_name): | |
dom_path = '%s/%s' % (backup_path, dom_name) | |
xml_file = '%s/%s.xml' % (dom_path, dom_name) | |
dom_xml = prepare_xml(xml_file) | |
state = "%s/%s.state" % (dom_path, dom_name) | |
timer = 0 | |
timeout = 120 | |
conn=libvirt.open("qemu:///system") | |
conn.saveImageDefineXML(state, dom_xml, 0) | |
network = conn.networkLookupByName('default') | |
if not network.isActive(): | |
network.create() | |
try: | |
conn.restore(state) | |
dom = conn.lookupByName(dom_name+'-backup') | |
dom.shutdown() | |
print 'Waiting for domain to shutdown' | |
while (timer <= timeout): | |
try: | |
dom_active = dom.isActive() | |
except libvirt.libvirtError: | |
break | |
else: | |
if not dom_active: | |
break | |
time.sleep(1) | |
timer += 1 | |
print '%s...' % timer | |
else: | |
raise Exception, 'Give up waiting for shutdown, not removing state file' | |
print 'Domain has been shutdown, removing state file' | |
os.remove(state) | |
finally: | |
network.destroy() | |
if __name__=="__main__": | |
dom_name = sys.argv[1] | |
finalize('/mnt/backup/tmp', dom_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment