Split out report entry-point

Change-Id: I2f2a14487c443fbfbe797f7647e8c8386b045939
This commit is contained in:
Ilya Shakhat 2015-03-11 00:25:03 +03:00 committed by Ilya Shakhat
parent cd32d76c81
commit 6403367518
7 changed files with 105 additions and 95 deletions

View File

@ -130,6 +130,14 @@
# Scenario file name, defaults to env[SHAKER_SCENARIO]. (string value)
#scenario = <None>
# File for output in JSON format, defaults to env[SHAKER_OUTPUT]. (string
# value)
#output = <None>
#
# From shaker.engine.config
#
# Report template in Jinja format (string value)
#report_template = shaker/resources/report_template.jinja2
@ -137,9 +145,9 @@
# report is printed to stdout. (string value)
#report = <None>
# File for output in JSON format, defaults to env[SHAKER_OUTPUT]. (string
# value)
#output = <None>
#
# From shaker.engine.config
#
# File to read test results from, defaults to env[SHAKER_INPUT]. (string value)
#input = <None>

View File

@ -25,6 +25,7 @@ packages =
[entry_points]
console_scripts =
shaker = shaker.engine.server:main
shaker-report = shaker.engine.report:main
shaker-agent = shaker.agent.agent:main
shaker-image-builder = shaker.engine.image_builder:build_image
shaker-cleanup = shaker.engine.image_builder:cleanup

View File

