Skip to content

Instantly share code, notes, and snippets.

@marcelcaraciolo
Created July 19, 2014 04:35
Show Gist options
  • Save marcelcaraciolo/af59841554fdc764e2f3 to your computer and use it in GitHub Desktop.
Save marcelcaraciolo/af59841554fdc764e2f3 to your computer and use it in GitHub Desktop.
example vm
class VM(object):
def __init__(self, uuid, name, status):
self.uuid = uuid
self.name = name
self.status = status
def __repr__(self):
return '%s - %s (%s)' % (self.uuid, self.name, self.status)
def export(self, directory_name=None):
start = time.time()
timestamp = time.strftime("%Y-%m-%d_%H-%M-%S", time.gmtime())
file_name = 'backup-%s--%s.xva' % (timestamp, self.name)
if directory_name is not None:
file_name = os.path.join(directory_name, file_name)
# create a snapshot
snapshot_uuid = check_output(['xe', 'vm-snapshot', 'uuid=' + self.uuid,
'new-name-label=backup-' + self.name]).strip()
logging.info('xe vm-snapshot uuid=%s new-name-label=backup-' % (self.uuid) + self.name + '...OK')
# change some params so that the snapshot can be exported
cmd = "xe template-param-set is-a-template=false ha-always-run=false uuid=" + snapshot_uuid
check_output(cmd, shell=True)
logging.info(cmd + '...OK')
# export snapshot
cmd = 'xe vm-export vm=%s filename="%s" compress=true' % (snapshot_uuid, file_name)
check_output(['xe', 'vm-export', 'vm=' + snapshot_uuid,
'filename=' + file_name, 'compress=true'])
logging.info(cmd + '...OK')
# remove old snapshot again
cmd = "xe vm-uninstall uuid=%s force=true" % snapshot_uuid
check_output(cmd, shell=True)
logging.info(cmd + '...OK')
logging.info('Exported VM "%s" in %s seconds.' % (self.name, time.time() - start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment