Exemple #1
0
    async def run_shutdown_command(self, root, info, **kwargs):
        application_log.debug(f"run_shutdown_command {kwargs}")
        cmd = "sudo shutdown -a now"

        if self.shutdown_proc:
            # already running?
            if self.shutdown_proc.complete:
                self.shutdown_proc = None
        if not self.shutdown_proc:
            # try to run the command
            self.shutdown_proc = ProcessRunner(
                cmd,
                started_callback=self.process_callback,
                output_callback=self.process_callback,
                complete_callback=self.process_callback,
            )
            self.shutdown_proc.start()
        return self.shutdown_command
Exemple #2
0
    async def run_configure_command(self, root, info, **kwargs):
        application_log.debug(f"run_configure_command {kwargs}")

        self.configure_command["dryrun"] = kwargs.get("dryrun", False)
        self.configure_command["debug"] = kwargs.get("debug", False)
        self.configure_command["profile"] = kwargs.get("profile", False)
        self.configure_command["module"] = kwargs.get("module", None)
        self.configure_command["environment"] = kwargs.get("environment", None)
        self.configure_command["terminate"] = kwargs.get("terminate", False)

        cmd = "maverick configure"
        if self.configure_command["environment"] is not None:
            cmd += f' --env={self.configure_command["environment"]}'
        if self.configure_command["dryrun"]:
            cmd += " --dryrun"
        if self.configure_command["debug"]:
            cmd += " --debug"
        if self.configure_command["profile"]:
            cmd += " --profile"
        if self.configure_command["module"] is not None:
            cmd += f' --module={self.configure_command["module"]}'
        application_log.info(f"Running configure command: {cmd}")

        if self.configure_command["terminate"]:
            # try to terminate a running command
            if self.configure_proc:
                self.configure_proc.terminate()
        if self.configure_proc:
            # already running?
            if self.configure_proc.complete:
                self.configure_proc = None
        if not self.configure_proc:
            # try to run the command
            self.configure_proc = ProcessRunner(
                cmd,
                started_callback=self.process_callback,
                output_callback=self.process_callback,
                complete_callback=self.process_callback,
            )
            self.configure_proc.start()
        return self.configure_command
    async def run_shell_command(self, root, info, **kwargs):
        application_log.debug(f"run_shell_command {kwargs}")

        self.shell_command["command"] = kwargs.get("command", "")
        self.shell_command["terminate"] = kwargs.get("terminate", False)
        self.shell_command["width"] = kwargs.get("width", 50)
        self.shell_command["height"] = kwargs.get("height", 50)
        cmd = self.shell_command["command"]
        application_log.info(f"Running shell command: {cmd}")

        if self.shell_command["terminate"]:
            # try to terminate a running command
            if self.shell_proc:
                self.shell_proc.terminate()
        if self.shell_proc:
            # already running?
            if self.shell_proc.complete:
                self.shell_proc = None
            else:
                application_log.debug(
                    "Process is still running. Providing input")
                special_cmd = kwargs.get("special", False)
                if special_cmd:
                    cmd += special_cmd
                else:
                    cmd += "\n"
                os.write(self.shell_proc.pty_master, cmd.encode())
        if not self.shell_proc:
            # try to run the command
            self.shell_proc = ProcessRunner(
                cmd,
                width=self.shell_command["width"],
                height=self.shell_command["height"],
                started_callback=self.start_process_callback,
                output_callback=self.output_process_callback,
                complete_callback=self.complete_process_callback,
            )
            self.shell_proc.start()
        return self.shell_command
 async def run_command(self, cmd):
     if cmd:
         try:
             ret = await asyncio.wait_for(
                 ProcessRunner(cmd).run(shell=True), self.process_runner_timeout
             )
             if ret == 0:
                 return True
             else:
                 return False
         except asyncio.TimeoutError:
             application_log.warning(
                 f"A timeout occurred while running service command: {cmd}"
             )
             return None
     else:
         application_log.warning(f"Not able to run service command: {cmd}")
         return None
