koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 1 | import os |
| 2 | import uuid |
| 3 | import logging |
| 4 | import functools |
| 5 | |
| 6 | import yaml |
| 7 | |
| 8 | try: |
| 9 | from petname import Generate as pet_generate |
| 10 | except ImportError: |
| 11 | def pet_generate(x, y): |
| 12 | return str(uuid.uuid4()) |
| 13 | |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 14 | import pretty_yaml |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 15 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 16 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 17 | class NoData(object): |
| 18 | @classmethod |
| 19 | def get(cls, name, x): |
| 20 | return cls |
| 21 | |
| 22 | |
| 23 | class Config(object): |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 24 | def __init__(self, val=None): |
| 25 | if val is not None: |
| 26 | self.update(val) |
| 27 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 28 | def get(self, name, defval=None): |
| 29 | obj = self.__dict__ |
| 30 | for cname in name.split("."): |
| 31 | obj = obj.get(cname, NoData) |
| 32 | |
| 33 | if obj is NoData: |
| 34 | return defval |
| 35 | return obj |
| 36 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 37 | def update(self, val): |
| 38 | self.__dict__.update(val) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 39 | |
| 40 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 41 | def get_test_files(results_dir): |
| 42 | in_var_dir = functools.partial(os.path.join, results_dir) |
| 43 | |
| 44 | res = dict( |
| 45 | run_params_file='run_params.yaml', |
| 46 | saved_config_file='config.yaml', |
| 47 | vm_ids_fname='os_vm_ids', |
| 48 | html_report_file='{0}_report.html', |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 49 | load_report_file='load_report.html', |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 50 | text_report_file='report.txt', |
| 51 | log_file='log.txt', |
| 52 | sensor_storage='sensor_storage', |
| 53 | nodes_report_file='nodes.yaml', |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 54 | results_storage='results', |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 55 | hwinfo_directory='hwinfo', |
| 56 | hwreport_fname='hwinfo.txt', |
| 57 | raw_results='raw_results.yaml') |
| 58 | |
| 59 | res = dict((k, in_var_dir(v)) for k, v in res.items()) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 60 | res['results_dir'] = results_dir |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 61 | return res |
| 62 | |
| 63 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 64 | def load_config(file_name): |
| 65 | file_name = os.path.abspath(file_name) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 66 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 67 | defaults = dict( |
| 68 | sensors_remote_path='/tmp/sensors', |
| 69 | testnode_log_root='/tmp/wally', |
| 70 | settings={} |
| 71 | ) |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 72 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 73 | raw_cfg = yaml.load(open(file_name).read()) |
| 74 | raw_cfg['config_folder'] = os.path.dirname(file_name) |
| 75 | if 'include' in raw_cfg: |
| 76 | default_path = os.path.join(raw_cfg['config_folder'], |
| 77 | raw_cfg.pop('include')) |
| 78 | default_cfg = yaml.load(open(default_path).read()) |
| 79 | |
| 80 | # TODO: Need more intelectual configs merge? |
| 81 | default_cfg.update(raw_cfg) |
| 82 | raw_cfg = default_cfg |
| 83 | |
| 84 | cfg = Config(defaults) |
| 85 | cfg.update(raw_cfg) |
| 86 | |
| 87 | results_storage = cfg.settings.get('results_storage', '/tmp') |
| 88 | results_storage = os.path.abspath(results_storage) |
| 89 | |
| 90 | existing = file_name.startswith(results_storage) |
| 91 | |
| 92 | if existing: |
| 93 | cfg.results_dir = os.path.dirname(file_name) |
| 94 | cfg.run_uuid = os.path.basename(cfg.results_dir) |
| 95 | else: |
| 96 | # genarate result folder name |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 97 | for i in range(10): |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 98 | cfg.run_uuid = pet_generate(2, "_") |
| 99 | cfg.results_dir = os.path.join(results_storage, |
| 100 | cfg.run_uuid) |
| 101 | if not os.path.exists(cfg.results_dir): |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 102 | break |
| 103 | else: |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 104 | cfg.run_uuid = str(uuid.uuid4()) |
| 105 | cfg.results_dir = os.path.join(results_storage, |
| 106 | cfg.run_uuid) |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 107 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 108 | # setup all files paths |
| 109 | cfg.update(get_test_files(cfg.results_dir)) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 110 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 111 | if existing: |
| 112 | cfg.update(load_run_params(cfg.run_params_file)) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 113 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 114 | testnode_log_root = cfg.get('testnode_log_root') |
koder aka kdanilov | afd9874 | 2015-04-24 01:27:22 +0300 | [diff] [blame] | 115 | testnode_log_dir = os.path.join(testnode_log_root, "{0}/{{name}}") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 116 | cfg.default_test_local_folder = testnode_log_dir.format(cfg.run_uuid) |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 117 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 118 | return cfg |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 119 | |
| 120 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 121 | def save_run_params(cfg): |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 122 | params = { |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 123 | 'comment': cfg.comment, |
| 124 | 'run_uuid': cfg.run_uuid |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 125 | } |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 126 | |
| 127 | with open(cfg.run_params_file, 'w') as fd: |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 128 | fd.write(pretty_yaml.dumps(params)) |
| 129 | |
| 130 | |
| 131 | def load_run_params(run_params_file): |
| 132 | with open(run_params_file) as fd: |
| 133 | dt = yaml.load(fd) |
| 134 | |
| 135 | return dict(run_uuid=dt['run_uuid'], |
| 136 | comment=dt.get('comment')) |
| 137 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 138 | |
| 139 | def color_me(color): |
| 140 | RESET_SEQ = "\033[0m" |
| 141 | COLOR_SEQ = "\033[1;%dm" |
| 142 | |
| 143 | color_seq = COLOR_SEQ % (30 + color) |
| 144 | |
| 145 | def closure(msg): |
| 146 | return color_seq + msg + RESET_SEQ |
| 147 | return closure |
| 148 | |
| 149 | |
| 150 | class ColoredFormatter(logging.Formatter): |
| 151 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
| 152 | |
| 153 | colors = { |
| 154 | 'WARNING': color_me(YELLOW), |
| 155 | 'DEBUG': color_me(BLUE), |
| 156 | 'CRITICAL': color_me(YELLOW), |
| 157 | 'ERROR': color_me(RED) |
| 158 | } |
| 159 | |
| 160 | def __init__(self, msg, use_color=True, datefmt=None): |
| 161 | logging.Formatter.__init__(self, msg, datefmt=datefmt) |
| 162 | self.use_color = use_color |
| 163 | |
| 164 | def format(self, record): |
| 165 | orig = record.__dict__ |
| 166 | record.__dict__ = record.__dict__.copy() |
| 167 | levelname = record.levelname |
| 168 | |
| 169 | prn_name = levelname + ' ' * (8 - len(levelname)) |
| 170 | if levelname in self.colors: |
| 171 | record.levelname = self.colors[levelname](prn_name) |
| 172 | else: |
| 173 | record.levelname = prn_name |
| 174 | |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame] | 175 | # super doesn't work here in 2.6 O_o |
| 176 | res = logging.Formatter.format(self, record) |
| 177 | |
| 178 | # res = super(ColoredFormatter, self).format(record) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 179 | |
| 180 | # restore record, as it will be used by other formatters |
| 181 | record.__dict__ = orig |
| 182 | return res |
| 183 | |
| 184 | |
| 185 | def setup_loggers(def_level=logging.DEBUG, log_fname=None): |
| 186 | logger = logging.getLogger('wally') |
| 187 | logger.setLevel(logging.DEBUG) |
| 188 | sh = logging.StreamHandler() |
| 189 | sh.setLevel(def_level) |
| 190 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 191 | log_format = '%(asctime)s - %(levelname)s - %(name)-15s - %(message)s' |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 192 | colored_formatter = ColoredFormatter(log_format, datefmt="%H:%M:%S") |
| 193 | |
| 194 | sh.setFormatter(colored_formatter) |
| 195 | logger.addHandler(sh) |
| 196 | |
| 197 | logger_api = logging.getLogger("wally.fuel_api") |
| 198 | |
| 199 | if log_fname is not None: |
| 200 | fh = logging.FileHandler(log_fname) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 201 | log_format = '%(asctime)s - %(levelname)8s - %(name)-15s - %(message)s' |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 202 | formatter = logging.Formatter(log_format, datefmt="%H:%M:%S") |
| 203 | fh.setFormatter(formatter) |
| 204 | fh.setLevel(logging.DEBUG) |
| 205 | logger.addHandler(fh) |
| 206 | logger_api.addHandler(fh) |
koder aka kdanilov | ec1b973 | 2015-04-23 20:43:29 +0300 | [diff] [blame] | 207 | else: |
| 208 | fh = None |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 209 | |
| 210 | logger_api.addHandler(sh) |
| 211 | logger_api.setLevel(logging.WARNING) |
koder aka kdanilov | ec1b973 | 2015-04-23 20:43:29 +0300 | [diff] [blame] | 212 | |
| 213 | logger = logging.getLogger('paramiko') |
| 214 | logger.setLevel(logging.WARNING) |
koder aka kdanilov | afd9874 | 2015-04-24 01:27:22 +0300 | [diff] [blame] | 215 | # logger.addHandler(sh) |
koder aka kdanilov | ec1b973 | 2015-04-23 20:43:29 +0300 | [diff] [blame] | 216 | if fh is not None: |
| 217 | logger.addHandler(fh) |