blob: e587a8ebf315a07334080505ab65fa30e91407f9 [file] [log] [blame]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03001import os
2import uuid
3import logging
4import functools
5
6import yaml
7
8try:
9 from petname import Generate as pet_generate
10except ImportError:
11 def pet_generate(x, y):
12 return str(uuid.uuid4())
13
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030014from pretty_yaml import dumps
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030015
16cfg_dict = {}
17
18
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030019class NoData(object):
20 @classmethod
21 def get(cls, name, x):
22 return cls
23
24
25class Config(object):
26 def get(self, name, defval=None):
27 obj = self.__dict__
28 for cname in name.split("."):
29 obj = obj.get(cname, NoData)
30
31 if obj is NoData:
32 return defval
33 return obj
34
35
36cfg = Config()
37cfg.__dict__ = cfg_dict
38
39
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030040def mkdirs_if_unxists(path):
41 if not os.path.exists(path):
42 os.makedirs(path)
43
44
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030045def get_test_files(results_dir):
46 in_var_dir = functools.partial(os.path.join, results_dir)
47
48 res = dict(
49 run_params_file='run_params.yaml',
50 saved_config_file='config.yaml',
51 vm_ids_fname='os_vm_ids',
52 html_report_file='{0}_report.html',
53 text_report_file='report.txt',
54 log_file='log.txt',
55 sensor_storage='sensor_storage',
56 nodes_report_file='nodes.yaml',
57 results='results',
58 hwinfo_directory='hwinfo',
59 hwreport_fname='hwinfo.txt',
60 raw_results='raw_results.yaml')
61
62 res = dict((k, in_var_dir(v)) for k, v in res.items())
63 res['var_dir'] = results_dir
64 return res
65
66
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030067def load_config(file_name, explicit_folder=None):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030068 cfg_dict.update(yaml.load(open(file_name).read()))
69
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030070 var_dir = cfg_dict.get('internal', {}).get('var_dir_root', '/tmp')
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030071 run_uuid = None
72 if explicit_folder is None:
73 for i in range(10):
74 run_uuid = pet_generate(2, "_")
75 results_dir = os.path.join(var_dir, run_uuid)
76 if not os.path.exists(results_dir):
77 break
78 else:
79 run_uuid = str(uuid.uuid4())
80 results_dir = os.path.join(var_dir, run_uuid)
koder aka kdanilov4a510ee2015-04-21 18:50:42 +030081 cfg_dict['run_uuid'] = run_uuid.replace('_', '-')
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030082 else:
83 results_dir = explicit_folder
84
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030085 cfg_dict.update(get_test_files(results_dir))
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030086 mkdirs_if_unxists(cfg_dict['var_dir'])
87
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030088 if explicit_folder is not None:
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030089 with open(cfg_dict['run_params_file']) as fd:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030090 cfg_dict['run_uuid'] = yaml.load(fd)['run_uuid']
91 run_uuid = cfg_dict['run_uuid']
92 else:
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030093 with open(cfg_dict['run_params_file'], 'w') as fd:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030094 fd.write(dumps({'run_uuid': cfg_dict['run_uuid']}))
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030095
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030096 mkdirs_if_unxists(cfg_dict['sensor_storage'])
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030097
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030098 if 'sensors_remote_path' not in cfg_dict:
99 cfg_dict['sensors_remote_path'] = '/tmp/sensors'
100
koder aka kdanilovafd98742015-04-24 01:27:22 +0300101 testnode_log_root = cfg_dict.get('testnode_log_root', '/var/wally')
102 testnode_log_dir = os.path.join(testnode_log_root, "{0}/{{name}}")
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300103 cfg_dict['default_test_local_folder'] = \
koder aka kdanilovafd98742015-04-24 01:27:22 +0300104 testnode_log_dir.format(cfg_dict['run_uuid'])
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300105
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300106 mkdirs_if_unxists(cfg_dict['results'])
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300107 mkdirs_if_unxists(cfg_dict['hwinfo_directory'])
108
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300109
110def color_me(color):
111 RESET_SEQ = "\033[0m"
112 COLOR_SEQ = "\033[1;%dm"
113
114 color_seq = COLOR_SEQ % (30 + color)
115
116 def closure(msg):
117 return color_seq + msg + RESET_SEQ
118 return closure
119
120
121class ColoredFormatter(logging.Formatter):
122 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
123
124 colors = {
125 'WARNING': color_me(YELLOW),
126 'DEBUG': color_me(BLUE),
127 'CRITICAL': color_me(YELLOW),
128 'ERROR': color_me(RED)
129 }
130
131 def __init__(self, msg, use_color=True, datefmt=None):
132 logging.Formatter.__init__(self, msg, datefmt=datefmt)
133 self.use_color = use_color
134
135 def format(self, record):
136 orig = record.__dict__
137 record.__dict__ = record.__dict__.copy()
138 levelname = record.levelname
139
140 prn_name = levelname + ' ' * (8 - len(levelname))
141 if levelname in self.colors:
142 record.levelname = self.colors[levelname](prn_name)
143 else:
144 record.levelname = prn_name
145
koder aka kdanilov6b1341a2015-04-21 22:44:21 +0300146 # super doesn't work here in 2.6 O_o
147 res = logging.Formatter.format(self, record)
148
149 # res = super(ColoredFormatter, self).format(record)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300150
151 # restore record, as it will be used by other formatters
152 record.__dict__ = orig
153 return res
154
155
156def setup_loggers(def_level=logging.DEBUG, log_fname=None):
157 logger = logging.getLogger('wally')
158 logger.setLevel(logging.DEBUG)
159 sh = logging.StreamHandler()
160 sh.setLevel(def_level)
161
koder aka kdanilov168f6092015-04-19 02:33:38 +0300162 log_format = '%(asctime)s - %(levelname)s - %(name)-15s - %(message)s'
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300163 colored_formatter = ColoredFormatter(log_format, datefmt="%H:%M:%S")
164
165 sh.setFormatter(colored_formatter)
166 logger.addHandler(sh)
167
168 logger_api = logging.getLogger("wally.fuel_api")
169
170 if log_fname is not None:
171 fh = logging.FileHandler(log_fname)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300172 log_format = '%(asctime)s - %(levelname)8s - %(name)-15s - %(message)s'
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300173 formatter = logging.Formatter(log_format, datefmt="%H:%M:%S")
174 fh.setFormatter(formatter)
175 fh.setLevel(logging.DEBUG)
176 logger.addHandler(fh)
177 logger_api.addHandler(fh)
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300178 else:
179 fh = None
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300180
181 logger_api.addHandler(sh)
182 logger_api.setLevel(logging.WARNING)
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300183
184 logger = logging.getLogger('paramiko')
185 logger.setLevel(logging.WARNING)
koder aka kdanilovafd98742015-04-24 01:27:22 +0300186 # logger.addHandler(sh)
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300187 if fh is not None:
188 logger.addHandler(fh)