Skip to content

Instantly share code, notes, and snippets.

@imagescape
Created November 1, 2012 22:04
Show Gist options
  • Save imagescape/3996959 to your computer and use it in GitHub Desktop.
Save imagescape/3996959 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os, sys, argparse, subprocess
from fabric.api import *
DONESND = os.environ.get('NOTIFY_DONE_SOUND', None)
ERRORSND = os.environ.get('NOTIFY_ERROR_SOUND', DONESND)
DEVNULL = open(os.devnull, 'w')
def get_args():
parser = argparse.ArgumentParser(description='Run bash commands on a remote host. Get notified when its done.')
parser.add_argument(
'hoststring',
type=str, nargs=1,
help='ssh user@host string'
)
parser.add_argument(
'commandstring',
type=str, nargs=1,
help='string of commands to run'
)
args = parser.parse_args()
return args.hoststring[0], args.commandstring[0]
def notify(hoststring, commandstring, failed=False):
prefix = "%s"
sound = DONESND
if failed:
prefix = "FAILED: %s"
sound = ERRORSND
if sound:
retproc = subprocess.Popen(
("playsound", sound),
# stdout=DEVNULL, stderr=DEVNULL
)
retproc = subprocess.Popen(
("notify-send", prefix % hoststring, commandstring),
# stdout=DEVNULL, stderr=DEVNULL
)
if "__main__" == __name__:
hoststring, commandstring = get_args()
env.host_string = hoststring
with settings(hide('warning'), warn_only=True):
retval = run(commandstring)
notify(hoststring, commandstring, retval.failed)
DEVNULL.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment