Last active
August 29, 2015 14:13
-
-
Save magmax/ea1dfcc84c06cee12f40 to your computer and use it in GitHub Desktop.
Run a remote command in several machines, using just one password
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fabric.api import run | |
def execute(command): | |
run(command, shell=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import argparse | |
import subprocess | |
class RunException(): | |
pass | |
def hosts(cluster): | |
path = cluster | |
if not os.path.exists(path): | |
raise RunException('Cluster not found') | |
with open(path) as fd: | |
return ','.join(fd.read().splitlines()) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('cluster', nargs=1, | |
help='Cluster where commands should be run') | |
parser.add_argument('command', nargs='+', | |
help='Command') | |
parser.add_argument('-u', '--user', | |
default=os.getlogin(), | |
help='select another user') | |
args = parser.parse_args() | |
try: | |
command = 'fab -u "{user}" -H "{hosts}" execute:"{script}"'.format( | |
script=' '.join(args.command).replace('"', '\"'), | |
hosts=hosts(args.cluster[0]), | |
user=args.user | |
) | |
subprocess.call(command, shell=True) | |
except RunException as e: | |
print(e) |
And you can use Fabric directly:
$ fab execute:"echo hello world" -H "$(cat cluster.txt)"
[localhost] Executing task 'execute'
[localhost] run: echo hello world
[localhost] Passphrase for private key:
[localhost] out: hello world
[localhost] out:
Done.
Disconnecting from localhost... done.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: