Merge "constants: cache YAML config values"
[yardstick.git] / yardstick / common / constants.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
10 import os
11 from functools import reduce
12
13 import pkg_resources
14
15 from yardstick.common.utils import parse_yaml
16
17 dirname = os.path.dirname
18 abspath = os.path.abspath
19 join = os.path.join
20 sep = os.path.sep
21
22 CONF = {}
23
24
25 def get_param(key, default=''):
26
27     # we have to defer this to runtime so that we can mock os.environ.get in unittests
28     conf_file = os.environ.get('CONF_FILE', '/etc/yardstick/yardstick.yaml')
29
30     # don't re-parse yaml for each lookup
31     if not CONF:
32         CONF.update(parse_yaml(conf_file))
33     try:
34         return reduce(lambda a, b: a[b], key.split('.'), CONF)
35     except KeyError:
36         if not default:
37             raise
38         return default
39
40
41 try:
42     SERVER_IP = get_param('api.server_ip')
43 except KeyError:
44     try:
45         from pyroute2 import IPDB
46     except ImportError:
47         SERVER_IP = '172.17.0.1'
48     else:
49         with IPDB() as ip:
50             try:
51                 SERVER_IP = ip.routes['default'].gateway
52             except KeyError:
53                 # during unittests ip.routes['default'] can be invalid
54                 SERVER_IP = '127.0.0.1'
55
56 if not SERVER_IP:
57     SERVER_IP = '127.0.0.1'
58
59
60 # dir
61 CONF_DIR = get_param('dir.conf', '/etc/yardstick')
62 REPOS_DIR = get_param('dir.repos', '/home/opnfv/repos/yardstick')
63 RELENG_DIR = get_param('dir.releng', '/home/opnfv/repos/releng')
64 LOG_DIR = get_param('dir.log', '/tmp/yardstick/')
65 YARDSTICK_ROOT_PATH = dirname(
66     dirname(abspath(pkg_resources.resource_filename(__name__, "")))) + sep
67 CONF_SAMPLE_DIR = join(REPOS_DIR, 'etc/yardstick/')
68 ANSIBLE_DIR = join(REPOS_DIR, 'ansible')
69 SAMPLE_CASE_DIR = join(REPOS_DIR, 'samples')
70 TESTCASE_DIR = join(YARDSTICK_ROOT_PATH, 'tests/opnfv/test_cases/')
71 TESTSUITE_DIR = join(YARDSTICK_ROOT_PATH, 'tests/opnfv/test_suites/')
72 DOCS_DIR = join(REPOS_DIR, 'docs/testing/user/userguide/')
73 OPENSTACK_CONF_DIR = '/etc/openstack'
74
75 # file
76 OPENRC = get_param('file.openrc', '/etc/yardstick/openstack.creds')
77 ETC_HOSTS = get_param('file.etc_hosts', '/etc/hosts')
78 CONF_FILE = join(CONF_DIR, 'yardstick.conf')
79 POD_FILE = join(CONF_DIR, 'pod.yaml')
80 CLOUDS_CONF = join(OPENSTACK_CONF_DIR, 'clouds.yml')
81 K8S_CONF_FILE = join(CONF_DIR, 'admin.conf')
82 CONF_SAMPLE_FILE = join(CONF_SAMPLE_DIR, 'yardstick.conf.sample')
83 FETCH_SCRIPT = get_param('file.fetch_script', 'utils/fetch_os_creds.sh')
84 FETCH_SCRIPT = join(RELENG_DIR, FETCH_SCRIPT)
85 CLEAN_IMAGES_SCRIPT = get_param('file.clean_image_script',
86                                 'tests/ci/clean_images.sh')
87 CLEAN_IMAGES_SCRIPT = join(REPOS_DIR, CLEAN_IMAGES_SCRIPT)
88 LOAD_IMAGES_SCRIPT = get_param('file.load_image_script',
89                                'tests/ci/load_images.sh')
90 LOAD_IMAGES_SCRIPT = join(REPOS_DIR, LOAD_IMAGES_SCRIPT)
91 DEFAULT_OUTPUT_FILE = get_param('file.output_file', '/tmp/yardstick.out')
92 DEFAULT_HTML_FILE = get_param('file.html_file', '/tmp/yardstick.htm')
93
94 # influxDB
95 INFLUXDB_IP = get_param('influxdb.ip', SERVER_IP)
96 INFLUXDB_PORT = get_param('influxdb.port', 8086)
97 INFLUXDB_USER = get_param('influxdb.username', 'root')
98 INFLUXDB_PASS = get_param('influxdb.password', 'root')
99 INFLUXDB_DB_NAME = get_param('influxdb.db_name', 'yardstick')
100 INFLUXDB_IMAGE = get_param('influxdb.image', 'tutum/influxdb')
101 INFLUXDB_TAG = get_param('influxdb.tag', '0.13')
102 INFLUXDB_DASHBOARD_PORT = 8083
103
104 # grafana
105 GRAFANA_IP = get_param('grafana.ip', SERVER_IP)
106 GRAFANA_PORT = get_param('grafana.port', 3000)
107 GRAFANA_USER = get_param('grafana.username', 'admin')
108 GRAFANA_PASS = get_param('grafana.password', 'admin')
109 GRAFANA_IMAGE = get_param('grafana.image', 'grafana/grafana')
110 GRAFANA_TAG = get_param('grafana.tag', '3.1.1')
111 GRAFANA_MAPPING_PORT = 1948
112
113 # api
114 API_PORT = 5000
115 DOCKER_URL = 'unix://var/run/docker.sock'
116 INSTALLERS = ['apex', 'compass', 'fuel', 'joid']
117 SQLITE = 'sqlite:////tmp/yardstick.db'
118
119 API_SUCCESS = 1
120 API_ERROR = 2
121 TASK_NOT_DONE = 0
122 TASK_DONE = 1
123 TASK_FAILED = 2
124
125 BASE_URL = 'http://localhost:5000'
126 ENV_ACTION_API = BASE_URL + '/yardstick/env/action'
127 ASYNC_TASK_API = BASE_URL + '/yardstick/asynctask'
128
129 # general
130 TESTCASE_PRE = 'opnfv_yardstick_'
131 TESTSUITE_PRE = 'opnfv_'