Example #1
0
    def _updateDatabase(self, dnsZone):
        with self.dbm.session() as session:
            try:
                dbValue = GlobalParametersDbHandler().getParameter(
                    session, 'DNSZone')

                # Update existing value
                dbValue.value = dnsZone
            except NoResultFound:
                dbValue = GlobalParameter(name='DNSZone', value=dnsZone)

                session.append(dbValue)

            session.commit()
Example #2
0
    def __init__(self):
        super().__init__()

        self._softwareProfilesDbHandler = SoftwareProfilesDbHandler()
        self._globalParametersDbHandler = GlobalParametersDbHandler()
        self._adminsDbHandler = AdminsDbHandler()
        self._osDbHandler = OperatingSystemsDbHandler()
Example #3
0
    def __init__(self):
        TortugaDbApi.__init__(self)

        self._softwareProfilesDbHandler = SoftwareProfilesDbHandler()
        self._nodesDbHandler = NodesDbHandler()
        self._globalParametersDbHandler = GlobalParametersDbHandler()
        self._adminsDbHandler = AdminsDbHandler()
        self._osDbHandler = OperatingSystemsDbHandler()
Example #4
0
    def __init__(self):
        TortugaDbObjectHandler.__init__(self)

        self._nicsDbHandler = NicsDbHandler()
        self._hardwareProfilesDbHandler = HardwareProfilesDbHandler()
        self._softwareProfilesDbHandler = SoftwareProfilesDbHandler()
        self._softwareUsesHardwareDbHandler = SoftwareUsesHardwareDbHandler()
        self._globalParametersDbHandler = GlobalParametersDbHandler()
Example #5
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()
Example #6
0
    def _getOldDnsZone(self):
        """
        Returns None if DNS zone previously undefined
        """

        with self.dbm.session() as session:
            try:
                result = GlobalParametersDbHandler().getParameter(
                    session, 'DNSZone')

                return result.value.lower() if result.value else None
            except ParameterNotFound:
                return None
Example #7
0
    def start(self,
              addNodesRequest,
              dbSession,
              dbHardwareProfile,
              dbSoftwareProfile=None) -> List[Nodes]:
        """
        Raises:
            CommandFailed
        """

        # 'nodeDetails' is a list of details (contained in a 'dict') for
        # one or more nodes. It can contain host name(s) and nic details
        # like MAC and/or IP address. It is an entirely optional data
        # structure and may be empty and/or undefined.

        nodeDetails = self.__get_node_details(addNodesRequest,
                                              dbHardwareProfile,
                                              dbSoftwareProfile)

        # return self.__add_predefined_nodes(
        #     addNodesRequest, dbSession, dbHardwareProfile,
        #     dbSoftwareProfile) \
        #     if nodeDetails else self.__dhcp_discovery(
        #         addNodesRequest, dbSession, dbHardwareProfile,
        #         dbSoftwareProfile)

        if not nodeDetails:
            raise CommandFailed('Invalid operation (DHCP discovery)')

        try:
            dns_zone = GlobalParametersDbHandler().getParameter(
                dbSession, 'DNSZone').value
        except ParameterNotFound:
            dns_zone = ''

        nodes = self.__add_predefined_nodes(addNodesRequest,
                                            dbSession,
                                            dbHardwareProfile,
                                            dbSoftwareProfile,
                                            dns_zone=dns_zone)

        # This is a necessary evil for the time being, until there's
        # a proper context manager implemented.
        self.addHostApi.clear_session_nodes(nodes)

        return nodes
Example #8
0
    def __init__(self):
        TortugaDbApi.__init__(self)

        self._globalParametersDbHandler = GlobalParametersDbHandler()
