From cce6fa7f1bca039bedc7484aff6f7862d9074053 Mon Sep 17 00:00:00 2001 From: chenjiankun Date: Fri, 14 Jul 2017 08:26:53 +0000 Subject: yardstick env influxdb/grafana cmd support centos JIRA: YARDSTICK-714 Currently yardstick env influxdb/grafana command do not support centos. Because we use the gateway ip to get the service of influxdb and grafana. But in centos, we can not access influxdb/grafana service via gateway ip. In this patch, I use docker inspect to get the ip of influxdb and grafana. So these command can support centos. Change-Id: I4599cbbd0fe43de9cf08e5d9e74d31edbb742998 Signed-off-by: chenjiankun --- api/resources/env_action.py | 49 +++++++++++++++++++++++++++++-------------- yardstick/common/constants.py | 2 ++ yardstick/common/utils.py | 9 +++++++- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/api/resources/env_action.py b/api/resources/env_action.py index 7bfaf27a7..50c737fa3 100644 --- a/api/resources/env_action.py +++ b/api/resources/env_action.py @@ -53,13 +53,16 @@ def _create_grafana(task_id): if not _check_image_exist(client, image): client.pull(consts.GRAFANA_IMAGE, consts.GRAFANA_TAG) - _create_grafana_container(client) + container = _create_grafana_container(client) time.sleep(5) - _create_data_source() + container = client.inspect_container(container['Id']) + ip = container['NetworkSettings']['Networks']['bridge']['IPAddress'] - _create_dashboard() + _create_data_source(ip) + + _create_dashboard(ip) _update_task_status(task_id) except Exception as e: @@ -67,8 +70,8 @@ def _create_grafana(task_id): logger.exception('Error: %s', e) -def _create_dashboard(): - url = 'http://admin:admin@%s:3000/api/dashboards/db' % consts.GRAFANA_IP +def _create_dashboard(ip): + url = 'http://admin:admin@{}:{}/api/dashboards/db'.format(ip, consts.GRAFANA_PORT) path = os.path.join(consts.REPOS_DIR, 'dashboard', '*dashboard.json') for i in sorted(glob.iglob(path)): @@ -77,13 +80,21 @@ def _create_dashboard(): HttpClient().post(url, data) -def _create_data_source(): - url = 'http://admin:admin@%s:3000/api/datasources' % consts.GRAFANA_IP +def _create_data_source(ip): + url = 'http://admin:admin@{}:{}/api/datasources'.format(ip, consts.GRAFANA_PORT) + + influx_conf = yardstick_utils.parse_ini_file(consts.CONF_FILE) + try: + influx_url = influx_conf['dispatcher_influxdb']['target'] + except KeyError: + logger.exception('influxdb url not set in yardstick.conf') + raise + data = { "name": "yardstick", "type": "influxdb", "access": "proxy", - "url": "http://%s:8086" % consts.INFLUXDB_IP, + "url": influx_url, "password": "root", "user": "root", "database": "yardstick", @@ -96,8 +107,8 @@ def _create_data_source(): def _create_grafana_container(client): - ports = [3000] - port_bindings = {k: k for k in ports} + ports = [consts.GRAFANA_PORT] + port_bindings = {consts.GRAFANA_PORT: consts.GRAFANA_MAPPING_PORT} host_config = client.create_host_config(port_bindings=port_bindings) container = client.create_container(image='%s:%s' % (consts.GRAFANA_IMAGE, @@ -107,6 +118,7 @@ def _create_grafana_container(client): tty=True, host_config=host_config) client.start(container) + return container def _check_image_exist(client, t): @@ -128,16 +140,20 @@ def _create_influxdb(task_id): client = Client(base_url=consts.DOCKER_URL) try: - _change_output_to_influxdb() - if not _check_image_exist(client, '%s:%s' % (consts.INFLUXDB_IMAGE, consts.INFLUXDB_TAG)): client.pull(consts.INFLUXDB_IMAGE, tag=consts.INFLUXDB_TAG) - _create_influxdb_container(client) + container = _create_influxdb_container(client) time.sleep(5) + container = client.inspect_container(container['Id']) + ip = container['NetworkSettings']['Networks']['bridge']['IPAddress'] + + logger.info('Changing output to influxdb') + _change_output_to_influxdb(ip) + _config_influxdb() _update_task_status(task_id) @@ -148,7 +164,7 @@ def _create_influxdb(task_id): def _create_influxdb_container(client): - ports = [8083, 8086] + ports = [consts.INFLUXDB_DASHBOARD_PORT, consts.INFLUXDB_PORT] port_bindings = {k: k for k in ports} host_config = client.create_host_config(port_bindings=port_bindings) @@ -159,6 +175,7 @@ def _create_influxdb_container(client): tty=True, host_config=host_config) client.start(container) + return container def _config_influxdb(): @@ -173,7 +190,7 @@ def _config_influxdb(): logger.debug('Failed to config influxDB: %s', e) -def _change_output_to_influxdb(): +def _change_output_to_influxdb(ip): yardstick_utils.makedirs(consts.CONF_DIR) parser = configparser.ConfigParser() @@ -181,7 +198,7 @@ def _change_output_to_influxdb(): parser.set('DEFAULT', 'dispatcher', 'influxdb') parser.set('dispatcher_influxdb', 'target', - 'http://%s:8086' % consts.INFLUXDB_IP) + 'http://{}:{}'.format(ip, consts.INFLUXDB_PORT)) with open(consts.CONF_FILE, 'w') as f: parser.write(f) diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py index cb98c356d..631cf81ba 100644 --- a/yardstick/common/constants.py +++ b/yardstick/common/constants.py @@ -63,6 +63,7 @@ INFLUXDB_PASS = get_param('influxdb.password', 'root') INFLUXDB_DB_NAME = get_param('influxdb.db_name', 'yardstick') INFLUXDB_IMAGE = get_param('influxdb.image', 'tutum/influxdb') INFLUXDB_TAG = get_param('influxdb.tag', '0.13') +INFLUXDB_DASHBOARD_PORT = 8083 # grafana GRAFANA_IP = get_param('grafana.ip', SERVER_IP) @@ -71,6 +72,7 @@ GRAFANA_USER = get_param('grafana.username', 'admin') GRAFANA_PASS = get_param('grafana.password', 'admin') GRAFANA_IMAGE = get_param('grafana.image', 'grafana/grafana') GRAFANA_TAG = get_param('grafana.tag', '3.1.1') +GRAFANA_MAPPING_PORT = 1948 # api DOCKER_URL = 'unix://var/run/docker.sock' diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index 7035f3374..1d7359f0e 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -155,7 +155,14 @@ def write_file(path, data, mode='w'): def parse_ini_file(path): parser = configparser.ConfigParser() - parser.read(path) + try: + files = parser.read(path) + except configparser.MissingSectionHeaderError: + logger.exception('invalid file type') + raise + else: + if not files: + raise RuntimeError('file not exist') try: default = {k: v for k, v in parser.items('DEFAULT')} -- cgit 1.2.3-korg