Ejemplo n.º 1
0
def get_endpoints(service_name):
    """
    Return a list of endpoint urls for the given service name.
    :param service_name:
    :return: list of url strings
    """

    local_conf = localconfig.get_config()
    urls = []

    try:
        if service_name + '_endpoint' in local_conf:
            urls = [re.sub("/+$", "", local_conf[service_name + '_endpoint'])]
        else:
            with session_scope() as dbsession:
                service_reports = db_services.get_byname(service_name, session=dbsession)
                if service_reports:
                    for service in service_reports:
                        base_url = service.get('base_url')
                        if base_url:
                            apiversion = service.get('version', '')
                            urls.append('/'.join([base_url, apiversion]))
                        else:
                            raise Exception("cannot load valid endpoint from DB for service {}".format(service_name))

            if not urls:
                raise Exception("cannot locate registered service in DB: " + service_name)
    except Exception as err:
        logger.exception('Error during endpoint lookup for service {}'.format(service_name))
        raise Exception("could not find valid endpoint - exception: " + str(err))

    return urls
Ejemplo n.º 2
0
def update_service_cache(servicename, skipcache=False):
    global scache, scache_template

    fromCache = True
    if skipcache or servicename not in scache:
        scache[servicename] = copy.deepcopy(scache_template)
        fromCache = False

    if not scache[servicename]['records']:
        fromCache = False
    else:
        for record in scache[servicename]['records']:
            if not record['status']:
                fromCache = False

    if (time.time() -
            scache[servicename]['last_updated']) > scache[servicename]['ttl']:
        fromCache = False

    if not fromCache:
        # refresh the cache for this service from catalog call
        try:
            logger.debug("fetching services (" + str(servicename) + ")")
            with session_scope() as dbsession:
                service_records = db_services.get_byname(servicename,
                                                         session=dbsession)
            logger.debug("services fetched: " + str(service_records))
        except Exception as err:
            logger.warn("cannot get service: " + str(err))
            service_records = []

        if service_records:
            new_records = []
            for service_record in service_records:
                if service_record['status']:
                    new_records.append(service_record)

            scache[servicename]['records'] = new_records
            scache[servicename]['last_updated'] = time.time()

    return (fromCache)
Ejemplo n.º 3
0
    def _register(self):
        if not self.is_enabled:
            logger.error(
                'Service not enabled in config, not registering service: ' +
                self.name)
            raise Exception('No service enabled, cannot continue bootstrap')

        logger.info('Registering service: {}'.format(self.name))

        service_template = {
            'type': 'anchore',
            'base_url': 'N/A',
            'status_base_url': 'N/A',
            'version': 'v1',
            'short_description': ''
        }

        hstring = 'http'
        if 'external_tls' in self.configuration:
            if self.configuration.get('external_tls', False):
                hstring = 'https'
        elif 'ssl_enable' in self.configuration:
            if self.configuration.get('ssl_enable', False):
                hstring = 'https'

        endpoint_hostname = endpoint_port = endpoint_hostport = None
        if self.configuration.get('external_hostname', False):
            endpoint_hostname = self.configuration.get('external_hostname')
        elif self.configuration.get('endpoint_hostname', False):
            endpoint_hostname = self.configuration.get('endpoint_hostname')

        if self.configuration.get('external_port', False):
            endpoint_port = int(self.configuration.get('external_port'))
        elif self.configuration.get('port', False):
            endpoint_port = int(self.configuration.get('port'))

        if endpoint_hostname:
            endpoint_hostport = endpoint_hostname
            if endpoint_port:
                endpoint_hostport = endpoint_hostport + ":" + str(
                    endpoint_port)

        if endpoint_hostport:
            service_template['base_url'] = "{}://{}".format(
                hstring, endpoint_hostport)
        else:
            raise Exception(
                "could not construct service base_url - please check service configuration for hostname/port settings"
            )

        try:
            service_template['status'] = False
            service_template['status_message'] = taskstate.base_state(
                'service_status')

            with session_scope() as dbsession:
                service_records = db_services.get_byname(self.__service_name__,
                                                         session=dbsession)

                # fail if trying to add a service that must be unique in the system, but one already is registered in DB
                if self.__is_unique_service__:
                    if len(service_records) > 1:
                        raise Exception(
                            'more than one entry for service type (' +
                            str(self.__service_name__) +
                            ') exists in DB, but service must be unique - manual DB intervention required'
                        )

                    for service_record in service_records:
                        if service_record and (service_record['hostid'] !=
                                               self.instance_id):
                            raise Exception(
                                'service type (' + str(self.__service_name__) +
                                ') already exists in system with different host_id - detail: my_host_id='
                                + str(self.instance_id) + ' db_host_id=' +
                                str(service_record['hostid']))

                # if all checks out, then add/update the registration
                ret = db_services.add(self.instance_id,
                                      self.__service_name__,
                                      service_template,
                                      session=dbsession)

                try:
                    my_service_record = {
                        'hostid': self.instance_id,
                        'servicename': self.__service_name__,
                    }
                    my_service_record.update(service_template)
                    servicestatus.set_my_service_record(my_service_record)
                    self.service_record = my_service_record
                except Exception as err:
                    logger.warn(
                        'could not set local service information - exception: {}'
                        .format(str(err)))

        except Exception as err:
            raise err

        service_record = servicestatus.get_my_service_record()
        servicestatus.set_status(service_record,
                                 up=True,
                                 available=True,
                                 update_db=True,
                                 versions=self.versions)
        logger.info('Service registration complete')
        return True
