-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (134 loc) · 3.94 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"strconv"
"time"
"os/exec"
"github.com/docker/cli/cli/compose/loader"
"github.com/docker/cli/cli/compose/schema"
composetypes "github.com/docker/cli/cli/compose/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
func init() {
pflag.StringP("compose-file", "c", "docker-compose.yml", "Path to a Compose file")
pflag.Bool("with-registry-auth", false, "Send registry authentication details to Swarm agents")
pflag.StringP("docker-binary", "d", "docker", "Alternative docker binary")
pflag.StringP("prefix", "p", "", "Prefix to be used for config prefix")
pflag.StringP("stack", "s", "", "Stack to be redployed")
pflag.StringP("workdir", "w", ".", "Specify workdir")
pflag.BoolP("output", "o", false, "Output YAML rather than redeploy")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
if viper.GetString("stack") == "" {
logrus.Fatal("Missing stack name")
}
}
func main() {
prefix := getPrefix()
config := parseComposefile(viper.GetString("compose-file"))
// Prefix toplevel configs
configMap := make(map[string]string)
newConfig := make(map[string]composetypes.ConfigObjConfig)
for oldKey, data := range config.Configs {
newKey := prefix + oldKey
configMap[oldKey] = newKey
newConfig[newKey] = data
}
config.Configs = newConfig
// prefix service configs
newServices := composetypes.Services{}
for _, serviceConfig := range config.Services {
var newServiceConfigConfig []composetypes.ServiceConfigObjConfig
for _, configData := range serviceConfig.Configs {
configData.Source = configMap[configData.Source]
newServiceConfigConfig = append(newServiceConfigConfig, configData)
}
serviceConfig.Configs = newServiceConfigConfig
newServices = append(newServices, serviceConfig)
}
config.Services = newServices
b, _ := yaml.Marshal(config)
if viper.GetBool("output") {
fmt.Println(string(b))
os.Exit(0)
}
// call docker deploy
f, err := ioutil.TempFile(viper.GetString("workdir"), prefix)
errorCheck(err)
if _, err := f.Write(b); err != nil {
errorCheck(err)
}
f.Close()
args := []string{"stack", "deploy", "-c"}
args = append(
args,
f.Name(),
)
if viper.GetBool("with-registry-auth") {
args = append(args, "--with-registry-auth")
}
args = append(args, viper.GetString("stack"))
cmd := exec.Command(viper.GetString("docker-binary"), args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
logrus.WithError(err).Error("Running docker failed")
}
errorCheck(os.Remove(viper.GetString("workdir") + "/" + f.Name()))
}
func parseComposefile(composeFile string) *composetypes.Config {
fileBytes, err := ioutil.ReadFile(composeFile)
errorCheck(err)
parsedConfig, err := loader.ParseYAML(fileBytes)
errorCheck(err)
data, err := composetypes.ConfigFile{
Filename: composeFile,
Config: parsedConfig,
}, nil
errorCheck(err)
var details composetypes.ConfigDetails
details.WorkingDir = "."
details.ConfigFiles = []composetypes.ConfigFile{data}
details.Version = schema.Version(details.ConfigFiles[0].Config)
details.Environment, err = buildEnvironment(os.Environ())
config, err := loader.Load(details)
errorCheck(err)
return config
}
func buildEnvironment(env []string) (map[string]string, error) {
result := make(map[string]string, len(env))
for _, s := range env {
// if value is empty, s is like "K=", not "K".
if !strings.Contains(s, "=") {
return result, errors.Errorf("unexpected environment %q", s)
}
kv := strings.SplitN(s, "=", 2)
result[kv[0]] = kv[1]
}
return result, nil
}
func getPrefix() string {
// determine prefix
prefix := viper.GetString("prefix")
if prefix == "" {
prefix = strconv.Itoa(
int(
time.Now().Unix(),
),
)
}
prefix = viper.GetString("stack") + "_" + prefix + "_"
return prefix
}
func errorCheck(err error) {
if err != nil {
logrus.WithError(err).Fatal()
}
}