예제 #1
0
    def __init__(self):
        TortugaDbApi.__init__(self)

        self._hardwareProfilesDbHandler = HardwareProfilesDbHandler()
        self._nodesDbHandler = NodesDbHandler()
        self._globalParametersDbHandler = GlobalParametersDbHandler()
        self._adminsDbHandler = AdminsDbHandler()
        self._nicsDbHandler = NicsDbHandler()
        self._resourceAdaptersDbHandler = ResourceAdaptersDbHandler()
        self._networkDevicesDbHandler = NetworkDevicesDbHandler()
        self._networksDbHandler = NetworksDbHandler()
예제 #2
0
    def __init__(self):
        TortugaDbApi.__init__(self)

        from tortuga.db.hardwareProfilesDbHandler \
            import HardwareProfilesDbHandler
        self._hardwareProfilesDbHandler = HardwareProfilesDbHandler()
        from tortuga.db.nodesDbHandler import NodesDbHandler
        from tortuga.db.nicsDbHandler import NicsDbHandler
        from tortuga.db.networksDbHandler import NetworksDbHandler
        self._nodesDbHandler = NodesDbHandler()
        self._nicsDbHandler = NicsDbHandler()
        self._networksDbHandler = NetworksDbHandler()
예제 #3
0
def test_getNetworkList(dbm):
    with dbm.session() as session:
        networks = NetworksDbHandler().getNetworkList(session)

        assert networks

        network2 = NetworksDbHandler().getNetworkById(session, networks[0].id)

        assert network2

        network3 = NetworksDbHandler().getNetwork(
            session, network2.address, network2.netmask)

        assert network3

        assert network2.id == network3.id
def test_generate_provisioning_ip_address(dbm):
    with dbm.session() as session:
        networks = NetworksDbHandler().getNetworkList(session)

        result = api.generate_provisioning_ip_address(networks[0])

        assert result
예제 #5
0
    def _get_provisioning_networks(self):
        """
        Get provisioning networks.

        :returns: Generator provisioning networks
        """
        for network in NetworksDbHandler().getNetworkList(self.session):
            if network.type == 'provision':
                yield network
예제 #6
0
    def _get_provisioning_networks():
        """
        Get provisioning networks.

        :returns: Generator provisioning networks
        """
        with DbManager().session() as session:
            for network in NetworksDbHandler().getNetworkList(session):
                if network.type == 'provision':
                    yield network
예제 #7
0
class NetworkDbApi(TortugaDbApi):
    """
    Network DB API class.
    """
    def __init__(self):
        TortugaDbApi.__init__(self)

        from tortuga.db.hardwareProfilesDbHandler \
            import HardwareProfilesDbHandler
        self._hardwareProfilesDbHandler = HardwareProfilesDbHandler()
        from tortuga.db.nodesDbHandler import NodesDbHandler
        from tortuga.db.nicsDbHandler import NicsDbHandler
        from tortuga.db.networksDbHandler import NetworksDbHandler
        self._nodesDbHandler = NodesDbHandler()
        self._nicsDbHandler = NicsDbHandler()
        self._networksDbHandler = NetworksDbHandler()

    def getNetworkList(self):
        """
        Get list of networks from the db.
        """

        session = DbManager().openSession()

        try:
            dbList = self._networksDbHandler.getNetworkList(session)

            return self.getTortugaObjectList(Network, dbList)
        except TortugaException as ex:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def getNetwork(self, address, netmask):
        """
        Get a network from the db.
        """

        session = DbManager().openSession()

        try:
            network = self._networksDbHandler.getNetwork(
                session, address, netmask)

            return Network.getFromDbDict(network.__dict__)
        except TortugaException as ex:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def getNetworkById(self, id_):
        """
        Get a network by id from the db.
        """

        session = DbManager().openSession()

        try:
            network = self._networksDbHandler.getNetworkById(session, id_)

            return Network.getFromDbDict(network.__dict__)
        except TortugaException as ex:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def addNetwork(self, network):
        """
        Insert network into the db.

            Returns:
                networkId
            Throws:
                NetworkAlreadyExists
                DbError
        """

        session = DbManager().openSession()

        try:
            dbNetwork = self._networksDbHandler.addNetwork(session, network)

            session.commit()

            return dbNetwork.id
        except TortugaException as ex:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def updateNetwork(self, network):
        """
        Updates network in DB..

            Returns:
                network
            Throws:
                NetworkNotFound
                DbError
        """

        session = DbManager().openSession()

        try:
            dbNetwork = self._networksDbHandler.updateNetwork(session, network)

            newNetwork = Network.getFromDbDict(dbNetwork.__dict__)

            session.commit()

            return newNetwork
        except TortugaException as ex:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def deleteNetwork(self, id_):
        """
        Delete network from the db.

            Returns:
                None
            Throws:
                NetworkInUse
                NetworkNotFound
                DbError
        """

        session = DbManager().openSession()

        try:
            self._networksDbHandler.deleteNetwork(session, id_)

            session.commit()
        except TortugaException as ex:
            session.rollback()
            raise
        except IntegrityError as ex:
            raise NetworkInUse('Network is in use')
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def getNetworkListByType(self, type_):
        """
        Return list of networks of the given type.

            Returns:
                [networks]
            Throws:
                DbError
        """

        session = DbManager().openSession()

        try:
            dbNetworks = self._networksDbHandler.\
                getNetworkListByType(session, type_)

            return self.getTortugaObjectList(Network, dbNetworks)
        except TortugaException as ex:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()
