forked from micahhausler/container-transform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose.py
More file actions
360 lines (300 loc) · 10.7 KB
/
Copy pathcompose.py
File metadata and controls
360 lines (300 loc) · 10.7 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import uuid
from functools import reduce
import yaml
from .transformer import BaseTransformer
class ComposeTransformer(BaseTransformer):
"""
A transformer for docker-compose v1 and v2
To use this class:
.. code-block:: python
transformer = ComposeTransformer('./docker-compose.yml')
normalized_keys = transformer.ingest_containers()
"""
def __init__(self, filename=None):
"""
We override ``.__init__()`` on purpose, we need to get the volume,
version, network, and possibly other data.
:param filename: The file to be loaded
:type filename: str
"""
if filename:
self._filename = filename
stream = self._read_file(filename)
self.stream_version = float(stream.get('version', '1'))
if self.stream_version > 1:
self.stream = stream.get('services')
self.volumes = stream.get('volumes', None)
self.networks = stream.get('networks', None)
else:
self.stream = stream
else:
self.stream = None
def _read_stream(self, stream):
return yaml.safe_load(stream=stream)
def ingest_containers(self, containers=None):
"""
Transform the YAML into a dict with normalized keys
"""
containers = containers or self.stream or {}
output_containers = []
for container_name, definition in containers.items():
container = definition.copy()
container['name'] = container_name
output_containers.append(container)
return output_containers
def emit_containers(self, containers, verbose=True):
services = {}
for container in containers:
name_in_container = container.get('name')
if not name_in_container:
name = str(uuid.uuid4())
else:
name = container.pop('name')
services[name] = container
output = {
'services': services,
'version': '2',
}
noalias_dumper = yaml.dumper.SafeDumper
noalias_dumper.ignore_aliases = lambda self, data: True
return yaml.dump(
output,
default_flow_style=False,
Dumper=noalias_dumper
)
@staticmethod
def validate(container):
return container
@staticmethod
def _parse_port_mapping(mapping):
protocol = 'udp' if 'udp' in str(mapping) else 'tcp'
output = {
'protocol': protocol
}
mapping = str(mapping).rstrip('/udp')
parts = str(mapping).split(':')
if len(parts) == 1:
output.update({
'container_port': int(parts[0])
})
elif len(parts) == 2 and '.' not in mapping:
output.update({
'host_port': int(parts[0]),
'container_port': int(parts[1]),
})
elif len(parts) == 3:
if '.' in parts[0]:
output.update({
'host_ip': parts[0],
'host_port': int(parts[1]),
'container_port': int(parts[2]),
})
else:
output.update({
'host_port': int(parts[0]),
'container_ip': parts[1],
'container_port': int(parts[2]),
})
elif len(parts) == 4:
output.update({
'host_ip': parts[0],
'host_port': int(parts[1]),
'container_ip': parts[2],
'container_port': int(parts[3]),
})
return output if len(output) >= 2 else None
def ingest_port_mappings(self, port_mappings):
"""
Transform the docker-compose port mappings to base schema port_mappings
:param port_mappings: The compose port mappings
:type port_mappings: list
:return: the base schema port_mappings
:rtype: list of dict
"""
return [self._parse_port_mapping(mapping) for mapping in port_mappings]
@staticmethod
def _emit_mapping(mapping):
parts = []
if mapping.get('host_ip'):
parts.append(str(mapping['host_ip']))
if mapping.get('host_port'):
parts.append(str(mapping['host_port']))
if mapping.get('container_ip'):
parts.append(str(mapping['container_ip']))
if mapping.get('container_port'):
parts.append(str(mapping['container_port']))
output = ':'.join(parts)
if mapping.get('protocol') == 'udp':
output += '/udp'
return output
def emit_port_mappings(self, port_mappings):
"""
:param port_mappings: the base schema port_mappings
:type port_mappings: list of dict
:return:
:rtype: list of str
"""
return [str(self._emit_mapping(mapping)) for mapping in port_mappings]
def ingest_memory(self, memory):
"""
Transform the memory into bytes
:param memory: Compose memory definition. (1g, 24k)
:type memory: memory string or integer
:return: The memory in bytes
:rtype: int
"""
def lshift(num, shift):
return num << shift
def rshift(num, shift):
return num >> shift
if isinstance(memory, int):
# Memory was specified as an integer, meaning it is in bytes
memory = '{}b'.format(memory)
bit_shift = {
'g': {'func': lshift, 'shift': 30},
'm': {'func': lshift, 'shift': 20},
'k': {'func': lshift, 'shift': 10},
'b': {'func': rshift, 'shift': 0}
}
unit = memory[-1]
number = int(memory[:-1])
return bit_shift[unit]['func'](number, bit_shift[unit]['shift'])
def emit_memory(self, memory):
return '{}b'.format(memory)
def ingest_cpu(self, cpu):
return cpu
def emit_cpu(self, cpu):
return cpu
def ingest_environment(self, environment):
output = {}
if type(environment) is list:
for kv in environment:
index = kv.find('=')
output[str(kv[:index])] = str(kv[index + 1:]).replace('$$', '$')
if type(environment) is dict:
for key, value in environment.items():
output[str(key)] = str(value).replace('$$', '$')
return output
def emit_environment(self, environment):
# Use double-dollar and avoid vairable substitution. Reference,
# https://docs.docker.com/compose/compose-file/compose-file-v2
for key, value in environment.items():
environment[key] = str(value).replace('$', '$$')
return environment
def ingest_command(self, command):
if isinstance(command, list):
return self._list2cmdline(command)
return command
def emit_command(self, command):
return command
def ingest_entrypoint(self, entrypoint):
if isinstance(entrypoint, list):
return self._list2cmdline(entrypoint)
return entrypoint
def emit_entrypoint(self, entrypoint):
return entrypoint
def ingest_volumes_from(self, volumes_from):
ingested_volumes_from = []
for vol in volumes_from:
ingested = {}
parts = vol.split(':')
rwo_value = None
assert len(parts) <= 3, \
"Volume string '{}' has too many colons".format(vol)
if len(parts) == 3:
# Is form 'service:name:ro' or 'container:name:ro'
# in new compose v2 format.
source_container, rwo_value = parts[1:]
elif len(parts) == 2:
# Is form 'name:ro' or 'service:name' (for >= v2)
if self.stream_version > 1 and parts[0] == 'service':
source_container = parts[1]
else:
assert(parts[1] in ['ro', 'rw'])
source_container = parts[0]
rwo_value = parts[1]
else:
source_container = parts[0]
if rwo_value == 'ro':
ingested['read_only'] = True
ingested['source_container'] = source_container
ingested_volumes_from.append(ingested)
return ingested_volumes_from
def emit_volumes_from(self, volumes_from):
return volumes_from
@staticmethod
def _ingest_volume(volume):
parts = volume.split(':')
if len(parts) == 1:
return {
'host': parts[0],
'container': parts[0]
}
if len(parts) == 2 and parts[1] != 'ro':
return {
'host': parts[0],
'container': parts[1]
}
if len(parts) == 2 and parts[1] == 'ro':
return {
'host': parts[0],
'container': parts[0],
'readonly': True
}
if len(parts) == 3 and parts[-1] == 'ro':
return {
'host': parts[0],
'container': parts[1],
'readonly': True
}
if len(parts) == 3 and parts[-1] == 'rw':
return {
'host': parts[0],
'container': parts[1],
}
def ingest_volumes(self, volumes):
return [
self._ingest_volume(volume)
for volume
in volumes
if self._ingest_volume(volume) is not None
]
@staticmethod
def _emit_volume(volume):
volume_str = '{0}:{1}'.format(volume.get('host'), volume.get('container', ':'))
volume_str = volume_str.strip(':')
if volume.get('readonly') and len(volume_str):
volume_str += ':ro'
return volume_str
def emit_volumes(self, volumes):
return [
self._emit_volume(volume)
for volume
in volumes
if len(self._emit_volume(volume))
]
@staticmethod
def _parse_label_string(label):
eq = label.find('=')
if eq == -1:
return {label: None}
else:
return {label[:eq]: label[eq+1:]}
def ingest_labels(self, labels):
if isinstance(labels, list):
return reduce(
lambda a, b: a.update(b) or a,
map(self._parse_label_string, labels),
{}
)
return labels
def emit_labels(self, labels):
return labels
def ingest_logging(self, logging):
return logging
def emit_logging(self, logging):
return logging
def ingest_privileged(self, privileged):
return privileged
def emit_privileged(self, privileged):
return privileged