Ejemplo n.º 1
0
    def __init__(self, host):
        logging.register_options(CONF)
        CONF(project='iotronic')
        logging.setup(CONF, "iotronic-conductor")

        signal.signal(signal.SIGINT, self.stop_handler)

        if not host:
            host = CONF.host
        self.host = host
        self.topic = MANAGER_TOPIC
        self.dbapi = dbapi.get_instance()

        try:
            cdr = self.dbapi.register_conductor(
                {'hostname': self.host})
        except exception.ConductorAlreadyRegistered:
            LOG.warn(_LW("A conductor with hostname %(hostname)s "
                         "was previously registered. Updating registration"),
                     {'hostname': self.host})

            cdr = self.dbapi.register_conductor({'hostname': self.host},
                                                update_existing=True)
        self.conductor = cdr

        transport = oslo_messaging.get_transport(cfg.CONF)
        target = oslo_messaging.Target(topic=self.topic, server=self.host,
                                       version=self.RPC_API_VERSION)

        ragent = self.dbapi.get_registration_wampagent()

        LOG.info("Found registration agent: %s on %s",
                 ragent.hostname, ragent.wsurl)

        endpoints = [
            endp.ConductorEndpoint(ragent),
        ]
        access_policy = dispatcher.DefaultRPCAccessPolicy
        self.server = oslo_messaging.get_rpc_server(
            transport, target,
            endpoints, executor='threading',
            access_policy=access_policy)

        self.server.start()

        while True:
            time.sleep(1)
Ejemplo n.º 2
0
    def __init__(self, host):

        signal.signal(signal.SIGINT, self.stop_handler)

        logging.register_options(CONF)

        CONF(project='iotronic')
        logging.setup(CONF, "iotronic-wamp-agent")

        if CONF.debug:
            txaio.start_logging(level="debug")

        # to be removed asap
        self.host = host
        self.dbapi = dbapi.get_instance()

        try:
            wpa = self.dbapi.register_wampagent(
                {'hostname': self.host, 'wsurl': CONF.wamp.wamp_transport_url})

        except exception.WampAgentAlreadyRegistered:
            LOG.warn(_LW("A wampagent with hostname %(hostname)s "
                         "was previously registered. Updating registration"),
                     {'hostname': self.host})

        wpa = self.dbapi.register_wampagent(
            {'hostname': self.host, 'wsurl': CONF.wamp.wamp_transport_url},
            update_existing=True)
        self.wampagent = wpa
        self.wampagent.ragent = CONF.wamp.register_agent
        self.wampagent.save()

        global AGENT_HOST
        AGENT_HOST = self.host

        self.r = RPCServer()
        self.w = WampManager()

        self.r.start()
        self.w.start()
Ejemplo n.º 3
0
class Fleet(base.IotronicObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    dbapi = db_api.get_instance()

    fields = {
        'id': int,
        'uuid': obj_utils.str_or_none,
        'name': obj_utils.str_or_none,
        'project': obj_utils.str_or_none,
        'description': obj_utils.str_or_none,
        'extra': obj_utils.dict_or_none,
    }

    @staticmethod
    def _from_db_object(fleet, db_fleet):
        """Converts a database entity to a formal object."""
        for field in fleet.fields:
            fleet[field] = db_fleet[field]
        fleet.obj_reset_changes()
        return fleet

    @base.remotable_classmethod
    def get(cls, context, fleet_id):
        """Find a fleet based on its id or uuid and return a Board object.

        :param fleet_id: the id *or* uuid of a fleet.
        :returns: a :class:`Board` object.
        """
        if strutils.is_int_like(fleet_id):
            return cls.get_by_id(context, fleet_id)
        elif uuidutils.is_uuid_like(fleet_id):
            return cls.get_by_uuid(context, fleet_id)
        else:
            raise exception.InvalidIdentity(identity=fleet_id)

    @base.remotable_classmethod
    def get_by_id(cls, context, fleet_id):
        """Find a fleet based on its integer id and return a Board object.

        :param fleet_id: the id of a fleet.
        :returns: a :class:`Board` object.
        """
        db_fleet = cls.dbapi.get_fleet_by_id(fleet_id)
        fleet = Fleet._from_db_object(cls(context), db_fleet)
        return fleet

    @base.remotable_classmethod
    def get_by_uuid(cls, context, uuid):
        """Find a fleet based on uuid and return a Board object.

        :param uuid: the uuid of a fleet.
        :returns: a :class:`Board` object.
        """
        db_fleet = cls.dbapi.get_fleet_by_uuid(uuid)
        fleet = Fleet._from_db_object(cls(context), db_fleet)
        return fleet

    @base.remotable_classmethod
    def get_by_name(cls, context, name):
        """Find a fleet based on name and return a Board object.

        :param name: the logical name of a fleet.
        :returns: a :class:`Board` object.
        """
        db_fleet = cls.dbapi.get_fleet_by_name(name)
        fleet = Fleet._from_db_object(cls(context), db_fleet)
        return fleet

    @base.remotable_classmethod
    def list(cls,
             context,
             limit=None,
             marker=None,
             sort_key=None,
             sort_dir=None,
             filters=None):
        """Return a list of Fleet objects.

        :param context: Security context.
        :param limit: maximum number of resources to return in a single result.
        :param marker: pagination marker for large data sets.
        :param sort_key: column to sort results by.
        :param sort_dir: direction to sort. "asc" or "desc".
        :param filters: Filters to apply.
        :returns: a list of :class:`Fleet` object.

        """
        db_fleets = cls.dbapi.get_fleet_list(filters=filters,
                                             limit=limit,
                                             marker=marker,
                                             sort_key=sort_key,
                                             sort_dir=sort_dir)
        return [Fleet._from_db_object(cls(context), obj) for obj in db_fleets]

    @base.remotable
    def create(self, context=None):
        """Create a Fleet record in the DB.

        Column-wise updates will be made based on the result of
        self.what_changed(). If target_power_state is provided,
        it will be checked against the in-database copy of the
        fleet before updates are made.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Fleet(context)

        """

        values = self.obj_get_changes()
        db_fleet = self.dbapi.create_fleet(values)
        self._from_db_object(self, db_fleet)

    @base.remotable
    def destroy(self, context=None):
        """Delete the Fleet from the DB.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Fleet(context)
        """
        self.dbapi.destroy_fleet(self.uuid)
        self.obj_reset_changes()

    @base.remotable
    def save(self, context=None):
        """Save updates to this Fleet.

        Column-wise updates will be made based on the result of
        self.what_changed(). If target_power_state is provided,
        it will be checked against the in-database copy of the
        fleet before updates are made.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Fleet(context)
        """
        updates = self.obj_get_changes()
        self.dbapi.update_fleet(self.uuid, updates)
        self.obj_reset_changes()

    @base.remotable
    def refresh(self, context=None):
        """Refresh the object by re-fetching from the DB.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Fleet(context)
        """
        current = self.__class__.get_by_uuid(self._context, self.uuid)
        for field in self.fields:
            if (hasattr(self, base.get_attrname(field))
                    and self[field] != current[field]):
                self[field] = current[field]
Ejemplo n.º 4
0
class Location(base.IotronicObject):
    VERSION = '1.0'

    dbapi = dbapi.get_instance()

    fields = {
        'id': int,
        'node_id': obj_utils.int_or_none,
        'longitude': obj_utils.str_or_none,
        'latitude': obj_utils.str_or_none,
        'altitude': obj_utils.str_or_none,
    }

    @staticmethod
    def _from_db_object(location, db_location):
        """Converts a database entity to a formal object."""
        for field in location.fields:
            location[field] = db_location[field]

        location.obj_reset_changes()
        return location

    @staticmethod
    def _from_db_object_list(db_objects, cls, context):
        """Converts a list of database entities to a list of formal objects."""
        return [
            Location._from_db_object(cls(context), obj) for obj in db_objects
        ]

    @base.remotable_classmethod
    def get(cls, context, location_id):
        """Find a location based on its id or uuid and return a Location object.

        :param location_id: the id *or* uuid of a location.
        :returns: a :class:`Location` object.
        """
        if strutils.is_int_like(location_id):
            return cls.get_by_id(context, location_id)
        elif uuidutils.is_uuid_like(location_id):
            return cls.get_by_uuid(context, location_id)
        else:
            raise exception.InvalidIdentity(identity=location_id)

    @base.remotable_classmethod
    def get_by_id(cls, context, location_id):
        """Find a location based on its integer id and return a Location object.

        :param location_id: the id of a location.
        :returns: a :class:`Location` object.
        """
        db_location = cls.dbapi.get_location_by_id(location_id)
        location = Location._from_db_object(cls(context), db_location)
        return location

    @base.remotable_classmethod
    def get_by_uuid(cls, context, uuid):
        """Find a location based on uuid and return a :class:`Location` object.

        :param uuid: the uuid of a location.
        :param context: Security context
        :returns: a :class:`Location` object.
        """
        db_location = cls.dbapi.get_location_by_uuid(uuid)
        location = Location._from_db_object(cls(context), db_location)
        return location

    @base.remotable_classmethod
    def list(cls,
             context,
             limit=None,
             marker=None,
             sort_key=None,
             sort_dir=None):
        """Return a list of Location objects.

        :param context: Security context.
        :param limit: maximum number of resources to return in a single result.
        :param marker: pagination marker for large data sets.
        :param sort_key: column to sort results by.
        :param sort_dir: direction to sort. "asc" or "desc".
        :returns: a list of :class:`Location` object.

        """
        db_locations = cls.dbapi.get_location_list(limit=limit,
                                                   marker=marker,
                                                   sort_key=sort_key,
                                                   sort_dir=sort_dir)
        return Location._from_db_object_list(db_locations, cls, context)

    @base.remotable_classmethod
    def list_by_node_id(cls,
                        context,
                        node_id,
                        limit=None,
                        marker=None,
                        sort_key=None,
                        sort_dir=None):
        """Return a list of Location objects associated with a given node ID.

        :param context: Security context.
        :param node_id: the ID of the node.
        :param limit: maximum number of resources to return in a single result.
        :param marker: pagination marker for large data sets.
        :param sort_key: column to sort results by.
        :param sort_dir: direction to sort. "asc" or "desc".
        :returns: a list of :class:`Location` object.

        """
        db_locations = cls.dbapi.get_locations_by_node_id(node_id,
                                                          limit=limit,
                                                          marker=marker,
                                                          sort_key=sort_key,
                                                          sort_dir=sort_dir)
        return Location._from_db_object_list(db_locations, cls, context)

    @base.remotable
    def create(self, context=None):
        """Create a Location record in the DB.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Location(context)

        """
        values = self.obj_get_changes()
        db_location = self.dbapi.create_location(values)
        self._from_db_object(self, db_location)

    @base.remotable
    def destroy(self, context=None):
        """Delete the Location from the DB.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Location(context)
        """
        self.dbapi.destroy_location(self.uuid)
        self.obj_reset_changes()

    @base.remotable
    def save(self, context=None):
        """Save updates to this Location.

        Updates will be made column by column based on the result
        of self.what_changed().

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Location(context)
        """
        updates = self.obj_get_changes()
        self.dbapi.update_location(self.uuid, updates)

        self.obj_reset_changes()

    @base.remotable
    def refresh(self, context=None):
        """Loads updates for this Location.

        Loads a location with the same uuid from the database and
        checks for updated attributes. Updates are applied from
        the loaded location column by column, if there are any updates.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Location(context)
        """
        current = self.__class__.get_by_uuid(self._context, uuid=self.uuid)
        for field in self.fields:
            if (hasattr(self, base.get_attrname(field))
                    and self[field] != current[field]):
                self[field] = current[field]
Ejemplo n.º 5
0
class ExposedService(base.IotronicObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    dbapi = db_api.get_instance()

    fields = {
        'id': int,
        'board_uuid': obj_utils.str_or_none,
        'service_uuid': obj_utils.str_or_none,
        'public_port': int
    }

    @staticmethod
    def _from_db_object(exposed_service, db_exposed_service):
        """Converts a database entity to a formal object."""
        for field in exposed_service.fields:
            exposed_service[field] = db_exposed_service[field]
        exposed_service.obj_reset_changes()
        return exposed_service

    @base.remotable_classmethod
    def get_by_id(cls, context, exposed_service_id):
        """Find a exposed_service based on its integer id and return a Board object.

        :param exposed_service_id: the id of a exposed_service.
        :returns: a :class:`exposed_service` object.
        """
        db_exp_service = cls.dbapi.get_exposed_service_by_id(
            exposed_service_id)
        exp_service = ExposedService._from_db_object(cls(context),
                                                     db_exp_service)
        return exp_service

    @base.remotable_classmethod
    def get_by_board_uuid(cls, context, board_uuid):
        """Return a list of ExposedService objects.

        :param context: Security context.
        :param limit: maximum number of resources to return in a single result.
        :param marker: pagination marker for large data sets.
        :param sort_key: column to sort results by.
        :param sort_dir: direction to sort. "asc" or "desc".
        :param filters: Filters to apply.
        :returns: a list of :class:`ExposedService` object.

        """
        db_exps = cls.dbapi.get_exposed_services_by_board_uuid(board_uuid)
        return [ExposedService._from_db_object(cls(context), obj)
                for obj in db_exps]

    @base.remotable_classmethod
    def get_by_service_uuid(cls, context, service_uuid):
        """Find a exposed_service based on uuid and return a Board object.

        :param service_uuid: the uuid of a exposed_service.
        :returns: a :class:`exposed_service` object.
        """
        db_exp_service = cls.dbapi.get_exposed_service_by_service_uuid(
            service_uuid)
        exp_service = ExposedService._from_db_object(cls(context),
                                                     db_exp_service)
        return exp_service

    @base.remotable_classmethod
    def get(cls, context, board_uuid, service_uuid):
        """Find a exposed_service based on uuid and return a Service object.

        :param board_uuid: the uuid of a exposed_service.
        :returns: a :class:`exposed_service` object.
        """
        db_exp_service = cls.dbapi.get_exposed_service_by_uuids(board_uuid,
                                                                service_uuid)
        exp_service = ExposedService._from_db_object(cls(context),
                                                     db_exp_service)
        return exp_service

    @base.remotable_classmethod
    def list(cls, context, board_uuid):
        """Return a list of ExposedService objects.

        :param context: Security context.
        :param limit: maximum number of resources to return in a single result.
        :param marker: pagination marker for large data sets.
        :param sort_key: column to sort results by.
        :param sort_dir: direction to sort. "asc" or "desc".
        :param filters: Filters to apply.
        :returns: a list of :class:`ExposedService` object.

        """
        db_exps = cls.dbapi.get_exposed_service_list(board_uuid)
        return [ExposedService._from_db_object(cls(context), obj)
                for obj in db_exps]

    @base.remotable
    def create(self, context=None):
        """Create a ExposedService record in the DB.

        Column-wise updates will be made based on the result of
        self.what_changed(). If target_power_state is provided,
        it will be checked against the in-database copy of the
        exposed_service before updates are made.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: ExposedService(context)

        """
        values = self.obj_get_changes()
        db_exposed_service = self.dbapi.create_exposed_service(values)
        self._from_db_object(self, db_exposed_service)

    @base.remotable
    def destroy(self, context=None):
        """Delete the ExposedService from the DB.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: ExposedService(context)
        """
        self.dbapi.destroy_exposed_service(self.id)
        self.obj_reset_changes()

    @base.remotable
    def save(self, context=None):
        """Save updates to this ExposedService.

        Column-wise updates will be made based on the result of
        self.what_changed(). If target_power_state is provided,
        it will be checked against the in-database copy of the
        exposed_service before updates are made.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: ExposedService(context)
        """
        updates = self.obj_get_changes()
        self.dbapi.update_exposed_service(self.id, updates)
        self.obj_reset_changes()
Ejemplo n.º 6
0
class Location(base.IotronicObject):
    VERSION = '1.0'

    dbapi = dbapi.get_instance()

    fields = {
        'id': int,
        'board_id': obj_utils.int_or_none,
        'longitude': obj_utils.str_or_none,
        'latitude': obj_utils.str_or_none,
        'altitude': obj_utils.str_or_none,
    }

    @staticmethod
    def _from_db_object(location, db_location):
        """Converts a database entity to a formal object."""
        for field in location.fields:
            location[field] = db_location[field]

        location.obj_reset_changes()
        return location

    @staticmethod
    def _from_db_object_list(db_objects, cls, context):
        """Converts a list of database entities to a list of formal objects."""
        return [
            Location._from_db_object(
                cls(context),
                obj) for obj in db_objects]

    @base.remotable_classmethod
    def get_by_id(cls, context, location_id):
        """Find a location based on its idand return a Location object.

        :param location_id: the id of a location.
        :returns: a :class:`Location` object.
        """
        db_location = cls.dbapi.get_location_by_id(location_id)
        location = Location._from_db_object(cls(context), db_location)
        return location

    def get_geo(self):

        updated = self._attr_to_primitive('updated_at')
        created = self._attr_to_primitive('created_at')

        geo = {
            'longitude': self.longitude,
            'latitude': self.latitude,
            'altitude': self.altitude,
        }
        if updated is None:
            geo['updated_at'] = created
        else:
            geo['updated_at'] = updated

        return geo

    # @base.remotable_classmethod
    # def get(cls, context, location_id):
    #     """Find a location based on its id or uuid and return
    #        a Location object.
    #
    #     :param location_id: the id *or* uuid of a location.
    #     :returns: a :class:`Location` object.
    #     """
    #     if strutils.is_int_like(location_id):
    #         return cls.get_by_id(context, location_id)
    #     elif uuidutils.is_uuid_like(location_id):
    #         return cls.get_by_uuid(context, location_id)
    #     else:
    #         raise exception.InvalidIdentity(identity=location_id)

    # @base.remotable_classmethod
    # def get_by_uuid(cls, context, uuid):
    #     """Find a location based on uuid and return a
    #        :class:`Location` object.
    #
    #     :param uuid: the uuid of a location.
    #     :param context: Security context
    #     :returns: a :class:`Location` object.
    #     """
    #     db_location = cls.dbapi.get_location_by_uuid(uuid)
    #     location = Location._from_db_object(cls(context), db_location)
    #     return location

    # @base.remotable_classmethod
    # def list(cls, context, limit=None, marker=None,
    #          sort_key=None, sort_dir=None):
    #     """Return a list of Location objects.
    #
    #     :param context: Security context.
    #     :param limit: maximum number of resources to return
    #                   in a single result.
    #     :param marker: pagination marker for large data sets.
    #     :param sort_key: column to sort results by.
    #     :param sort_dir: direction to sort. "asc" or "desc".
    #     :returns: a list of :class:`Location` object.
    #
    #     """
    #     db_locations = cls.dbapi.get_location_list(limit=limit,
    #                                                marker=marker,
    #                                                sort_key=sort_key,
    #                                                sort_dir=sort_dir)
    #     return Location._from_db_object_list(db_locations, cls, context)

    @base.remotable_classmethod
    def list_by_board_uuid(cls, context, board_uuid, limit=None, marker=None,
                           sort_key=None, sort_dir=None):
        """Return a list of Location objects associated with a given board ID.

        :param context: Security context.
        :param board_id: the ID of the board.
        :param limit: maximum number of resources to return in a single result.
        :param marker: pagination marker for large data sets.
        :param sort_key: column to sort results by.
        :param sort_dir: direction to sort. "asc" or "desc".
        :returns: a list of :class:`Location` object.

        """
        board_id = cls.dbapi.get_board_id_by_uuid(board_uuid)[0]
        db_loc = cls.dbapi.get_locations_by_board_id(board_id,
                                                     limit=limit,
                                                     marker=marker,
                                                     sort_key=sort_key,
                                                     sort_dir=sort_dir)
        return Location._from_db_object_list(db_loc, cls, context)

    @base.remotable_classmethod
    def list_by_board_id(cls, context, board_id, limit=None, marker=None,
                         sort_key=None, sort_dir=None):
        """Return a list of Location objects associated with a given board ID.

        :param context: Security context.
        :param board_id: the ID of the board.
        :param limit: maximum number of resources to return in a single result.
        :param marker: pagination marker for large data sets.
        :param sort_key: column to sort results by.
        :param sort_dir: direction to sort. "asc" or "desc".
        :returns: a list of :class:`Location` object.

        """
        db_loc = cls.dbapi.get_locations_by_board_id(board_id,
                                                     limit=limit,
                                                     marker=marker,
                                                     sort_key=sort_key,
                                                     sort_dir=sort_dir)
        return Location._from_db_object_list(db_loc, cls, context)

    @base.remotable
    def create(self, context=None):
        """Create a Location record in the DB.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Location(context)

        """
        values = self.obj_get_changes()
        db_location = self.dbapi.create_location(values)
        self._from_db_object(self, db_location)

    @base.remotable
    def destroy(self, context=None):
        """Delete the Location from the DB.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Location(context)
        """
        self.dbapi.destroy_location(self.uuid)
        self.obj_reset_changes()

    @base.remotable
    def save(self, context=None):
        """Save updates to this Location.

        Updates will be made column by column based on the result
        of self.what_changed().

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Location(context)
        """
        updates = self.obj_get_changes()
        self.dbapi.update_location(self.uuid, updates)

        self.obj_reset_changes()
Ejemplo n.º 7
0
class Board(base.IotronicObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    dbapi = db_api.get_instance()

    fields = {
        'id': int,
        'uuid': obj_utils.str_or_none,
        'code': obj_utils.str_or_none,
        'status': obj_utils.str_or_none,
        'name': obj_utils.str_or_none,
        'type': obj_utils.str_or_none,
        'agent': obj_utils.str_or_none,
        'owner': obj_utils.str_or_none,
        'project': obj_utils.str_or_none,
        'fleet': obj_utils.str_or_none,
        'mobile': bool,
        'config': obj_utils.dict_or_none,
        'extra': obj_utils.dict_or_none,
    }

    def check_if_online(self):
        if self.status != states.ONLINE:
            raise exception.BoardNotConnected(board=self.uuid)

    def is_online(self):
        if self.status == states.ONLINE:
            return True
        return False

    @staticmethod
    def _from_db_object(board, db_board):
        """Converts a database entity to a formal object."""
        for field in board.fields:
            board[field] = db_board[field]
        board.obj_reset_changes()
        return board

    @base.remotable_classmethod
    def get(cls, context, board_id):
        """Find a board based on its id or uuid and return a Board object.

        :param board_id: the id *or* uuid of a board.
        :returns: a :class:`Board` object.
        """
        if strutils.is_int_like(board_id):
            return cls.get_by_id(context, board_id)
        elif uuidutils.is_uuid_like(board_id):
            return cls.get_by_uuid(context, board_id)
        else:
            raise exception.InvalidIdentity(identity=board_id)

    @base.remotable_classmethod
    def get_by_id(cls, context, board_id):
        """Find a board based on its integer id and return a Board object.

        :param board_id: the id of a board.
        :returns: a :class:`Board` object.
        """
        db_board = cls.dbapi.get_board_by_id(board_id)
        board = Board._from_db_object(cls(context), db_board)
        return board

    @base.remotable_classmethod
    def get_by_uuid(cls, context, uuid):
        """Find a board based on uuid and return a Board object.

        :param uuid: the uuid of a board.
        :returns: a :class:`Board` object.
        """
        db_board = cls.dbapi.get_board_by_uuid(uuid)
        board = Board._from_db_object(cls(context), db_board)
        return board

    @base.remotable_classmethod
    def get_by_code(cls, context, code):
        """Find a board based on name and return a Board object.

        :param name: the logical name of a board.
        :returns: a :class:`Board` object.
        """
        db_board = cls.dbapi.get_board_by_code(code)
        board = Board._from_db_object(cls(context), db_board)
        return board

    @base.remotable_classmethod
    def get_by_name(cls, context, name):
        """Find a board based on name and return a Board object.

        :param name: the logical name of a board.
        :returns: a :class:`Board` object.
        """
        db_board = cls.dbapi.get_board_by_name(name)
        board = Board._from_db_object(cls(context), db_board)
        return board

    @base.remotable_classmethod
    def list(cls,
             context,
             limit=None,
             marker=None,
             sort_key=None,
             sort_dir=None,
             filters=None):
        """Return a list of Board objects.

        :param context: Security context.
        :param limit: maximum number of resources to return in a single result.
        :param marker: pagination marker for large data sets.
        :param sort_key: column to sort results by.
        :param sort_dir: direction to sort. "asc" or "desc".
        :param filters: Filters to apply.
        :returns: a list of :class:`Board` object.

        """
        db_boards = cls.dbapi.get_board_list(filters=filters,
                                             limit=limit,
                                             marker=marker,
                                             sort_key=sort_key,
                                             sort_dir=sort_dir)
        return [Board._from_db_object(cls(context), obj) for obj in db_boards]

    @base.remotable_classmethod
    def reserve(cls, context, tag, board_id):
        """Get and reserve a board.

        To prevent other ManagerServices from manipulating the given
        Board while a Task is performed, mark it reserved by this host.

        :param context: Security context.
        :param tag: A string uniquely identifying the reservation holder.
        :param board_id: A board id or uuid.
        :raises: BoardNotFound if the board is not found.
        :returns: a :class:`Board` object.

        """
        db_board = cls.dbapi.reserve_board(tag, board_id)
        board = Board._from_db_object(cls(context), db_board)
        return board

    @base.remotable_classmethod
    def release(cls, context, tag, board_id):
        """Release the reservation on a board.

        :param context: Security context.
        :param tag: A string uniquely identifying the reservation holder.
        :param board_id: A board id or uuid.
        :raises: BoardNotFound if the board is not found.

        """
        cls.dbapi.release_board(tag, board_id)

    @base.remotable
    def create(self, context=None):
        """Create a Board record in the DB.

        Column-wise updates will be made based on the result of
        self.what_changed(). If target_power_state is provided,
        it will be checked against the in-database copy of the
        board before updates are made.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Board(context)

        """
        values = self.obj_get_changes()
        db_board = self.dbapi.create_board(values)
        self._from_db_object(self, db_board)

    @base.remotable
    def destroy(self, context=None):
        """Delete the Board from the DB.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Board(context)
        """
        self.dbapi.destroy_board(self.uuid)
        self.obj_reset_changes()

    @base.remotable
    def save(self, context=None):
        """Save updates to this Board.

        Column-wise updates will be made based on the result of
        self.what_changed(). If target_power_state is provided,
        it will be checked against the in-database copy of the
        board before updates are made.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Board(context)
        """
        updates = self.obj_get_changes()
        self.dbapi.update_board(self.uuid, updates)
        self.obj_reset_changes()

    @base.remotable
    def refresh(self, context=None):
        """Refresh the object by re-fetching from the DB.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: Board(context)
        """
        current = self.__class__.get_by_uuid(self._context, self.uuid)
        for field in self.fields:
            if (hasattr(self, base.get_attrname(field))
                    and self[field] != current[field]):
                self[field] = current[field]
Ejemplo n.º 8
0
Run storage database migration.
"""

from __future__ import print_function

import sys

from iotronic.common import context
from iotronic.common.i18n import _
from iotronic.common import service
from iotronic.db import api as db_api
from iotronic.db import migration
from oslo_config import cfg

CONF = cfg.CONF
dbapi = db_api.get_instance()

# NOTE(rloo): This is a list of functions to perform online data migrations
# (from previous releases) for this release, in batches. It may be empty.
# The migration functions should be ordered by execution order; from earlier
# to later releases.
#
# Each migration function takes two arguments -- the context and maximum
# number of objects to migrate, and returns a 2-tuple -- the total number of
# objects that need to be migrated at the beginning of the function, and the
# number migrated. If the function determines that no migrations are needed,
# it returns (0, 0).
#
# Example of a function docstring:
#
#        def sample_data_migration(context, max_count):
Ejemplo n.º 9
0
 def before(self, state):
     state.request.dbapi = dbapi.get_instance()
Ejemplo n.º 10
0
class WampAgent(base.IotronicObject):
    dbapi = db_api.get_instance()

    fields = {
        'id': int,
        'hostname': str,
        'wsurl': str,
        'online': bool,
        'ragent': bool,
    }

    @staticmethod
    def _from_db_object(wampagent, db_obj):
        """Converts a database entity to a formal object."""
        for field in wampagent.fields:
            wampagent[field] = db_obj[field]

        wampagent.obj_reset_changes()
        return wampagent

    @base.remotable_classmethod
    def get_by_hostname(cls, context, hostname):
        """Get a WampAgent record by its hostname.

        :param hostname: the hostname on which a WampAgent is running
        :returns: a :class:`WampAgent` object.
        """
        db_obj = cls.dbapi.get_wampagent(hostname)
        wampagent = WampAgent._from_db_object(cls(context), db_obj)
        return wampagent

    @base.remotable_classmethod
    def get_registration_agent(cls, context=None):
        """Get a Registration WampAgent

        :param hostname: the hostname on which a WampAgent is running
        :returns: a :class:`WampAgent` object.
        """
        db_obj = cls.dbapi.get_registration_wampagent()
        wampagent = WampAgent._from_db_object(cls(context), db_obj)
        return wampagent

    def save(self, context):
        """Save is not supported by WampAgent objects."""
        raise NotImplementedError(
            _('Cannot update a wampagent record directly.'))

    @base.remotable
    def refresh(self, context=None):
        """Loads and applies updates for this WampAgent.

        Loads a :class:`WampAgent` with the same uuid from the database and
        checks for updated attributes.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: WampAgent(context)
        """
        current = self.__class__.get_by_hostname(self._context,
                                                 hostname=self.hostname)
        for field in self.fields:
            if (hasattr(
                    self, base.get_attrname(field)) and
                    self[field] != current[field]):
                self[field] = current[field]

    @base.remotable
    def touch(self, context):
        """Touch this wampagent's DB record, marking it as up-to-date."""
        self.dbapi.touch_wampagent(self.hostname)

    @base.remotable_classmethod
    def list(cls, context, limit=None, marker=None, sort_key=None,
             sort_dir=None, filters=None):
        """Return a list of WampAgent objects.

        :param context: Security context.
        :param limit: maximum number of resources to return in a single result.
        :param marker: pagination marker for large data sets.
        :param sort_key: column to sort results by.
        :param sort_dir: direction to sort. "asc" or "desc".
        :param filters: Filters to apply.
        :returns: a list of :class:`WampAgent` object.

        """
        db_wampagents = cls.dbapi.get_wampagent_list(filters=filters,
                                                     limit=limit,
                                                     marker=marker,
                                                     sort_key=sort_key,
                                                     sort_dir=sort_dir)
        return [WampAgent._from_db_object(cls(context),
                                          obj) for obj in db_wampagents]