github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/sawtooth_validator/config/path.py (about) 1 # Copyright 2017 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 16 import collections 17 import logging 18 import os 19 import sys 20 21 import toml 22 23 from sawtooth_validator.exceptions import LocalConfigurationError 24 25 26 LOGGER = logging.getLogger(__name__) 27 28 29 def get_default_path_config(): 30 """Returns the default PathConfig as calculated based on SAWTOOTH_HOME 31 (if set) and operating system. 32 """ 33 if 'SAWTOOTH_HOME' in os.environ: 34 home_dir = os.environ['SAWTOOTH_HOME'] 35 return PathConfig( 36 config_dir=os.path.join(home_dir, 'etc'), 37 log_dir=os.path.join(home_dir, 'logs'), 38 data_dir=os.path.join(home_dir, 'data'), 39 key_dir=os.path.join(home_dir, 'keys'), 40 policy_dir=os.path.join(home_dir, 'policy')) 41 42 if os.name == 'nt': 43 # Paths appropriate for Windows. 44 base_dir = \ 45 os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))) 46 return PathConfig( 47 config_dir=os.path.join(base_dir, 'conf'), 48 log_dir=os.path.join(base_dir, 'logs'), 49 data_dir=os.path.join(base_dir, 'data'), 50 key_dir=os.path.join(base_dir, 'conf', 'keys'), 51 policy_dir=os.path.join(base_dir, 'policy')) 52 53 # Paths appropriate for modern Linux distributions. 54 return PathConfig( 55 config_dir='/etc/sawtooth', 56 log_dir='/var/log/sawtooth', 57 data_dir='/var/lib/sawtooth', 58 key_dir='/etc/sawtooth/keys', 59 policy_dir='/etc/sawtooth/policy') 60 61 62 def load_toml_path_config(filename): 63 """Returns a PathConfig created by loading a TOML file from the 64 filesystem. 65 """ 66 if not os.path.exists(filename): 67 LOGGER.info( 68 "Skipping path loading from non-existent config file: %s", 69 filename) 70 return PathConfig() 71 72 LOGGER.info("Loading path information from config: %s", filename) 73 74 try: 75 with open(filename) as fd: 76 raw_config = fd.read() 77 except IOError as e: 78 raise LocalConfigurationError( 79 "Unable to load path configuration file: {}".format(str(e))) 80 81 toml_config = toml.loads(raw_config) 82 83 invalid_keys = set(toml_config.keys()).difference( 84 ['data_dir', 'key_dir', 'log_dir', 'policy_dir']) 85 if invalid_keys: 86 raise LocalConfigurationError("Invalid keys in path config: {}".format( 87 ", ".join(sorted(list(invalid_keys))))) 88 89 config = PathConfig( 90 config_dir=None, 91 data_dir=toml_config.get('data_dir', None), 92 key_dir=toml_config.get('key_dir', None), 93 log_dir=toml_config.get('log_dir', None), 94 policy_dir=toml_config.get('policy_dir', None) 95 ) 96 97 return config 98 99 100 def merge_path_config(configs, config_dir_override): 101 """ 102 Given a list of PathConfig objects, merges them into a single PathConfig, 103 giving priority in the order of the configs (first has highest priority). 104 """ 105 config_dir = None 106 log_dir = None 107 data_dir = None 108 key_dir = None 109 policy_dir = None 110 111 for config in reversed(configs): 112 if config.config_dir is not None: 113 config_dir = config.config_dir 114 if config.log_dir is not None: 115 log_dir = config.log_dir 116 if config.data_dir is not None: 117 data_dir = config.data_dir 118 if config.key_dir is not None: 119 key_dir = config.key_dir 120 if config.policy_dir is not None: 121 policy_dir = config.policy_dir 122 123 if config_dir_override is not None: 124 config_dir = config_dir_override 125 126 return PathConfig( 127 config_dir=config_dir, 128 log_dir=log_dir, 129 data_dir=data_dir, 130 key_dir=key_dir, 131 policy_dir=policy_dir) 132 133 134 def load_path_config(config_dir=None): 135 default_config = get_default_path_config() 136 137 if config_dir is None: 138 conf_file = os.path.join(default_config.config_dir, 'path.toml') 139 else: 140 conf_file = os.path.join(config_dir, 'path.toml') 141 142 toml_config = load_toml_path_config(conf_file) 143 144 return merge_path_config(configs=[toml_config, default_config], 145 config_dir_override=config_dir) 146 147 148 class PathConfig: 149 def __init__(self, config_dir=None, log_dir=None, data_dir=None, 150 key_dir=None, policy_dir=None): 151 152 self._config_dir = config_dir 153 self._log_dir = log_dir 154 self._data_dir = data_dir 155 self._key_dir = key_dir 156 self._policy_dir = policy_dir 157 158 @property 159 def config_dir(self): 160 return self._config_dir 161 162 @property 163 def log_dir(self): 164 return self._log_dir 165 166 @property 167 def data_dir(self): 168 return self._data_dir 169 170 @property 171 def key_dir(self): 172 return self._key_dir 173 174 @property 175 def policy_dir(self): 176 return self._policy_dir 177 178 def __repr__(self): 179 return \ 180 "{}(config_dir={}, log_dir={}, data_dir={}, key_dir={}," \ 181 " policy_dir={})".format( 182 self.__class__.__name__, 183 repr(self._config_dir), 184 repr(self._log_dir), 185 repr(self._data_dir), 186 repr(self._key_dir), 187 repr(self._policy_dir)) 188 189 def to_dict(self): 190 return collections.OrderedDict([ 191 ('config_dir', self._config_dir), 192 ('key_dir', self._key_dir), 193 ('data_dir', self._data_dir), 194 ('log_dir', self._log_dir), 195 ('policy_dir', self._policy_dir) 196 ]) 197 198 def to_toml_string(self): 199 return str(toml.dumps(self.to_dict())).strip().split('\n')