Created
April 5, 2010 20:05
-
-
Save oremj/356796 to your computer and use it in GitHub Desktop.
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/python | |
from optparse import OptionParser | |
import multicommandlib | |
if __name__ == "__main__": | |
parser = OptionParser() | |
parser.add_option("-l", "--list", | |
action="store_true", | |
help="list systems in group") | |
parser.add_option("-g", "--list-groups", | |
action="store_true", | |
help="list systems in group") | |
parser.add_option("-c", "--count", | |
default=8, | |
help="number of systems to run cmd on at once") | |
parser.disable_interspersed_args() | |
(options, args) = parser.parse_args() | |
if options.list: | |
print "\n".join(get_systems(args[0])) | |
elif options.list_groups: | |
multicommandlib.list_groups() | |
else: | |
multicommandlib.issue_command(args[0], " ".join(args[1:]), int(options.count)) |
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/python | |
import os | |
import subprocess | |
import sys | |
import threading | |
import time | |
import multicommandconfig | |
YELLOW="\x1b[33;40m" | |
GREEN="\x1b[32;40m" | |
WHITE="\x1b[0m" | |
def get_systems(host_group): | |
return multicommandconfig.hostgroups[host_group] | |
def list_groups(): | |
groups = sorted(multicommandconfig.hostgroups.keys()) | |
for g in groups: | |
print "%s%s%s: %s" % (GREEN, g, WHITE, ",".join(get_systems(g))) | |
def issue_command(host_group, command_arg, count=8): | |
output_lock = threading.Lock() | |
try: | |
systems = get_systems(host_group) | |
states = {} | |
for s in systems: | |
states[s] = "never ran" | |
except KeyError: | |
sys.stdout.write("%s is not a valid host group\n" % host_group) | |
sys.exit(1) | |
class IssueCommandThread ( threading.Thread ): | |
def __init__(self, hosts, command): | |
self.hosts = hosts | |
self.command = command | |
threading.Thread.__init__(self) | |
self.setDaemon(True) | |
def run(self): | |
for host in self.hosts: | |
states[host] = 'did not finish' | |
p = subprocess.Popen(['ssh', host, '/bin/bash'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
command_out, command_err = p.communicate(self.command) | |
output = "%s===%s===%s\n" % (GREEN, host, WHITE) | |
output += command_out | |
output_lock.acquire() | |
print output | |
sys.stderr.write(command_err) | |
output_lock.release() | |
if p.returncode == 0: | |
del states[host] | |
else: | |
states[host] = 'failed' | |
if sys.argv.__len__() == 1: | |
print "usage: $0 <command to run on remote servers>"; | |
sys.exit() | |
threads = {} | |
systems_iter = iter(systems) | |
for i in range(count): | |
threads[i] = IssueCommandThread(systems_iter, command_arg) | |
threads[i].start() | |
try: | |
for t in threads: | |
threads[t].join() | |
except KeyboardInterrupt: | |
for s in systems: | |
try: | |
print "%s%s%s %s" % (GREEN, s, WHITE, states[s]) | |
except KeyError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment