Beispiel #1
0
 def show(self, req, endpoint, entity, body=None):
     body = body or {}
     show_ports = body.get('ports', False)
     endpoint = validateutils.validate_endpoint(endpoint)
     entity = int(entity)
     session = get_session(readonly=True)
     query = model_query(session,
                         AgentEntity,
                         filter=and_(AgentEntity.endpoint == endpoint,
                                     AgentEntity.entity == entity))
     if show_ports:
         query = query.options(
             joinedload(AgentEntity.ports, innerjoin=False))
     _entity = query.one_or_none()
     if not _entity:
         raise InvalidArgument('no entity found for %s' % endpoint)
         # return resultutils.results(result='no entity found', resultcode=manager_common.RESULT_ERROR)
     return resultutils.results(
         result='show entity success',
         data=[
             dict(endpoint=_entity.endpoint,
                  agent_id=_entity.agent_id,
                  metadata=BaseContorller.agent_metadata(_entity.agent_id),
                  entity=_entity.entity,
                  ports=sorted([x.port for x in _entity.ports])
                  if show_ports else [])
         ])
Beispiel #2
0
 def _validate_endpoint(self, endpoints):
     remote_endpoints = set()
     for endpoint in six.iterkeys(endpoints):
         remote_endpoints.add(validate_endpoint(endpoint))
     local_endpoints = set([endpoint.namespace for endpoint in self.endpoints])
     add_endpoints = local_endpoints - remote_endpoints
     delete_endpoints = remote_endpoints - local_endpoints
     return add_endpoints, delete_endpoints
Beispiel #3
0
 def entitys(self, req, endpoint):
     session = get_session(readonly=True)
     endpoint = validateutils.validate_endpoint(endpoint)
     query = model_query(session,
                         AgentEntity,
                         filter=AgentEntity.endpoint == endpoint)
     query = query.options(joinedload(AgentEntity.ports, innerjoin=False))
     return resultutils.results(
         result='get endpoint %s entitys success' % endpoint,
         data=[
             dict(agent_id=entity.agent_id,
                  entity=entity.entity,
                  ports=[port.port for port in entity.ports])
             for entity in query
         ])
Beispiel #4
0
 def delete(self, req, endpoint, entity, body=None, action='delete'):
     body = body or {}
     notify = body.pop('notify', True)
     endpoint = validateutils.validate_endpoint(endpoint)
     entity = int(entity)
     session = get_session()
     glock = get_global().lock('entitys')
     result = 'delete entity success.'
     with glock(endpoint, [
             entity,
     ]) as agents:
         with session.begin():
             query = model_query(session,
                                 AgentEntity,
                                 filter=and_(
                                     AgentEntity.endpoint == endpoint,
                                     AgentEntity.entity == entity))
             if notify:
                 agent_id = agents.pop()
                 metadata = BaseContorller.agent_metadata(agent_id)
                 if not metadata:
                     raise InvalidArgument('Agent not online or not exist')
             _entity = query.one_or_none()
             if not _entity:
                 LOG.warning('Delete no entitys, but expect count 1')
             else:
                 query.delete()
             if notify:
                 target = targetutils.target_agent_by_string(
                     metadata.get('agent_type'), metadata.get('host'))
                 target.namespace = endpoint
                 delete_result = self.notify_delete(target, agent_id,
                                                    entity, body, action)
                 if not delete_result:
                     raise RpcResultError('delete entitys result is None')
                 if delete_result.get(
                         'resultcode') != manager_common.RESULT_SUCCESS:
                     raise RpcResultError('delete entity fail %s' %
                                          delete_result.get('result'))
                 result += delete_result.get('result')
                 notify = delete_result
     return resultutils.results(
         result=result,
         data=[dict(entity=entity, endpoint=endpoint, notify=notify)])
