2006-06-28 15:50:17 -04:00
|
|
|
#
|
|
|
|
|
# Copyright (C) 2006 Red Hat, Inc.
|
|
|
|
|
# Copyright (C) 2006 Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
#
|
2006-06-14 10:59:40 -04:00
|
|
|
|
2006-06-14 13:52:46 -04:00
|
|
|
import gobject
|
2006-06-14 10:59:40 -04:00
|
|
|
import libvirt
|
2006-06-14 17:52:49 -04:00
|
|
|
from time import time
|
2006-06-14 10:59:40 -04:00
|
|
|
|
2006-06-14 17:52:49 -04:00
|
|
|
from virtManager.domain import vmmDomain
|
2006-06-14 10:59:40 -04:00
|
|
|
|
2006-06-14 13:52:46 -04:00
|
|
|
class vmmConnection(gobject.GObject):
|
|
|
|
|
__gsignals__ = {
|
|
|
|
|
"vm-added": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2006-06-14 17:52:49 -04:00
|
|
|
[str, str]),
|
2006-06-14 13:52:46 -04:00
|
|
|
"vm-removed": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2006-06-14 17:52:49 -04:00
|
|
|
[str, str]),
|
2006-06-14 16:20:06 -04:00
|
|
|
"disconnected": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, [str])
|
2006-06-14 13:52:46 -04:00
|
|
|
}
|
|
|
|
|
|
2006-06-14 16:56:49 -04:00
|
|
|
def __init__(self, config, uri, readOnly):
|
2006-06-14 13:52:46 -04:00
|
|
|
self.__gobject_init__()
|
2006-06-14 10:59:40 -04:00
|
|
|
self.config = config
|
2006-06-14 14:36:26 -04:00
|
|
|
self.uri = uri
|
2006-06-28 10:40:35 -04:00
|
|
|
self.readOnly = readOnly
|
2006-06-14 10:59:40 -04:00
|
|
|
|
2006-06-27 14:16:13 -04:00
|
|
|
openURI = uri
|
|
|
|
|
if openURI == "Xen":
|
|
|
|
|
openURI = None
|
2006-06-14 10:59:40 -04:00
|
|
|
if readOnly:
|
2006-06-27 14:16:13 -04:00
|
|
|
self.vmm = libvirt.openReadOnly(openURI)
|
2006-06-14 10:59:40 -04:00
|
|
|
else:
|
2006-06-27 14:16:13 -04:00
|
|
|
self.vmm = libvirt.open(openURI)
|
2006-06-14 10:59:40 -04:00
|
|
|
|
|
|
|
|
self.vms = {}
|
2006-06-14 17:52:49 -04:00
|
|
|
self.tick()
|
2006-06-14 10:59:40 -04:00
|
|
|
|
2006-06-28 10:40:35 -04:00
|
|
|
def is_read_only(self):
|
|
|
|
|
return self.readOnly
|
|
|
|
|
|
2006-06-14 14:36:26 -04:00
|
|
|
def get_uri(self):
|
|
|
|
|
return self.uri
|
|
|
|
|
|
2006-06-14 16:20:06 -04:00
|
|
|
def get_vm(self, uuid):
|
|
|
|
|
return self.vms[uuid]
|
2006-06-14 10:59:40 -04:00
|
|
|
|
2006-06-14 13:52:46 -04:00
|
|
|
def disconnect(self):
|
2006-06-14 14:36:26 -04:00
|
|
|
if self.vmm == None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
#self.vmm.close()
|
2006-06-14 13:52:46 -04:00
|
|
|
self.vmm = None
|
2006-06-14 16:20:06 -04:00
|
|
|
self.emit("disconnected", self.uri)
|
2006-06-14 13:52:46 -04:00
|
|
|
|
2006-06-14 10:59:40 -04:00
|
|
|
def get_host_info(self):
|
2006-06-14 17:52:49 -04:00
|
|
|
return self.hostinfo
|
2006-06-14 10:59:40 -04:00
|
|
|
|
2006-06-14 13:52:46 -04:00
|
|
|
def connect(self, name, callback):
|
|
|
|
|
gobject.GObject.connect(self, name, callback)
|
2006-06-14 18:51:58 -04:00
|
|
|
|
2006-06-14 13:52:46 -04:00
|
|
|
if name == "vm-added":
|
2006-06-14 10:59:40 -04:00
|
|
|
for uuid in self.vms.keys():
|
2006-06-14 17:52:49 -04:00
|
|
|
self.emit("vm-added", self.uri, uuid)
|
|
|
|
|
|
|
|
|
|
def host_memory_size(self):
|
|
|
|
|
return self.hostinfo[1]*1024
|
|
|
|
|
|
|
|
|
|
def host_active_processor_count(self):
|
|
|
|
|
return self.hostinfo[2]
|
|
|
|
|
|
|
|
|
|
def host_maximum_processor_count(self):
|
|
|
|
|
return self.hostinfo[4] * self.hostinfo[5] * self.hostinfo[6] * self.hostinfo[7]
|
|
|
|
|
|
2006-06-14 10:59:40 -04:00
|
|
|
|
|
|
|
|
def tick(self):
|
2006-06-14 13:52:46 -04:00
|
|
|
if self.vmm == None:
|
|
|
|
|
return
|
|
|
|
|
|
2006-06-14 10:59:40 -04:00
|
|
|
doms = self.vmm.listDomainsID()
|
|
|
|
|
newVms = {}
|
|
|
|
|
if doms != None:
|
|
|
|
|
for id in doms:
|
|
|
|
|
vm = self.vmm.lookupByID(id)
|
2006-06-14 17:52:49 -04:00
|
|
|
uuid = self.uuidstr(vm.UUID())
|
|
|
|
|
newVms[uuid] = vmmDomain(self.config, self, vm, uuid)
|
2006-06-14 10:59:40 -04:00
|
|
|
|
|
|
|
|
for uuid in self.vms.keys():
|
|
|
|
|
if not(newVms.has_key(uuid)):
|
|
|
|
|
del self.vms[uuid]
|
2006-06-14 16:20:06 -04:00
|
|
|
self.emit("vm-removed", self.uri, uuid)
|
2006-06-14 10:59:40 -04:00
|
|
|
|
|
|
|
|
for uuid in newVms.keys():
|
|
|
|
|
if not(self.vms.has_key(uuid)):
|
|
|
|
|
self.vms[uuid] = newVms[uuid]
|
2006-06-14 17:52:49 -04:00
|
|
|
self.emit("vm-added", self.uri, uuid)
|
2006-06-14 10:59:40 -04:00
|
|
|
|
2006-06-14 17:52:49 -04:00
|
|
|
now = time()
|
|
|
|
|
self.hostinfo = self.vmm.getInfo()
|
2006-06-14 10:59:40 -04:00
|
|
|
for uuid in self.vms.keys():
|
2006-06-14 17:52:49 -04:00
|
|
|
self.vms[uuid].tick(now)
|
2006-06-14 10:59:40 -04:00
|
|
|
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
def uuidstr(self, rawuuid):
|
|
|
|
|
hex = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
|
|
|
|
|
uuid = []
|
|
|
|
|
for i in range(16):
|
|
|
|
|
uuid.append(hex[((ord(rawuuid[i]) >> 4) & 0xf)])
|
|
|
|
|
uuid.append(hex[(ord(rawuuid[i]) & 0xf)])
|
|
|
|
|
if i == 3 or i == 5 or i == 7 or i == 9:
|
|
|
|
|
uuid.append('-')
|
|
|
|
|
return "".join(uuid)
|
|
|
|
|
|
2006-06-14 13:52:46 -04:00
|
|
|
gobject.type_register(vmmConnection)
|
2006-06-14 10:59:40 -04:00
|
|
|
|