Пример #1
0
    def run_borg_raw(self, args):

        # prepare the command
        os.environ.update(self.publish_passcode())
        os.environ["BORG_DISPLAY_PASSPHRASE"] = "no"
        executable = self.value("borg_executable", BORG)
        remote_path = self.value("remote_path")
        remote_path = ["--remote-path", remote_path] if remote_path else []
        repository = str(self.repository)
        command = ([executable] + remote_path +
                   [a.replace('@repo', repository) for a in args])

        # run the command
        narrate("running:\n{}".format(
            indent(render_command(command, borg_options_arg_count))))
        with cd(self.working_dir):
            narrate("running in:", cwd())
            starts_at = arrow.now()
            log("starts at: {!s}".format(starts_at))
            borg = Run(command, modes="soeW", env=os.environ, log=False)
            ends_at = arrow.now()
            log("ends at: {!s}".format(ends_at))
            log("elapsed = {!s}".format(ends_at - starts_at))
        if borg.stdout:
            narrate("Borg stdout:")
            narrate(indent(borg.stdout))
        if borg.stderr:
            narrate("Borg stderr:")
            narrate(indent(borg.stderr))
        if borg.status:
            narrate("Borg exit status:", borg.status)

        return borg
Пример #2
0
    def run_borg(self, cmd, args='', borg_opts=None, emborg_opts=()):

        # prepare the command
        os.environ.update(self.publish_passcode())
        os.environ['BORG_DISPLAY_PASSPHRASE'] = 'no'
        if self.ssh_command:
            os.environ['BORG_RSH'] = self.ssh_command
        executable = self.value('borg_executable', BORG)
        if borg_opts is None:
            borg_opts = self.borg_options(cmd, emborg_opts)
        command = ([executable] + cmd.split() + borg_opts +
                   (args.split() if is_str(args) else args))
        environ = {
            k: v
            for k, v in os.environ.items() if k.startswith('BORG_')
        }
        if 'BORG_PASSPHRASE' in environ:
            environ['BORG_PASSPHRASE'] = '<redacted>'
        narrate('setting environment variables:', render(environ))

        # check if ssh agent is present
        if self.needs_ssh_agent:
            for ssh_var in 'SSH_AGENT_PID SSH_AUTH_SOCK'.split():
                if ssh_var not in os.environ:
                    warn(
                        'environment variable not found, is ssh-agent running?',
                        culprit=ssh_var)

        # run the command
        narrate('running:\n{}'.format(
            indent(render_command(command, borg_options_arg_count))))
        narrating = 'verbose' in emborg_opts or 'narrate' in emborg_opts
        modes = 'soeW' if narrating else 'sOEW'
        return Run(command, modes=modes, stdin='', env=os.environ, log=False)
Пример #3
0
    def run_borg_raw(self, args):

        # prepare the command
        os.environ.update(self.publish_passcode())
        os.environ['BORG_DISPLAY_PASSPHRASE'] = 'no'
        executable = self.value('borg_executable', BORG)
        repository = str(self.repository)
        command = ([executable] + [(repository if a == '@repo' else a)
                                   for a in args])

        # run the command
        narrate('running:\n{}'.format(
            indent(render_command(command, borg_options_arg_count))))
        return Run(command, modes='soeW', env=os.environ, log=False)
Пример #4
0
    def run_borg_raw(self, args):

        # run the run_before_borg commands
        self.run_user_commands('run_before_borg')

        # prepare the command
        self.publish_passcode()
        os.environ["BORG_DISPLAY_PASSPHRASE"] = "no"
        executable = self.value("borg_executable", BORG)
        remote_path = self.value("remote_path")
        remote_path = ["--remote-path", remote_path] if remote_path else []
        repository = str(self.repository)
        command = ([executable] + remote_path +
                   [a.replace('@repo', repository) for a in args])

        # run the command
        narrate("running:\n{}".format(
            indent(render_command(command, borg_options_arg_count))))
        with cd(self.working_dir):
            narrate("running in:", cwd())
            starts_at = arrow.now()
            log("starts at: {!s}".format(starts_at))
            try:
                borg = Run(command, modes="soeW1", env=os.environ, log=False)
            except Error as e:
                self.report_borg_error(e, executable)
            ends_at = arrow.now()
            log("ends at: {!s}".format(ends_at))
            log("elapsed = {!s}".format(ends_at - starts_at))
        if borg.status == 1:
            warn('Warning emitted by Borg, see logfile for details.')
        if borg.stdout:
            narrate("Borg stdout:")
            narrate(indent(borg.stdout))
        if borg.stderr:
            narrate("Borg stderr:")
            narrate(indent(borg.stderr))
        if borg.status:
            narrate("Borg exit status:", borg.status)

        return borg
Пример #5
0
    def run_borg_raw(self, args):

        # prepare the command
        os.environ.update(self.publish_passcode())
        os.environ['BORG_DISPLAY_PASSPHRASE'] = 'no'
        executable = self.value('borg_executable', BORG)
        remote_path = self.value('remote_path')
        remote_path = ['--remote-path', remote_path] if remote_path else []
        repository = str(self.repository)
        command = ([executable] + remote_path +
                   [(repository if a == '@repo' else a) for a in args])

        # run the command
        narrate('running:\n{}'.format(
            indent(render_command(command, borg_options_arg_count))))
        starts_at = arrow.now()
        narrate('starts at: {!s}'.format(starts_at))
        borg = Run(command, modes='soeW', env=os.environ, log=False)
        ends_at = arrow.now()
        narrate('ends at: {!s}'.format(ends_at))
        narrate('elapsed = {!s}'.format(ends_at - starts_at))
        return borg
