forked from docker-archive/docker-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
70 lines (54 loc) · 1.87 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import rsa
import yaml
class Config(object):
def __init__(self, config):
self._config = config
def __repr__(self):
return repr(self._config)
def __getattr__(self, key):
if key in self._config:
return self._config[key]
def get(self, *args, **kwargs):
return self._config.get(*args, **kwargs)
def _walk_object(obj, callback):
if not hasattr(obj, '__iter__'):
return callback(obj)
if isinstance(obj, dict):
for i, value in obj.iteritems():
obj[i] = _walk_object(value, callback)
return obj
for i, value in enumerate(obj):
obj[i] = _walk_object(value, callback)
return obj
def convert_env_vars(config):
def _replace_env(s):
if isinstance(s, basestring) and s.startswith('_env:'):
parts = s.split(':', 2)
varname = parts[1]
vardefault = '!ENV_NOT_FOUND' if len(parts) < 3 else parts[2]
return os.environ.get(varname, vardefault)
return s
return _walk_object(config, _replace_env)
_config = None
def load():
global _config
if _config is not None:
return _config
data = None
config_path = os.environ.get('DOCKER_REGISTRY_CONFIG', 'config.yml')
if not os.path.isabs(config_path):
config_path = os.path.join(os.path.dirname(__file__), '..',
'config', config_path)
with open(config_path) as f:
data = yaml.load(f)
config = data.get('common', {})
flavor = os.environ.get('SETTINGS_FLAVOR', 'dev')
config.update(data.get(flavor, {}))
config['flavor'] = flavor
config = convert_env_vars(config)
if 'privileged_key' in config:
with open(config['privileged_key']) as f:
config['privileged_key'] = rsa.PublicKey.load_pkcs1(f.read())
_config = Config(config)
return _config