-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigManager.py
170 lines (153 loc) · 6.33 KB
/
ConfigManager.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
import json
from Utils import printe
required_fields = [
'name',
'description',
'type',
'kind',
'flavor',
]
required_machine_fields = [
]
# Optional machine config fields with their default values.
optional_machine_fields = {
'experimental': False,
'consul_machine': False,
'multihost_networking': False,
'args': False,
}
required_container_fields = [
'image',
]
# Optional container config fields with their default values.
optional_container_fields = {
'command': [],
'environment': {},
'expose': [],
'links': [],
'ports': [],
'restart': None,
'volumes': [],
'volumes-from': None,
'net': None,
'device': None,
'capabilities': [],
'user': None,
'privileged': None,
'interactive': None,
'tty': None,
}
class ConfigManager(object):
def __init__(self, config_directory, filter=None):
if config_directory.startswith('~'):
config_directory = os.path.expanduser('~') + config_directory[1:]
if not os.path.exists(config_directory):
os.makedirs(config_directory)
try:
configs = os.listdir(config_directory)
except:
printe('Could not list files in the directory:', config_directory,
terminate=True)
self.configs = {}
for config in configs:
if not config.endswith('.json'):
continue
with open('%s/%s' % (config_directory, config)) as file:
configJson = json.load(file)
if filter and configJson['type'] != filter:
continue
for field in required_fields:
if field not in configJson:
printe('Config %s is missing its %s.' % (config, field),
terminate=3)
if configJson['type'] == 'container':
all_fields = list(required_fields)
all_fields += required_container_fields
all_fields += optional_container_fields
for field in configJson:
if field not in all_fields:
printe('Container config {config} has unknown field '
'"{field}".'.format(config=config, field=field),
terminate=3)
for field in required_container_fields:
if field not in configJson:
printe('Container config {config} is missing its '
'field.'.format(config=config, field=field),
terminate=3)
for field in optional_container_fields:
if field not in configJson:
configJson[field] = optional_container_fields[field]
elif configJson['type'] == 'machine':
all_fields = list(required_fields)
all_fields += required_machine_fields
all_fields += optional_machine_fields
for field in configJson:
if field not in all_fields:
printe('Machine config {config} has unknown field '
'"{field}".'.format(config=config, field=field),
terminate=3)
for field in required_machine_fields:
if field not in configJson:
printe('Machine config {config} is missing its '
'{field}.'.format(config=config, field=field),
terminate=3)
for field in optional_machine_fields:
if field not in configJson:
configJson[field] = optional_machine_fields[field]
else:
printe('Unknown type "{}". Available types are: container, '
'machine'.format(configJson['type']), terminate=3)
config_type = configJson['type']
kind = configJson['kind']
flavor = configJson['flavor']
if config_type not in self.configs:
self.configs[config_type] = {}
if kind not in self.configs[config_type]:
self.configs[config_type][kind] = {}
elif flavor in self.configs[config_type][kind]:
printe(
"Duplicate kind:flavor configs found: {file} {config}. "
"Please change or remove one of these configs to have a "
"different kind:flavor combination.".format(
file=self.configs[config_type][kind][flavor]
.get('file_name'),
config=config,
),
terminate=True
)
del configJson['type']
del configJson['kind']
del configJson['flavor']
configJson['file_name'] = config
self.configs[config_type][kind][flavor] = configJson
def getContainerConfig(self, kind, flavor):
return self.get('container', kind, flavor)
def getMachineConfig(self, kind, flavor):
return self.get('machine', kind, flavor)
def get(self, config_type, kind, flavor):
if kind not in self.configs[config_type]:
printe('Unknown %s kind: "%s"' % (config_type, kind), terminate=2)
if flavor not in self.configs[config_type][kind]:
printe('Unknown {type} flavor for {kind}: "{flavor}"'.format(
type=config_type, kind=kind, flavor=flavor), terminate=2)
return self.configs[config_type][kind][flavor]
def describeContainer(self, kind, flavor):
return self.describe('container', kind, flavor)
def describeMachine(self, kind, flavor):
return self.describe('machine', kind, flavor)
def describe(self, config_type, kind, flavor):
config = self.get(config_type, kind, flavor)
return '"%s":\t%s' % (config['name'], config['description'])
def listContainers(self):
return self.list('container')
def listMachines(self):
return self.list('machine')
def list(self, config_type):
ls = []
if config_type not in self.configs:
return ls
for kind in self.configs[config_type]:
for flavor in self.configs[config_type][kind]:
ls += ['%s:%s' % (kind, flavor)]
return ls