An example python "script" that manages various services on multiple remote servers:
What follows is a hacked together script that can be used to manage various services on servers that you have SSH access to.
You will ideally want to have an ssh-agent running, or you will be typing your passphrase a lot of times.
For commands that require elevated privileges on the remote machine, you can see that "sudo" is called. This means that you need to modify your sudoers file on each remote machine, and add entries like this (assuming your username == deploy
):
Defaults:deploy !requiretty
Defaults:deploy !authenticate
deploy ALL=\
/sbin/service httpd status,\
/sbin/service httpd configtest,\
/sbin/service httpd graceful
The first two lines allow the deploy
user to run sudo
without having a tty or re-typing the password -- which means it can be run straight over ssh without any further input. Here is an example python command to take advantage of sudo
on a remote:
CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd graceful'))
Anyway, this is not a "straight" answer to your question, but rather an illustration of how easy you can use python and a couple of other techniques to create a systems management tool that is 100% tailored to your specific needs.
And by the way, the following script does "tell you loud and clear" if any of the commands returned an exit status > 0, so you can analyze the output yourself.
This was hacked together when a project I was working with started using a load balancer and it was no longer equitable to run all the commands on each server. You could modify or extend this to work with rsync to deploy files, or even deploy updates to the scripts that you host on the remote servers to "do the work".
#!/usr/bin/python
from optparse import OptionParser
import subprocess
import sys
def die(sMessage):
print
print sMessage
print
sys.exit(2)
###################################################################################################
# Settings
# The user@host: for the SourceURLs (NO TRAILING SLASH)
RemoteUsers = [
"[email protected]",
"[email protected]",
]
###################################################################################################
# Global Variables
# optparse.Parser instance
Parser = None
# optparse.Values instance full of command line options
Opt = None
# List of command line arguments
Arg = None
###################################################################################################
Parser = OptionParser(usage="%prog [options] [Command[, Subcommand]]")
Parser.add_option("--interactive",
dest = "Interactive",
action = "store_true",
default = False,
help = "Ask before doing each operation."
)
# Parse command line
Opt, Arg = Parser.parse_args()
def HelpAndExit():
print "This command is used to run commands on the application servers."
print
print "Usage:"
print " deploy-control [--interactive] Command"
print
print "Options:"
print " --interactive :: will ask before executing each operation"
print
print "Servers:"
for s in RemoteUsers: print " " + s
print
print "Web Server Commands:"
print " deploy-control httpd status"
print " deploy-control httpd configtest"
print " deploy-control httpd graceful"
print " deploy-control loadbalancer in"
print " deploy-control loadbalancer out"
print
print "App Server Commands:"
print " deploy-control 6x6server status"
print " deploy-control 6x6server stop"
print " deploy-control 6x6server start"
print " deploy-control 6x6server status"
print " deploy-control wb4server stop"
print " deploy-control wb4server start"
print " deploy-control wb4server restart"
print " deploy-control wb4server restart"
print
print "System Commands:"
print " deploy-control disk usage"
print " deploy-control uptime"
print
sys.exit(2)
def YesNo(sPrompt):
while True:
s = raw_input(sPrompt)
if s in ('y', 'yes'):
return True
elif s in ('n', 'no'):
return False
else:
print "Invalid input!"
# Implicitly verified below in if/else
Command = tuple(Arg)
if Command in (('help',), ()):
HelpAndExit()
ResultList = []
###################################################################################################
for UH in RemoteUsers:
print "-"*80
print "Running %s command on: %s" % (Command, UH)
if Opt.Interactive and not YesNo("Do you want to run this command? "):
print "Skipping!"
print
continue
#----------------------------------------------------------------------------------------------
if Command == ('httpd', 'configtest'):
CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd configtest'))
#----------------------------------------------------------------------------------------------
elif Command == ('httpd', 'graceful'):
CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd graceful'))
#----------------------------------------------------------------------------------------------
elif Command == ('httpd', 'status'):
CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd status'))
#----------------------------------------------------------------------------------------------
elif Command == ('loadbalancer', 'in'):
CommandResult = subprocess.call(('ssh', UH, 'bin-slave/loadbalancer-in'))
#----------------------------------------------------------------------------------------------
elif Command == ('loadbalancer', 'out'):
CommandResult = subprocess.call(('ssh', UH, 'bin-slave/loadbalancer-out'))
#----------------------------------------------------------------------------------------------
elif Command == ('disk', 'usage'):
CommandResult = subprocess.call(('ssh', UH, 'df -h'))
#----------------------------------------------------------------------------------------------
elif Command == ('uptime',):
CommandResult = subprocess.call(('ssh', UH, 'uptime'))
#----------------------------------------------------------------------------------------------
elif Command == ('6x6server', 'status'):
CommandResult = subprocess.call(('ssh', UH, 'bin-slave/6x6server-status'))
if CommandResult > 0:
print "Servers not running!!!"
#----------------------------------------------------------------------------------------------
elif Command == ('6x6server', 'stop'):
CommandResult = subprocess.call(('ssh', UH, 'bin-slave/6x6server-stop'))
#----------------------------------------------------------------------------------------------
elif Command == ('6x6server', 'start'):
CommandResult = subprocess.call(('ssh', UH, 'bin-slave/6x6server-start'))
#----------------------------------------------------------------------------------------------
elif Command == ('6x6server', 'restart'):
CommandResult = subprocess.call(('ssh', UH, 'bin-slave/6x6server-restart'))
#----------------------------------------------------------------------------------------------
elif Command == ('wb4server', 'status'):
CommandResult = subprocess.call(('ssh', UH, 'bin-slave/wb4server-status'))
if CommandResult > 0:
print "Servers not running!!!"
#----------------------------------------------------------------------------------------------
elif Command == ('wb4server', 'stop'):
CommandResult = subprocess.call(('ssh', UH, 'bin-slave/wb4server-stop'))
#----------------------------------------------------------------------------------------------
elif Command == ('wb4server', 'start'):
CommandResult = subprocess.call(('ssh', UH, 'bin-slave/wb4server-start'))
#----------------------------------------------------------------------------------------------
elif Command == ('wb4server', 'restart'):
CommandResult = subprocess.call(('ssh', UH, 'bin-slave/wb4server-restart'))
#----------------------------------------------------------------------------------------------
else:
print
print "#"*80
print
print "Error: invalid command"
print
HelpAndExit()
#----------------------------------------------------------------------------------------------
ResultList.append(CommandResult)
print
###################################################################################################
if any(ResultList):
print "#"*80
print "#"*80
print "#"*80
print
print "ERRORS FOUND. SEE ABOVE"
print
sys.exit(0)
else:
print "-"*80
print
print "Looks OK!"
print
sys.exit(1)