def __init__(self, args):
self.args = args
self.userdir = args.userdir
self.resources = args.resources
self.credentials = args.credentials
self.resources_paws_file = args.resources_paws_file
self.verbose = args.verbose
self.util = Util(self)
self.ansible = Ansible(self.userdir)
self.resources_paws = None
# Libvirt domain/VM states
self.states = {
libvirt.VIR_DOMAIN_NOSTATE: 'no state',
libvirt.VIR_DOMAIN_RUNNING: 'running',
libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
libvirt.VIR_DOMAIN_CRASHED: 'crashed',
}
python类VIR_DOMAIN_PAUSED的实例源码
def resume_paused():
MAX_ATTEMPTS = 10
conn = libvirt.open("qemu:///system")
if conn == None:
print 'Failed to open connection to qemu:///system'
exit(1)
paused = [i.name() for i in conn.listAllDomains() if i.info()[0] == libvirt.VIR_DOMAIN_PAUSED]
if len(paused) > 0:
print "Following VM are paused"
print "\n".join(paused)
print
for vm in paused:
print "Resume VM: " + vm.name()
vm.resume()
for _ in range(MAX_ATTEMPTS):
if len([i for i in conn.listAllDomains() if i.info()[0] == libvirt.VIR_DOMAIN_PAUSED]) == 0:
break
time.sleep(1)
else:
print "Can't resume VMs:%s" % ", ".join(i.name() for i in conn.listAllDomains() if i.info()[0] == libvirt.VIR_DOMAIN_PAUSED)
paused = []
conn.close()
return paused
def suspend(self, id):
domain = self._get_domain(id)
if domain.state(0)[0] == libvirt.VIR_DOMAIN_PAUSED:
return True
return domain.suspend() == 0
def _clone(self, id, name, clonefrom):
domain = self.connection.lookupByUUIDString(id)
domainconfig = domain.XMLDesc()
name = '%s_%s.qcow2' % (name, time.time())
destination_path = os.path.join(self.templatepath, name)
if domain.state()[0] in [
libvirt.VIR_DOMAIN_SHUTDOWN,
libvirt.VIR_DOMAIN_SHUTOFF,
libvirt.VIR_DOMAIN_CRASHED,
libvirt.VIR_DOMAIN_PAUSED] or not self._isRootVolume(
domain,
clonefrom):
if not self.isLocked(id):
lock = self._lockDomain(id)
if lock != LOCKCREATED:
raise Exception('Failed to create lock: %s' % str(lock))
else:
raise Exception("Can't perform this action on a locked domain")
q2 = Qcow2(clonefrom)
try:
q2.export(destination_path)
finally:
if self.isLocked(id):
self._unlockDomain(id)
else:
domain.undefine()
try:
domain.blockRebase(clonefrom, destination_path,
0, libvirt.VIR_DOMAIN_BLOCK_REBASE_COPY)
rebasedone = False
while not rebasedone:
rebasedone = self._block_job_info(domain, clonefrom)
domain.blockJobAbort(clonefrom, 0)
except BaseException:
self.connection.defineXML(domainconfig)
raise
self.connection.defineXML(domainconfig)
return destination_path
def guest_state_report(guest):
try:
_uuid = guest.UUIDString()
state, maxmem, mem, ncpu, cputime = guest.info()
# state ?????
# http://libvirt.org/docs/libvirt-appdev-guide-python/en-US/html/libvirt_application_development_guide_using_python-Guest_Domains-Information-State.html
# http://stackoverflow.com/questions/4986076/alternative-to-virsh-libvirt
log = u' '.join([u'?', guest.name(), u', UUID', _uuid, u'??????'])
if state == libvirt.VIR_DOMAIN_RUNNING:
log += u' Running?'
guest_event_emit.running(uuid=_uuid)
elif state == libvirt.VIR_DOMAIN_BLOCKED:
log += u' Blocked?'
guest_event_emit.blocked(uuid=_uuid)
elif state == libvirt.VIR_DOMAIN_PAUSED:
log += u' Paused?'
guest_event_emit.paused(uuid=_uuid)
elif state == libvirt.VIR_DOMAIN_SHUTDOWN:
log += u' Shutdown?'
guest_event_emit.shutdown(uuid=_uuid)
elif state == libvirt.VIR_DOMAIN_SHUTOFF:
log += u' Shutoff?'
guest_event_emit.shutoff(uuid=_uuid)
elif state == libvirt.VIR_DOMAIN_CRASHED:
log += u' Crashed?'
guest_event_emit.crashed(uuid=_uuid)
elif state == libvirt.VIR_DOMAIN_PMSUSPENDED:
log += u' PM_Suspended?'
guest_event_emit.pm_suspended(uuid=_uuid)
else:
log += u' NO_State?'
guest_event_emit.no_state(uuid=_uuid)
logger.info(log)
log_emit.info(log)
except Exception as e:
logger.error(e.message)
log_emit.error(e.message)