Merge "Make VM machine type as a user configuration"
[yardstick.git] / yardstick / cmd / commands / env.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 from __future__ import print_function
11
12 import os
13 import sys
14 import time
15
16 from six.moves import range
17
18 from yardstick.common import constants as consts
19 from yardstick.common.httpClient import HttpClient
20
21
22 class EnvCommand(object):
23     """
24
25         Set of commands to prepare environment
26     """
27
28     def do_influxdb(self, args):
29         data = {'action': 'create_influxdb'}
30         task_id = self._start_async_task(data)
31
32         start = '* creating influxDB'
33         self._check_status(task_id, start)
34
35     def do_grafana(self, args):
36         data = {'action': 'create_grafana'}
37         task_id = self._start_async_task(data)
38
39         start = '* creating grafana'
40         self._check_status(task_id, start)
41
42     def do_prepare(self, args):
43         data = {'action': 'prepare_env'}
44         task_id = self._start_async_task(data)
45
46         start = '* preparing yardstick environment'
47         self._check_status(task_id, start)
48
49     def _start_async_task(self, data):
50         url = consts.ENV_ACTION_API
51         return HttpClient().post(url, data)['result']['task_id']
52
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)
56
57         CHECK_STATUS_RETRY = 20
58         CHECK_STATUS_DELAY = 5
59
60         for retry in range(CHECK_STATUS_RETRY):
61             response = HttpClient().get(url)
62             status = response['status']
63
64             if status:
65                 break
66
67             # wait until the async task finished
68             time.sleep(CHECK_STATUS_DELAY * (retry + 1))
69
70         switcher = {
71             0: 'Timeout',
72             1: 'Finished',
73             2: 'Error'
74         }
75         self._print_status(start, '[{}]'.format(switcher[status]))
76         if status == 2:
77             print(response['result'])
78             sys.stdout.flush()
79         return status
80
81     def _print_status(self, s, e):
82         try:
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)
86             sys.stdout.flush()
87         except IndexError:
88             pass