@@ -5,19 +5,42 @@ import (
5
5
"path/filepath"
6
6
7
7
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/containerconfig"
8
+ "github.com/aws/amazon-ecs-cli/ecs-cli/modules/utils/compose"
9
+ "github.com/aws/aws-sdk-go/service/ecs"
8
10
"github.com/pkg/errors"
9
- "github.com/sirupsen/logrus"
11
+ log "github.com/sirupsen/logrus"
10
12
11
13
"github.com/docker/cli/cli/compose/loader"
12
14
"github.com/docker/cli/cli/compose/types"
15
+ "github.com/docker/libcompose/yaml"
13
16
)
14
17
15
18
func (p * ecsProject ) parseV3 () (* []containerconfig.ContainerConfig , error ) {
16
- logrus .Debug ("Parsing v3 project..." )
19
+ log .Debug ("Parsing v3 project..." )
17
20
18
- // load and parse each compose file into a configFile
21
+ v3Config , err := getV3Config (p .ecsContext .ComposeFiles )
22
+ if err != nil {
23
+ return nil , err
24
+ }
25
+
26
+ // convert ServiceConfigs to ContainerConfigs
27
+ conConfigs := []containerconfig.ContainerConfig {}
28
+ for _ , service := range v3Config .Services {
29
+ cCon , err := convertToContainerConfig (service )
30
+ if err != nil {
31
+ return nil , err
32
+ }
33
+ conConfigs = append (conConfigs , * cCon )
34
+ }
35
+
36
+ // TODO: process v3Config.Volumes as well
37
+ return & conConfigs , nil
38
+ }
39
+
40
+ // parses compose files into a docker/cli Config, which contains v3 ServiceConfigs
41
+ func getV3Config (composeFiles []string ) (* types.Config , error ) {
19
42
configFiles := []types.ConfigFile {}
20
- for _ , file := range p . ecsContext . ComposeFiles {
43
+ for _ , file := range composeFiles {
21
44
22
45
loadedFile , err := ioutil .ReadFile (file )
23
46
if err != nil {
@@ -34,7 +57,7 @@ func (p *ecsProject) parseV3() (*[]containerconfig.ContainerConfig, error) {
34
57
configFiles = append (configFiles , configFile )
35
58
}
36
59
37
- wrkDir , err := getWorkingDir (p . ecsContext . ComposeFiles [0 ])
60
+ wrkDir , err := getWorkingDir (composeFiles [0 ])
38
61
if err != nil {
39
62
return nil , err
40
63
}
@@ -51,24 +74,89 @@ func (p *ecsProject) parseV3() (*[]containerconfig.ContainerConfig, error) {
51
74
return nil , err
52
75
}
53
76
54
- // convert ServiceConfigs to ContainerConfigs
55
- conConfigs := []containerconfig.ContainerConfig {}
56
- for _ , service := range config .Services {
57
- cCon , err := convertToContainerConfig (service )
77
+ return config , nil
78
+ }
79
+
80
+ func convertToContainerConfig (serviceConfig types.ServiceConfig ) (* containerconfig.ContainerConfig , error ) {
81
+ //TODO: Add Healthcheck, Devices to ContainerConfig
82
+ c := & containerconfig.ContainerConfig {
83
+ CapAdd : serviceConfig .CapAdd ,
84
+ CapDrop : serviceConfig .CapDrop ,
85
+ DockerSecurityOptions : serviceConfig .SecurityOpt ,
86
+ Entrypoint : serviceConfig .Entrypoint ,
87
+ Name : serviceConfig .Name ,
88
+ Image : serviceConfig .Image ,
89
+ Hostname : serviceConfig .Hostname ,
90
+ Links : serviceConfig .Links ,
91
+ Privileged : serviceConfig .Privileged ,
92
+ ReadOnly : serviceConfig .ReadOnly ,
93
+ Command : serviceConfig .Command ,
94
+ User : serviceConfig .User ,
95
+ WorkingDirectory : serviceConfig .WorkingDir ,
96
+ }
97
+
98
+ extraHosts , err := utils .ConvertToExtraHosts (serviceConfig .ExtraHosts )
99
+ if err != nil {
100
+ return nil , err
101
+ }
102
+ c .ExtraHosts = extraHosts
103
+
104
+ if serviceConfig .DNS != nil {
105
+ c .DNSServers = serviceConfig .DNS
106
+ }
107
+ if serviceConfig .DNSSearch != nil {
108
+ c .DNSSearchDomains = serviceConfig .DNSSearch
109
+ }
110
+
111
+ if serviceConfig .Tmpfs != nil {
112
+ tmpfs := yaml .Stringorslice (serviceConfig .Tmpfs )
113
+ ecsTmpfs , err := utils .ConvertToTmpfs (tmpfs ) // TODO: change ConvertToTmpfs to take in []string
58
114
if err != nil {
59
115
return nil , err
60
116
}
61
- conConfigs = append ( conConfigs , * cCon )
117
+ c . Tmpfs = ecsTmpfs
62
118
}
119
+ if serviceConfig .Logging != nil {
120
+ logConfig := ecs.LogConfiguration {}
121
+ logConfig .SetLogDriver (serviceConfig .Logging .Driver )
63
122
64
- return & conConfigs , nil
65
- }
123
+ optionsMap := makePointerMapForStringMap (serviceConfig .Logging .Options )
124
+ logConfig .SetOptions (optionsMap )
125
+ c .LogConfiguration = & logConfig
126
+ }
127
+ if serviceConfig .Labels != nil {
128
+ labelsMap := makePointerMapForStringMap (serviceConfig .Labels )
129
+ c .DockerLabels = labelsMap
130
+ }
131
+ if len (serviceConfig .Ports ) > 0 {
132
+ var portMappings []* ecs.PortMapping
133
+ for _ , portConfig := range serviceConfig .Ports {
134
+ mapping := convertPortConfigToECSMapping (portConfig )
135
+ portMappings = append (portMappings , mapping )
136
+ }
137
+ c .PortMappings = portMappings
138
+ }
139
+ // TODO: reconcile with top-level Volumes key
140
+ if len (serviceConfig .Volumes ) > 0 {
141
+ mountPoints := []* ecs.MountPoint {}
66
142
67
- func convertToContainerConfig (serviceConfig types.ServiceConfig ) (* containerconfig.ContainerConfig , error ) {
68
- //TODO: fully convert ServiceConfig to ContainerConfig
69
- c := & containerconfig.ContainerConfig {
70
- Name : serviceConfig .Name ,
143
+ for _ , volConfig := range serviceConfig .Volumes {
144
+ if volConfig .Type == "volume" {
145
+ mp := & ecs.MountPoint {
146
+ ContainerPath : & volConfig .Target ,
147
+ SourceVolume : & volConfig .Source ,
148
+ ReadOnly : & volConfig .ReadOnly ,
149
+ }
150
+ mountPoints = append (mountPoints , mp )
151
+ } else {
152
+ log .Warnf ("Unsupported mount type found: %s" , volConfig .Type )
153
+ }
154
+ }
155
+ c .MountPoints = mountPoints
71
156
}
157
+
158
+ // TODO: add Environtment, EnvFile to ContainerConfig
159
+ // TODO: log out unsupported fields
72
160
return c , nil
73
161
}
74
162
@@ -79,3 +167,25 @@ func getWorkingDir(fileName string) (string, error) {
79
167
}
80
168
return filepath .Dir (pwd ), nil
81
169
}
170
+
171
+ func convertPortConfigToECSMapping (portConfig types.ServicePortConfig ) * ecs.PortMapping {
172
+ containerPort := int64 (portConfig .Target )
173
+ hostPort := int64 (portConfig .Published )
174
+
175
+ var ecsMapping = ecs.PortMapping {
176
+ ContainerPort : & containerPort ,
177
+ HostPort : & hostPort ,
178
+ Protocol : & portConfig .Protocol ,
179
+ }
180
+ return & ecsMapping
181
+ }
182
+
183
+ func makePointerMapForStringMap (m map [string ]string ) map [string ]* string {
184
+ pointerMap := make (map [string ]* string )
185
+ for k , v := range m {
186
+ val := new (string )
187
+ * val = v
188
+ pointerMap [k ] = val
189
+ }
190
+ return pointerMap
191
+ }
0 commit comments