@ -74,8 +74,16 @@ OPENSTACK_OPTS = [
SERVER_OPTS = [
cfg.StrOpt('scenario',
default=utils.env('SHAKER_SCENARIO'),
required=True,
help='Scenario file name, defaults to env[SHAKER_SCENARIO].'),
cfg.StrOpt('output',
default=utils.env('SHAKER_OUTPUT'),
help='File for output in JSON format, '
'defaults to env[SHAKER_OUTPUT].'),
]
REPORT_OPTS = [
cfg.StrOpt('report-template',
default=(utils.env('SHAKER_REPORT_TEMPLATE') or
'shaker/resources/report_template.jinja2'),
@ -84,13 +92,12 @@ SERVER_OPTS = [
default=utils.env('SHAKER_REPORT'),
help='Report file name, defaults to env[SHAKER_REPORT]. '
'If no value provided the report is printed to stdout.'),
]
cfg.StrOpt('output',
default=utils.env('SHAKER_OUTPUT'),
help='File for output in JSON format, '
'defaults to env[SHAKER_OUTPUT].'),
INPUT_OPTS = [
cfg.StrOpt('input',
default=utils.env('SHAKER_INPUT'),
required=True,
help='File to read test results from, '
'defaults to env[SHAKER_INPUT].'),
]
@ -115,5 +122,7 @@ def list_opts():
yield (None, copy.deepcopy(COMMON_OPTS))
yield (None, copy.deepcopy(OPENSTACK_OPTS))
yield (None, copy.deepcopy(SERVER_OPTS))
yield (None, copy.deepcopy(REPORT_OPTS))
yield (None, copy.deepcopy(INPUT_OPTS))
yield (None, copy.deepcopy(AGENT_OPTS))
yield (None, copy.deepcopy(IMAGE_BUILDER_OPTS))

View File

@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging as std_logging
import uuid
from oslo_config import cfg
@ -32,25 +31,8 @@ LOG = logging.getLogger(__name__)
def init():
# init conf and logging
conf = cfg.CONF
opts = config.OPENSTACK_OPTS + config.IMAGE_BUILDER_OPTS
conf.register_cli_opts(opts)
conf.register_opts(opts)
logging.register_options(conf)
logging.set_defaults()
try:
conf(project='shaker')
utils.validate_required_opts(conf, opts)
except cfg.RequiredOptError as e:
print('Error: %s' % e)
conf.print_usage()
exit(1)
logging.setup(conf, 'shaker')
LOG.info('Logging enabled')
conf.log_opt_values(LOG, std_logging.DEBUG)
utils.init_config_and_logging(
config.OPENSTACK_OPTS + config.IMAGE_BUILDER_OPTS)
openstack_client = openstack.OpenStackClient(
username=cfg.CONF.os_username, password=cfg.CONF.os_password,

View File

@ -13,11 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import sys
import jinja2
from oslo_config import cfg
from oslo_log import log as logging
from shaker.engine import config
from shaker.engine import utils
@ -25,9 +28,11 @@ LOG = logging.getLogger(__name__)
def generate_report(report_template, report_filename, data):
LOG.debug('Generating report, template: %s, output: %s',
report_template, report_filename or 'stdout')
template = utils.read_file(report_template)
compiled_template = jinja2.Template(template)
rendered_template = compiled_template.render(dict(report=data))
if report_filename:
@ -38,3 +43,16 @@ def generate_report(report_template, report_filename, data):
fd.write(rendered_template)
fd.close()
LOG.info('Report generated')
def main():
utils.init_config_and_logging(config.REPORT_OPTS + config.INPUT_OPTS)
LOG.debug('Reading JSON data from: %s', cfg.CONF.input)
report_data = json.loads(utils.read_file(cfg.CONF.input))
generate_report(cfg.CONF.report_template, cfg.CONF.report, report_data)
if __name__ == "__main__":
main()

View File

@ -12,10 +12,9 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import copy
import json
import logging as std_logging
import os
import time
import uuid
@ -198,76 +197,48 @@ def execute(execution, agents):
def main():
# init conf and logging
conf = cfg.CONF
opts = config.COMMON_OPTS + config.OPENSTACK_OPTS + config.SERVER_OPTS
conf.register_cli_opts(opts)
conf.register_opts(opts)
logging.register_options(conf)
logging.set_defaults()
utils.init_config_and_logging(
config.COMMON_OPTS + config.OPENSTACK_OPTS + config.SERVER_OPTS +
config.REPORT_OPTS
)
scenario = read_scenario()
deployment = None
agents = {}
result = []
try:
conf(project='shaker')
utils.validate_required_opts(conf, opts)
if not cfg.CONF.scenario and not cfg.CONF.input:
raise cfg.RequiredOptError('One of "scenario" or "input" options '
'must be set')
except cfg.RequiredOptError as e:
print('Error: %s' % e)
conf.print_usage()
exit(1)
deployment = deploy.Deployment(cfg.CONF.os_username,
cfg.CONF.os_password,
cfg.CONF.os_tenant_name,
cfg.CONF.os_auth_url,
cfg.CONF.os_region_name,
cfg.CONF.server_endpoint,
cfg.CONF.external_net,
cfg.CONF.flavor_name,
cfg.CONF.image_name)
logging.setup(conf, 'shaker')
LOG.info('Logging enabled')
conf.log_opt_values(LOG, std_logging.DEBUG)
agents = deployment.deploy(scenario['deployment'],
base_dir=os.path.dirname(cfg.CONF.scenario))
LOG.debug('Deployed agents: %s', agents)
report_data = None
if not agents:
LOG.warning('No agents deployed.')
else:
result = execute(scenario['execution'], agents)
LOG.debug('Result: %s', result)
except Exception as e:
LOG.error('Error while executing scenario: %s', e)
finally:
if deployment:
deployment.cleanup()
if cfg.CONF.scenario:
# run scenario
scenario = read_scenario()
deployment = None
agents = {}
result = []
try:
deployment = deploy.Deployment(cfg.CONF.os_username,
cfg.CONF.os_password,
cfg.CONF.os_tenant_name,
cfg.CONF.os_auth_url,
cfg.CONF.os_region_name,
cfg.CONF.server_endpoint,
cfg.CONF.external_net,
cfg.CONF.flavor_name,
cfg.CONF.image_name)
agents = deployment.deploy(
scenario['deployment'],
base_dir=os.path.dirname(cfg.CONF.scenario))
LOG.debug('Deployed agents: %s', agents)
if not agents:
LOG.warning('No agents deployed.')
else:
result = execute(scenario['execution'], agents)
LOG.debug('Result: %s', result)
except Exception as e:
LOG.error('Error while executing scenario: %s', cfg.CONF.scenario)
finally:
if deployment:
deployment.cleanup()
report_data = dict(scenario=yaml.dump(scenario),
agents=agents.values(),
result=result)
if cfg.CONF.output:
utils.write_file(json.dumps(report_data), cfg.CONF.output)
elif cfg.CONF.input:
# read json results
LOG.debug('Reading JSON data from %s', cfg.CONF.input)
report_data = json.loads(utils.read_file(cfg.CONF.input))
report_data = dict(scenario=yaml.dump(scenario),
agents=agents.values(),
result=result)
if cfg.CONF.output:
utils.write_file(json.dumps(report_data), cfg.CONF.output)
report.generate_report(cfg.CONF.report_template, cfg.CONF.report,
report_data)

View File

@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging as std_logging
import os
import random
@ -44,6 +45,26 @@ def validate_required_opts(conf, opts):
raise cfg.RequiredOptError(opt.name)
def init_config_and_logging(opts):
conf = cfg.CONF
conf.register_cli_opts(opts)
conf.register_opts(opts)
logging.register_options(conf)
logging.set_defaults()
try:
conf(project='shaker')
validate_required_opts(conf, opts)
except cfg.RequiredOptError as e:
print('Error: %s' % e)
conf.print_usage()
exit(1)
logging.setup(conf, 'shaker')
LOG.info('Logging enabled')
conf.log_opt_values(LOG, std_logging.DEBUG)
def read_file(file_name, base_dir=''):
full_path = os.path.normpath(os.path.join(base_dir, file_name))
fd = None