blob: d8b70855e1b14e130a391baf178b096607da6c69 [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
14
15cfg_dict = {}
16
17
18def mkdirs_if_unxists(path):
19 if not os.path.exists(path):
20 os.makedirs(path)
21
22
23def load_config(file_name, explicit_folder=None):
24 first_load = len(cfg_dict) == 0
25 cfg_dict.update(yaml.load(open(file_name).read()))
26
27 if first_load:
28 var_dir = cfg_dict.get('internal', {}).get('var_dir_root', '/tmp')
29
30 run_uuid = None
31 if explicit_folder is None:
32 for i in range(10):
33 run_uuid = pet_generate(2, "_")
34 results_dir = os.path.join(var_dir, run_uuid)
35 if not os.path.exists(results_dir):
36 break
37 else:
38 run_uuid = str(uuid.uuid4())
39 results_dir = os.path.join(var_dir, run_uuid)
koder aka kdanilov4a510ee2015-04-21 18:50:42 +030040 cfg_dict['run_uuid'] = run_uuid.replace('_', '-')
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030041 else:
42 results_dir = explicit_folder
43
44 cfg_dict['var_dir'] = results_dir
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030045 mkdirs_if_unxists(cfg_dict['var_dir'])
46
47 in_var_dir = functools.partial(os.path.join, cfg_dict['var_dir'])
48
49 cfg_dict['charts_img_path'] = in_var_dir('charts')
50 mkdirs_if_unxists(cfg_dict['charts_img_path'])
51
52 cfg_dict['vm_ids_fname'] = in_var_dir('os_vm_ids')
53 cfg_dict['html_report_file'] = in_var_dir('report.html')
54 cfg_dict['text_report_file'] = in_var_dir('report.txt')
55 cfg_dict['log_file'] = in_var_dir('log.txt')
56 cfg_dict['sensor_storage'] = in_var_dir('sensor_storage.txt')
57
58 cfg_dict['test_log_directory'] = in_var_dir('test_logs')
59 mkdirs_if_unxists(cfg_dict['test_log_directory'])
60
61
62def color_me(color):
63 RESET_SEQ = "\033[0m"
64 COLOR_SEQ = "\033[1;%dm"
65
66 color_seq = COLOR_SEQ % (30 + color)
67
68 def closure(msg):
69 return color_seq + msg + RESET_SEQ
70 return closure
71
72
73class ColoredFormatter(logging.Formatter):
74 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
75
76 colors = {
77 'WARNING': color_me(YELLOW),
78 'DEBUG': color_me(BLUE),
79 'CRITICAL': color_me(YELLOW),
80 'ERROR': color_me(RED)
81 }
82
83 def __init__(self, msg, use_color=True, datefmt=None):
84 logging.Formatter.__init__(self, msg, datefmt=datefmt)
85 self.use_color = use_color
86
87 def format(self, record):
88 orig = record.__dict__
89 record.__dict__ = record.__dict__.copy()
90 levelname = record.levelname
91
92 prn_name = levelname + ' ' * (8 - len(levelname))
93 if levelname in self.colors:
94 record.levelname = self.colors[levelname](prn_name)
95 else:
96 record.levelname = prn_name
97
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030098 # super doesn't work here in 2.6 O_o
99 res = logging.Formatter.format(self, record)
100
101 # res = super(ColoredFormatter, self).format(record)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300102
103 # restore record, as it will be used by other formatters
104 record.__dict__ = orig
105 return res
106
107
108def setup_loggers(def_level=logging.DEBUG, log_fname=None):
109 logger = logging.getLogger('wally')
110 logger.setLevel(logging.DEBUG)
111 sh = logging.StreamHandler()
112 sh.setLevel(def_level)
113
koder aka kdanilov168f6092015-04-19 02:33:38 +0300114 log_format = '%(asctime)s - %(levelname)s - %(name)-15s - %(message)s'
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300115 colored_formatter = ColoredFormatter(log_format, datefmt="%H:%M:%S")
116
117 sh.setFormatter(colored_formatter)
118 logger.addHandler(sh)
119
120 logger_api = logging.getLogger("wally.fuel_api")
121
122 if log_fname is not None:
123 fh = logging.FileHandler(log_fname)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300124 log_format = '%(asctime)s - %(levelname)8s - %(name)-15s - %(message)s'
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300125 formatter = logging.Formatter(log_format, datefmt="%H:%M:%S")
126 fh.setFormatter(formatter)
127 fh.setLevel(logging.DEBUG)
128 logger.addHandler(fh)
129 logger_api.addHandler(fh)
130
131 logger_api.addHandler(sh)
132 logger_api.setLevel(logging.WARNING)