blob: e5962da44598214838fb2473608a61b8175a1dcb [file] [log] [blame]
gstepanov725cc302015-02-09 15:08:06 +02001import os
koder aka kdanilove06762a2015-03-22 23:32:09 +02002import yaml
koder aka kdanilov66839a92015-04-11 13:22:31 +03003
4from petname import Generate as pet_generate
gstepanov725cc302015-02-09 15:08:06 +02005
stgleb75c70412015-02-17 02:52:00 +02006
koder aka kdanilov66839a92015-04-11 13:22:31 +03007cfg_dict = {}
gstepanovc82391e2015-03-17 15:46:36 +02008
9
koder aka kdanilovcee43342015-04-14 22:52:53 +030010def load_config(file_name, explicit_folder=None):
koder aka kdanilov66839a92015-04-11 13:22:31 +030011 global cfg_dict
12 first_load = len(cfg_dict) == 0
13 cfg_dict.update(yaml.load(open(file_name).read()))
gstepanov5f936df2015-03-18 19:15:59 +020014
koder aka kdanilov66839a92015-04-11 13:22:31 +030015 if first_load:
16 var_dir = cfg_dict.get('internal', {}).get('var_dir_root', '/tmp')
gstepanov5f936df2015-03-18 19:15:59 +020017
koder aka kdanilovcee43342015-04-14 22:52:53 +030018 if explicit_folder is None:
19 while True:
20 dr = os.path.join(var_dir, pet_generate(2, "_"))
21 if not os.path.exists(dr):
22 break
23 else:
24 dr = explicit_folder
gstepanov5f936df2015-03-18 19:15:59 +020025
koder aka kdanilov66839a92015-04-11 13:22:31 +030026 cfg_dict['var_dir'] = dr
koder aka kdanilovcee43342015-04-14 22:52:53 +030027 if not os.path.exists(cfg_dict['var_dir']):
28 os.makedirs(cfg_dict['var_dir'])
gstepanov5f936df2015-03-18 19:15:59 +020029
koder aka kdanilov652cd802015-04-13 12:21:07 +030030 def in_var_dir(fname):
31 return os.path.join(cfg_dict['var_dir'], fname)
32
koder aka kdanilovcee43342015-04-14 22:52:53 +030033 charts_img_path = in_var_dir('charts')
34 cfg_dict['charts_img_path'] = charts_img_path
35 if not os.path.exists(charts_img_path):
36 os.makedirs(charts_img_path)
gstepanovb3c78962015-03-20 15:47:58 +020037
koder aka kdanilov652cd802015-04-13 12:21:07 +030038 cfg_dict['vm_ids_fname'] = in_var_dir('os_vm_ids')
39 cfg_dict['html_report_file'] = in_var_dir('report.html')
40 cfg_dict['text_report_file'] = in_var_dir('report.txt')
41 cfg_dict['log_file'] = in_var_dir('log.txt')
koder aka kdanilov12ae0632015-04-15 01:13:43 +030042 cfg_dict['sensor_storage'] = in_var_dir('sensor_storage.txt')