Beispiel #5
0
 def readlog(self, req, endpoint, entity, body=None):
     body = body or {}
     path = body.get('path')
     lines = body.get('lines', 10)
     if not path or '..' in path:
         raise InvalidArgument('Path value error')
     endpoint = validateutils.validate_endpoint(endpoint)
     entity = int(entity)
     session = get_session(readonly=True)
     query = model_query(session,
                         AgentEntity,
                         filter=and_(AgentEntity.endpoint == endpoint,
                                     AgentEntity.entity == entity))
     _entity = query.one_or_none()
     if not _entity:
         raise InvalidArgument('no entity found for %s' % endpoint)
     metadata = BaseContorller.agent_metadata(_entity.agent_id)
     if not metadata:
         raise InvalidArgument('Can not get log from off line agent')
     target = targetutils.target_agent_by_string(manager_common.APPLICATION,
                                                 metadata.get('host'))
     target.namespace = endpoint
     rpc = get_client()
     rpc_ret = rpc.call(target,
                        ctxt={'finishtime': rpcfinishtime()[0]},
                        msg={
                            'method': 'readlog',
                            'args': {
                                'entity': entity,
                                'path': path,
                                'lines': lines
                            }
                        })
     if not rpc_ret:
         raise RpcResultError('Get %s.%d log rpc result is None' %
                              (endpoint, entity))
     if rpc_ret.get('resultcode') != manager_common.RESULT_SUCCESS:
         raise InvalidArgument(('Get %s.%d log agent rpc result: ' %
                                (endpoint, entity)) + rpc_ret.get('result'))
     return resultutils.results(result=rpc_ret.get('result'),
                                data=[rpc_ret.get('uri')])
Beispiel #6
0
 def index(self, req, agent_id, endpoint, body=None):
     body = body or {}
     show_ports = body.get('ports')
     endpoint = validateutils.validate_endpoint(endpoint)
     session = get_session(readonly=True)
     query = model_query(session,
                         AgentEntity,
                         filter=and_(AgentEntity.endpoint == endpoint,
                                     AgentEntity.agent_id == agent_id))
     if show_ports:
         query = query.options(
             joinedload(AgentEntity.ports, innerjoin=False))
     entitys = query.all()
     return resultutils.results(
         result='show endpoint entitys success',
         data=[
             dict(entity=entity.entity,
                  ports=[port.port
                         for port in entity.ports] if show_ports else [])
             for entity in entitys
         ])
Beispiel #7
0
    def shows(endpoint, entitys=None, agents=None, ports=True, metadata=True):
        entitys_map = {}
        endpoint = validateutils.validate_endpoint(endpoint)
        session = get_session(readonly=True)
        filters = [AgentEntity.endpoint == endpoint]
        if not entitys and not agents:
            return entitys_map
        if entitys:
            entitys = argutils.map_to_int(entitys)
            filters.append(AgentEntity.entity.in_(entitys))
        if agents:
            agents = argutils.map_to_int(agents)
            filters.append(AgentEntity.agent_id.in_(agents))
        if len(filters) > 1:
            filter = and_(*filters)
        else:
            filter = filters[0]
        query = model_query(session, AgentEntity, filter=filter)
        if ports:
            query = query.options(
                joinedload(AgentEntity.ports, innerjoin=False))
        agents = set()
        for _entity in query:
            agents.add(_entity.agent_id)
            entitys_map[_entity.entity] = dict(agent_id=_entity.agent_id)
            if ports:
                entitys_map[_entity.entity].setdefault(
                    'ports', sorted([x.port for x in _entity.ports]))
        if metadata:
            agents_map = BaseContorller.agents_metadata(agents)

        for _entity in entitys_map:
            agent_id = entitys_map[_entity].get('agent_id')
            if metadata:
                entitys_map[_entity].setdefault('metadata',
                                                agents_map[agent_id])

        return entitys_map
Beispiel #8
0
 def post_create_entity(self, entity, endpoint, **kwargs):
     entity = int(entity)
     endpoint = validateutils.validate_endpoint(endpoint)
     session = get_session(readonly=True)
     query = model_query(session,
                         AgentEntity,
                         filter=and_(AgentEntity.endpoint == endpoint,
                                     AgentEntity.entity == entity))
     _entity = query.one()
     metadata = BaseContorller.agent_metadata(_entity.agent_id)
     if not metadata:
         raise RpcPrepareError('Agent not online, can not sen post create')
     target = targetutils.target_agent_by_string(
         metadata.get('agent_type'),
         metadata.get('host'),
     )
     target.namespace = endpoint
     body = dict(entity=entity)
     body.update(kwargs)
     rpc = get_client()
     rpc.cast(target,
              ctxt={
                  'finishtime': body.pop('finishtime',
                                         rpcfinishtime()[0]),
                  'entitys': [
                      entity,
                  ]
              },
              msg={
                  'method': 'post_create_entity',
                  'args': body
              })
     return resultutils.results(result='notify post create success',
                                data=[
                                    dict(entity=entity,
                                         agent_id=_entity.agent_id,
                                         endpoint=endpoint)
                                ])
