From dad54491b5af06ec5191286854478f5674d5e8fb Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Tue, 24 Sep 2013 07:36:49 -0400 Subject: [PATCH] parse settings from ini file rather than cli arguments also added in basic support for setup scripts --- bin/employ | 42 ++++++++++++++++++++++-------------------- employ/commands/ab.py | 29 +++++++++++++++++++---------- employ/manager.py | 31 ++++++++++++++++++++++++------- 3 files changed, 65 insertions(+), 37 deletions(-) diff --git a/bin/employ b/bin/employ index a84f5ad..ee6977c 100755 --- a/bin/employ +++ b/bin/employ @@ -3,23 +3,21 @@ Employ instance to do your bidding Usage: + employ --help + employ --version employ help (commands | command | regions) - employ run [options] [ ...] + employ run [ ...] Global Options: - -h --help Show this message + -h, --help Show this message --version Show the version number -Run Options: - -i NUM, --instances NUM Number of instances to start [default: 1] - -r REGION, --region REGION Which region to start the instances up on [default: us-east-1] - -n NAME, --name NAME Which name to assign to each new instance [default: employed] - Help Commands: commands List all available commands command Print the docstring for the provided command regions List all available regions names """ +from ConfigParser import RawConfigParser import sys from docopt import docopt @@ -49,20 +47,24 @@ def list_regions(): print " %s" % region -def run(arguments): +def run(config_file, setup_scripts): + config = RawConfigParser(allow_no_value=True) + config.read(config_file) + commands = [] + all_commands = employ.manager.Manager.available_commands() - if arguments[""] not in all_commands: - sys.exit("Unknown command '%s'" % command) - command_cls = all_commands[arguments[""]] - command = command_cls(*arguments[""]) - - manager = employ.manager.Manager( - num_instances=arguments["--instances"], - name=arguments["--name"], - region=arguments["--region"], - ) + for command in config.sections(): + if command == "employ": + continue + if command not in all_commands: + sys.exit("Unknown command '%s'" % command) + commands.append(all_commands[command].from_config(config)) + manager = employ.manager.Manager.from_config(config) with manager: - manager.run(command) + 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__) @@ -74,4 +76,4 @@ if arguments["help"]: elif arguments["regions"]: list_regions() elif arguments["run"]: - run(arguments) + run(arguments[""], arguments[""]) diff --git a/employ/commands/ab.py b/employ/commands/ab.py index 504c74e..e7bb1b6 100644 --- a/employ/commands/ab.py +++ b/employ/commands/ab.py @@ -5,25 +5,34 @@ class ABCommand(Command): """ Command used to run ApacheBench (ab) - Command Options: - [ ] + Command Settings: + [ab] + target= + requests= + concurrency= + keepalive=(True|False) - Examples: - employ run ab http://127.0.0.1/test.html 1000 - employ run ab https://127.0.0.1:8000/test.html 1000 10 - employ run ab https://127.0.0.1:8000/test.html 1000 10 "-k -C 'cookie=1234'" + Eample: + ; run_ab.ini + [ab] + target=http://127.0.0.1:8000/test.html + requests=1000 + concurrency=100 + keepalive=False + + employ run ab run_ab.ini """ name = "ab" - def __init__(self, target, requests, concurrency=1, extra_args=""): + def __init__(self, target, requests, concurrency=1, keepalive=True): self.target = target self.requests = requests self.concurrency = concurrency - self.extra_args = extra_args - super(ABCommand, self).__init__() + self.keepalive = keepalive def command(self): - return "ab %s -n %s -c %s %s" % (self.target, self.requests, self.concurrency, self.extra_args) + keepalive = " -k" if self.keepalive else "" + return "ab %s -n %s -c %s%s" % (self.target, self.requests, self.concurrency, keepalive) def aggregate(self, results): pass diff --git a/employ/manager.py b/employ/manager.py index 3e2e92f..e0eb80f 100644 --- a/employ/manager.py +++ b/employ/manager.py @@ -1,17 +1,25 @@ import boto.ec2 +import boto.manage from employ import commands class Manager(object): - def __init__(self, num_instances=1, name="deployed", region="us-east-1"): + def __init__( + self, ami_image_id, num_instances=1, instance_name="deployed", + region="us-east-1", instance_type="t1.micro", + ): self.instances = [] + self.ami_image_id = ami_image_id self.num_instances = num_instances - self.name = name + self.instance_name = instance_name self.region = region + self.instance_type = instance_type def setup_instances(self): - print "starting %s instances named %s in region %s" % (self.num_instances, self.name, self.region) + print "starting %s %s instances named %s in region %s" % ( + self.num_instances, self.instance_type, self.instance_name, self.region + ) __enter__ = setup_instances @@ -21,6 +29,13 @@ class Manager(object): def __exit__(self, type, value, traceback): self.cleanup_instances() + @classmethod + def from_config(cls, config): + settings = {} + if config.has_section("employ"): + settings = dict(config.items("employ")) + return cls(**settings) + @classmethod def available_regions(cls): for region in boto.ec2.regions(): @@ -29,12 +44,14 @@ class Manager(object): @classmethod def available_commands(cls): all_commands = {} - for cls in commands.__all__: - the_cls = getattr(commands, cls) - if the_cls.name == "command": + for command_cls in commands: + if command_cls.name == "command": continue - all_commands[the_cls.name] = the_cls + all_commands[command_cls.name] = command_cls return all_commands + def setup(self, script): + print "executing setup script: %s" % script + def run(self, command): print "running command '%s'" % command.command()