示例#1
0
文件: node.py 项目: gunturur/suzieq
    async def exec_service(self, service_callback, svc_defn: dict,
                           cb_token: RsltToken):

        result = []  # same type as gather function
        cmd = None
        if not svc_defn:
            return result

        if self._status == "init":
            if self.init_again_at < time.time():
                await self.init_node()

        if self._status == "init":
            result.append(self._create_error(svc_defn.get("service", "-")))
            return await service_callback(result, cb_token)

        # Update our boot time value into the callback token
        if cb_token:
            cb_token.bootupTimestamp = self.bootupTimestamp

        self.svcs_proc.add(svc_defn.get("service"))
        use = svc_defn.get(self.hostname, None)
        if not use:
            use = svc_defn.get(self.devtype, {})
        if not use:
            if svc_defn.get("service") not in self.error_svcs_proc:
                result.append(
                    self._create_result(svc_defn, HTTPStatus.NOT_FOUND,
                                        "No service definition"))
                self.error_svcs_proc.add(svc_defn.get("service"))
            return await service_callback(result, cb_token)

        # TODO This kind of logic should be encoded in config and node shouldn't have to know about it
        if "copy" in use:
            use = svc_defn.get(use.get("copy"))

        if use:
            if isinstance(use, list):
                # There's more than one version here, we have to pick ours
                for item in use:
                    if (item["version"] == "all"
                            or item["version"] == self.version):
                        cmd = item.get("command", None)
                        break
            else:
                cmd = use.get("command", None)

        oformat = svc_defn.get(self.devtype, {}).get("format", "json")

        if not cmd:
            return result

        if type(cmd) is not list:
            cmd = [cmd]

        await self.exec_cmd(service_callback,
                            cmd,
                            cb_token,
                            oformat=oformat,
                            timeout=cb_token.timeout)
示例#2
0
 def post_commands(self, service_callback, svc_defn: dict,
                   cb_token: RsltToken):
     if cb_token:
         cb_token.nodeQsize = self._service_queue.qsize()
     self._service_queue.put_nowait([service_callback, svc_defn, cb_token])