Exemple #5
0
class MaverickConfigureSchema(schemaBase):
    def __init__(self):
        super().__init__(self)
        self.name = "MaverickConfigure"
        self.configure_command_defaults = {
            "environment": None,
            "module": None,
            "debug": False,
            "profile": False,
            "dryrun": False,
            "running": False,
            "uptime": None,
            "stdout": None,
            "stderror": None,
            "returncode": None,
        }
        self.configure_command = copy.deepcopy(self.configure_command_defaults)
        self.configure_proc = None

        self.configure_command_type = GraphQLObjectType(
            self.name,
            lambda: {
                "environment":
                GraphQLField(
                    GraphQLString,
                    description=
                    "Environment to configure: bootstrap, dev, flight or minimal",
                ),
                "dryrun":
                GraphQLField(
                    GraphQLBoolean,
                    description=
                    "Configure dry run - just show what would be done",
                ),
                "debug":
                GraphQLField(GraphQLBoolean, description="Debug output"),
                "profile":
                GraphQLField(GraphQLBoolean,
                             description="Profile performance data"),
                "module":
                GraphQLField(GraphQLString,
                             description="Only make changes to module <module>"
                             ),
                "running":
                GraphQLField(GraphQLBoolean, description=""),
                "uptime":
                GraphQLField(
                    GraphQLFloat,
                    description=
                    "Number of seconds the process has been running for",
                ),
                "terminate":
                GraphQLField(GraphQLBoolean, description=""),
                "stdout":
                GraphQLField(GraphQLString, description=""),
                "stderror":
                GraphQLField(GraphQLString, description=""),
                "returncode":
                GraphQLField(GraphQLInt, description=""),
            },
            description="Maverick configure interface",
        )

        self.q = {
            self.name:
            GraphQLField(self.configure_command_type,
                         resolve=self.get_configure_command_status)
        }

        self.m = {
            self.name:
            GraphQLField(
                self.configure_command_type,
                args=self.get_mutation_args(self.configure_command_type),
                resolve=self.run_configure_command,
            )
        }

        self.s = {
            self.name:
            GraphQLField(
                self.configure_command_type,
                subscribe=self.sub_configure_command_status,
                resolve=None,
            )
        }

    async def run_configure_command(self, root, info, **kwargs):
        application_log.debug(f"run_configure_command {kwargs}")

        self.configure_command["dryrun"] = kwargs.get("dryrun", False)
        self.configure_command["debug"] = kwargs.get("debug", False)
        self.configure_command["profile"] = kwargs.get("profile", False)
        self.configure_command["module"] = kwargs.get("module", None)
        self.configure_command["environment"] = kwargs.get("environment", None)
        self.configure_command["terminate"] = kwargs.get("terminate", False)

        cmd = "maverick configure"
        if self.configure_command["environment"] is not None:
            cmd += f' --env={self.configure_command["environment"]}'
        if self.configure_command["dryrun"]:
            cmd += " --dryrun"
        if self.configure_command["debug"]:
            cmd += " --debug"
        if self.configure_command["profile"]:
            cmd += " --profile"
        if self.configure_command["module"] is not None:
            cmd += f' --module={self.configure_command["module"]}'
        application_log.info(f"Running configure command: {cmd}")

        if self.configure_command["terminate"]:
            # try to terminate a running command
            if self.configure_proc:
                self.configure_proc.terminate()
        if self.configure_proc:
            # already running?
            if self.configure_proc.complete:
                self.configure_proc = None
        if not self.configure_proc:
            # try to run the command
            self.configure_proc = ProcessRunner(
                cmd,
                started_callback=self.process_callback,
                output_callback=self.process_callback,
                complete_callback=self.process_callback,
            )
            self.configure_proc.start()
        return self.configure_command

    def process_callback(self, *args, **kwargs):
        self.configure_command["running"] = self.configure_proc.running
        self.configure_command["uptime"] = self.configure_proc.uptime
        self.configure_command["stdout"] = self.configure_proc.stdout
        self.configure_command["stderror"] = self.configure_proc.stderror
        self.configure_command["returncode"] = self.configure_proc.returncode
        application_log.debug(self.configure_proc.stdout_log)
        application_log.debug(self.configure_proc.stderror_log)
        self.subscriptions.emit(
            self.subscription_string + self.name,
            {self.name: self.configure_command},
        )
        return self.configure_command

    def sub_configure_command_status(self, root, info):
        return SimplePubSubIterator(
            self.subscriptions,
            self.subscription_string + self.name,
        )

    def get_configure_command_status(self, root, info):
        return self.configure_command
