def _do_sse_request(self, path, params=None, data=None): from sseclient import SSEClient headers = {'Accept': 'text/event-stream'} if self.dcos == True: headers['Authorization'] = "token=%s" % (self._token()) self.auth = None messages = None servers = list(self.servers) while servers and messages is None: server = servers.pop(0) url = ''.join([server.rstrip('/'), path]) try: messages = SSEClient(url, params=params, data=data, headers=headers, auth=self.auth) except Exception as e: marathon.log.error('Error while calling %s: %s', url, e.message) if messages is None: raise MarathonError('No remaining Marathon servers to try') return messages
def process(self, event): event_type = event['eventType'] if event_type in self.event_to_class: clazz = self.event_to_class[event_type] return clazz.from_json(event) else: raise MarathonError('Unknown event_type: {}'.format(event_type))
def _do_request(self, method, path, params=None, data=None): """Query Marathon server.""" headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } if self.dcos is True: headers['Authorization'] = "token=%s" % (self._token()) self.auth = None response = None servers = list(self.servers) while servers and response is None: server = servers.pop(0) url = ''.join([server.rstrip('/'), path]) try: logging.info(data) logging.info(url) logging.info(headers) response = self.session.request(method, url, params=params, data=data, headers=headers, auth=self.auth, timeout=self.timeout) marathon.log.info('Got response from %s', server) except requests.exceptions.RequestException as e: marathon.log.error('Error while calling %s: %s', url, str(e)) if response is None: raise MarathonError('No remaining Marathon servers to try') if response.status_code >= 500: marathon.log.error('Got HTTP {code}: {body}'.format( code=response.status_code, body=response.text)) raise InternalServerError(response) elif response.status_code >= 400: marathon.log.error('Got HTTP {code}: {body}'.format( code=response.status_code, body=response.text)) if response.status_code == 404: raise NotFoundError(response) else: raise MarathonHttpError(response) elif response.status_code >= 300: marathon.log.warn('Got HTTP {code}: {body}'.format( code=response.status_code, body=response.text)) else: marathon.log.debug('Got HTTP {code}: {body}'.format( code=response.status_code, body=response.text)) return response
def _validate_schema(self, config_json, schema_name): if schema_name == None: return True if config_json == None: return False try: schema_file = 'resources/schema/%s.json' % schema_name with open(schema_file, "r") as schema_text: schema = json.load(schema_text) except IOError as err: raise MarathonError("%s: schema not found" % err.filename) validate(config_json, schema)
async def _stream_events(self, events_tx, event_types): ef = EventFactory() async with self._client() as client: headers = {"Accept": "text/event-stream"} params = {} if event_types: params["event_type"] = event_types async with client.stream( "GET", "/v2/events", headers=headers, params=params, timeout=60 ) as response: async for line in response.aiter_lines(): _data = line.split(":", 1) if _data[0] == "data": event_data = json.loads(_data[1].strip()) if "eventType" not in event_data: raise MarathonError("Invalid event data received.") event = ef.process(event_data) if event_types and event.event_type not in event_types: # We're filtering events, but got an unwanted one # anyway. Ignore it and move on. continue self.log.info(f"Received marathon event: {event}") await events_tx.send(event)