Пример #1
0
 def add_command(self, cmd, stdin=None, rm_stdin=False, user="******"):
     command = CommandLog()
     command.server = self.server_object
     command.command = cmd
     command.execute_user = user
     command.stdin = stdin
     command.rm_stdin = rm_stdin
     command.save()
     return command
Пример #2
0
 def add_command(self, cmd, stdin=None, rm_stdin=False, user="******"):
     command = CommandLog()
     command.server = self.server_object
     command.command = cmd
     command.execute_user = user
     command.stdin = stdin
     command.rm_stdin = rm_stdin
     command.save()
     return command
Пример #3
0
 def run(self, cmd):
     log = CommandLog()
     log.server = self.server_object
     ssh = SSHClient()
     ssh.load_system_host_keys(settings.SSH_HOSTKEYS)
     ssh.set_missing_host_key_policy(AutoAddPolicy())
     ssh.connect(self.server_object.domain, username="******", key_filename=settings.SSH_PRIVATEKEY)
     log.command = cmd
     log.execute_user = "******"
     log.save()
     stdin, stdout, stderr = ssh.exec_command(cmd)
     log.result_stdout = stdout.read()
     log.result_stderr = stderr.read()
     log.status_code = stdout.channel.recv_exit_status()
     log.processed = True
     log.save()
     ssh.close()
     return {"stdout": stdout.read(), "stderr": stderr.read()}
Пример #4
0
 def run(self, cmd):
     log = CommandLog()
     log.server = self.server_object
     ssh = SSHClient()
     ssh.load_system_host_keys(settings.SSH_HOSTKEYS)
     ssh.set_missing_host_key_policy(AutoAddPolicy())
     ssh.connect(self.server_object.domain,
                 username="******",
                 key_filename=settings.SSH_PRIVATEKEY)
     log.command = cmd
     log.execute_user = "******"
     log.save()
     stdin, stdout, stderr = ssh.exec_command(cmd)
     data = {"stdout": stdout.read(), "stderr": stderr.read()}
     log.result_stdout = data.get("stdout")
     log.result_stderr = data.get("stderr")
     log.status_code = stdout.channel.recv_exit_status()
     log.processed = True
     log.save()
     ssh.close()
     return data
Пример #5
0
def commit_requests(requests, server, task_log=None):
    """
        Take requests for specific server and run them one by one. Summary can be saved in task_log.
    """
    logger = commit_requests.get_logger()

    ssh = SSHClient()
    ssh.load_system_host_keys(settings.SSH_HOSTKEYS)
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(server.domain,
                username="******",
                key_filename=settings.SSH_PRIVATEKEY)
    error = False
    msg = ""
    try:
        for req in requests:
            log = CommandLog()
            log.server = server
            if req.get("type") == "cmd":
                req_user = req.get("user").strip()
                if req_user == "root" or not req_user:
                    req_cmd = req.get("cmd")
                else:
                    req_cmd = "su %s -c '%s'" % (req_user, req.get("cmd"))
                req_stdin = req.get("stdin")
                log.command = req_cmd
                log.execute_user = req_user
                log.save()
                logger.info("CMD (%s): %s" % (req_user, req_cmd))
                stdin, stdout, stderr = ssh.exec_command(req_cmd)
                if req_stdin:
                    if not req.get("rm_stdin"):
                        log.stdin = req_stdin
                    log.rm_stdin = req.get("rm_stdin")
                    log.save()
                    stdin.write(req_stdin)
                    stdin.flush()
                stdin.channel.shutdown(1)
                log.result_stdout = stdout.read()
                log.result_stderr = stderr.read()
                log.save()
            elif req.get("type") == "file":
                req_path = req.get("path")
                req_cmd = "tee '%s'" % req_path
                req_stdin = req.get("content")
                req_owner = req.get("owner")
                log.command = req_cmd
                log.execute_user = req_owner
                log.save()
                logger.info("CMD (%s): %s" % (req_owner, req_cmd))
                stdin, stdout, stderr = ssh.exec_command(req_cmd)
                if req_stdin:
                    log.stdin = req_stdin
                    log.save()
                    stdin.write(req_stdin)
                    stdin.flush()
                stdin.channel.shutdown(1)
                if req_owner:
                    ssh.exec_command("chown %s:%s '%s'" %
                                     (req_owner, req_owner, req_path))
                log.result_stdout = stdout.read()
                log.result_stderr = stderr.read()
                log.save()
            if stdout.channel.recv_exit_status() != 0:
                error = True
            log.status_code = stdout.channel.recv_exit_status()
            log.processed = True
            log.save()
    finally:
        ssh.close()

    if task_log:
        task_log.complete = True
        task_log.error = error
        task_log.backend_msg = msg
        task_log.save()

    return not error
Пример #6
0
def commit_requests(requests, server, task_log=None):
    """
        Take requests for specific server and run them one by one. Summary can be saved in task_log.
    """
    logger = commit_requests.get_logger()

    ssh = SSHClient()
    ssh.load_system_host_keys(settings.SSH_HOSTKEYS)
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(server.domain, username="******", key_filename=settings.SSH_PRIVATEKEY)
    error = False
    msg = ""
    try:
        for req in requests:
            log = CommandLog()
            log.server = server
            if req.get("type") == "cmd":
                req_user = req.get("user").strip()
                if req_user == "root" or not req_user:
                    req_cmd = req.get("cmd")
                else:
                    req_cmd = "su %s -c '%s'" % (req_user, req.get("cmd"))
                req_stdin = req.get("stdin")
                log.command = req_cmd
                log.execute_user = req_user
                log.save()
                logger.info("CMD (%s): %s" % (req_user ,req_cmd))
                stdin, stdout, stderr = ssh.exec_command(req_cmd)
                if req_stdin:
                    if not req.get("rm_stdin"):
                        log.stdin = req_stdin
                    log.rm_stdin = req.get("rm_stdin")
                    log.save()
                    stdin.write(req_stdin)
                    stdin.flush()
                stdin.channel.shutdown(1)
                log.result_stdout = stdout.read()
                log.result_stderr = stderr.read()
                log.save()
            elif req.get("type") == "file":
                req_path = req.get("path")
                req_cmd = "tee '%s'" % req_path
                req_stdin = req.get("content")
                req_owner = req.get("owner")
                log.command = req_cmd
                log.execute_user = req_owner
                log.save()
                logger.info("CMD (%s): %s" % (req_owner, req_cmd))
                stdin, stdout, stderr = ssh.exec_command(req_cmd)
                if req_stdin:
                    log.stdin = req_stdin
                    log.save()
                    stdin.write(req_stdin)
                    stdin.flush()
                stdin.channel.shutdown(1)
                if req_owner:
                    ssh.exec_command("chown %s:%s '%s'" % (req_owner, req_owner, req_path))
                log.result_stdout = stdout.read()
                log.result_stderr = stderr.read()
                log.save()
            if stdout.channel.recv_exit_status() != 0:
                error = True
            log.status_code = stdout.channel.recv_exit_status()
            log.processed = True
            log.save()
    finally:
        ssh.close()

    if task_log:
        task_log.complete = True
        task_log.error = error
        task_log.backend_msg = msg
        task_log.save()

    return not error