From d79780b98b66c0de8ae99d055b3b1593c673ea1d Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Mon, 30 Dec 2024 19:20:02 +0000 Subject: [PATCH] add better handling of borg exit codes --- docs/norgbackupweb | 2 +- norg.nimble | 2 +- norg/borg/borg.nim | 1 - norg/borg/create.nim | 22 +++++++++++++--------- norg/model/exit_codes.nim | 15 +++++++++++++++ norg/utils/run.nim | 4 ++-- 6 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 norg/model/exit_codes.nim diff --git a/docs/norgbackupweb b/docs/norgbackupweb index 9b5c6dd..b7d4362 160000 --- a/docs/norgbackupweb +++ b/docs/norgbackupweb @@ -1 +1 @@ -Subproject commit 9b5c6dd04825d33cb27a9c6715764ad7e23182c4 +Subproject commit b7d436287a9ecbe87466babf56d81f3c9fb56959 diff --git a/norg.nimble b/norg.nimble index f30058a..d913048 100644 --- a/norg.nimble +++ b/norg.nimble @@ -1,6 +1,6 @@ # Package -version = "0.1.13" +version = "0.1.14" author = "Paul Wilde" description = "A Borg Backup Orchestration Tool" license = "AGPL-3.0-or-later" diff --git a/norg/borg/borg.nim b/norg/borg/borg.nim index 2eeaaf7..892f063 100644 --- a/norg/borg/borg.nim +++ b/norg/borg/borg.nim @@ -10,7 +10,6 @@ import mount import extract - proc initRepo(nc: NorgConfig, repo: Repository): int = return runDiscard genCommand(cmd = "init", repo = repo.path, further_args = nc.args.further_args) diff --git a/norg/borg/create.nim b/norg/borg/create.nim index 46eff5e..c778133 100644 --- a/norg/borg/create.nim +++ b/norg/borg/create.nim @@ -1,5 +1,7 @@ import ../model/config_type import ../model/state_type +import ../model/exit_codes +import ../model/tool_type import ../notifier/notifier import ../model/log_type @@ -16,33 +18,35 @@ proc genArchiveName(): string = let ts = getTime().format("yyyy-MM-dd'T'HH:mm:ss'.'ffffff") return fmt"{hostname}-{ts}" -proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry: int = 0): int = +proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry: int = 0): (EXIT_CODE,string) = let further_args = nc.args.further_args let res = run genCreateCommand(repo = archivename, sources = nc.source_directories, stats=nc.args.stats, exc=nc.exclusions, further_args = further_args) if res != 0: info "Failed to run Borg. Waiting 15 seconds and trying again" sleep 15 * 1000 # 15 seconds if retry == nc.retries: - return 1 + return (BORG_ERROR, "Max Retries Reached") else: return createArchive(nc, repo, archivename, retry + 1) - return res + return (res.toExitCode(BORG),"Success") -proc createBackup*(nc: NorgConfig, repo: Repository): int = +proc createBackup*(nc: NorgConfig, repo: Repository): EXIT_CODE = let start_time = now() discard notify(nc.notifiers, state=Running) let archivename = repo.path & "::" & genArchiveName() debug "Creating Archive: ", archivename - let res = createArchive(nc, repo, archivename) + let (res,msg) = createArchive(nc, repo, archivename) let end_time = now() let total = (end_time - start_time).inMilliSeconds() case res - of 0: + of BORG_SUCCESS: if not repo.append_only: discard pruneRepo(nc, repo) - discard notify(nc.notifiers, state=Success, runtime=total) - of 1: - discard notify(nc.notifiers, state=Failure, runtime=total) + discard notify(nc.notifiers, state=Success, runtime=total, msg=msg) + of BORG_WARNING: + discard notify(nc.notifiers, state=Success, runtime=total, msg="Warning") + of BORG_ERROR: + discard notify(nc.notifiers, state=Failure, runtime=total, msg=msg) else: discard notify(nc.notifiers, state=Failure, runtime=total, msg = $res) return res diff --git a/norg/model/exit_codes.nim b/norg/model/exit_codes.nim new file mode 100644 index 0000000..2167102 --- /dev/null +++ b/norg/model/exit_codes.nim @@ -0,0 +1,15 @@ +import tool_type + +import std/enumutils + +type + EXIT_CODE* = enum + BORG_SUCCESS = 0 + BORG_WARNING = 1 + BORG_ERROR = 2 + OTHER = 99 + +proc toExitCode*(i: int, tool: BackupTool): EXIT_CODE = + for code in EXIT_CODE: + if i == ord(code): return code + return OTHER diff --git a/norg/utils/run.nim b/norg/utils/run.nim index 307a754..9b7d5e4 100644 --- a/norg/utils/run.nim +++ b/norg/utils/run.nim @@ -11,7 +11,7 @@ proc run*(cmd: string): int = return exitcode except: error getCurrentExceptionMsg() - return 1 + return 2 proc runDiscard*(cmd: string): int = debug fmt"Trying to run : {cmd}" @@ -22,5 +22,5 @@ proc runDiscard*(cmd: string): int = return exitcode except: error getCurrentExceptionMsg() - return 1 + return 2