Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import os 

2 

3from cfg_checker.common.exception import ConfigException 

4 

5from cfg_checker.common.log import logger_cli 

6 

7from cfg_checker.common.other import utils 

8 

9pkg_dir = os.path.dirname(__file__) 

10pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir) 

11pkg_dir = os.path.normpath(pkg_dir) 

12pkg_dir = os.path.abspath(pkg_dir) 

13 

14_default_work_folder = os.path.normpath(pkg_dir) 

15 

16 

17class CheckerConfiguration(object): 

18 def load_nodes_list(): 

19 return utils.get_nodes_list( 

20 os.environ.get('CFG_ALL_NODES', None), 

21 os.environ.get('SALT_NODE_LIST_FILE', None) 

22 ) 

23 

24 def _init_values(self): 

25 """Load values from environment variables or put default ones 

26 """ 

27 

28 self.name = "CheckerConfig" 

29 self.working_folder = os.environ.get( 

30 'CFG_TESTS_WORK_DIR', 

31 _default_work_folder 

32 ) 

33 self.date_format = "%Y-%m-%d %H:%M:%S.%f%z" 

34 self.default_tz = "UTC" 

35 

36 self.pkg_versions_map = 'versions_map.csv' 

37 

38 self.ssh_uses_sudo = False 

39 self.ssh_key = os.environ.get('SSH_KEY', None) 

40 self.ssh_user = os.environ.get('SSH_USER', None) 

41 self.ssh_host = os.environ.get('SSH_HOST', None) 

42 

43 self.salt_host = os.environ.get('SALT_URL', None) 

44 self.salt_port = os.environ.get('SALT_PORT', '6969') 

45 self.salt_user = os.environ.get('SALT_USER', 'salt') 

46 self.salt_timeout = os.environ.get('SALT_TIMEOUT', 30) 

47 self.salt_file_root = os.environ.get('SALT_FILE_ROOT', None) 

48 self.salt_scripts_folder = os.environ.get( 

49 'SALT_SCRIPTS_FOLDER', 

50 'cfg_checker_scripts' 

51 ) 

52 

53 self.skip_nodes = utils.node_string_to_list(os.environ.get( 

54 'CFG_SKIP_NODES', 

55 None 

56 )) 

57 

58 def _init_env(self, env_name=None): 

59 """Inits the environment vars from the env file 

60 Uses simple validation for the values and names 

61 

62 Keyword Arguments: 

63 env_name {str} -- environment name to search configuration 

64 files in etc/<env_name>.env (default: {None}) 

65 

66 Raises: 

67 ConfigException -- on IO error when loading env file 

68 ConfigException -- on env file failed validation 

69 """ 

70 # load env file as init os.environment with its values 

71 if env_name is None: 

72 _env_name = 'local' 

73 else: 

74 _env_name = env_name 

75 _config_path = os.path.join(pkg_dir, 'etc', _env_name + '.env') 

76 if os.path.isfile(_config_path): 

77 with open(_config_path) as _f: 

78 _list = _f.read().splitlines() 

79 logger_cli.info( 

80 "# Loading env vars from '{}'".format( 

81 _config_path 

82 ) 

83 ) 

84 else: 

85 raise ConfigException( 

86 "# Failed to load enviroment vars from '{}'".format( 

87 _config_path 

88 ) 

89 ) 

90 for index in range(len(_list)): 

91 _line = _list[index] 

92 # skip comments 

93 if _line.strip().startswith('#'): 

94 continue 

95 # validate 

96 _errors = [] 

97 if _line.find('=') < 0 or _line.count('=') > 1: 

98 _errors.append("Line {}: {}".format(index, _line)) 

99 else: 

100 # save values 

101 _t = _line.split('=') 

102 _key, _value = _t[0], _t[1] 

103 os.environ[_key] = _value 

104 # if there was errors, report them 

105 if _errors: 

106 raise ConfigException( 

107 "# Environment file failed validation in lines: {}".format( 

108 "\n".join(_errors) 

109 ) 

110 ) 

111 else: 

112 logger_cli.debug( 

113 "-> ...loaded total of '{}' vars".format( 

114 len(_list) 

115 ) 

116 ) 

117 self.salt_env = _env_name 

118 

119 def __init__(self): 

120 """Base configuration class. Only values that are common for all scripts 

121 """ 

122 _env = os.getenv('SALT_ENV', None) 

123 self._init_env(_env) 

124 self._init_values() 

125 

126 

127config = CheckerConfiguration()