blob: 08a70d7f33ceefd1a685a6dfa2e62bd5e3598b8a [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
19def mkdirs_if_unxists(path):
20 if not os.path.exists(path):
21 os.makedirs(path)
22
23
24def load_config(file_name, explicit_folder=None):
25 first_load = len(cfg_dict) == 0
26 cfg_dict.update(yaml.load(open(file_name).read()))
27
28 if first_load:
29 var_dir = cfg_dict.get('internal', {}).get('var_dir_root', '/tmp')
30
31 run_uuid = None
32 if explicit_folder is None:
33 for i in range(10):
34 run_uuid = pet_generate(2, "_")
35 results_dir = os.path.join(var_dir, run_uuid)
36 if not os.path.exists(results_dir):
37 break
38 else:
39 run_uuid = str(uuid.uuid4())
40 results_dir = os.path.join(var_dir, run_uuid)
koder aka kdanilov4a510ee2015-04-21 18:50:42 +030041 cfg_dict['run_uuid'] = run_uuid.replace('_', '-')
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030042 else:
43 results_dir = explicit_folder
44
45 cfg_dict['var_dir'] = results_dir
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030046 mkdirs_if_unxists(cfg_dict['var_dir'])
47
48 in_var_dir = functools.partial(os.path.join, cfg_dict['var_dir'])
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030049 run_params_file = in_var_dir('run_params.yaml')
50
51 if explicit_folder is not None:
52 with open(run_params_file) as fd:
53 cfg_dict['run_uuid'] = yaml.load(fd)['run_uuid']
54 run_uuid = cfg_dict['run_uuid']
55 else:
56 with open(run_params_file, 'w') as fd:
57 fd.write(dumps({'run_uuid': cfg_dict['run_uuid']}))
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030058
59 cfg_dict['charts_img_path'] = in_var_dir('charts')
60 mkdirs_if_unxists(cfg_dict['charts_img_path'])
61
62 cfg_dict['vm_ids_fname'] = in_var_dir('os_vm_ids')
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030063 cfg_dict['html_report_file'] = in_var_dir('{0}_report.html')
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030064 cfg_dict['text_report_file'] = in_var_dir('report.txt')
65 cfg_dict['log_file'] = in_var_dir('log.txt')
66 cfg_dict['sensor_storage'] = in_var_dir('sensor_storage.txt')
67
koder aka kdanilovafd98742015-04-24 01:27:22 +030068 testnode_log_root = cfg_dict.get('testnode_log_root', '/var/wally')
69 testnode_log_dir = os.path.join(testnode_log_root, "{0}/{{name}}")
koder aka kdanilov2066daf2015-04-23 21:05:41 +030070 cfg_dict['default_test_local_folder'] = \
koder aka kdanilovafd98742015-04-24 01:27:22 +030071 testnode_log_dir.format(cfg_dict['run_uuid'])
koder aka kdanilov2066daf2015-04-23 21:05:41 +030072
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030073 cfg_dict['test_log_directory'] = in_var_dir('test_logs')
74 mkdirs_if_unxists(cfg_dict['test_log_directory'])
75
76
77def color_me(color):
78 RESET_SEQ = "\033[0m"
79 COLOR_SEQ = "\033[1;%dm"
80
81 color_seq = COLOR_SEQ % (30 + color)
82
83 def closure(msg):
84 return color_seq + msg + RESET_SEQ
85 return closure
86
87
88class ColoredFormatter(logging.Formatter):
89 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
90
91 colors = {
92 'WARNING': color_me(YELLOW),
93 'DEBUG': color_me(BLUE),
94 'CRITICAL': color_me(YELLOW),
95 'ERROR': color_me(RED)
96 }
97
98 def __init__(self, msg, use_color=True, datefmt=None):
99 logging.Formatter.__init__(self, msg, datefmt=datefmt)
100 self.use_color = use_color
101
102 def format(self, record):
103 orig = record.__dict__
104 record.__dict__ = record.__dict__.copy()
105 levelname = record.levelname
106
107 prn_name = levelname + ' ' * (8 - len(levelname))
108 if levelname in self.colors:
109 record.levelname = self.colors[levelname](prn_name)
110 else:
111 record.levelname = prn_name
112
koder aka kdanilov6b1341a2015-04-21 22:44:21 +0300113 # super doesn't work here in 2.6 O_o
114 res = logging.Formatter.format(self, record)
115
116 # res = super(ColoredFormatter, self).format(record)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300117
118 # restore record, as it will be used by other formatters
119 record.__dict__ = orig
120 return res
121
122
123def setup_loggers(def_level=logging.DEBUG, log_fname=None):
124 logger = logging.getLogger('wally')
125 logger.setLevel(logging.DEBUG)
126 sh = logging.StreamHandler()
127 sh.setLevel(def_level)
128
koder aka kdanilov168f6092015-04-19 02:33:38 +0300129 log_format = '%(asctime)s - %(levelname)s - %(name)-15s - %(message)s'
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300130 colored_formatter = ColoredFormatter(log_format, datefmt="%H:%M:%S")
131
132 sh.setFormatter(colored_formatter)
133 logger.addHandler(sh)
134
135 logger_api = logging.getLogger("wally.fuel_api")
136
137 if log_fname is not None:
138 fh = logging.FileHandler(log_fname)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300139 log_format = '%(asctime)s - %(levelname)8s - %(name)-15s - %(message)s'
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300140 formatter = logging.Formatter(log_format, datefmt="%H:%M:%S")
141 fh.setFormatter(formatter)
142 fh.setLevel(logging.DEBUG)
143 logger.addHandler(fh)
144 logger_api.addHandler(fh)
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300145 else:
146 fh = None
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300147
148 logger_api.addHandler(sh)
149 logger_api.setLevel(logging.WARNING)
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300150
151 logger = logging.getLogger('paramiko')
152 logger.setLevel(logging.WARNING)
koder aka kdanilovafd98742015-04-24 01:27:22 +0300153 # logger.addHandler(sh)
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300154 if fh is not None:
155 logger.addHandler(fh)