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 ##############################################################################
9 from __future__ import absolute_import
10 from __future__ import print_function
16 from six.moves import range
18 from yardstick.common import constants as consts
19 from yardstick.common.httpClient import HttpClient
22 class EnvCommand(object):
25 Set of commands to prepare environment
28 def do_influxdb(self, args):
29 data = {'action': 'createInfluxDBContainer'}
30 task_id = self._start_async_task(data)
32 start = '* creating influxDB'
33 self._check_status(task_id, start)
35 def do_grafana(self, args):
36 data = {'action': 'createGrafanaContainer'}
37 task_id = self._start_async_task(data)
39 start = '* creating grafana'
40 self._check_status(task_id, start)
42 def do_prepare(self, args):
43 data = {'action': 'prepareYardstickEnv'}
44 task_id = self._start_async_task(data)
46 start = '* preparing yardstick environment'
47 self._check_status(task_id, start)
49 def _start_async_task(self, data):
50 url = consts.ENV_ACTION_API
51 return HttpClient().post(url, data)['result']['task_id']
53 def _check_status(self, task_id, start):
54 self._print_status(start, '[]\r')
55 url = '{}?task_id={}'.format(consts.ASYNC_TASK_API, task_id)
57 CHECK_STATUS_RETRY = 20
58 CHECK_STATUS_DELAY = 5
60 for retry in range(CHECK_STATUS_RETRY):
61 response = HttpClient().get(url)
62 status = response['status']
67 # wait until the async task finished
68 time.sleep(CHECK_STATUS_DELAY * (retry + 1))
75 self._print_status(start, '[{}]'.format(switcher[status]))
77 print(response['result'])
81 def _print_status(self, s, e):
83 columns = int(os.popen('stty size', 'r').read().split()[1])
84 word = '{}{}{}'.format(s, ' ' * (columns - len(s) - len(e)), e)
85 sys.stdout.write(word)