This repository was archived by the owner on Dec 26, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
85 lines (72 loc) · 1.88 KB
/
app.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
package main
import (
"fmt"
"io/ioutil"
"path"
"gopkg.in/yaml.v2"
"github.com/docker/libcompose/config"
"github.com/docker/libcompose/docker"
"github.com/docker/libcompose/project"
)
// Defines app package format
type App struct {
Project project.APIProject
Name string
Metadata AppMetadata
}
type AppMetadata struct {
Name string `json:"name"`
Level string `json:"level"`
Description string `json:"description"`
IconUrl string `json:"iconUrl" yaml:"icon"`
ProjectUrl string `json:"projectUrl" yaml:"url"`
}
func parseProject(composePath string) (project.APIProject, error) {
appName := path.Base(path.Dir(composePath))
assignTraefikRule := func(configs map[string]*config.ServiceConfig) (map[string]*config.ServiceConfig, error) {
for _, service := range configs {
if service.Labels == nil {
service.Labels = map[string]string{}
}
service.Labels["traefik.frontend.rule"] = fmt.Sprintf("Host:%s.localhost", appName)
}
return configs, nil
}
return docker.NewProject(&docker.Context{
Context: project.Context{
ComposeFiles: []string{composePath},
ProjectName: appName,
},
}, &config.ParseOptions{
Interpolate: true,
Validate: true,
Postprocess: assignTraefikRule,
})
}
func parseMetadata(metadataPath string) (AppMetadata, error) {
var metadata AppMetadata
data, err := ioutil.ReadFile(metadataPath)
if err != nil {
return metadata, err
}
yaml.Unmarshal(data, &metadata)
return metadata, err
}
func ParseApp(appDir string) (App, error) {
name := path.Base(appDir)
composePath := path.Join(appDir, "docker-compose.yml")
metadataPath := path.Join(appDir, "metadata.yml")
project, err := parseProject(composePath)
if err != nil {
return App{}, err
}
metadata, err := parseMetadata(metadataPath)
if err != nil {
return App{}, nil
}
return App{
Name: name,
Metadata: metadata,
Project: project,
}, err
}