blob: 03b7ac911f3090cc38a3fe3a54380e09ef87dc9c [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)
40 else:
41 results_dir = explicit_folder
42
43 cfg_dict['var_dir'] = results_dir
44 cfg_dict['run_uuid'] = run_uuid.replace('_', '-')
45 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
98 res = super(ColoredFormatter, self).format(record)
99
100 # restore record, as it will be used by other formatters
101 record.__dict__ = orig
102 return res
103
104
105def setup_loggers(def_level=logging.DEBUG, log_fname=None):
106 logger = logging.getLogger('wally')
107 logger.setLevel(logging.DEBUG)
108 sh = logging.StreamHandler()
109 sh.setLevel(def_level)
110
111 log_format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s'
112 colored_formatter = ColoredFormatter(log_format, datefmt="%H:%M:%S")
113
114 sh.setFormatter(colored_formatter)
115 logger.addHandler(sh)
116
117 logger_api = logging.getLogger("wally.fuel_api")
118
119 if log_fname is not None:
120 fh = logging.FileHandler(log_fname)
121 log_format = '%(asctime)s - %(levelname)8s - %(name)s - %(message)s'
122 formatter = logging.Formatter(log_format, datefmt="%H:%M:%S")
123 fh.setFormatter(formatter)
124 fh.setLevel(logging.DEBUG)
125 logger.addHandler(fh)
126 logger_api.addHandler(fh)
127
128 logger_api.addHandler(sh)
129 logger_api.setLevel(logging.WARNING)