Add subcommands to the client

* introduces subcommands to the client
* adds a new subcommand to show a specific fractal
* prepares subcommands delete, download and list

Change-Id: I42bbf76f3cad1b0e3415de8fa7b52a229405e8e3
This commit is contained in:
Christian Berendt 2015-04-01 17:02:23 +02:00
parent f9214999fc
commit 3f19cb8460
2 changed files with 132 additions and 58 deletions

189
bin/faafo
View File

@ -19,6 +19,7 @@ import uuid
from oslo_config import cfg
from oslo_log import log
from prettytable import PrettyTable
import requests
from faafo import version
@ -27,58 +28,18 @@ from faafo import version
LOG = log.getLogger('faafo.client')
CONF = cfg.CONF
producer_cli_opts = [
cfg.IntOpt("max-height", default=1024,
help="The maximum height of the generate image."),
cfg.IntOpt("max-width", default=1024,
help="The maximum width of the generated image."),
cfg.FloatOpt("max-xa", default=-4.0,
help="The maximum value for the parameter 'xa'."),
cfg.FloatOpt("max-xb", default=4.0,
help="The maximum value for the parameter 'xb'."),
cfg.FloatOpt("max-ya", default=-3,
help="The maximum value for the parameter 'ya'."),
cfg.FloatOpt("max-yb", default=3,
help="The maximum value for the parameter 'yb'."),
cfg.IntOpt("max-iterations", default=512,
help="The maximum number of iterations."),
cfg.IntOpt("min-height", default=256,
help="The minimum height of the generate image."),
cfg.IntOpt("min-width", default=256,
help="The minimum width of the generated image."),
cfg.FloatOpt("min-xa", default=-1.0,
help="The minimum value for the parameter 'xa'."),
cfg.FloatOpt("min-xb", default=1.0,
help="The minimum value for the parameter 'xb'."),
cfg.FloatOpt("min-ya", default=-0.5,
help="The minimum value for the parameter 'ya'."),
cfg.FloatOpt("min-yb", default=0.5,
help="The minimum value for the parameter 'yb'."),
cfg.IntOpt("min-iterations", default=128,
help="The minimum number of iterations."),
cfg.IntOpt("min-tasks", default=1,
help="The minimum number of generated tasks."),
cfg.IntOpt("max-tasks", default=10,
help="The maximum number of generated tasks."),
cfg.StrOpt('endpoint-url',
default='http://localhost:5000',
help='API connection URL')
]
CONF.register_cli_opts(producer_cli_opts)
def get_random_task():
random.seed()
width = random.randint(CONF.min_width, CONF.max_width)
height = random.randint(CONF.min_height, CONF.max_height)
iterations = random.randint(CONF.min_iterations,
CONF.max_iterations)
xa = random.uniform(CONF.min_xa, CONF.max_xa)
xb = random.uniform(CONF.min_xb, CONF.max_xb)
ya = random.uniform(CONF.min_ya, CONF.max_ya)
yb = random.uniform(CONF.min_yb, CONF.max_yb)
width = random.randint(CONF.command.min_width, CONF.command.max_width)
height = random.randint(CONF.command.min_height, CONF.command.max_height)
iterations = random.randint(CONF.command.min_iterations,
CONF.command.max_iterations)
xa = random.uniform(CONF.command.min_xa, CONF.command.max_xa)
xb = random.uniform(CONF.command.min_xb, CONF.command.max_xb)
ya = random.uniform(CONF.command.min_ya, CONF.command.max_ya)
yb = random.uniform(CONF.command.min_yb, CONF.command.max_yb)
task = {
'uuid': str(uuid.uuid4()),
@ -93,6 +54,127 @@ def get_random_task():
return task
def do_get_fractal():
LOG.error("command 'download' not yet implemented")
def do_show_fractal():
LOG.info("showing fractal %s" % CONF.command.uuid)
result = requests.get("%s/v1/fractal/%s" %
(CONF.endpoint_url, CONF.command.uuid))
if result.status_code == 200:
data = json.loads(result.text)
output = PrettyTable(["Parameter", "Value"])
output.align["Parameter"] = "l"
output.align["Value"] = "l"
output.add_row(["uuid", data['uuid']])
output.add_row(["duration", "%f seconds" % data['duration']])
output.add_row(["dimensions", "%d x %d pixels" %
(data['width'], data['height'])])
output.add_row(["iterations", data['iterations']])
output.add_row(["xa", data['xa']])
output.add_row(["xb", data['xb']])
output.add_row(["ya", data['ya']])
output.add_row(["yb", data['yb']])
output.add_row(["size", "%d bytes" % data['size']])
output.add_row(["checksum", data['checksum']])
print(output)
else:
LOG.error("fractal '%s' not found" % CONF.command.uuid)
def do_list_fractals():
LOG.error("command 'list' not yet implemented")
def do_delete_fractal():
LOG.info("deleting fractal %s" % CONF.command.uuid)
result = requests.delete("%s/v1/fractal/%s" %
(CONF.endpoint_url, CONF.command.uuid))
LOG.debug("result: %s" %result)
def do_create_fractal():
random.seed()
number = random.randint(CONF.command.min_tasks, CONF.command.max_tasks)
LOG.info("generating %d task(s)" % number)
for i in xrange(0, number):
task = get_random_task()
LOG.debug("created task %s" % task)
# NOTE(berendt): only necessary when using requests < 2.4.2
headers = {'Content-type': 'application/json',
'Accept': 'text/plain'}
requests.post("%s/v1/fractal" % CONF.endpoint_url,
json.dumps(task), headers=headers)
def add_command_parsers(subparsers):
parser = subparsers.add_parser('create')
parser.set_defaults(func=do_create_fractal)
parser.add_argument("--max-height", default=1024,
help="The maximum height of the generate image.")
parser.add_argument("--max-width", default=1024,
help="The maximum width of the generated image.")
parser.add_argument("--max-xa", default=-4.0,
help="The maximum value for the parameter 'xa'.")
parser.add_argument("--max-xb", default=4.0,
help="The maximum value for the parameter 'xb'.")
parser.add_argument("--max-ya", default=-3,
help="The maximum value for the parameter 'ya'.")
parser.add_argument("--max-yb", default=3,
help="The maximum value for the parameter 'yb'.")
parser.add_argument("--max-iterations", default=512,
help="The maximum number of iterations.")
parser.add_argument("--min-height", default=256,
help="The minimum height of the generate image.")
parser.add_argument("--min-width", default=256,
help="The minimum width of the generated image.")
parser.add_argument("--min-xa", default=-1.0,
help="The minimum value for the parameter 'xa'.")
parser.add_argument("--min-xb", default=1.0,
help="The minimum value for the parameter 'xb'.")
parser.add_argument("--min-ya", default=-0.5,
help="The minimum value for the parameter 'ya'.")
parser.add_argument("--min-yb", default=0.5,
help="The minimum value for the parameter 'yb'.")
parser.add_argument("--min-iterations", default=128,
help="The minimum number of iterations.")
parser.add_argument("--min-tasks", default=1,
help="The minimum number of generated tasks.")
parser.add_argument("--max-tasks", default=10,
help="The maximum number of generated tasks.")
parser = subparsers.add_parser('delete')
parser.set_defaults(func=do_delete_fractal)
parser.add_argument("uuid", help="Fractal to delete.")
parser = subparsers.add_parser('show')
parser.set_defaults(func=do_show_fractal)
parser.add_argument("uuid", help="Fractal to show.")
parser = subparsers.add_parser('get')
parser.set_defaults(func=do_get_fractal)
parser.add_argument("uuid", help="Fractal to download.")
parser = subparsers.add_parser('list')
parser.set_defaults(func=do_list_fractals)
client_commands = cfg.SubCommandOpt('command', title='Commands',
help='Show available commands.',
handler=add_command_parsers)
CONF.register_cli_opts([client_commands])
client_cli_opts = [
cfg.StrOpt('endpoint-url',
default='http://localhost:5000',
help='API connection URL')
]
CONF.register_cli_opts(client_cli_opts)
if __name__ == '__main__':
log.register_options(CONF)
log.set_defaults()
@ -103,13 +185,4 @@ if __name__ == '__main__':
log.setup(CONF, 'client',
version=version.version_info.version_string())
random.seed()
number = random.randint(CONF.min_tasks, CONF.max_tasks)
LOG.info("generating %d task(s)" % number)
for i in xrange(0, number):
task = get_random_task()
# NOTE(berendt): only necessary when using requests < 2.4.2
headers = {'Content-type': 'application/json',
'Accept': 'text/plain'}
requests.post("%s/v1/fractal" % CONF.endpoint_url,
json.dumps(task), headers=headers)
CONF.command.func()

View File

@ -12,6 +12,7 @@ oslo.config>=1.9.3,<1.10.0 # Apache-2.0
oslo.log>=1.0.0,<1.1.0 # Apache-2.0
oslo.messaging>=1.8.0,<1.9.0 # Apache-2.0
glance_store>=0.3.0 # Apache-2.0
PrettyTable>=0.7,<0.8
# dependencies of the glance.store library
python-swiftclient>=2.2.0