def create(self, req, agent_id, endpoint, entity, body): body = body or {} ports = argutils.map_with(body.get('ports'), validators['type:port']) glock = get_global().lock('agents') with glock([ agent_id, ]): self.unsafe_create(agent_id, endpoint, entity, ports) return resultutils.results(result='edit ports success')
def _reflect_database(self, session, **kwargs): """impl reflect code""" req = kwargs.pop('req') entitys = kwargs.get('entitys', None) if entitys: entitys = argutils.map_with(entitys, str) _filter = GopDatabase.reflection_id.in_(entitys) else: _filter = None yield 'entity', _filter
def send_file_to_agents(self, agent_id, file_id, body): resp, results = self.put( action=self.file_ext_path % (','.join(argutils.map_with(agent_id, str)), file_id), body=body) if results['resultcode'] != common.RESULT_SUCCESS: raise ServerExecuteRequestError(message='agent send file fail:%d' % results['resultcode'], code=resp.status_code, resone=results['result']) return results
def ports_delete(self, agent_id, endpoint, entity, ports, body=None): resp, results = self.delete(action=self.port_path % (str(agent_id), endpoint, str(entity), ','.join(argutils.map_with(ports, str))), body=body) if results['resultcode'] != common.RESULT_SUCCESS: raise ServerExecuteRequestError(message='add entitys fail:%d' % results['resultcode'], code=resp.status_code, resone=results['result']) return results
def entitys_show(self, endpoint, entitys, body=None): resp, results = self.get( action=self.entity_path % (endpoint, ','.join(argutils.map_with(entitys, str))), body=body) if results['resultcode'] != common.RESULT_SUCCESS: raise ServerExecuteRequestError(message='add entitys fail:%d' % results['resultcode'], code=resp.status_code, resone=results['result']) return results
def count(self, req, endpoint): session = get_session(readonly=True) data = [] for endpoint in argutils.map_with(endpoint, validateutils.validate_endpoint): count = model_count_with_key( session, AgentEndpoint.endpoint, filter=AgentEndpoint.endpoint == endpoint) data.append(dict(endpoint=endpoint, count=count)) return resultutils.results(result='count endpoint for success', data=data)
def _lock_agents(self, agents): agents = argutils.map_to_int(agents) overtime = self.alloctime + int(time.time()*1000) client = self.client agents_ids = argutils.map_with(agents, str) while True: wpipe = None try: if (agents - self.all_agents): raise exceptions.TargetCountUnequal('Target agents not in all agents id list') locked = True while int(time.time()*1000) < overtime: try: for _id in agents_ids: if client.sismember(self.AGENT_KEY, _id): eventlet.sleep(0.01) continue locked = False except ResponseError as e: if not e.message.startswith('WRONGTYPE'): raise if locked: raise exceptions.AllocLockTimeout('Lock agents timeout') wpipe = client.pipeline() wpipe.watch(self.AGENT_KEY) wpipe.multi() wpipe.sadd(self.AGENT_KEY, *agents_ids) wpipe.execute() break except WatchError: if int(time.time()*1000) > overtime: raise exceptions.AllocLockTimeout('Lock agents timeout') finally: if wpipe: wpipe.reset() try: yield finally: if agents_ids: self.garbage_member_collection(self.AGENT_KEY, agents_ids)
def delete(self, req, agent_id, endpoint, entity, ports, body=None): body = body or {} ports = argutils.map_with(ports, validators['type:port']) strict = body.get('strict', True) if not ports: raise InvalidArgument('Ports is None for delete ports') for port in ports: if not isinstance(port, (int, long)): raise InvalidArgument( 'Port in ports not int, can not edit ports') if not (0 <= port <= 65535): raise InvalidArgument( 'Port in ports over range, can not edit ports') session = get_session() glock = get_global().lock('agents') with glock([ agent_id, ]): with session.begin(subtransactions=True): query = model_query(session, AllocatedPort, filter=and_( AllocatedPort.agent_id == agent_id, AllocatedPort.endpoint == endpoint, AllocatedPort.entity == entity, AllocatedPort.port.in_(ports))) delete_count = query.delete() need_to_delete = len(ports) if delete_count != len(ports): LOG.warning('Delete %d ports, but expect count is %d' % (delete_count, need_to_delete)) if strict: raise InvalidArgument( 'Submit %d ports, but only %d ports found' % (len(ports), need_to_delete)) return resultutils.results(result='edit ports success')
def select_database(self, **kwargs): dbtype = kwargs.pop('dbtype', 'mysql') filters = [ GopDatabase.status == common.OK, GopDatabase.dbtype == dbtype ] affinitys = kwargs.get('affinitys') if affinitys: affinitys = argutils.map_with(affinitys, int) ors = [] # 通过位运算匹配亲和性 for affinity in affinitys: ors.append(GopDatabase.affinity.op('&')(affinity) > 0) if len(ors) > 1: filters.append(or_(*ors)) else: filters.append(ors[0]) session = endpoint_session(readonly=True) query = model_query(session, GopDatabase, filter=and_(*filters)) query = query.options(joinedload(GopDatabase.schemas, innerjoin=False)) results = self._select_database(session, query, dbtype, **kwargs) # 结果按照亲和性从小到大排序 # 亲和性数值越大,匹配范围越广 results.sort(key=lambda x: x['affinity']) return results
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) ])
def _reflect_database(self, session, **kwargs): """impl reflect code""" records = kwargs.get('records') records = argutils.map_with(records, str) filter = GopDatabase.reflection_id.in_(records) yield 'record_id', filter