1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
17 from docker import Client
19 from yardstick.common import constants as config
20 from yardstick.common import utils as yardstick_utils
21 from yardstick.common.httpClient import HttpClient
22 from api import conf as api_conf
23 from api.utils import influx
24 from api.utils.common import result_handler
26 logger = logging.getLogger(__name__)
29 def createGrafanaContainer(args):
30 thread = threading.Thread(target=_create_grafana)
32 return result_handler('success', [])
35 def _create_grafana():
36 client = Client(base_url=config.DOCKER_URL)
39 if not _check_image_exist(client, '%s:%s' % (config.GRAFANA_IMAGE,
40 config.GRAFANA_TAGS)):
41 client.pull(config.GRAFANA_IMAGE, config.GRAFANA_TAGS)
43 _create_grafana_container(client)
50 except Exception as e:
51 logger.debug('Error: %s', e)
54 def _create_dashboard():
55 url = 'http://admin:admin@%s:3000/api/dashboards/db' % api_conf.GATEWAY_IP
56 with open('../dashboard/ping_dashboard.json') as dashboard_json:
57 data = json.load(dashboard_json)
58 HttpClient().post(url, data)
61 def _create_data_source():
62 url = 'http://admin:admin@%s:3000/api/datasources' % api_conf.GATEWAY_IP
67 "url": "http://%s:8086" % api_conf.GATEWAY_IP,
70 "database": "yardstick",
72 "basicAuthUser": "admin",
73 "basicAuthPassword": "admin",
76 HttpClient().post(url, data)
79 def _create_grafana_container(client):
81 port_bindings = {k: k for k in ports}
82 host_config = client.create_host_config(port_bindings=port_bindings)
84 container = client.create_container(image='%s:%s' % (config.GRAFANA_IMAGE,
89 host_config=host_config)
90 client.start(container)
93 def _check_image_exist(client, t):
94 return any(t in a['RepoTags'][0] for a in client.images() if a['RepoTags'])
97 def createInfluxDBContainer(args):
98 thread = threading.Thread(target=_create_influxdb)
100 return result_handler('success', [])
103 def _create_influxdb():
104 client = Client(base_url=config.DOCKER_URL)
107 _config_output_file()
109 if not _check_image_exist(client, '%s:%s' % (config.INFLUXDB_IMAGE,
110 config.INFLUXDB_TAG)):
111 client.pull(config.INFLUXDB_IMAGE, tag=config.INFLUXDB_TAG)
113 _create_influxdb_container(client)
118 except Exception as e:
119 logger.debug('Error: %s', e)
122 def _create_influxdb_container(client):
125 port_bindings = {k: k for k in ports}
126 host_config = client.create_host_config(port_bindings=port_bindings)
128 container = client.create_container(image='%s:%s' % (config.INFLUXDB_IMAGE,
129 config.INFLUXDB_TAG),
133 host_config=host_config)
134 client.start(container)
137 def _config_influxdb():
139 client = influx.get_data_db_client()
140 client.create_user(config.USER, config.PASSWORD, config.DATABASE)
141 client.create_database(config.DATABASE)
142 logger.info('Success to config influxDB')
143 except Exception as e:
144 logger.debug('Failed to config influxDB: %s', e)
147 def _config_output_file():
148 yardstick_utils.makedirs(config.YARDSTICK_CONFIG_DIR)
149 with open(config.YARDSTICK_CONFIG_FILE, 'w') as f:
153 dispatcher = influxdb
156 file_path = /tmp/yardstick.out
160 # target = http://127.0.0.1:8000/results
162 [dispatcher_influxdb]
164 target = http://%s:8086
169 % api_conf.GATEWAY_IP)
172 def prepareYardstickEnv(args):
173 thread = threading.Thread(target=_prepare_env_daemon)
175 return result_handler('success', [])
178 def _prepare_env_daemon():
180 installer_ip = os.environ.get('INSTALLER_IP', 'undefined')
181 installer_type = os.environ.get('INSTALLER_TYPE', 'undefined')
183 _check_variables(installer_ip, installer_type)
185 _create_directories()
187 rc_file = config.OPENSTACK_RC_FILE
189 _get_remote_rc_file(rc_file, installer_ip, installer_type)
191 _source_file(rc_file)
193 _append_external_network(rc_file)
195 # update the external_network
196 _source_file(rc_file)
203 def _check_variables(installer_ip, installer_type):
205 if installer_ip == 'undefined':
206 raise SystemExit('Missing INSTALLER_IP')
208 if installer_type == 'undefined':
209 raise SystemExit('Missing INSTALLER_TYPE')
210 elif installer_type not in config.INSTALLERS:
211 raise SystemExit('INSTALLER_TYPE is not correct')
214 def _create_directories():
215 yardstick_utils.makedirs(config.YARDSTICK_CONFIG_DIR)
218 def _source_file(rc_file):
219 yardstick_utils.source_env(rc_file)
222 def _get_remote_rc_file(rc_file, installer_ip, installer_type):
224 os_fetch_script = os.path.join(config.RELENG_DIR, config.OS_FETCH_SCRIPT)
227 cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type,
229 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
232 if p.returncode != 0:
233 logger.debug('Failed to fetch credentials from installer')
235 if e.errno != errno.EEXIST:
239 def _append_external_network(rc_file):
240 neutron_client = yardstick_utils.get_neutron_client()
241 networks = neutron_client.list_networks()['networks']
243 ext_network = next(n['name'] for n in networks if n['router:external'])
244 except StopIteration:
245 logger.warning("Can't find external network")
247 cmd = 'export EXTERNAL_NETWORK=%s' % ext_network
249 with open(rc_file, 'a') as f:
252 if e.errno != errno.EEXIST:
257 cmd = [config.CLEAN_IMAGES_SCRIPT]
258 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
259 cwd=config.YARDSTICK_REPOS_DIR)
260 output = p.communicate()[0]
261 logger.debug('The result is: %s', output)
265 cmd = [config.LOAD_IMAGES_SCRIPT]
266 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
267 cwd=config.YARDSTICK_REPOS_DIR)
268 output = p.communicate()[0]
269 logger.debug('The result is: %s', output)