Ejemplo n.º 1
0
class Model(BaseModel):
    def __init__(self, libvirt_uri='qemu:///system', objstore_loc=None):
        self.objstore = ObjectStore(objstore_loc)
        self.conn = LibvirtConnection(libvirt_uri)
        kargs = {'objstore': self.objstore, 'conn': self.conn}

        if 'qemu:///' in libvirt_uri:
            self._default_pool_check()
            self._default_network_check()

        this = os.path.basename(__file__)
        this_mod = os.path.splitext(this)[0]

        models = []
        for mod_name in listPathModules(os.path.dirname(__file__)):
            if mod_name.startswith("_") or mod_name == this_mod:
                continue

            module = import_module('model.' + mod_name)
            members = inspect.getmembers(module, inspect.isclass)
            for cls_name, instance in members:
                if inspect.getmodule(instance) == module:
                    if cls_name.endswith('Model'):
                        models.append(instance(**kargs))

        return super(Model, self).__init__(models)

    def _default_network_check(self):
        conn = self.conn.get()
        xml = """
            <network>
              <name>default</name>
              <forward mode='nat'/>
              <bridge name='virbr0' stp='on' delay='0' />
              <ip address='192.168.122.1' netmask='255.255.255.0'>
                <dhcp>
                  <range start='192.168.122.2' end='192.168.122.254' />
                </dhcp>
              </ip>
            </network>
        """
        try:
            net = conn.networkLookupByName("default")
        except libvirt.libvirtError:
            try:
                net = conn.networkDefineXML(xml)
            except libvirt.libvirtError, e:
                cherrypy.log.error("Fatal: Cannot create default network "
                                   "because of %s, exit kimchid" % e.message,
                                   severity=logging.ERROR)
                sys.exit(1)

        if net.isActive() == 0:
            try:
                net.create()
            except libvirt.libvirtError, e:
                cherrypy.log.error("Fatal: Cannot activate default network "
                                   "because of %s, exit kimchid" % e.message,
                                   severity=logging.ERROR)
                sys.exit(1)
Ejemplo n.º 2
0
class Model(BaseModel):
    def __init__(self, libvirt_uri='qemu:///system', objstore_loc=None):
        self.objstore = ObjectStore(objstore_loc)
        self.conn = LibvirtConnection(libvirt_uri)
        kargs = {'objstore': self.objstore, 'conn': self.conn}

        if 'qemu:///' in libvirt_uri:
            self._default_pool_check()

        this = os.path.basename(__file__)
        this_mod = os.path.splitext(this)[0]

        models = []
        for mod_name in listPathModules(os.path.dirname(__file__)):
            if mod_name.startswith("_") or mod_name == this_mod:
                continue

            module = import_module('model.' + mod_name)
            members = inspect.getmembers(module, inspect.isclass)
            for cls_name, instance in members:
                if inspect.getmodule(instance) == module:
                    if cls_name.endswith('Model'):
                        models.append(instance(**kargs))

        return super(Model, self).__init__(models)

    def _default_pool_check(self):
        conn = self.conn.get()
        xml = """
            <pool type='dir'>
              <name>default</name>
              <target>
                <path>/var/lib/libvirt/images</path>
              </target>
            </pool>
        """
        try:
            pool = conn.storagePoolLookupByName("default")
        except libvirt.libvirtError:
            try:
                pool = conn.storagePoolDefineXML(xml, 0)
                pool.setAutostart(1)
            except libvirt.libvirtError, e:
                cherrypy.log.error("Fatal: Cannot create default pool because "
                                   "of %s, exit kimchid" % e.message,
                                   severity=logging.ERROR)
                sys.exit(1)

        if pool.isActive() == 0:
            try:
                pool.create(0)
            except libvirt.libvirtError, e:
                err = "Fatal: Default pool cannot be activated, exit kimchid"
                cherrypy.log.error(err, severity=logging.ERROR)
                sys.exit(1)
