From 7c937519fc3c80a164d8b2c26897c113fb64cd17 Mon Sep 17 00:00:00 2001 From: "m1kc (Max Musatov)" Date: Tue, 7 Aug 2018 10:34:50 +0300 Subject: [PATCH] Add support for pre- and post-commands --- plasma-agent.example.json | 5 ++++- plasma-agent/main.py | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/plasma-agent.example.json b/plasma-agent.example.json index e4ee567..67e3b95 100644 --- a/plasma-agent.example.json +++ b/plasma-agent.example.json @@ -11,5 +11,8 @@ "host": "127.0.0.1", "login": "plasma" }, - "remoteFolder": "/data/backup" + "remoteFolder": "/data/backup", + + "executeBefore": ["df -h"], + "executeAfter": ["echo THIS WAS A TRIUMPH", "echo I AM MAKING A NOTE HERE: HUGE SUCCESS"] } diff --git a/plasma-agent/main.py b/plasma-agent/main.py index bc93c2a..adcfef3 100755 --- a/plasma-agent/main.py +++ b/plasma-agent/main.py @@ -46,13 +46,22 @@ def main(): ssh_login = config['ssh']['login'] remote_folder = config['remoteFolder'] - # Make the backup + execute_before = config['executeBefore'] + execute_after = config['executeAfter'] + # Pick up strategy strategy = STRATEGIES[target_strategy](target_folders, temp_path, options) log.debug('Checking if we can use this strategy: %s', target_strategy) if not strategy.can_execute(): raise OSError("Cannot execute strategy") + # Execute pre-commands, cancel operation if any fails + for cmd in execute_before: + log.info("Executing pre-command: %s", cmd) + subprocess.run(cmd, check=True, shell=True) + + # Make the backup + try: log.debug('Checking for previous backup') os.stat(temp_path) @@ -91,6 +100,14 @@ def main(): log.warning("plasma-rotate failed, old backups were not deleted") sys.exit(1) + # Execute pre-commands, cancel operation if any fails + for cmd in execute_after: + log.info("Executing post-command: %s", cmd) + try: + subprocess.run(cmd, check=True, shell=True) + except: + log.warning("Command failed, continuing anyway") + # Glad it's done log.info("Everything's fine, exiting.")