This repository was archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
412 lines (387 loc) · 10.4 KB
/
reader.go
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package model
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
)
var idRegexp = regexp.MustCompile("^[a-z0-9\\-]+$")
type Descriptors struct {
descriptors map[string]*PodDescriptor
defaultVolumeBaseDir string
}
func NewDescriptors(defaultVolumeBaseDir string) *Descriptors {
return &Descriptors{map[string]*PodDescriptor{}, defaultVolumeBaseDir}
}
func (self *Descriptors) Descriptor(file string) (r *PodDescriptor, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("model: %s", e)
}
}()
file, err = filepath.Abs(file)
if err != nil {
err = fmt.Errorf("Invalid descriptor file path: %s", err)
return
}
file = path.Clean(filepath.ToSlash(file))
r = self.loadDescriptor(file)
return
}
func (self *Descriptors) loadDescriptor(filePath string) (r *PodDescriptor) {
defer func() {
if e := recover(); e != nil {
panic(fmt.Sprintf("%q: %s", filePath, e))
}
}()
r = self.descriptors[filePath]
if r == nil {
filePath = resolveDescriptorFile(filePath)
fileExt := filepath.Ext(filePath)
r = NewPodDescriptor()
if fileExt == ".yml" || fileExt == ".yaml" {
self.readDockerCompose(filePath, r)
} else {
readPodJson(filePath, r)
}
r.File = filePath
// Set defaults
for _, v := range r.Services {
if v.Entrypoint == nil {
v.Entrypoint = []string{}
}
if v.Command == nil {
v.Command = []string{}
}
if v.EnvFile == nil {
v.EnvFile = []string{}
}
if v.Environment == nil {
v.Environment = map[string]string{}
}
if v.Ports == nil {
v.Ports = []*PortBindingDescriptor{}
}
if v.Mounts == nil {
v.Mounts = map[string]string{}
}
}
if r.SharedKeys == nil {
r.SharedKeys = map[string]string{}
}
validate(r)
self.descriptors[filePath] = r
}
return r
}
func validate(d *PodDescriptor) {
assertTrue(d.Services != nil && len(d.Services) > 0, "empty", ".services")
for k, v := range d.Services {
kPath := ".services." + k
assertTrue(idRegexp.MatchString(k), "invalid service name", kPath)
assertTrue(len(v.Image) > 0 || v.Build != nil || v.Extends != nil, "empty", kPath+".{image|build|extends}")
assertTrue(v.Build == nil || len(v.Build.Context) > 0, "empty", kPath+".build.context")
assertTrue(v.Extends == nil || len(v.Extends.Service) > 0, "empty", kPath+".extends.service")
}
for k, v := range d.Volumes {
assertTrue(len(v.Source) > 0, "empty", ".volumes."+k+".source")
}
}
func resolveDescriptorFile(file string) string {
if !fileExists(file) {
panic("File does not exist")
}
if isDirectory(file) {
for _, f := range [3]string{"/pod.json", "/docker-compose.yml", "/docker-compose.yaml"} {
f = file + "/" + f
if fileExists(f) {
return f
}
}
panic("Descriptor file not found. Lookedup: pod.json, docker-compose.ya?ml")
}
return file
}
func readPodJson(file string, r *PodDescriptor) {
bytes := readFile(file)
err := json.Unmarshal(bytes, r)
panicOnError(err)
}
func (self *Descriptors) readDockerCompose(file string, r *PodDescriptor) {
c := dockerCompose{}
bytes := readFile(file)
err := yaml.Unmarshal(bytes, &c)
panicOnError(err)
self.transformDockerCompose(&c, r)
}
func readFile(file string) []byte {
b, e := ioutil.ReadFile(filepath.FromSlash(file))
panicOnError(e)
return b
}
func (self *Descriptors) transformDockerCompose(c *dockerCompose, r *PodDescriptor) {
version, err := strconv.ParseFloat(c.Version, 32)
if err != nil {
panic("Invalid version format: " + c.Version)
}
if version > 3 {
os.Stderr.WriteString("Warn: docker compose version >3 is not supported\n")
}
r.SharedKeys = map[string]string{}
for k, v := range c.Services {
p := "services." + k
s := &ServiceDescriptor{}
if v.Extends != nil {
s.Extends = &ServiceDescriptorExtension{v.Extends.File, v.Extends.Service}
}
if v.Image != "" {
if v.Build == nil {
s.Image = "docker://" + v.Image
} else {
s.Image = v.Image
}
}
s.Build = toServiceBuildDescriptor(v.Build, p+".build")
s.Entrypoint = toStringArray(v.Entrypoint, p+".entrypoint")
s.Command = toStringArray(v.Command, p+".command")
s.EnvFile = v.EnvFile
s.Environment = toStringMap(v.Environment, p+".environment")
if v.Hostname != "" {
r.Hostname = v.Hostname
}
if v.Domainname != "" {
r.Domainname = v.Domainname
}
if v.StopGracePeriod != "" {
r.StopGracePeriod = v.StopGracePeriod
}
s.Mounts = toVolumeMounts(v.Volumes, p+".volumes")
s.Ports = toPorts(v.Ports, p+".ports")
s.HealthCheck = toHealthCheckDescriptor(v.HealthCheck, p+".healthcheck")
if httpHost := s.Environment["HTTP_HOST"]; httpHost != "" {
httpPort := s.Environment["HTTP_PORT"]
if httpPort == "" {
panic("HTTP_HOST without HTTP_PORT env var defined in service: " + k)
}
r.SharedKeys["http/"+httpHost] = k + ":" + httpPort
}
r.Services[k] = s
}
for k := range c.Volumes {
r.Volumes[k] = &VolumeDescriptor{self.defaultVolumeBaseDir + "/" + k, "host", "false"}
}
}
func toPorts(p []string, path string) []*PortBindingDescriptor {
r := []*PortBindingDescriptor{}
for _, e := range p {
sp := strings.Split(e, "/")
if len(sp) > 2 {
panic(fmt.Sprintf("Invalid port entry %q at %s", e, path))
}
prot := "tcp"
if len(sp) == 2 {
prot = strings.ToLower(sp[1])
}
s := strings.Split(sp[0], ":")
if len(s) > 3 {
panic(fmt.Sprintf("Invalid port entry %q at %s", e, path))
}
var hostIP, hostPortExpr, podPortExpr string
switch len(s) {
case 1:
hostPortExpr = s[0]
podPortExpr = hostPortExpr
case 2:
hostPortExpr = s[0]
podPortExpr = s[1]
case 3:
hostIP = s[0]
hostPortExpr = s[1]
podPortExpr = s[2]
}
hostFrom, hostTo := toPortRange(hostPortExpr, path)
podFrom, podTo := toPortRange(podPortExpr, path)
rangeSize := podTo - podFrom
if (hostTo - hostFrom) != rangeSize {
panic(fmt.Sprintf("Port %q's range size differs between host and destination at %s", e, path))
}
for d := 0; d <= rangeSize; d++ {
r = append(r, &PortBindingDescriptor{NumberVal(strconv.Itoa(podFrom + d)), NumberVal(strconv.Itoa(hostFrom + d)), hostIP, prot})
}
}
return r
}
func toPortRange(rangeExpr string, path string) (from, to int) {
s := strings.Split(rangeExpr, "-")
if len(s) < 3 {
from, err := strconv.Atoi(s[0])
if err == nil {
if len(s) == 2 {
to, err := strconv.Atoi(s[1])
if err == nil && from <= to {
return from, to
}
} else {
to = from
return from, to
}
}
}
panic(fmt.Sprintf("Invalid port range %q at %s", rangeExpr, path))
}
func toVolumeMounts(dcVols []string, path string) map[string]string {
if dcVols == nil {
return nil
}
r := map[string]string{}
for _, e := range dcVols {
s := strings.SplitN(e, ":", 2)
if len(s) != 2 {
panic(fmt.Sprintf("Invalid volume entry %q at %s", e, path))
}
f := s[0]
r[s[1]] = f
}
return r
}
func toServiceBuildDescriptor(d interface{}, path string) *ServiceBuildDescriptor {
switch d.(type) {
case string:
return &ServiceBuildDescriptor{d.(string), "", nil}
case map[interface{}]interface{}:
m := d.(map[interface{}]interface{})
r := &ServiceBuildDescriptor{}
for k, v := range m {
ks := toString(k, path)
switch ks {
case "context":
r.Context = toString(v, path+"."+ks)
case "dockerfile":
r.Dockerfile = toString(v, path+"."+ks)
case "args":
r.Args = toStringMap(v, path+"."+ks)
}
}
return r
case nil:
return nil
default:
panic(fmt.Sprintf("string or []string expected at %s but was: %s", path, d))
}
}
func toHealthCheckDescriptor(c *dcHealthCheckDescriptor, path string) *HealthCheckDescriptor {
if c == nil {
return nil
} else {
test := toStringArray(c.Test, path)
if len(test) == 0 {
panic(fmt.Sprintf("%s: undefined health test command", path+".test"))
}
var cmd []string
switch test[0] {
case "CMD":
cmd = test[1:]
case "CMD-SHELL":
cmd = append([]string{"/bin/sh", "-c"}, test[1:]...)
default:
cmd = append([]string{"/bin/sh", "-c"}, strings.Join(test, " "))
}
interval := c.Interval
timeout := c.Timeout
return &HealthCheckDescriptor{cmd, "", interval, timeout, NumberVal(c.Retries), BoolVal(c.Disable)}
}
}
func toStringArray(v interface{}, path string) []string {
switch v.(type) {
case []interface{}:
l := v.([]interface{})
r := make([]string, len(l))
for i, u := range l {
r[i] = toString(u, path)
}
return r
case string:
return strings.Split(strings.Trim(v.(string), " "), " ")
case nil:
return []string{}
default:
panic(fmt.Sprintf("string or []string expected at %s but was: %s", path, v))
}
}
func toStringMap(v interface{}, path string) map[string]string {
switch v.(type) {
case map[interface{}]interface{}:
u := v.(map[interface{}]interface{})
r := map[string]string{}
for k, v := range u {
r[toString(k, path)] = toString(v, path)
}
return r
case []interface{}:
r := map[string]string{}
for _, u := range v.([]interface{}) {
e := toString(u, path)
s := strings.SplitN(e, "=", 2)
if len(s) != 2 {
panic(fmt.Sprintf("Invalid environment entry %q at %s", e, path))
}
r[s[0]] = s[1]
}
return r
case nil:
return map[string]string{}
default:
panic(fmt.Sprintf("map[string]string or []string expected at %s but was: %s", path, v))
}
}
func toString(v interface{}, ctx string) string {
switch v.(type) {
case string:
return v.(string)
case bool:
return strconv.FormatBool(v.(bool))
case int:
return strconv.Itoa(v.(int))
default:
panic(fmt.Sprintf("String expected at %s", ctx))
}
}
// See https://docs.docker.com/compose/compose-file/
type dockerCompose struct {
Version string
Services map[string]*dcServiceDescriptor
Volumes map[string]interface{}
}
type dcServiceDescriptor struct {
Extends *dcServiceDescriptorExtension
Image string
Build interface{} // string or map[interface{}]interface{}
Hostname string
Domainname string
Entrypoint interface{} // string or array
Command interface{} // string or array
EnvFile []string `yaml:"env_file"`
Environment interface{} // array of VAR=VAL or map
HealthCheck *dcHealthCheckDescriptor `yaml:"healthcheck"`
Ports []string
Volumes []string
StopGracePeriod string `yaml:"stop_grace_period"`
// TODO: Checkout 'secret' dc property
}
type dcServiceDescriptorExtension struct {
File string
Service string
}
type dcHealthCheckDescriptor struct {
Test interface{}
Interval string
Timeout string
Retries string
Disable string
}