예제 #8
0
class HardwareProfileDbApi(TortugaDbApi):
    """
    HardwareProfile DB API class.
    """
    def __init__(self):
        TortugaDbApi.__init__(self)

        self._hardwareProfilesDbHandler = HardwareProfilesDbHandler()
        self._nodesDbHandler = NodesDbHandler()
        self._globalParametersDbHandler = GlobalParametersDbHandler()
        self._adminsDbHandler = AdminsDbHandler()
        self._nicsDbHandler = NicsDbHandler()
        self._resourceAdaptersDbHandler = ResourceAdaptersDbHandler()
        self._networkDevicesDbHandler = NetworkDevicesDbHandler()
        self._networksDbHandler = NetworksDbHandler()

    def getHardwareProfile(self,
                           name: str,
                           optionDict: Optional[Union[dict, None]] = None):
        """
        Get hardwareProfile from the db.

            Returns:
                hardwareProfile
            Throws:
                HardwareProfileNotFound
                DbError
        """

        session = DbManager().openSession()

        try:
            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfile(session, name)

            self.loadRelations(dbHardwareProfile, optionDict)
            self.loadRelations(dbHardwareProfile, dict(tags=True))

            return HardwareProfile.getFromDbDict(dbHardwareProfile.__dict__)
        except TortugaException as ex:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def getHardwareProfileById(self,
                               hardwareProfileId: int,
                               optionDict: Optional[Union[dict, None]] = None):
        """
        Get hardwareProfile from the db.

            Returns:
                hardwareProfile
            Throws:
                HardwareProfileNotFound
                DbError
        """

        session = DbManager().openSession()

        try:
            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfileById(session, hardwareProfileId)

            self.loadRelations(dbHardwareProfile, optionDict)

            return HardwareProfile.getFromDbDict(dbHardwareProfile.__dict__)
        except TortugaException as ex:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def getHardwareProfileList(self,
                               optionDict: Optional[Union[dict, None]] = None,
                               tags: Optional[Union[dict, None]] = None):
        """
        Get list of all available hardwareProfiles from the db.

            Returns:
                [hardwareProfile]
            Throws:
                DbError
        """

        session = DbManager().openSession()

        try:
            dbHardwareProfileList = self._hardwareProfilesDbHandler.\
                getHardwareProfileList(session, tags=tags)

            hardwareProfileList = TortugaObjectList()

            for dbHardwareProfile in dbHardwareProfileList:
                # For now expand networks
                self.loadRelation(dbHardwareProfile, 'hardwareprofilenetworks')

                self.loadRelations(dbHardwareProfile, optionDict)

                self.loadRelations(dbHardwareProfile, dict(tags=True))

                hardwareProfileList.append(
                    HardwareProfile.getFromDbDict(dbHardwareProfile.__dict__))

            return hardwareProfileList
        except TortugaException as ex:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def setIdleSoftwareProfile(self,
                               hardwareProfileName,
                               softwareProfileName=None):
        """
        Sets the idle software profile

        Returns:
            -none-

        Raises:
            SoftwareProfileNotFound
            SoftwareProfileNotIdle
        """

        session = DbManager().openSession()

        try:
            dbSoftwareProfile = SoftwareProfilesDbHandler().\
                getSoftwareProfile(session, softwareProfileName) \
                if softwareProfileName else None

            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfile(session, hardwareProfileName)

            self._hardwareProfilesDbHandler.setIdleSoftwareProfile(
                dbHardwareProfile, dbSoftwareProfile)

            session.commit()
        except TortugaException as ex:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def addHardwareProfile(self, hardwareProfile, session=None):
        """
        Insert hardwareProfile into the db.

            Returns:
                (none)
            Throws:
                HardwareProfileAlreadyExists
                DbError
        """

        # Keep local 'session' instance.  If 'session' is None,
        # ensure transaction is committed before returning to the
        # caller, otherwise the caller is responsible.  On exceptions,
        # the rollback is performed regardless.

        _session = session

        if _session is None:
            _session = DbManager().openSession()

        try:
            try:
                self._hardwareProfilesDbHandler.getHardwareProfile(
                    _session, hardwareProfile.getName())

                raise HardwareProfileAlreadyExists(
                    'Hardware profile [%s] already exists' % (hardwareProfile))
            except HardwareProfileNotFound as ex:
                pass

            dbHardwareProfile = self.__populateHardwareProfile(
                _session, hardwareProfile)

            _session.add(dbHardwareProfile)

            if session is None:
                _session.commit()

            self.getLogger().info('Added hardware profile [%s]' %
                                  (dbHardwareProfile.name))
        except TortugaException as ex:
            _session.rollback()
            raise
        except Exception as ex:
            _session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            if session is None:
                DbManager().closeSession()

    def deleteHardwareProfile(self, name):
        """
        Delete hardwareProfile from the db.

            Returns:
                None
            Throws:
                HardwareProfileNotFound
                DbError
                TortugaException
        """

        session = DbManager().openSession()

        try:
            hwProfile = self._hardwareProfilesDbHandler.getHardwareProfile(
                session, name)

            if hwProfile.nodes:
                raise TortugaException(
                    'Unable to remove hardware profile with associated'
                    ' nodes')

            # First delete the mappings
            hwProfile.mappedsoftwareprofiles = []

            self.getLogger().debug(
                'Marking hardware profile [%s] for deletion' % (name))

            session.delete(hwProfile)

            session.commit()
        except TortugaException as ex:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def copyHardwareProfile(self, srcHardwareProfileName,
                            dstHardwareProfileName):
        session = DbManager().openSession()

        try:
            srcHardwareProfile = self.getHardwareProfile(
                srcHardwareProfileName, {
                    'admins': True,
                    'hardwareprofilenetworks': True,
                    'nics': True,
                    'resourceadapter': True,
                })

            dstHardwareProfile = \
                self.getHardwareProfile(srcHardwareProfileName)

            dstHardwareProfile.setName(dstHardwareProfileName)

            newDescription = 'Copy of %s' % (
                dstHardwareProfile.getDescription())

            dstHardwareProfile.setDescription(newDescription)

            dstHardwareProfile.setNetworks(srcHardwareProfile.getNetworks())

            dstHardwareProfile.setProvisioningNics(
                srcHardwareProfile.getProvisioningNics())

            dstHardwareProfile.setResourceAdapter(
                srcHardwareProfile.getResourceAdapter())

            self.addHardwareProfile(dstHardwareProfile, session)

            session.commit()
        finally:
            DbManager().closeSession()

    def getNode(self, name):
        """
        Get node from the db.

            Returns:
                node
            Throws:
                NodeNotFound
                DbError
        """

        session = DbManager().openSession()

        try:
            return self._nodesDbHandler.getNode(session, name)
        except TortugaException as ex:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def deleteNode(self, name):
        """
        Delete node from the db.

            Returns:
                None
            Throws:
                NodeNotFound
                DbError
        """

        session = DbManager().openSession()

        try:
            self._nodesDbHandler.deleteNode(session, name)

            session.commit()
        except TortugaException as ex:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def addAdmin(self, hardwareProfileName, adminUsername):
        """
        Add an admin to this hardware profile

        Raises:
            AdminAlreadyExists
        """

        session = DbManager().openSession()

        try:
            dbAdmin = self._adminsDbHandler.getAdmin(session, adminUsername)

            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfile(session, hardwareProfileName)

            if dbAdmin in dbHardwareProfile.admins:
                raise AdminAlreadyExists(
                    'The admin %s is already associated with %s.' %
                    (adminUsername, hardwareProfileName))

            dbHardwareProfile.admins.append(dbAdmin)

            session.commit()
        except TortugaException as ex:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def deleteAdmin(self, hardwareProfileName, adminUsername):
        """
        Delete an admin from a hardware profile

        Raises:
            AdminNotFound
        """

        session = DbManager().openSession()

        try:
            dbAdmin = self._adminsDbHandler.getAdmin(session, adminUsername)

            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfile(session, hardwareProfileName)

            if dbAdmin not in dbHardwareProfile.admins:
                raise AdminNotFound('Admin user [%s] not associated with %s.' %
                                    (adminUsername, hardwareProfileName))

            dbHardwareProfile.admins.remove(dbAdmin)

            session.commit()
        except TortugaException as ex:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def updateHardwareProfile(self, hardwareProfileObject):
        """ Update Hardware Profile Object """

        session = DbManager().openSession()

        try:
            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfileById(session,
                                       hardwareProfileObject.getId())

            self.__populateHardwareProfile(session, hardwareProfileObject,
                                           dbHardwareProfile)

            session.commit()
        except TortugaException as ex:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def __getInstallerNode(self, session):
        return self._nodesDbHandler.getNode(session,
                                            ConfigManager().getInstaller())

    def __get_provisioning_nics(self, session):
        return self.__getInstallerNode(session).nics

    def __get_all_networks(self, session):
        return self._networksDbHandler.getNetworkList(session)

    def __get_network_devices(self, session):         \
            # pylint: disable=no-self-use

        return session.query(NetworkDevices).all()

    def __populateHardwareProfile(self,
                                  session,
                                  hardwareProfile,
                                  dbHardwareProfile=None):
        """
        Helper function for creating / updating HardwareProfiles. If
        'dbHardwareProfile' is specified, this is an update (vs. add) operation

        Raises:
            NicNotFound
        """

        # Preload provisioning nics and networks
        prov_nics = self.__get_provisioning_nics(session)
        all_networks = self.__get_all_networks(session)

        networkdevices = self.__get_network_devices(session)

        # Validate hw profile
        if hardwareProfile.getName() is None:
            raise ConfigurationError('Hardware profile requires name.')

        if hardwareProfile.getNameFormat() is None:
            raise ConfigurationError(
                'Hardware profile requires name format field.')

        # Handle the special case of a hardware profile not having an
        # associated idle software profile (ie. Installer hardware
        # profile)
        idleSoftwareProfileId = hardwareProfile.getIdleSoftwareProfileId() \
            if hardwareProfile.getIdleSoftwareProfileId else None

        if dbHardwareProfile is None:
            dbHardwareProfile = HardwareProfiles()

        dbHardwareProfile.name = hardwareProfile.getName()
        dbHardwareProfile.description = hardwareProfile.getDescription()
        dbHardwareProfile.nameFormat = hardwareProfile.getNameFormat()

        if hardwareProfile.getInstallType() is None:
            if hardwareProfile.getLocation() == 'remote':
                dbHardwareProfile.installType = 'bootstrap'
            else:
                raise ConfigurationError(
                    'Hardware profile must have valid install type.')
        else:
            dbHardwareProfile.installType = hardwareProfile.\
                getInstallType()

        if hardwareProfile.getLocation() != 'remote':
            dbHardwareProfile.kernel = hardwareProfile.getKernel()
            dbHardwareProfile.kernelParams = hardwareProfile.\
                getKernelParams()
            dbHardwareProfile.initrd = hardwareProfile.getInitrd()
            dbHardwareProfile.localBootParams = hardwareProfile.\
                getLocalBootParams()

        dbHardwareProfile.softwareOverrideAllowed = hardwareProfile.\
            getSoftwareOverrideAllowed()
        dbHardwareProfile.idleSoftwareProfileId = idleSoftwareProfileId
        dbHardwareProfile.location = hardwareProfile.getLocation()

        dbHardwareProfile.hypervisorSoftwareProfileId = hardwareProfile.\
            getHypervisorSoftwareProfileId()
        dbHardwareProfile.maxUnits = hardwareProfile.getMaxUnits()
        dbHardwareProfile.bcastEnabled = hardwareProfile.getBcastEnabled()
        dbHardwareProfile.mcastEnabled = hardwareProfile.getMcastEnabled()
        dbHardwareProfile.cost = hardwareProfile.getCost()

        # Add resource adapter
        resourceAdapter = hardwareProfile.getResourceAdapter()
        if resourceAdapter:
            dbHardwareProfile.resourceAdapter = self.\
                _resourceAdaptersDbHandler.getResourceAdapter(
                    session, resourceAdapter.getName())

            dbHardwareProfile.resourceAdapterId = dbHardwareProfile.\
                resourceAdapter.id

        # Add networks
        networks = []
        for network in hardwareProfile.getNetworks():
            for prov_network in all_networks:
                if prov_network.address == network.getAddress():
                    dbNetwork = prov_network

                    break
            else:
                raise NetworkNotFound('Network [%s] does not exist' %
                                      (network.getAddress()))

            # Lookup network device
            for network_device in networkdevices:
                if network.getNetworkDevice().getName() == network_device.name:
                    dbNetworkDevice = network_device

                    break
            else:
                dbNetworkDevice = NetworkDevices()
                dbNetworkDevice.name = network.getNetworkDevice().getName()

            # Now check if we have this one already...
            for dbHardwareProfileNetwork in dbHardwareProfile.\
                    hardwareprofilenetworks:
                if dbHardwareProfileNetwork.\
                    networkDeviceId == dbNetworkDevice.id and \
                        dbHardwareProfileNetwork.networkId == dbNetwork.id:
                    break
            else:
                dbHardwareProfileNetwork = HardwareProfileNetworks()

                if dbNetwork.id is not None:
                    dbHardwareProfileNetwork.networkId = dbNetwork.id
                else:
                    dbHardwareProfileNetwork.network = dbNetwork

                dbHardwareProfileNetwork.hardwareProfileId = \
                    dbHardwareProfile.id

                if dbNetworkDevice.id is not None:
                    dbHardwareProfileNetwork.networkDeviceId = \
                        dbNetworkDevice.id
                else:
                    dbHardwareProfileNetwork.networkdevice = dbNetworkDevice

                dbHardwareProfile.hardwareprofilenetworks.append(
                    dbHardwareProfileNetwork)

            networks.append(dbHardwareProfileNetwork)

        # Now remove all old networks
        for dbNetwork in dbHardwareProfile.hardwareprofilenetworks:
            for network in networks:
                if network.networkDeviceId == dbNetwork.networkDeviceId \
                        and network.networkId == dbNetwork.networkId:
                    # Its a keeper
                    break
            else:
                # No match...delete time
                session.delete(dbNetwork)

        # Add provisioning Nics
        if hardwareProfile.getProvisioningNics():
            # Only one provisioning nic is possible
            nic = hardwareProfile.getProvisioningNics()[0]

            for prov_nic in prov_nics:
                if nic.getIp() == prov_nic.ip:
                    dbNic = prov_nic

                    break
            else:
                raise NicNotFound('Provisioning NIC with IP [%s] not found' %
                                  (nic.getIp()))

            if dbNic not in dbHardwareProfile.nics:
                dbHardwareProfile.nics.append(dbNic)

        return dbHardwareProfile

    def getHypervisorNodes(self, hardwareProfileName):
        """
        Get list of nodes that belong to hypervisorSoftwareProfileId
        assigned to the given hardware profile name.
        """

        session = DbManager().openSession()

        try:
            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfile(session, hardwareProfileName)

            if not dbHardwareProfile.hypervisor:
                return TortugaObjectList()

            return self.getTortugaObjectList(
                Node, dbHardwareProfile.hypervisor.nodes)
        except TortugaException as ex:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def setProvisioningNic(self, hardwareProfileName, nicId):
        session = DbManager().openSession()

        try:
            dbNic = self._nicsDbHandler.getNicById(session, nicId)

            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfile(session, hardwareProfileName)

            dbHardwareProfile.nics.append(dbNic)

            session.commit()
        except sqlalchemy.exc.IntegrityError as ex:
            # Entry for this hwProfile/nicId already exists, ignore
            self.getLogger().debug(
                'setProvisioningNic(): entry already exists for'
                ' hwProfile=%s, nicId=%d' % (hardwareProfileName, nicId))
        except TortugaException as ex:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def getProvisioningNicForNetwork(self, network, netmask):
        """
        Raises:
            NicNotFound
        """

        session = DbManager().openSession()

        try:
            installer_node = self.__getInstallerNode(session)

            nics = [
                dbNic for dbNic in installer_node.hardwareprofile.nics
                if dbNic.network.address == network
                and dbNic.network.netmask == netmask
            ]

            if not nics:
                raise NicNotFound(
                    'Unable to find provisioning NIC for network [%s]'
                    ' netmask [%s]' % (network, netmask))

            return tortuga.objects.nic.Nic.getFromDbDict(nics[0].__dict__)
        except TortugaException as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()
