-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconvert-docker-compose-to-cloudformation.rb
executable file
·91 lines (77 loc) · 2.58 KB
/
convert-docker-compose-to-cloudformation.rb
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
#!/usr/bin/env ruby
require 'json'
require 'yaml'
FAMILY = 'eric-test-family'
IMAGE = '############.dkr.ecr.us-east-1.amazonaws.com/eric-test/'
docker_compose_file = './docker-compose.yml'
cloudformation_file = './aws/cloud-formation-task.json'
raise 'Docker compose file not found.' unless File.exist?(docker_compose_file)
# skeleton task definition from aws cli
cf_skeleton = JSON.parse(`aws ecs register-task-definition --generate-cli-skeleton`)
cf_skeleton.delete 'placementConstraints'
cf_skeleton.delete 'taskRoleArn'
cf_skeleton.delete 'networkMode'
cf_skeleton['family'] = FAMILY
cf_volume = cf_skeleton['volumes'].pop
cf_container = cf_skeleton['containerDefinitions'].pop
cf_container['command'] = []
cf_container['environment'] = []
cf_container['links'] = []
cf_container['mountPoints'] = []
cf_container['portMappings'] = []
# load docker-compose yaml
docker_compose_data = YAML.load_file docker_compose_file
# transpose shared volumes
volumes_map = {}
docker_compose_data['volumes'].each_with_index do |(key, value), index|
volume = Marshal.load(Marshal.dump(cf_volume))
volume['host']['sourcePath'] = key
volume['name'] = "volume-#{index}"
cf_skeleton['volumes'] << volume
volumes_map[key] = "volume-#{index}"
end
# transpose containers
docker_compose_data['services'].each do |service|
container = Marshal.load(Marshal.dump(cf_container))
container['name'] = service[0]
container['image'] = "#{IMAGE}#{service[0]}:latest"
unless service[1]['volumes'].nil?
service[1]['volumes'].each do |volume|
(name, path) = volume.split(':')
container['mountPoints'] << {
'sourceVolume' => volumes_map[name],
'readOnly' => false,
'containerPath' => path
}
end
end
unless service[1]['ports'].nil?
service[1]['ports'].each do |port|
(host_port, container_port) = port.split(':')
container['portMappings'] << {
'protocol' => 'tcp',
'containerPort' => container_port,
'hostPort' => host_port,
}
end
end
unless service[1]['depends_on'].nil?
service[1]['depends_on'].each do |depends_on|
container['links'] << depends_on
end
end
unless service[1]['command'].nil?
container['command'] << service[1]['command']
end
unless service[1]['environment'].nil?
service[1]['environment'].each do |environment|
(name, value) = environment.split('=')
container['environment'] << {
'name' => name,
'value' => value,
}
end
end
cf_skeleton['containerDefinitions'] << container
end
File.write(cloudformation_file, JSON.pretty_generate(cf_skeleton))