Exemple #6
0
class MaverickShutdownSchema(schemaBase):
    def __init__(self):
        super().__init__(self)
        self.name = "MaverickShutdown"
        self.shutdown_command_defaults = {
            "running": False,
            "uptime": None,
            "stdout": None,
            "stderror": None,
            "returncode": None,
        }
        self.shutdown_command = copy.deepcopy(self.shutdown_command_defaults)
        self.shutdown_proc = None

        self.shutdown_command_type = GraphQLObjectType(
            self.name,
            lambda: {
                "running":
                GraphQLField(GraphQLBoolean, description=""),
                "uptime":
                GraphQLField(
                    GraphQLInt,
                    description=
                    "Number of seconds the process has been running for",
                ),
                "stdout":
                GraphQLField(GraphQLString, description=""),
                "stderror":
                GraphQLField(GraphQLString, description=""),
                "returncode":
                GraphQLField(GraphQLInt, description=""),
            },
            description="Maverick shutdown interface",
        )

        self.q = {
            self.name:
            GraphQLField(self.shutdown_command_type,
                         resolve=self.get_shutdown_command_status)
        }

        self.m = {
            self.name:
            GraphQLField(
                self.shutdown_command_type,
                args=self.get_mutation_args(self.shutdown_command_type),
                resolve=self.run_shutdown_command,
            )
        }

        self.s = {
            self.name:
            GraphQLField(
                self.shutdown_command_type,
                subscribe=self.sub_shutdown_command_status,
                resolve=None,
            )
        }

    async def run_shutdown_command(self, root, info, **kwargs):
        application_log.debug(f"run_shutdown_command {kwargs}")
        cmd = "sudo shutdown -a now"

        if self.shutdown_proc:
            # already running?
            if self.shutdown_proc.complete:
                self.shutdown_proc = None
        if not self.shutdown_proc:
            # try to run the command
            self.shutdown_proc = ProcessRunner(
                cmd,
                started_callback=self.process_callback,
                output_callback=self.process_callback,
                complete_callback=self.process_callback,
            )
            self.shutdown_proc.start()
        return self.shutdown_command

    def process_callback(self, *args, **kwargs):
        self.shutdown_command["running"] = self.shutdown_proc.running
        self.shutdown_command["uptime"] = self.shutdown_proc.uptime
        self.shutdown_command["stdout"] = self.shutdown_proc.stdout
        self.shutdown_command["stderror"] = self.shutdown_proc.stderror
        self.shutdown_command["returncode"] = self.shutdown_proc.returncode
        self.subscriptions.emit(
            self.subscription_string + self.name,
            {self.name: self.shutdown_command},
        )
        return self.shutdown_command

    def sub_shutdown_command_status(self, root, info):
        return EventEmitterAsyncIterator(
            self.subscriptions,
            self.subscription_string + self.name,
        )

    def get_shutdown_command_status(self, root, info):
        return self.shutdown_command
