def getProjects(self, delegate, **kwargs): if tools.supports(delegate, "handleProjects") == False: raise Exception( "the delegate must implement the handleProjects function") url = self.url opts = tools.Options(**kwargs) if opts.hasOpt("namespace"): url += "/namespaces/" + opts.getOpt("namespace") url += "/espservers" if opts.hasOpt("name"): url += "/" + opts.getOpt("name") response = requests.get(url) if response.status_code == 404: if tools.supports(delegate, "notFound"): delegate.notFound() else: raise Exception("error: not found") return data = response.json() if "code" in data and data["code"] == 404: s = "" if self._ns != None: s += self._ns + "/" s += self._project if tools.supports(delegate, "notFound"): delegate.notFound(s) else: raise Exception("project not found: " + s) else: kind = data["kind"] a = None if kind == "ESPServer": a = [data] elif kind == "ESPServerList": a = data["items"] delegate.handleProjects(a)
def handlePod(self, pod): url = self._k8s.baseUrl url += "api/v1" url += "/namespaces/" + self._k8s.namespace url += "/pods/" + pod["metadata"]["name"] url += "/log" response = requests.get(url) if (response.status_code == 200): log = [] o = None for line in response.text.split("\n"): if len(line) > 0: try: o = json.loads(line) except: o = {} o["message"] = line log.append(o) delegate.handleLog(log) else: if tools.supports(delegate, "error"): delegate.error(request, error) else: raise Exception("error: " + error)
def addDelegate(self, delegate): if tools.supports(delegate, "handleLog") == False: raise Exception("the delegate must implement the handleLog method") if tools.addTo(self._delegates, delegate): if len(self._delegates) == 1: self.start()
def message(self, message): if (self.isHandshakeComplete): return name = "" value = None for i in range(0, len(message)): c = message[i] if (c == '\n'): if (len(name) == 0): break if (self._headers == None): self._headers = {} if (value != None): self._headers[name] = value.strip() else: self._headers[name] = "" name = "" value = None elif (value != None): value += c elif (c == ':'): value = "" else: name += c status = self.getHeader("status") if (status != None): value = int(status) if (value == 200): self._handshakeComplete = True self.handshakeComplete() if (tools.supports(self._delegate, "connected")): self._delegate.connected(self) elif (value == 401): if (self._authorization != None): self._websocket.send(self._authorization) elif (tools.supports(self._delegate, "authenticate")): scheme = self.getHeader("www-authenticate") self._delegate.authenticate(self, scheme)
def closed(self): for c in self._datasources.values(): c.clear() if tools.supports(self._delegate, "closed"): self._delegate.closed(self) if self._autoReconnect: thread = threading.Thread(target=self.reconnect) thread.daemon = True thread.start()
def run(self): while self._running: if self._paused: while True: time.sleep(1) if self._paused == False: break current = datetime.datetime.now() eventsources = [] depsPending = False minInterval = 5000 completed = 0 for es in self._eventsources.values(): if es.repeat >= 0 and es.done == False: if es.sending == False: if es.checkDependencies(): diff = current.timestamp() - es.timestamp interval = es.interval if diff > interval: if interval < minInterval: minInterval = interval eventsources.append(es) else: diff = current.getTime( ) - es.timestamp + interval if diff < minInterval: minInterval = diff else: depsPending = True else: completed += 1 for es in eventsources: if es.repeat >= 0: es.process() if completed == len(self._eventsources.values()): self._running = False else: if depsPending: interval = 1 else: interval = minInterval / 1000 time.sleep(interval) if tools.supports(self._delegate, "complete"): threading.Timer(1, self._delegate.complete(self)).start()
def handshakeComplete(self): for c in self._datasources.values(): c.open() for p in self._publishers.values(): p.open() if len(self._log._delegates) > 0: self._log.start() if len(self._stats._delegates) > 0: self._stats.set() if tools.supports(self._delegate, "connected"): self._delegate.connected(self)
def loadModel(self, delegate): if tools.supports(delegate, "modelLoaded") == False: raise Exception( "The stats delegate must implement the modelLoaded method") id = tools.guid() self._modelDelegates[id] = ModelDelegate(self, delegate) o = {} o["request"] = "model" o["id"] = id o["schema"] = True o["index"] = True o["xml"] = True self.send(o)
def getNamespaces(self,delegate): if tools.supports(delegate,"handleNamespaces") == False: raise Exception("the delegate must implement the handleNamespaces function") url = self.baseUrl url += "api/v1/namespaces" response = requests.get(url) a = [] if response.status_code == 200: data = response.json() a = data["items"] delegate.handleNamespaces(a)
def getLog(self, delegate): if tools.supports(delegate, "handleLog") == False: raise Exception( "the delegate must implement the handleLog function") if self.namespace == None or self.project == None: raise Exception( "the instance requires both namespace and project name to get the pod" ) class Tmp(object): def __init__(self, k8s): self._k8s = k8s def handlePod(self, pod): url = self._k8s.baseUrl url += "api/v1" url += "/namespaces/" + self._k8s.namespace url += "/pods/" + pod["metadata"]["name"] url += "/log" response = requests.get(url) if (response.status_code == 200): log = [] o = None for line in response.text.split("\n"): if len(line) > 0: try: o = json.loads(line) except: o = {} o["message"] = line log.append(o) delegate.handleLog(log) else: if tools.supports(delegate, "error"): delegate.error(request, error) else: raise Exception("error: " + error) self.getPod(Tmp(self))
def notFound(self): if tools.supports(delegate, "notFound"): delegate.notFound() else: raise Exception("error: not found")
def deliverInfoChange(self): for d in self._delegates: if tools.supports(d, "infoChanged"): d.infoChanged(self)
def addDelegate(self, delegate): if tools.supports(delegate, "dataChanged") == False: raise Exception( "the delegate must implement the dataChanged method") tools.addTo(self._delegates, delegate)
def setSchema(self, xml): self._schema.fromXml(xml) for d in self._delegates: if tools.supports(d, "schemaSet"): d.schemaSet(self)
def deliver(self, xml): model = Model(xml) if tools.supports(self._delegate, "modelLoaded"): self._delegate.modelLoaded(model, self._connection)