예제 #9
0
    def __init__(self):
        super().__init__()

        self._networksDbHandler = NetworksDbHandler()
예제 #10
0
class NetworkDbApi(TortugaDbApi):
    """
    Network DB API class.
    """
    def __init__(self):
        super().__init__()

        self._networksDbHandler = NetworksDbHandler()

    def getNetworkList(self, session: Session) -> TortugaObjectList:
        """
        Get list of networks from the db.
        """

        self._logger.debug('getNetworkList()')

        try:
            dbList = self._networksDbHandler.getNetworkList(session)

            return self.getTortugaObjectList(Network, dbList)
        except TortugaException:
            raise
        except Exception as ex:
            self._logger.exception(str(ex))
            raise

    def getNetwork(self, session: Session, address: str, netmask: str):
        """
        Get a network from the db.
        """

        self._logger.debug('Retrieving network [%s/%s]' % (address, netmask))

        try:
            network = self._networksDbHandler.getNetwork(
                session, address, netmask)

            return Network.getFromDbDict(network.__dict__)
        except TortugaException:
            raise
        except Exception as ex:
            self._logger.exception(str(ex))
            raise

    def getNetworkById(self, session: Session, id_):
        """
        Get a network by id from the db.
        """

        self._logger.debug('Retrieving network ID [%s]' % (id_))

        try:
            network = self._networksDbHandler.getNetworkById(session, id_)

            return Network.getFromDbDict(network.__dict__)
        except TortugaException:
            raise
        except Exception as ex:
            self._logger.exception(str(ex))
            raise

    def addNetwork(self, session: Session, network):
        """
        Insert network into the db.

            Returns:
                networkId
            Throws:
                NetworkAlreadyExists
        """

        self._logger.debug('Adding network [%s]' % network)

        try:
            try:
                self._networksDbHandler.getNetwork(session,
                                                   network.getAddress(),
                                                   network.getNetmask())

                raise NetworkAlreadyExists('Network [%s] already exists' %
                                           (network))
            except NetworkNotFound:
                pass

            dbNetwork = populate_network(network)

            session.add(dbNetwork)

            session.commit()

            self._logger.info('Added network [%s]' % (network))

            return dbNetwork.id
        except TortugaException:
            session.rollback()

            raise
        except Exception as ex:
            session.rollback()

            self._logger.exception(str(ex))

            raise

    def updateNetwork(self, session: Session, network: Network) -> Network:
        """
        Updates network in DB..

            Returns:
                network
            Throws:
                NetworkNotFound
                InvalidArgument
        """

        try:
            if not network.getId():
                raise InvalidArgument(
                    'Network id not set: unable to identify network')

            self._logger.debug('Updating network [%s]' % (network))

            dbNetwork = self._networksDbHandler.getNetworkById(
                session, network.getId())

            dbNetwork = populate_network(network, dbNetwork)

            newNetwork = Network.getFromDbDict(dbNetwork.__dict__)

            session.commit()

            self._logger.info('Updated network [%s]' % (network))

            return newNetwork
        except TortugaException:
            session.rollback()

            raise
        except Exception as ex:
            session.rollback()

            self._logger.exception(str(ex))

            raise

    def deleteNetwork(self, session: Session, id_: int):
        """
        Delete network from the db.

            Returns:
                None
            Throws:
                NetworkInUse
                NetworkNotFound
                DeleteNetworkFailed
        """

        try:
            dbNetwork = \
                self._networksDbHandler.getNetworkById(session, id_)

            self._logger.debug('Attempting to delete network [%s/%s]' %
                               (dbNetwork.address, dbNetwork.netmask))

            # Make sure this network is not associated with anything
            if dbNetwork.hardwareprofilenetworks:
                nets = [
                    net.hardwareprofile.name
                    for net in dbNetwork.hardwareprofilenetworks
                ]

                raise DeleteNetworkFailed(
                    'Network enabled on hardware profile(s): [%s]' %
                    (' '.join(nets)))

            if dbNetwork.nics:
                raise DeleteNetworkFailed('Network has active NIC(s)')

            session.delete(dbNetwork)

            session.commit()

            self._logger.info('Deleted network [%s/%s]' %
                              (dbNetwork.address, dbNetwork.netmask))
        except TortugaException:
            session.rollback()

            raise
        except IntegrityError:
            raise NetworkInUse('Network is in use')
        except Exception as ex:
            session.rollback()

            self._logger.exception(str(ex))

            raise
