Browse Source

parse settings from ini file rather than cli arguments

also added in basic support for setup scripts
master
Brett Langdon 12 years ago
parent
commit
dad54491b5
3 changed files with 65 additions and 37 deletions
  1. +22
    -20
      bin/employ
  2. +19
    -10
      employ/commands/ab.py
  3. +24
    -7
      employ/manager.py

+ 22
- 20
bin/employ View File

@ -3,23 +3,21 @@
Employ instance to do your bidding Employ instance to do your bidding
Usage: Usage:
employ --help
employ --version
employ help (commands | command <command> | regions) employ help (commands | command <command> | regions)
employ run <command> [options] [<command_options> ...]
employ run <command> <config_file> [<setup_script> ...]
Global Options: Global Options:
-h --help Show this message
-h, --help Show this message
--version Show the version number --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: Help Commands:
commands List all available commands commands List all available commands
command <command> Print the docstring for the provided command command <command> Print the docstring for the provided command
regions List all available regions names regions List all available regions names
""" """
from ConfigParser import RawConfigParser
import sys import sys
from docopt import docopt from docopt import docopt
@ -49,20 +47,24 @@ def list_regions():
print " %s" % region 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() all_commands = employ.manager.Manager.available_commands()
if arguments["<command>"] not in all_commands:
sys.exit("Unknown command '%s'" % command)
command_cls = all_commands[arguments["<command>"]]
command = command_cls(*arguments["<command_options>"])
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: 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__) arguments = docopt(__doc__, help=True, version="employ %s" % employ.__version__)
@ -74,4 +76,4 @@ if arguments["help"]:
elif arguments["regions"]: elif arguments["regions"]:
list_regions() list_regions()
elif arguments["run"]: elif arguments["run"]:
run(arguments)
run(arguments["<config_file>"], arguments["<setup_script>"])

+ 19
- 10
employ/commands/ab.py View File

@ -5,25 +5,34 @@ class ABCommand(Command):
""" """
Command used to run ApacheBench (ab) Command used to run ApacheBench (ab)
Command Options:
<target> <requests> [<concurrency> <extra_args>]
Command Settings:
[ab]
target=<target>
requests=<requests>
concurrency=<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" name = "ab"
def __init__(self, target, requests, concurrency=1, extra_args=""):
def __init__(self, target, requests, concurrency=1, keepalive=True):
self.target = target self.target = target
self.requests = requests self.requests = requests
self.concurrency = concurrency self.concurrency = concurrency
self.extra_args = extra_args
super(ABCommand, self).__init__()
self.keepalive = keepalive
def command(self): 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): def aggregate(self, results):
pass pass

+ 24
- 7
employ/manager.py View File

@ -1,17 +1,25 @@
import boto.ec2 import boto.ec2
import boto.manage
from employ import commands from employ import commands
class Manager(object): 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.instances = []
self.ami_image_id = ami_image_id
self.num_instances = num_instances self.num_instances = num_instances
self.name = name
self.instance_name = instance_name
self.region = region self.region = region
self.instance_type = instance_type
def setup_instances(self): 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 __enter__ = setup_instances
@ -21,6 +29,13 @@ class Manager(object):
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
self.cleanup_instances() self.cleanup_instances()
@classmethod
def from_config(cls, config):
settings = {}
if config.has_section("employ"):
settings = dict(config.items("employ"))
return cls(**settings)
@classmethod @classmethod
def available_regions(cls): def available_regions(cls):
for region in boto.ec2.regions(): for region in boto.ec2.regions():
@ -29,12 +44,14 @@ class Manager(object):
@classmethod @classmethod
def available_commands(cls): def available_commands(cls):
all_commands = {} 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 continue
all_commands[the_cls.name] = the_cls
all_commands[command_cls.name] = command_cls
return all_commands return all_commands
def setup(self, script):
print "executing setup script: %s" % script
def run(self, command): def run(self, command):
print "running command '%s'" % command.command() print "running command '%s'" % command.command()

Loading…
Cancel
Save