class MaverickShellSchema(schemaBase):
    def __init__(self):
        super().__init__(self)
        self.name = "MaverickShell"
        self.shell_command_defaults = {
            "command": "",
            "running": False,
            "uptime": None,
            "stdout": None,
            "stderror": None,
            "returncode": None,
        }
        self.shell_command = copy.deepcopy(self.shell_command_defaults)
        self.shell_proc = None

        self.shell_command_type = GraphQLObjectType(
            self.name,
            lambda: {
                "command":
                GraphQLField(
                    GraphQLString,
                    description="The command to run in the shell",
                ),
                "running":
                GraphQLField(GraphQLBoolean,
                             description="Is a process running"),
                "uptime":
                GraphQLField(
                    GraphQLFloat,
                    description=
                    "Number of seconds the process has been running for",
                ),
                "terminate":
                GraphQLField(GraphQLBoolean, description=""),
                "stdout":
                GraphQLField(GraphQLString, description=""),
                "stderror":
                GraphQLField(GraphQLString, description=""),
                "returncode":
                GraphQLField(GraphQLInt, description=""),
                "width":
                GraphQLField(GraphQLInt, description=""),
                "height":
                GraphQLField(GraphQLInt, description=""),
                "special":
                GraphQLField(GraphQLString, description=""),
            },
            description="Maverick shell interface",
        )

        self.q = {
            self.name:
            GraphQLField(self.shell_command_type,
                         resolve=self.get_shell_command_status)
        }

        self.m = {
            self.name:
            GraphQLField(
                self.shell_command_type,
                args=self.get_mutation_args(self.shell_command_type),
                resolve=self.run_shell_command,
            )
        }

        self.s = {
            self.name:
            GraphQLField(
                self.shell_command_type,
                subscribe=self.sub_shell_command_status,
                resolve=None,
            )
        }

    async def run_shell_command(self, root, info, **kwargs):
        application_log.debug(f"run_shell_command {kwargs}")

        self.shell_command["command"] = kwargs.get("command", "")
        self.shell_command["terminate"] = kwargs.get("terminate", False)
        self.shell_command["width"] = kwargs.get("width", 50)
        self.shell_command["height"] = kwargs.get("height", 50)
        cmd = self.shell_command["command"]
        application_log.info(f"Running shell command: {cmd}")

        if self.shell_command["terminate"]:
            # try to terminate a running command
            if self.shell_proc:
                self.shell_proc.terminate()
        if self.shell_proc:
            # already running?
            if self.shell_proc.complete:
                self.shell_proc = None
            else:
                application_log.debug(
                    "Process is still running. Providing input")
                special_cmd = kwargs.get("special", False)
                if special_cmd:
                    cmd += special_cmd
                else:
                    cmd += "\n"
                os.write(self.shell_proc.pty_master, cmd.encode())
        if not self.shell_proc:
            # try to run the command
            self.shell_proc = ProcessRunner(
                cmd,
                width=self.shell_command["width"],
                height=self.shell_command["height"],
                started_callback=self.start_process_callback,
                output_callback=self.output_process_callback,
                complete_callback=self.complete_process_callback,
            )
            self.shell_proc.start()
        return self.shell_command

    def start_process_callback(self, *args, **kwargs):
        ret = {}
        ret["command"] = kwargs["command"]
        ret["running"] = kwargs["running"]
        ret["uptime"] = kwargs["uptime"]
        ret["stdout"] = kwargs["stdout"]
        ret["stderror"] = kwargs["stderror"]
        ret["returncode"] = kwargs["returncode"]

    def output_process_callback(self, *args, **kwargs):
        ret = {}
        ret["command"] = kwargs["command"]
        ret["running"] = kwargs["running"]
        ret["uptime"] = kwargs["uptime"]
        ret["stdout"] = kwargs["stdout"]
        ret["stderror"] = kwargs["stderror"]
        ret["returncode"] = kwargs["returncode"]
        # if self.shell_proc.complete:
        self.subscriptions.emit(
            self.subscription_string + self.name,
            {self.name: ret},
        )

    def complete_process_callback(self, *args, **kwargs):
        ret = {}
        ret["command"] = kwargs["command"]
        ret["running"] = kwargs["running"]
        ret["uptime"] = kwargs["uptime"]
        ret["stdout"] = ""
        ret["stderror"] = ""
        ret["returncode"] = kwargs["returncode"]
        application_log.debug(self.shell_proc.stdout_log)
        application_log.debug(self.shell_proc.stderror_log)
        self.subscriptions.emit(
            self.subscription_string + self.name,
            {self.name: ret},
        )

    def sub_shell_command_status(self, root, info):
        return SimplePubSubIterator(
            self.subscriptions,
            self.subscription_string + self.name,
        )

    def get_shell_command_status(self, root, info):
        return self.shell_command