Skip to content

Commit

Permalink
Add support for pre- and post-commands
Browse files Browse the repository at this point in the history
  • Loading branch information
m1kc committed Aug 7, 2018
1 parent 671a641 commit 7c93751
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion plasma-agent.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
19 changes: 18 additions & 1 deletion plasma-agent/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.")

Expand Down

0 comments on commit 7c93751

Please sign in to comment.