Ejemplo n.º 3
0
class Model(BaseModel):
    def __init__(self, libvirt_uri=None, objstore_loc=None):

        self.objstore = ObjectStore(objstore_loc)
        self.conn = LibvirtConnection(libvirt_uri)
        kargs = {'objstore': self.objstore, 'conn': self.conn}

        if self.conn.isQemuURI():
            for pool_name, pool_arg in DEFAULT_POOLS.iteritems():
                self._default_pool_check(pool_name, pool_arg)

        this = os.path.basename(__file__)
        this_mod = os.path.splitext(this)[0]

        models = []
        for mod_name in listPathModules(os.path.dirname(__file__)):
            if mod_name.startswith("_") or mod_name == this_mod:
                continue

            module = import_module('model.' + mod_name)
            members = inspect.getmembers(module, inspect.isclass)
            for cls_name, instance in members:
                if inspect.getmodule(instance) == module:
                    if cls_name.endswith('Model'):
                        models.append(instance(**kargs))

        return super(Model, self).__init__(models)

    def _default_pool_check(self, pool_name, pool_arg):
        conn = self.conn.get()
        pool = E.pool(E.name(pool_name), type='dir')
        pool.append(E.target(E.path(pool_arg['path'])))
        xml = ET.tostring(pool)
        try:
            pool = conn.storagePoolLookupByName(pool_name)
        except libvirt.libvirtError:
            try:
                pool = conn.storagePoolDefineXML(xml, 0)
                # Add build step to make sure target directory created
                pool.build(libvirt.VIR_STORAGE_POOL_BUILD_NEW)
                pool.setAutostart(1)
            except libvirt.libvirtError, e:
                cherrypy.log.error("Fatal: Cannot create default pool because "
                                   "of %s, exit kimchid" % e.message,
                                   severity=logging.ERROR)
                sys.exit(1)

        if pool.isActive() == 0:
            try:
                pool.create(0)
            except libvirt.libvirtError, e:
                err = "Fatal: Default pool cannot be activated, exit kimchid"
                cherrypy.log.error(err, severity=logging.ERROR)
                sys.exit(1)
Ejemplo n.º 4
0
class Model(BaseModel):
    def __init__(self, libvirt_uri="qemu:///system", objstore_loc=None):
        self.objstore = ObjectStore(objstore_loc)
        self.conn = LibvirtConnection(libvirt_uri)
        kargs = {"objstore": self.objstore, "conn": self.conn}

        if "qemu:///" in libvirt_uri:
            for pool_name, pool_arg in DEFAULT_POOLS.iteritems():
                self._default_pool_check(pool_name, pool_arg)

        this = os.path.basename(__file__)
        this_mod = os.path.splitext(this)[0]

        models = []
        for mod_name in listPathModules(os.path.dirname(__file__)):
            if mod_name.startswith("_") or mod_name == this_mod:
                continue

            module = import_module("model." + mod_name)
            members = inspect.getmembers(module, inspect.isclass)
            for cls_name, instance in members:
                if inspect.getmodule(instance) == module:
                    if cls_name.endswith("Model"):
                        models.append(instance(**kargs))

        return super(Model, self).__init__(models)

    def _default_pool_check(self, pool_name, pool_arg):
        conn = self.conn.get()
        pool = E.pool(E.name(pool_name), type="dir")
        pool.append(E.target(E.path(pool_arg["path"])))
        xml = ET.tostring(pool)
        try:
            pool = conn.storagePoolLookupByName(pool_name)
        except libvirt.libvirtError:
            try:
                pool = conn.storagePoolDefineXML(xml, 0)
                # Add build step to make sure target directory created
                pool.build(libvirt.VIR_STORAGE_POOL_BUILD_NEW)
                pool.setAutostart(1)
            except libvirt.libvirtError, e:
                cherrypy.log.error(
                    "Fatal: Cannot create default pool because " "of %s, exit kimchid" % e.message,
                    severity=logging.ERROR,
                )
                sys.exit(1)

        if pool.isActive() == 0:
            try:
                pool.create(0)
            except libvirt.libvirtError, e:
                err = "Fatal: Default pool cannot be activated, exit kimchid"
                cherrypy.log.error(err, severity=logging.ERROR)
                sys.exit(1)