diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/actions/env.py | 85 | ||||
-rw-r--r-- | api/actions/result.py | 20 | ||||
-rwxr-xr-x | api/api-prepare.sh | 40 | ||||
-rw-r--r-- | api/urls.py | 3 | ||||
-rw-r--r-- | api/utils/common.py | 2 | ||||
-rw-r--r-- | api/views.py | 13 | ||||
-rw-r--r-- | api/yardstick.ini | 2 |
7 files changed, 124 insertions, 41 deletions
diff --git a/api/actions/env.py b/api/actions/env.py new file mode 100644 index 000000000..321649940 --- /dev/null +++ b/api/actions/env.py @@ -0,0 +1,85 @@ +############################################################################## +# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import logging +import threading +import time + +from docker import Client + +from yardstick.common import constants as config +from yardstick.common import utils as yardstick_utils +from api import conf as api_conf +from api.utils import common as common_utils +from api.utils import influx + +logger = logging.getLogger(__name__) + + +def createInfluxDBContainer(args): + try: + container = _create_influxdb_container() + _config_output_file() + thread = threading.Thread(target=_config_influxdb) + thread.start() + return common_utils.result_handler('success', container) + except Exception as e: + message = 'Failed to create influxdb container: %s' % e + return common_utils.error_handler(message) + + +def _create_influxdb_container(): + client = Client(base_url=config.DOCKER_URL) + + ports = [8083, 8086] + port_bindings = {k: k for k in ports} + host_config = client.create_host_config(port_bindings=port_bindings) + + container = client.create_container(image='tutum/influxdb', + ports=ports, + detach=True, + tty=True, + host_config=host_config) + client.start(container) + return container + + +def _config_influxdb(): + time.sleep(20) + try: + client = influx.get_data_db_client() + client.create_user(config.USER, config.PASSWORD, config.DATABASE) + client.create_database(config.DATABASE) + logger.info('Success to config influxDB') + except Exception as e: + logger.debug('Failed to config influxDB: %s', e) + + +def _config_output_file(): + yardstick_utils.makedirs('/etc/yardstick') + with open('/etc/yardstick/yardstick.conf', 'w') as f: + f.write("""\ +[DEFAULT] +debug = True +dispatcher = influxdb + +[dispatcher_file] +file_path = /tmp/yardstick.out + +[dispatcher_http] +timeout = 5 +# target = http://127.0.0.1:8000/results + +[dispatcher_influxdb] +timeout = 5 +target = http://%s:8086 +db_name = yardstick +username = root +password = root +""" + % api_conf.GATEWAY_IP) diff --git a/api/actions/result.py b/api/actions/result.py index 9f606d2cb..10112ac68 100644 --- a/api/actions/result.py +++ b/api/actions/result.py @@ -7,6 +7,8 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## import logging +import uuid +import re from api.utils import influx as influx_utils from api.utils import common as common_utils @@ -19,23 +21,27 @@ def getResult(args): try: measurement = args['measurement'] task_id = args['task_id'] + + if re.search("[^a-zA-Z0-9_-]", measurement): + raise ValueError('invalid measurement parameter') + + uuid.UUID(task_id) except KeyError: - message = 'measurement and task_id must be needed' + message = 'measurement and task_id must be provided' return common_utils.error_handler(message) measurement = conf.TEST_CASE_PRE + measurement - query_sql = "select * from $table where task_id='$task_id'" - param = {'table': 'tasklist', 'task_id': task_id} - data = common_utils.translate_to_str(influx_utils.query(query_sql, param)) + query_template = "select * from %s where task_id='%s'" + query_sql = query_template % ('tasklist', task_id) + data = common_utils.translate_to_str(influx_utils.query(query_sql)) def _unfinished(): return common_utils.result_handler(0, []) def _finished(): - param = {'table': measurement, 'task_id': task_id} - data = common_utils.translate_to_str(influx_utils.query(query_sql, - param)) + query_sql = query_template % (measurement, task_id) + data = common_utils.translate_to_str(influx_utils.query(query_sql)) return common_utils.result_handler(1, data) diff --git a/api/api-prepare.sh b/api/api-prepare.sh index 075d7875c..c05dbb5ff 100755 --- a/api/api-prepare.sh +++ b/api/api-prepare.sh @@ -1,41 +1,19 @@ #!/bin/bash - -# yardstick output config -output_config='/etc/yardstick/yardstick.conf' - -if [[ ! -e "${output_config}" ]];then - gateway_ip=$(ip route | grep default | awk '{print $3}') - echo "${gateway_ip}" - - install -d /etc/yardstick -m 0755 -o root - - cat << EOF > "${output_config}" -[DEFAULT] -debug = True -dispatcher = influxdb - -[dispatcher_file] -file_path = /tmp/yardstick.out - -[dispatcher_http] -timeout = 5 -# target = http://127.0.0.1:8000/results - -[dispatcher_influxdb] -timeout = 5 -target = http://${gateway_ip}:8086 -db_name = yardstick -username = root -password = root -EOF -fi +############################################################################## +# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## # nginx config nginx_config='/etc/nginx/conf.d/yardstick.conf' if [[ ! -e "${nginx_config}" ]];then - cat << EOF >> "${nginx_config}" + cat << EOF > "${nginx_config}" server { listen 5000; server_name localhost; diff --git a/api/urls.py b/api/urls.py index 2a9e72a76..eaaf8b6d1 100644 --- a/api/urls.py +++ b/api/urls.py @@ -12,5 +12,6 @@ from api.utils.common import Url urlpatterns = [ Url('/yardstick/test/action', views.Test, 'test'), - Url('/yardstick/result/action', views.Result, 'result') + Url('/yardstick/result/action', views.Result, 'result'), + Url('/yardstick/env/action', views.Env, 'env') ] diff --git a/api/utils/common.py b/api/utils/common.py index 04a6fe0d6..09cfc04d4 100644 --- a/api/utils/common.py +++ b/api/utils/common.py @@ -34,7 +34,7 @@ def get_command_list(command_list, opts, args): task_args = opts.get('task-args', '') if task_args: - command_list.extend(['--task-args', task_args]) + command_list.extend(['--task-args', str(task_args)]) return command_list diff --git a/api/views.py b/api/views.py index e78389f5a..7357625e0 100644 --- a/api/views.py +++ b/api/views.py @@ -14,6 +14,7 @@ from flask_restful import Resource from api.utils import common as common_utils from api.actions import test as test_action from api.actions import result as result_action +from api.actions import env as env_action logger = logging.getLogger(__name__) @@ -40,3 +41,15 @@ class Result(Resource): return getattr(result_action, action)(args) except AttributeError: return common_utils.error_handler('Wrong action') + + +class Env(Resource): + def post(self): + action = common_utils.translate_to_str(request.json.get('action', '')) + args = common_utils.translate_to_str(request.json.get('args', {})) + logger.debug('Input args is: action: %s, args: %s', action, args) + + try: + return getattr(env_action, action)(args) + except AttributeError: + return common_utils.error_handler('Wrong action') diff --git a/api/yardstick.ini b/api/yardstick.ini index 535022960..01025c2ef 100644 --- a/api/yardstick.ini +++ b/api/yardstick.ini @@ -12,5 +12,5 @@ chmod-socket = 666 callable = app enable-threads = true close-on-exec = 1 -daemonize=/home/kklt/kklt/api/uwsgi.log +daemonize=/home/opnfv/repos/yardstick/api/uwsgi.log socket = /home/opnfv/repos/yardstick/api/yardstick.sock |