Beispiel #9
0
 def agents(self, req, endpoint):
     session = get_session(readonly=True)
     endpoint = validateutils.validate_endpoint(endpoint)
     query = model_query(session,
                         AgentEndpoint,
                         filter=AgentEndpoint.endpoint == endpoint)
     agents = set()
     for endpoint in query.all():
         agents.add(endpoint.agent_id)
     if not agents:
         raise InvalidArgument('No agent found for %s' % endpoint)
     query = model_query(session,
                         Agent,
                         filter=and_(Agent.status > manager_common.DELETED,
                                     Agent.agent_id.in_(agents)))
     return resultutils.results(
         result='get agent for %s success' % endpoint,
         data=[
             dict(agent_id=agent.agent_id,
                  agent_host=agent.host,
                  cpu=agent.cpu,
                  memory=agent.memory) for agent in query.all()
         ])
Beispiel #10
0
    def create(self, req, agent_id, endpoint, body=None, action='create'):
        body = body or {}
        endpoint = validateutils.validate_endpoint(endpoint)
        ports = body.pop('ports', None)
        notify = body.pop('notify', True)
        desc = body.pop('desc', None)
        session = get_session()
        metadata = None
        if ports:
            ports = argutils.map_with(ports, validators['type:port'])
            used_ports = model_count_with_key(
                session,
                AllocatedPort.port,
                filter=and_(AllocatedPort.agent_id == agent_id,
                            AllocatedPort.port.in_(ports)))
            if used_ports:
                raise InvalidArgument('Ports has been used count %d' %
                                      used_ports)

        if notify:
            metadata = BaseContorller.agent_metadata(agent_id)
            # make sure agent is online
            if not metadata:
                raise RpcPrepareError(
                    'Can not create entity on a offline agent %d' % agent_id)

        entity = 0
        glock = get_global().lock('agents')
        elock = get_global().lock('endpoint')
        result = 'add entity success.'
        with glock([
                agent_id,
        ]):
            with elock(endpoint):
                with session.begin(subtransactions=True):
                    query = model_query(session,
                                        Agent,
                                        filter=Agent.agent_id == agent_id)
                    query = query.options(
                        joinedload(Agent.endpoints, innerjoin=False))
                    agent = query.one()
                    if agent.status != manager_common.ACTIVE:
                        raise InvalidArgument(
                            'Create entity fail, agent status is not active')

                    _endpoint = None
                    for e in agent.endpoints:
                        if endpoint == e.endpoint:
                            _endpoint = e
                            break
                    if not _endpoint:
                        raise InvalidArgument(
                            'Create entity fail, agent %d has no endpoint %s' %
                            (agent_id, endpoint))
                    entity_autoincrement_id = model_autoincrement_id(
                        session,
                        AgentEntity.entity,
                        filter=AgentEntity.endpoint == endpoint)
                    entity = body.get('entity') or entity_autoincrement_id
                    if entity < entity_autoincrement_id:
                        raise InvalidArgument(
                            'Create entity fail, entity less then autoincrement id'
                        )
                    _entity = AgentEntity(entity=entity,
                                          endpoint=endpoint,
                                          agent_id=agent_id,
                                          endpoint_id=_endpoint.id,
                                          desc=desc)
                    session.add(_entity)
                    session.flush()
                    LOG.info('Create entity %s.%d with entity id %s' %
                             (endpoint, entity, _entity.id))
                    if ports:
                        for port in ports:
                            session.add(
                                AllocatedPort(port=port,
                                              agent_id=agent_id,
                                              endpoint_id=_endpoint.id,
                                              entity_id=entity.id,
                                              endpoint=endpoint,
                                              entity=entity))
                            session.flush()
                    if notify:
                        target = targetutils.target_agent(agent)
                        target.namespace = endpoint
                        create_result = self.notify_create(
                            target, agent.agent_id, entity, body, action)
                        if not create_result:
                            raise RpcResultError(
                                'create entitys result is None')
                        if create_result.get(
                                'resultcode') != manager_common.RESULT_SUCCESS:
                            raise RpcResultError('create entity fail %s' %
                                                 create_result.get('result'))
                        result += create_result.get('result')
                        notify = create_result
        return resultutils.results(result=result,
                                   data=[
                                       dict(entity=entity,
                                            agent_id=agent_id,
                                            metadata=metadata,
                                            endpoint=endpoint,
                                            ports=ports or [],
                                            notify=notify)
                                   ])