Пример #6
0
    def run_borg(
        self,
        cmd,
        args=(),
        borg_opts=None,
        emborg_opts=(),
        strip_prefix=False,
        show_borg_output=False,
        use_working_dir=False,
    ):
        # prepare the command
        os.environ.update(self.publish_passcode())
        os.environ["BORG_DISPLAY_PASSPHRASE"] = "no"
        if self.ssh_command:
            os.environ["BORG_RSH"] = self.ssh_command
        executable = self.value("borg_executable", BORG)
        borg_opts = self.borg_options(cmd, borg_opts, emborg_opts,
                                      strip_prefix)
        command = [executable] + cmd.split() + borg_opts + args
        environ = {
            k: v
            for k, v in os.environ.items() if k.startswith("BORG_")
        }
        if "BORG_PASSPHRASE" in environ:
            environ["BORG_PASSPHRASE"] = "<redacted>"
        narrate("Borg-related environment variables:", render(environ))

        # check if ssh agent is present
        if self.needs_ssh_agent:
            if "SSH_AUTH_SOCK" not in os.environ:
                warn(
                    "SSH_AUTH_SOCK environment variable not found.",
                    "Is ssh-agent running?",
                )

        # run the command
        narrate("running:\n{}".format(
            indent(render_command(command, borg_options_arg_count))))
        with cd(self.working_dir if use_working_dir else "."):
            narrate("running in:", cwd())
            starts_at = arrow.now()
            log("starts at: {!s}".format(starts_at))
            narrating = (show_borg_output or "--verbose" in borg_opts
                         or "verbose" in emborg_opts or "narrate"
                         in emborg_opts) and "--json" not in command
            if narrating:
                modes = "soeW"
                display("\nRunning Borg {} command ...".format(cmd))
            else:
                modes = "sOEW"
            try:
                borg = Run(command,
                           modes=modes,
                           stdin="",
                           env=os.environ,
                           log=False)
            except Error as e:
                e.reraise(culprit=f"borg {cmd}")
            ends_at = arrow.now()
            log("ends at: {!s}".format(ends_at))
            log("elapsed = {!s}".format(ends_at - starts_at))
        if borg.stdout:
            narrate("Borg stdout:")
            narrate(indent(borg.stdout))
        if borg.stderr:
            narrate("Borg stderr:")
            narrate(indent(borg.stderr))
        if borg.status:
            narrate("Borg exit status:", borg.status)
        return borg
Пример #7
0
    def run_borg(
        self,
        cmd,
        args=(),
        borg_opts=None,
        emborg_opts=(),
        strip_prefix=False,
        show_borg_output=False,
        use_working_dir=False,
    ):

        # run the run_before_borg commands
        self.run_user_commands('run_before_borg')

        # prepare the command
        self.publish_passcode()
        if "BORG_PASSPHRASE" in os.environ:
            os.environ["BORG_DISPLAY_PASSPHRASE"] = "no"
        if self.ssh_command:
            os.environ["BORG_RSH"] = self.ssh_command
        environ = {
            k: v
            for k, v in os.environ.items() if k.startswith("BORG_")
        }
        if "BORG_PASSPHRASE" in environ:
            environ["BORG_PASSPHRASE"] = "<redacted>"
        executable = self.value("borg_executable", BORG)
        borg_opts = self.borg_options(cmd, borg_opts, emborg_opts,
                                      strip_prefix)
        command = [executable] + cmd.split() + borg_opts + args
        narrate("Borg-related environment variables:", render(environ))

        # check if ssh agent is present
        if self.needs_ssh_agent:
            if "SSH_AUTH_SOCK" not in os.environ:
                warn(
                    "SSH_AUTH_SOCK environment variable not found.",
                    "Is ssh-agent running?",
                )

        # run the command
        narrate("running:\n{}".format(
            indent(render_command(command, borg_options_arg_count))))
        with cd(self.working_dir if use_working_dir else "."):
            narrate("running in:", cwd())
            if "--json" in command or "--json-lines" in command:
                narrating = False
            else:
                narrating = (show_borg_output or "--verbose" in borg_opts
                             or "--progress" in borg_opts
                             or "verbose" in emborg_opts
                             or "narrate" in emborg_opts)
            if narrating:
                modes = "soeW1"
                display("\nRunning Borg {} command ...".format(cmd))
            else:
                modes = "sOEW1"
            starts_at = arrow.now()
            log("starts at: {!s}".format(starts_at))
            try:
                borg = Run(command,
                           modes=modes,
                           stdin="",
                           env=os.environ,
                           log=False)
            except Error as e:
                self.report_borg_error(e, cmd)
            finally:
                # remove passcode env variables created by emborg
                if self.borg_passcode_env_var_set_by_emborg:
                    narrate(
                        f"Unsetting {self.borg_passcode_env_var_set_by_emborg}."
                    )
                    del os.environ[self.borg_passcode_env_var_set_by_emborg]
            ends_at = arrow.now()
            log("ends at: {!s}".format(ends_at))
            log("elapsed = {!s}".format(ends_at - starts_at))
        if borg.status:
            narrate("Borg exit status:", borg.status)
        if borg.status == 1 and borg.stderr:
            warnings = borg.stderr.partition(72 * '-')[0]
            warn('Warning emitted by Borg:', codicil=warnings)
        if borg.stdout:
            narrate("Borg stdout:")
            narrate(indent(borg.stdout))
        if borg.stderr:
            narrate("Borg stderr:")
            narrate(indent(borg.stderr))

        return borg