Example #9
0
class GlobalParameterDbApi(TortugaDbApi):
    """
    Global Parameter DB API class
    """

    def __init__(self):
        TortugaDbApi.__init__(self)

        self._globalParametersDbHandler = GlobalParametersDbHandler()

    def getParameter(self, session: Session, name: str) -> Parameter:
        """
        Returns the named parameter
        """

        try:
            dbParam = self._globalParametersDbHandler.getParameter(
                session, name)

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

    def getParameterList(self, session: Session) -> TortugaObjectList:
        """
        Get list of all available parameters from the db.

            Returns:
                [parameter]
            Throws:
                DbError
        """

        try:
            dbParameters = self._globalParametersDbHandler.getParameterList(
                session)

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

    def addParameter(self, session: Session, parameter: Parameter) -> None:
        """
        Insert parameter into the db.

            Returns:
                parameterId
            Throws:
                ParameterAlreadyExists
                DbError
        """

        try:
            self._globalParametersDbHandler.addParameter(
                session, parameter)

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

    def upsertParameter(self, session: Session, parameter: Parameter) -> None:
        try:
            self._globalParametersDbHandler.upsertParameter(
                session, parameter.getName(), parameter.getValue(),
                description=parameter.getDescription())

            session.commit()
        except TortugaException:
            session.rollback()
            raise
        except Exception:
            session.rollback()
            self._logger.exception('upsertParameter failed')
            raise

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

            Returns:
                None
            Throws:
                ParameterNotFound
                DbError
        """

        try:
            p = self._globalParametersDbHandler.getParameter(session, name)

            self._logger.debug('Deleting parameter [%s]' % (name))

            session.delete(p)

            session.commit()

            self._logger.info('Deleted parameter [%s]' % name)
        except TortugaException:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self._logger.exception(str(ex))
            raise
Example #10
0
def init_global_parameters(session, settings):
    # Create Global Parameters
    pApi = GlobalParametersDbHandler()

    pApi.addParameter(session, Parameter('Language', settings['language']))
    pApi.addParameter(session, Parameter('Keyboard', settings['keyboard']))
    pApi.addParameter(session, Parameter('Timezone_zone',
                                         settings['timezone']))
    pApi.addParameter(session, Parameter('Timezone_utc', settings['utc']))
    pApi.addParameter(session, Parameter('DbSchemaVers', '3'))
    pApi.addParameter(session, Parameter('IntWebPort', settings['intWebPort']))
    pApi.addParameter(
        session, Parameter('IntWebServicePort', settings['intWebServicePort']))
    pApi.addParameter(session,
                      Parameter('WebservicePort', settings['adminPort']))
    pApi.addParameter(session,
                      Parameter('EulaAccepted', settings['eulaAccepted']))
    pApi.addParameter(session, Parameter('DNSZone', 'private'))
    pApi.addParameter(session, Parameter('depot', settings['depotpath']))
Example #11
0
def get_puppet_node_yaml(session, nodeName):
    _cm = ConfigManager()

    publicInstallerFQDN = _cm.getInstaller().lower()
    primaryInstallerHostName = publicInstallerFQDN.split('.', 1)[0]

    try:
        dnsZone = GlobalParametersDbHandler().getParameter(
            session, 'DNSZone').value.lower()
    except ParameterNotFound:
        dnsZone = None

    try:
        depot_path = GlobalParametersDbHandler().getParameter(
            session, 'depot').value.lower()

        _cm.setDepotDir(depot_path)
    except ParameterNotFound:
        pass

    bInstaller = primaryInstallerHostName == nodeName.split('.', 1)[0]

    try:
        dbNode = NodesDbHandler().getNode(session, nodeName)
    except NodeNotFound:
        sys.exit(1)

    data = None
    try:
        from tortuga.db.dataRequestsDbHandler import DataRequestsDbHandler
        dbDataRequest = DataRequestsDbHandler().get_by_addHostSession(
            session, dbNode.addHostSession)
        if dbDataRequest:
            data = dbDataRequest.request
    except Exception as e:
        pass

    if dbNode.hardwareprofile.nics:
        privateInstallerFQDN = '%s%s%s' % (primaryInstallerHostName,
                                           get_installer_hostname_suffix(
                                               dbNode.hardwareprofile.nics[0],
                                               enable_interface_aliases=None),
                                           '.%s' %
                                           (dnsZone) if dnsZone else '')
    else:
        privateInstallerFQDN = '%s%s' % (primaryInstallerHostName, '.%s' %
                                         (dnsZone) if dnsZone else '')

    if not bInstaller and dbNode.hardwareprofile.location == 'local':
        # If the hardware profile does not have an associated provisioning
        # NIC, use the public installer FQDN by default. This can happen if
        # the user has added their own "public" nodes to a local hardware
        # profile.

        if not dbNode.hardwareprofile.nics:
            installerHostName = publicInstallerFQDN
        else:
            installerHostName = privateInstallerFQDN
    else:
        # If the specified node is the installer itself or a node
        # accessing the installer through it's public interface, use the
        # public host name.
        installerHostName = publicInstallerFQDN

    puppet_classes = {}

    enabledKits = set()

    if dbNode.softwareprofile:

        for dbComponent in dbNode.softwareprofile.components:

            if not dbComponent.kit.isOs:
                #
                # Load the kit and component installers
                #
                kit_spec = (dbComponent.kit.name, dbComponent.kit.version,
                            dbComponent.kit.iteration)
                kit_installer = get_kit_installer(kit_spec)()
                kit_installer.session = session
                _component = kit_installer.get_component_installer(
                    dbComponent.name)

                #
                # Get the puppet args for the component
                #
                try:
                    puppet_class_args = _component.run_action(
                        'get_puppet_args',
                        dbNode.softwareprofile,
                        dbNode.hardwareprofile,
                        data=data)
                    if puppet_class_args is not None:
                        puppet_classes[_component.puppet_class] = \
                            puppet_class_args
                except Exception:  # noqa pylint: disable=broad-except
                    # suppress exception if unable to get Puppet args
                    puppet_classes[_component.puppet_class] = {}

            else:
                #
                # OS kit component is omitted on installer. The installer
                # is assumed to have a pre-existing OS repository
                # configuration.
                #
                if bInstaller:
                    continue

            enabledKits.add(dbComponent.kit)

    dataDict = {}

    if puppet_classes:
        dataDict['classes'] = puppet_classes

    parametersDict = {}
    dataDict['parameters'] = parametersDict

    # software profile
    if dbNode.softwareprofile:
        parametersDict['swprofilename'] = dbNode.softwareprofile.name

    # hardware profile
    parametersDict['hwprofilename'] = dbNode.hardwareprofile.name

    # installer hostname
    parametersDict['primary_installer_hostname'] = installerHostName

    # Local repos directory
    repodir = os.path.join(_cm.getDepotDir(), 'kits')

    # Build YUM repository entries only if we have kits associated with
    # the software profile.
    if enabledKits:
        repourl = _cm.getIntWebRootUrl(installerHostName) + '/repos' \
            if not bInstaller else 'file://{0}'.format(repodir)

        repo_type = None

        if dbNode.softwareprofile.os.family.name == 'rhel':
            repo_type = 'yum'
        # elif dbNode.softwareprofile.os.family == 'ubuntu':
        #     repo_type = 'apt'

        if repo_type:
            # Only add 'repos' entries for supported operating system
            # families.

            repos_dict = {}

            for kit in enabledKits:
                if kit.isOs:
                    verstr = str(kit.version)
                    arch = kit.components[0].os[0].arch
                else:
                    verstr = '%s-%s' % (kit.version, kit.iteration)
                    arch = 'noarch'

                for dbKitSource in dbNode.softwareprofile.kitsources:
                    if dbKitSource in kit.sources:
                        baseurl = dbKitSource.url
                        break
                else:
                    subpath = '%s/%s/%s' % (kit.name, verstr, arch)

                    if not kit.isOs and not os.path.exists(
                            os.path.join(repodir, subpath,
                                         'repodata/repomd.xml')):
                        continue

                    baseurl = '%s/%s' % (repourl, subpath)

                    # [TODO] temporary workaround for handling RHEL media
                    # path.
                    #
                    # This code is duplicated from tortuga.boot.distro
                    if kit.isOs and \
                       dbNode.softwareprofile.os.name == 'rhel' and \
                       dbNode.softwareprofile.os.family.version != '7':
                        subpath += '/Server'

                if repo_type == 'yum':
                    if dbNode.hardwareprofile.location == 'remote':
                        cost = 1200
                    else:
                        cost = 1000

                    repos_dict['uc-kit-%s' % (kit.name)] = {
                        'type': repo_type,
                        'baseurl': baseurl,
                        'cost': cost,
                    }

            if repos_dict:
                parametersDict['repos'] = repos_dict

    # Enable '3rdparty' repo
    if dbNode.softwareprofile:
        third_party_repo_subpath = '3rdparty/%s/%s/%s' % (
            dbNode.softwareprofile.os.family.name,
            dbNode.softwareprofile.os.family.version,
            dbNode.softwareprofile.os.arch)

        local_repos_path = os.path.join(repodir, third_party_repo_subpath)

        # Check for existence of repository metadata to validate existence
        if enabledKits and os.path.exists(
                os.path.join(local_repos_path, 'repodata', 'repomd.xml')):
            third_party_repo_dict = {
                'tortuga-third-party': {
                    'type': 'yum',
                    'baseurl': os.path.join(repourl, third_party_repo_subpath),
                },
            }

            if 'repos' not in parametersDict:
                parametersDict['repos'] = third_party_repo_dict
            else:
                parametersDict['repos'] = dict(
                    list(parametersDict['repos'].items()) +
                    list(third_party_repo_dict.items()))

    # environment
    dataDict['environment'] = 'production'

    sys.stdout.write(
        yaml.safe_dump(dataDict, default_flow_style=False,
                       explicit_start=True))
Example #12
0
class GlobalParameterDbApi(TortugaDbApi):
    """
    Global Parameter DB API class
    """

    def __init__(self):
        TortugaDbApi.__init__(self)

        self._globalParametersDbHandler = GlobalParametersDbHandler()

    def getParameter(self, name):
        """
        Returns the named parameter
        """

        session = DbManager().openSession()

        try:
            dbParam = self._globalParametersDbHandler.getParameter(
                session, name)

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

    def getParameterList(self):
        """
        Get list of all available parameters from the db.

            Returns:
                [parameter]
            Throws:
                DbError
        """

        session = DbManager().openSession()

        try:
            dbParameters = self._globalParametersDbHandler.getParameterList(
                session)

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

    def addParameter(self, parameter):
        """
        Insert parameter into the db.

            Returns:
                parameterId
            Throws:
                ParameterAlreadyExists
                DbError
        """

        session = DbManager().openSession()

        try:
            self._globalParametersDbHandler.addParameter(
                session, parameter)

            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 upsertParameter(self, parameter):
        session = DbManager().openSession()

        try:
            self._globalParametersDbHandler.upsertParameter(
                session, parameter.getName(), parameter.getValue(),
                description=parameter.getDescription())

            session.commit()
        except TortugaException:
            session.rollback()
            raise
        except Exception:
            session.rollback()
            self.getLogger().exception('upsertParameter failed')
            raise
        finally:
            DbManager().closeSession()

    def deleteParameter(self, name):
        """
        Delete parameter from the db.

            Returns:
                None
            Throws:
                ParameterNotFound
                DbError
        """

        session = DbManager().openSession()

        try:
            self._globalParametersDbHandler.deleteParameter(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()