Ejemplo n.º 4
0
def registerService(sname, config, enforce_unique=True):
    ret = False
    myconfig = config['services'][sname]

    # TODO add version support/detection here

    service_template = {'type': 'anchore', 'base_url': 'N/A', 'version': 'v1'}

    if 'ssl_enable' in myconfig and myconfig['ssl_enable']:
        hstring = "https"
    else:
        hstring = "http"

    endpoint_hostname = endpoint_port = endpoint_hostport = None
    if 'endpoint_hostname' in myconfig:
        endpoint_hostname = myconfig['endpoint_hostname']
        service_template[
            'base_url'] = hstring + "://" + myconfig['endpoint_hostname']
    if 'port' in myconfig:
        endpoint_port = int(myconfig['port'])
        service_template['base_url'] += ":" + str(endpoint_port)

    if endpoint_hostname:
        endpoint_hostport = endpoint_hostname
        if endpoint_port:
            endpoint_hostport = endpoint_hostport + ":" + str(endpoint_port)

    try:
        service_template['status'] = True
        service_template['status_message'] = "registered"

        with session_scope() as dbsession:
            service_records = db_services.get_byname(sname, session=dbsession)

            # fail if trying to add a service that must be unique in the system, but one already is registered in DB
            if enforce_unique:
                if len(service_records) > 1:
                    raise Exception(
                        "more than one entry for service type (" + str(sname) +
                        ") exists in DB, but service must be unique - manual DB intervention required"
                    )

                for service_record in service_records:
                    if service_record and (service_record['hostid'] !=
                                           config['host_id']):
                        raise Exception(
                            "service type (" + str(sname) +
                            ") already exists in system with different host_id - detail: my_host_id="
                            + str(config['host_id']) + " db_host_id=" +
                            str(service_record['hostid']))

            # in any case, check if another host is registered that has the same endpoint
            for service_record in service_records:
                if service_record[
                        'base_url'] and service_record['base_url'] != 'N/A':
                    service_hostport = re.sub("^http.//", "",
                                              service_record['base_url'])
                    # if a different host_id has the same endpoint, fail
                    if (service_hostport == endpoint_hostport) and (
                            config['host_id'] != service_record['hostid']):
                        raise Exception(
                            "trying to add new host but found conflicting endpoint from another host in DB - detail: my_host_id="
                            + str(config['host_id']) + " db_host_id=" +
                            str(service_record['hostid']) +
                            " my_host_endpoint=" + str(endpoint_hostport) +
                            " db_host_endpoint=" + str(service_hostport))
                7
            # if all checks out, then add/update the registration
            ret = db_services.add(config['host_id'],
                                  sname,
                                  service_template,
                                  session=dbsession)

    except Exception as err:
        raise err

    return (ret)
