#!/usr/bin/env python
|
|
"""
|
|
Employ instance to do your bidding
|
|
|
|
Usage:
|
|
employ --help
|
|
employ --version
|
|
employ help (commands | command <command> | managers | manager <manager>)
|
|
employ <manager> run <config_file> [<setup_script> ...]
|
|
|
|
Global Options:
|
|
-h, --help Show this message
|
|
--version Show the version number
|
|
|
|
Help Commands:
|
|
commands List all available commands
|
|
command <command> Print the docstring for the provided command
|
|
managers List all available managers
|
|
manager <manager> Print the docstring for the provided manager
|
|
|
|
"""
|
|
from ConfigParser import RawConfigParser
|
|
import sys
|
|
|
|
from docopt import docopt
|
|
|
|
import employ
|
|
|
|
def command_doc(command):
|
|
all_commands = employ.available_commands()
|
|
if all_commands.get(command):
|
|
print all_commands[command].__doc__
|
|
else:
|
|
sys.exit("Unknown command: '%s'" % command)
|
|
|
|
|
|
def list_commands(command=None):
|
|
all_commands = employ.available_commands()
|
|
print "List of Available Commands:"
|
|
for cls in all_commands:
|
|
print " %s" % cls
|
|
|
|
def manager_doc(manager):
|
|
all_managers = employ.available_managers()
|
|
if all_managers.get(manager):
|
|
print all_managers[manager].__doc__
|
|
else:
|
|
sys.exit("Unknown manager: '%s'" % manager)
|
|
|
|
|
|
def list_managers(manager=None):
|
|
all_managers = employ.available_managers()
|
|
print "List of Available Managers:"
|
|
for cls in all_managers:
|
|
print " %s" % cls
|
|
|
|
|
|
def list_regions():
|
|
print "List of Available Regions:"
|
|
for region in sorted(employ.manager.Manager.available_regions()):
|
|
print " %s" % region
|
|
|
|
|
|
def run(manager_cls, config_file, setup_scripts):
|
|
config = RawConfigParser(allow_no_value=True)
|
|
config.read(config_file)
|
|
commands = []
|
|
|
|
all_commands = employ.available_commands()
|
|
for command in config.sections():
|
|
if command == "employ" or command == manager_cls:
|
|
continue
|
|
if command not in all_commands:
|
|
sys.exit("Unknown command '%s'" % command)
|
|
commands.append(all_commands[command].from_config(config))
|
|
|
|
all_managers = employ.available_managers()
|
|
if manager_cls not in all_managers:
|
|
sys.exit("Unknown manager: '%s'" % manager_cls)
|
|
|
|
manager = all_managers[manager_cls].from_config(config)
|
|
with manager:
|
|
for setup_script in setup_scripts:
|
|
manager.setup(setup_script)
|
|
for command in commands:
|
|
manager.run(command)
|
|
|
|
|
|
arguments = docopt(__doc__, help=True, version="employ %s" % employ.__version__)
|
|
if arguments["help"]:
|
|
if arguments["commands"]:
|
|
list_commands()
|
|
elif arguments["command"] and arguments["<command>"]:
|
|
command_doc(arguments["<command>"])
|
|
elif arguments["managers"]:
|
|
list_managers()
|
|
elif arguments["manager"] and arguments["<manager>"]:
|
|
manager_doc(arguments["<manager>"])
|
|
elif arguments["run"]:
|
|
run(arguments["<manager>"], arguments["<config_file>"], arguments["<setup_script>"])
|