Ejemplo n.º 1
0
 def __init__(self, user_id, details, request_id=None, resource_id=None):
     self.user_id = user_id
     self.details = details if isinstance(details, dict) else ({'msg': str(details)} if details is not None else {})
     self.timestamp = datetime.datetime.utcnow().isoformat()
     self.request_id = request_id
     self.resource_id = resource_id
     self.service_record = get_my_service_record()
Ejemplo n.º 2
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.º 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