예제 #11
0
class HardwareProfileDbApi(TagsDbApiMixin, TortugaDbApi):
    """
    HardwareProfile DB API class.

    """
    tag_model = HardwareProfileTag

    def __init__(self):
        super().__init__()

        self._hardwareProfilesDbHandler = HardwareProfilesDbHandler()
        self._nodesDbHandler = NodesDbHandler()
        self._globalParametersDbHandler = GlobalParametersDbHandler()
        self._adminsDbHandler = AdminsDbHandler()
        self._nicsDbHandler = NicsDbHandler()
        self._resourceAdaptersDbHandler = ResourceAdaptersDbHandler()
        self._networkDevicesDbHandler = NetworkDevicesDbHandler()
        self._networksDbHandler = NetworksDbHandler()

    def getHardwareProfile(self, session: Session, name: str,
                           optionDict: Optional[OptionDict] = None) \
            -> HardwareProfile:
        """
        Get hardwareProfile from the db.

            Returns:
                hardwareProfile
            Throws:
                HardwareProfileNotFound
                DbError
        """

        try:
            dbHardwareProfile = \
                self._hardwareProfilesDbHandler.getHardwareProfile(
                    session, name)

            self.loadRelations(
                dbHardwareProfile, get_default_relations(optionDict))

            return HardwareProfile.getFromDbDict(
                dbHardwareProfile.__dict__)
        except TortugaException:
            raise
        except Exception as ex:
            self._logger.exception(str(ex))
            raise

    def getHardwareProfileById(
            self, session: Session, hardwareProfileId: int,
            optionDict: Optional[OptionDict] = None) -> HardwareProfile:
        """
        Get hardwareProfile from the db.

            Returns:
                hardwareProfile
            Throws:
                HardwareProfileNotFound
                DbError
        """

        try:
            dbHardwareProfile = \
                self._hardwareProfilesDbHandler.getHardwareProfileById(
                    session, hardwareProfileId)

            self.loadRelations(
                dbHardwareProfile, get_default_relations(optionDict))

            return HardwareProfile.getFromDbDict(dbHardwareProfile.__dict__)
        except TortugaException:
            raise
        except Exception as ex:
            self._logger.exception(str(ex))
            raise

    def getHardwareProfileList(
            self, session: Session, optionDict: Optional[OptionDict] = None,
            tags: Optional[Tags] = None) -> TortugaObjectList:
        """
        Get list of all available hardwareProfiles from the db.

            Returns:
                [hardwareProfile]
            Throws:
                DbError
        """

        try:
            dbHardwareProfileList = \
                self._hardwareProfilesDbHandler.getHardwareProfileList(
                    session, tags=tags)

            hardwareProfileList = TortugaObjectList()

            for dbHardwareProfile in dbHardwareProfileList:
                options = dict.copy(optionDict or {})
                options['hardwareprofilenetworks'] = True

                self.loadRelations(
                    dbHardwareProfile, get_default_relations(options))

                hardwareProfileList.append(
                    HardwareProfile.getFromDbDict(
                        dbHardwareProfile.__dict__))

            return hardwareProfileList
        except TortugaException:
            raise
        except Exception as ex:
            self._logger.exception(str(ex))
            raise

    def addHardwareProfile(
            self, session: Session,
            hardwareProfile: HardwareProfile) -> None:
        """
        Insert hardwareProfile into the db.

            Returns:
                (none)
            Throws:
                HardwareProfileAlreadyExists
                DbError
        """

        try:
            try:
                self._hardwareProfilesDbHandler.getHardwareProfile(
                    session, hardwareProfile.getName())

                raise HardwareProfileAlreadyExists(
                    'Hardware profile [%s] already exists' % (
                        hardwareProfile))
            except HardwareProfileNotFound as ex:
                pass

            dbHardwareProfile = self.__populateHardwareProfile(
                session, hardwareProfile)

            session.add(dbHardwareProfile)
            session.flush()
            self._set_tags(dbHardwareProfile, hardwareProfile.getTags())
            session.commit()

            self._logger.info(
                'Added hardware profile [%s]' % (dbHardwareProfile.name))
        except TortugaException:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self._logger.exception(str(ex))
            raise

    def deleteHardwareProfile(self, session: Session, name: str) -> None:
        """
        Delete hardwareProfile from the db.

            Returns:
                None
            Throws:
                HardwareProfileNotFound
                DbError
                TortugaException
        """

        try:
            hwProfile = self._hardwareProfilesDbHandler.getHardwareProfile(
                session, name)

            if hwProfile.nodes:
                raise TortugaException(
                    'Unable to delete hardware profile with associated nodes'
                )

            # First delete the mappings
            hwProfile.mappedsoftwareprofiles = []

            self._logger.debug(
                'Marking hardware profile [%s] for deletion' % (name))

            session.delete(hwProfile)

            session.commit()
        except TortugaException:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self._logger.exception(str(ex))
            raise

    def copyHardwareProfile(self, session: Session,
                            srcHardwareProfileName: str,
                            dstHardwareProfileName: str):
        #
        # Ensure the destination hardware profile doesn't already exist
        #
        try:
            self.getHardwareProfile(session, dstHardwareProfileName)
            raise TortugaException(
                'Destination hardware profile already exists: {}'.format(
                    dstHardwareProfileName)
            )
        except HardwareProfileNotFound:
            pass

        srcHardwareProfile = self.getHardwareProfile(
            session,
            srcHardwareProfileName, {
                'admins': True,
                'hardwareprofilenetworks': True,
                'nics': True,
                'resourceadapter': True,
            })

        dstHardwareProfile = \
            self.getHardwareProfile(session, srcHardwareProfileName)

        dstHardwareProfile.setName(dstHardwareProfileName)

        newDescription = 'Copy of %s' % (
            dstHardwareProfile.getDescription())

        dstHardwareProfile.setDescription(newDescription)

        dstHardwareProfile.setNetworks(srcHardwareProfile.getNetworks())

        dstHardwareProfile.setProvisioningNics(
            srcHardwareProfile.getProvisioningNics())

        dstHardwareProfile.setResourceAdapter(
            srcHardwareProfile.getResourceAdapter())

        self.addHardwareProfile(session, dstHardwareProfile)

    def addAdmin(
            self, session: Session, hardwareProfileName,
            adminUsername: str) -> None:
        """
        Add an admin to this hardware profile

        Raises:
            AdminAlreadyExists
        """

        try:
            dbAdmin = self._adminsDbHandler.getAdmin(
                session, adminUsername)

            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfile(session, hardwareProfileName)

            if dbAdmin in dbHardwareProfile.admins:
                raise AdminAlreadyExists(
                    'The admin %s is already associated with %s.' % (
                        adminUsername, hardwareProfileName))

            dbHardwareProfile.admins.append(dbAdmin)

            session.commit()
        except TortugaException:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self._logger.exception(str(ex))
            raise

    def deleteAdmin(
            self, session: Session, hardwareProfileName: str,
            adminUsername: str) -> None:
        """
        Delete an admin from a hardware profile

        Raises:
            AdminNotFound
        """

        try:
            dbAdmin = self._adminsDbHandler.getAdmin(session, adminUsername)

            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfile(session, hardwareProfileName)

            if dbAdmin not in dbHardwareProfile.admins:
                raise AdminNotFound(
                    'Admin user [%s] not associated with %s.' % (
                        adminUsername, hardwareProfileName))

            dbHardwareProfile.admins.remove(dbAdmin)

            session.commit()
        except TortugaException:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self._logger.exception(str(ex))
            raise

    def updateHardwareProfile(
            self, session: Session,
            hardwareProfileObject: HardwareProfile) -> None:
        """
        Update Hardware Profile Object
        """

        try:
            dbHardwareProfile = \
                self._hardwareProfilesDbHandler.getHardwareProfileById(
                    session, hardwareProfileObject.getId())

            self.__populateHardwareProfile(
                session, hardwareProfileObject, dbHardwareProfile)
            self._set_tags(dbHardwareProfile, hardwareProfileObject.getTags())
            session.commit()

        except TortugaException:
            session.rollback()
            raise

        except Exception as ex:
            session.rollback()
            self._logger.exception(str(ex))
            raise

    def __getInstallerNode(self, session: Session) -> Node:
        return self._nodesDbHandler.getNode(
            session, ConfigManager().getInstaller())

    def __get_provisioning_nics(self, session: Session) -> List[Nic]:
        return self.__getInstallerNode(session).nics

    def __get_all_networks(self, session: Session) -> List[Network]:
        return self._networksDbHandler.getNetworkList(session)

    def __populateHardwareProfile(
            self, session: Session, hardwareProfile: HardwareProfile,
            dbHardwareProfile: Optional[HardwareProfileModel] = None) \
            -> HardwareProfileModel:
        """
        Helper function for creating / updating hardware profiles. If
        'dbHardwareProfile' is specified, this is an update (vs. add)
        operation

        Raises:
            NicNotFound
            ResourceAdapterNotFound
            InvalidArgument
            ConfigurationError

        """
        # Preload provisioning nics and networks
        prov_nics = self.__get_provisioning_nics(session)
        all_networks = self.__get_all_networks(session)

        # Validate hw profile
        if hardwareProfile.getName() is None:
            raise ConfigurationError('Hardware profile requires name.')

        if hardwareProfile.getNameFormat() is None:
            raise ConfigurationError(
                'Hardware profile requires name format field.')

        if dbHardwareProfile is None:
            dbHardwareProfile = HardwareProfileModel()

        dbHardwareProfile.name = hardwareProfile.getName()
        dbHardwareProfile.description = hardwareProfile.getDescription()
        dbHardwareProfile.nameFormat = hardwareProfile.getNameFormat()

        if hardwareProfile.getInstallType() is None:
            if hardwareProfile.getLocation() == 'remote':
                dbHardwareProfile.installType = 'bootstrap'
            else:
                dbHardwareProfile.installType = 'package'
        else:
            dbHardwareProfile.installType = hardwareProfile.\
                getInstallType()

        if hardwareProfile.getLocation() != 'remote':
            dbHardwareProfile.kernel = hardwareProfile.getKernel()
            dbHardwareProfile.kernelParams = \
                hardwareProfile.getKernelParams()
            dbHardwareProfile.initrd = hardwareProfile.getInitrd()
            dbHardwareProfile.localBootParams = \
                hardwareProfile.getLocalBootParams()

        dbHardwareProfile.softwareOverrideAllowed = hardwareProfile.\
            getSoftwareOverrideAllowed()

        dbHardwareProfile.location = hardwareProfile.getLocation()

        dbHardwareProfile.cost = hardwareProfile.getCost()

        # Add resource adapter
        resource_adapter_name = \
            hardwareProfile.getResourceAdapter().getName() \
            if hardwareProfile.getResourceAdapter() else 'default'

        dbHardwareProfile.resourceadapter = \
            self._resourceAdaptersDbHandler.getResourceAdapter(
                session, resource_adapter_name)

        if hardwareProfile.getDefaultResourceAdapterConfig():
            adapter_cfg = None

            self._logger.debug(
                'Setting default resource adapter config: {}'.format(
                    hardwareProfile.getDefaultResourceAdapterConfig())
            )

            for adapter_cfg in \
                    dbHardwareProfile.resourceadapter.resource_adapter_config:
                if adapter_cfg.name == \
                        hardwareProfile.getDefaultResourceAdapterConfig():
                    break
            else:
                raise InvalidArgument(
                    'Resource adapter configuration profile [{}] is'
                    ' invalid'.format(
                        hardwareProfile.getDefaultResourceAdapterConfig())
                )

            dbHardwareProfile.default_resource_adapter_config = adapter_cfg
        else:
            dbHardwareProfile.default_resource_adapter_config = None

        # Add networks
        networks = []
        for network in hardwareProfile.getNetworks():
            for prov_network in all_networks:
                if prov_network.address == network.getAddress():
                    dbNetwork = prov_network

                    break
            else:
                raise NetworkNotFound(
                    'Network [%s] does not exist' % (network.getAddress()))

            dbNetworkDevice = \
                self._networkDevicesDbHandler.createNetworkDeviceIfNotExists(
                    session, network.getNetworkDevice().getName())

            # Now check if we have this one already...
            for dbHardwareProfileNetwork in \
                    dbHardwareProfile.hardwareprofilenetworks:
                if dbHardwareProfileNetwork.networkDeviceId == \
                        dbNetworkDevice.id and \
                        dbHardwareProfileNetwork.networkId == dbNetwork.id:
                    break
            else:
                dbHardwareProfileNetwork = HardwareProfileNetwork()
                dbHardwareProfileNetwork.hardwareprofile = dbHardwareProfile

                if dbNetwork.id is not None:
                    dbHardwareProfileNetwork.networkId = dbNetwork.id
                else:
                    dbHardwareProfileNetwork.network = dbNetwork

                dbHardwareProfileNetwork.hardwareProfileId = \
                    dbHardwareProfile.id

                if dbNetworkDevice.id is not None:
                    dbHardwareProfileNetwork.networkDeviceId = \
                        dbNetworkDevice.id
                else:
                    dbHardwareProfileNetwork.networkdevice = dbNetworkDevice

                dbHardwareProfile.hardwareprofilenetworks.append(
                    dbHardwareProfileNetwork)

            networks.append(dbHardwareProfileNetwork)

        # Now remove all old networks
        for dbNetwork in dbHardwareProfile.hardwareprofilenetworks:
            for network in networks:
                if network.networkDeviceId == dbNetwork.networkDeviceId \
                        and network.networkId == dbNetwork.networkId:
                    # Its a keeper
                    break
            else:
                # No match...delete time
                session.delete(dbNetwork)

        # Add provisioning Nics
        if hardwareProfile.getProvisioningNics():
            # Only one provisioning nic is possible
            nic = hardwareProfile.getProvisioningNics()[0]

            for prov_nic in prov_nics:
                if nic.getIp() == prov_nic.ip:
                    dbNic = prov_nic

                    break
            else:
                raise NicNotFound(
                    'Provisioning NIC with IP [%s] not found' % nic.getIp())

            if dbNic not in dbHardwareProfile.nics:
                dbHardwareProfile.nics.append(dbNic)

        return dbHardwareProfile

    def setProvisioningNic(
            self, session: Session, hardwareProfileName: str,
            nicId: int) -> None:
        try:
            dbNic = self._nicsDbHandler.getNicById(session, nicId)

            dbHardwareProfile = self._hardwareProfilesDbHandler.\
                getHardwareProfile(session, hardwareProfileName)

            dbHardwareProfile.nics.append(dbNic)

            session.commit()
        except sqlalchemy.exc.IntegrityError as ex:
            # Entry for this hwProfile/nicId already exists, ignore
            self._logger.debug(
                'setProvisioningNic(): entry already exists for'
                ' hwProfile=%s, nicId=%d' % (hardwareProfileName, nicId))
        except TortugaException:
            raise
        except Exception as ex:
            self._logger.exception(str(ex))
            raise

    def getProvisioningNicForNetwork(
            self, session: Session, network: str, netmask: str) -> Nic:
        """
        Raises:
            NicNotFound
        """

        try:
            installer_node = self.__getInstallerNode(session)

            nics = [
                dbNic for dbNic in installer_node.hardwareprofile.nics
                if dbNic.network.address == network and
                dbNic.network.netmask == netmask]

            if not nics:
                raise NicNotFound(
                    'Unable to find provisioning NIC for network [%s]'
                    ' netmask [%s]' % (network, netmask))

            return tortuga.objects.nic.Nic.getFromDbDict(nics[0].__dict__)
        except TortugaException as ex:
            self._logger.exception(str(ex))
            raise
예제 #12
0
def test_getNetworkById(dbm):
    with dbm.session() as session:
        with pytest.raises(NetworkNotFound):
            NetworksDbHandler().getNetworkById(session, 9999)
예제 #13
0
def test_validate_mac_address(dbm, mac):
    with dbm.session() as session:
        prov_network = NetworksDbHandler().getNetworkList(session)[0]

        api._validate_mac_address(session, mac, prov_network)