Ejemplo n.º 5
0
def registerService(sname, config, enforce_unique=True):
    ret = False
    myconfig = config['services'][sname]

    # TODO add version support/detection here

    service_template = {
        'type': 'anchore',
        'base_url': 'N/A',
        'status_base_url': 'N/A',
        'version': 'v1',
        'short_description': ''
    }

    #if 'ssl_enable' in myconfig and myconfig['ssl_enable']:
    if myconfig.get('ssl_enable', False) or myconfig.get(
            'external_tls', False):
        hstring = "https"
    else:
        hstring = "http"

    endpoint_hostname = endpoint_port = endpoint_hostport = None

    if 'endpoint_hostname' in myconfig:
        endpoint_hostname = myconfig['endpoint_hostname']
        service_template[
            'base_url'] = hstring + "://" + myconfig['endpoint_hostname']
    if 'port' in myconfig:
        endpoint_port = int(myconfig['port'])
        service_template['base_url'] += ":" + str(endpoint_port)

    if endpoint_hostname:
        endpoint_hostport = endpoint_hostname
        if endpoint_port:
            endpoint_hostport = endpoint_hostport + ":" + str(endpoint_port)

    try:
        service_template['status'] = False
        service_template['status_message'] = taskstate.base_state(
            'service_status')

        with session_scope() as dbsession:
            service_records = db_services.get_byname(sname, session=dbsession)

            # fail if trying to add a service that must be unique in the system, but one already is registered in DB
            if enforce_unique:
                if len(service_records) > 1:
                    raise Exception(
                        "more than one entry for service type (" + str(sname) +
                        ") exists in DB, but service must be unique - manual DB intervention required"
                    )

                for service_record in service_records:
                    if service_record and (service_record['hostid'] !=
                                           config['host_id']):
                        raise Exception(
                            "service type (" + str(sname) +
                            ") already exists in system with different host_id - detail: my_host_id="
                            + str(config['host_id']) + " db_host_id=" +
                            str(service_record['hostid']))

            # if all checks out, then add/update the registration
            ret = db_services.add(config['host_id'],
                                  sname,
                                  service_template,
                                  session=dbsession)

            try:
                my_service_record = {
                    'hostid': config['host_id'],
                    'servicename': sname,
                }
                my_service_record.update(service_template)
                servicestatus.set_my_service_record(my_service_record)
            except Exception as err:
                logger.warn(
                    "could not set local service information - exception: {}".
                    format(str(err)))

    except Exception as err:
        raise err

    return (ret)
Ejemplo n.º 6
0
    def _register(self):
        if not self.is_enabled:
            logger.error(
                "Service not enabled in config, not registering service: " +
                self.name)
            raise Exception("No service enabled, cannot continue bootstrap")

        logger.info("Registering service: {}".format(self.name))

        service_template = {
            "type": "anchore",
            "base_url": "N/A",
            "status_base_url": "N/A",
            "version": "v1",
            "short_description": "",
        }

        hstring = "http"
        if "external_tls" in self.configuration:
            if self.configuration.get("external_tls", False):
                hstring = "https"
        elif "ssl_enable" in self.configuration:
            if self.configuration.get("ssl_enable", False):
                hstring = "https"

        endpoint_hostname = endpoint_port = endpoint_hostport = None
        if self.configuration.get("external_hostname", False):
            endpoint_hostname = self.configuration.get("external_hostname")
        elif self.configuration.get("endpoint_hostname", False):
            endpoint_hostname = self.configuration.get("endpoint_hostname")

        if self.configuration.get("external_port", False):
            endpoint_port = int(self.configuration.get("external_port"))
        elif self.configuration.get("port", False):
            endpoint_port = int(self.configuration.get("port"))

        if endpoint_hostname:
            endpoint_hostport = endpoint_hostname
            if endpoint_port:
                endpoint_hostport = endpoint_hostport + ":" + str(
                    endpoint_port)

        if endpoint_hostport:
            service_template["base_url"] = "{}://{}".format(
                hstring, endpoint_hostport)
        else:
            raise Exception(
                "could not construct service base_url - please check service configuration for hostname/port settings"
            )

        try:
            service_template["status"] = False
            service_template["status_message"] = taskstate.base_state(
                "service_status")

            with session_scope() as dbsession:
                service_records = db_services.get_byname(self.__service_name__,
                                                         session=dbsession)

                # fail if trying to add a service that must be unique in the system, but one already is registered in DB
                if self.__is_unique_service__:
                    if len(service_records) > 1:
                        raise Exception(
                            "more than one entry for service type (" +
                            str(self.__service_name__) +
                            ") exists in DB, but service must be unique - manual DB intervention required"
                        )

                    for service_record in service_records:
                        if service_record and (service_record["hostid"] !=
                                               self.instance_id):
                            raise Exception(
                                "service type (" + str(self.__service_name__) +
                                ") already exists in system with different host_id - detail: my_host_id="
                                + str(self.instance_id) + " db_host_id=" +
                                str(service_record["hostid"]))

                # if all checks out, then add/update the registration
                ret = db_services.add(
                    self.instance_id,
                    self.__service_name__,
                    service_template,
                    session=dbsession,
                )

                try:
                    my_service_record = {
                        "hostid": self.instance_id,
                        "servicename": self.__service_name__,
                    }
                    my_service_record.update(service_template)
                    servicestatus.set_my_service_record(my_service_record)
                    self.service_record = my_service_record
                except Exception as err:
                    logger.warn(
                        "could not set local service information - exception: {}"
                        .format(str(err)))

        except Exception as err:
            raise err

        service_record = servicestatus.get_my_service_record()
        servicestatus.set_status(
            service_record,
            up=True,
            available=True,
            update_db=True,
            versions=self.versions,
        )
        logger.info("Service registration complete")
        return True