Esempio n. 1
0
def assert_solr_schema_is_the_dgu_variant():
    '''
        Checks if the schema version of the SOLR server is compatible
        with DGU.

        Based on ckan.lib.search.check_solr_schema_version
    '''

    import urllib2
    from ckan.lib.search.common import is_available, SolrSettings
    from ckan.lib.search import SOLR_SCHEMA_FILE_OFFSET

    if not is_available():
        # Something is wrong with the SOLR server
        log.warn('Problems were found while connecting to the SOLR server')
        return False

    # Request the schema XML file to extract the version
    solr_url, solr_user, solr_password = SolrSettings.get()
    http_auth = None
    if solr_user is not None and solr_password is not None:
        http_auth = solr_user + ':' + solr_password
        http_auth = 'Basic ' + http_auth.encode('base64').strip()

    url = solr_url.strip('/') + SOLR_SCHEMA_FILE_OFFSET

    req = urllib2.Request(url=url)
    if http_auth:
        req.add_header('Authorization', http_auth)

    solr_schema = urllib2.urlopen(req).read()
    assert 'DGU variant' in solr_schema, 'Change your development.ini to use the DGU schema.'
Esempio n. 2
0
def assert_solr_schema_is_the_dgu_variant():
    '''
        Checks if the schema version of the SOLR server is compatible
        with DGU.

        Based on ckan.lib.search.check_solr_schema_version
    '''

    import urllib2
    from ckan.lib.search.common import is_available, SolrSettings
    from ckan.lib.search import SOLR_SCHEMA_FILE_OFFSET

    if not is_available():
        # Something is wrong with the SOLR server
        log.warn('Problems were found while connecting to the SOLR server')
        return False

    # Request the schema XML file to extract the version
    solr_url, solr_user, solr_password = SolrSettings.get()
    http_auth = None
    if solr_user is not None and solr_password is not None:
        http_auth = solr_user + ':' + solr_password
        http_auth = 'Basic ' + http_auth.encode('base64').strip()

    url = solr_url.strip('/') + SOLR_SCHEMA_FILE_OFFSET

    req = urllib2.Request(url=url)
    if http_auth:
        req.add_header('Authorization', http_auth)

    solr_schema = urllib2.urlopen(req).read()
    assert 'DGU variant' in solr_schema, 'Change your development.ini to use the DGU schema.'
Esempio n. 3
0
def check_solr_schema_version(schema_file=None):
    '''
        Checks if the schema version of the SOLR server is compatible
        with this CKAN version.

        The schema will be retrieved from the SOLR server, using the
        offset defined in SOLR_SCHEMA_FILE_OFFSET_MANAGED
        ('/schema?wt=schema.xml'). If SOLR is set to use the manually
        edited `schema.xml`, the schema will be retrieved from the SOLR
        server using the offset defined in
        SOLR_SCHEMA_FILE_OFFSET_CLASSIC ('/admin/file/?file=schema.xml').

        The schema_file parameter allows to override this pointing to
        different schema file, but it should only be used for testing
        purposes.

        If the CKAN instance is configured to not use SOLR or the SOLR
        server is not available, the function will return False, as the
        version check does not apply. If the SOLR server is available,
        a SearchError exception will be thrown if the version could not
        be extracted or it is not included in the supported versions list.

        :schema_file: Absolute path to an alternative schema file. Should
                      be only used for testing purposes (Default is None)
    '''

    if not is_available():
        # Something is wrong with the SOLR server
        log.warn('Problems were found while connecting to the SOLR server')
        return False

    # Try to get the schema XML file to extract the version
    if not schema_file:
        try:
            # Try Managed Schema
            res = _get_schema_from_solr(SOLR_SCHEMA_FILE_OFFSET_MANAGED)
            res.raise_for_status()
        except requests.HTTPError:
            # Fallback to Manually Edited schema.xml
            res = _get_schema_from_solr(SOLR_SCHEMA_FILE_OFFSET_CLASSIC)
        schema_content = res.text
    else:
        with open(schema_file, 'rb') as f:
            schema_content = f.read()

    tree = xml.dom.minidom.parseString(schema_content)

    version = tree.documentElement.getAttribute('version')
    if not len(version):
        msg = 'Could not extract version info from the SOLR schema'
        if schema_file:
            msg += ', using file {}'.format(schema_file)
        raise SearchError(msg)

    if not version in SUPPORTED_SCHEMA_VERSIONS:
        raise SearchError('SOLR schema version not supported: %s. Supported'
                          ' versions are [%s]'
                          % (version, ', '.join(SUPPORTED_SCHEMA_VERSIONS)))
    return True