-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
242 lines (221 loc) · 8.96 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
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
// Tool dockerize simplify running applications in docker containers.
package main
import (
"flag"
"fmt"
"log"
urlpkg "net/url"
"os"
"os/exec"
"path"
"runtime"
"strings"
"syscall"
"time"
)
const (
schemeFile = "file"
schemeTCP = "tcp"
schemeTCP4 = "tcp4"
schemeTCP6 = "tcp6"
schemeUnix = "unix"
schemeHTTP = "http"
schemeHTTPS = "https"
schemeAMQP = "amqp"
schemeAMQPS = "amqps"
defWaitTimeout = 10 * time.Second
defWaitRetryInterval = time.Second
exitCodeUsage = 2
exitCodeFatal = 123
)
// Read-only globals for use only within init() and main().
//
//nolint:gochecknoglobals // By design.
var (
app = strings.TrimSuffix(path.Base(os.Args[0]), ".test")
ver = "unknown" // set by ./release
cfg struct {
version bool
ini iniConfig
templatePaths stringsFlag // file or file:file or dir or dir:dir
template templateConfig
waitURLs urlsFlag
wait waitConfig
caCert string
tailStdout stringsFlag
tailStderr stringsFlag
exitCodeFatal int
waitList string
exec bool
}
)
// One-time initialization shared with tests.
func init() { //nolint:gochecknoinits // By design.
flag.BoolVar(&cfg.version, "version", false, "print version and exit")
flag.StringVar(&cfg.ini.source, "env", "", "path or URL to INI file with default values for unset env vars")
flag.BoolVar(&cfg.ini.options.AllowPythonMultilineValues, "multiline", false, "allow Python-like multi-line values in INI file")
flag.StringVar(&cfg.ini.section, "env-section", "", "section name in INI file")
flag.Var(&cfg.ini.headers, "env-header", "`name:value` or path to file containing name:value for HTTP header to send\n(if -env is an URL)\ncan be passed multiple times")
flag.Var(&cfg.templatePaths, "template", "template `src:dst` file or dir paths, :dst part is optional\ncan be passed multiple times")
flag.BoolVar(&cfg.template.noOverwrite, "no-overwrite", false, "do not overwrite existing destination file from template")
flag.BoolVar(&cfg.template.strict, "template-strict", false, "fail if template mention unset environment variable")
flag.Var(&cfg.template.delims, "delims", "action delimiters in templates")
flag.Var(&cfg.waitURLs, "wait", "wait for `url` (file/tcp/tcp4/tcp6/unix/http/https/amqp/amqps)\ncan be passed multiple times")
flag.Var(&cfg.wait.headers, "wait-http-header", "`name:value` for HTTP header to send\n(if -wait use HTTP)\ncan be passed multiple times")
flag.BoolVar(&cfg.wait.skipTLSVerify, "skip-tls-verify", false, "skip TLS verification for HTTPS/AMQPS -wait and -env urls")
flag.StringVar(&cfg.caCert, "cacert", "", "path to CA certificate for HTTPS/AMQPS -wait and -env urls")
flag.BoolVar(&cfg.wait.skipRedirect, "wait-http-skip-redirect", false, "do not follow HTTP redirects\n(if -wait use HTTP)")
flag.Var(&cfg.wait.statusCodes, "wait-http-status-code", "HTTP status `code` to wait for (2xx by default)\ncan be passed multiple times")
flag.DurationVar(&cfg.wait.timeout, "timeout", defWaitTimeout, "timeout for -wait")
flag.DurationVar(&cfg.wait.delay, "wait-retry-interval", defWaitRetryInterval, "delay before retrying failed -wait")
flag.Var(&cfg.tailStdout, "stdout", "file `path` to tail to stdout\ncan be passed multiple times")
flag.Var(&cfg.tailStderr, "stderr", "file `path` to tail to stderr\ncan be passed multiple times")
flag.IntVar(&cfg.exitCodeFatal, "exit-code", exitCodeFatal, "exit code for dockerize errors")
flag.StringVar(&cfg.waitList, "wait-list", "", "a space-separated list of URLs to wait for\ncan be combined with -wait flag")
flag.BoolVar(&cfg.exec, "exec", false, "replace dockerize process with given command")
flag.Usage = usage //nolint:reassign // By design.
}
func main() { //nolint:gocyclo,gocognit,funlen // TODO Refactor?
if !flag.Parsed() { // flags may be already parsed by tests
flag.Parse()
}
var iniURL, iniHTTP, templatePathBad, waitBadScheme, waitHTTP, waitAMQPS bool
if u, err := urlpkg.Parse(cfg.ini.source); err == nil && u.IsAbs() {
iniURL = true
iniHTTP = u.Scheme == schemeHTTP || u.Scheme == schemeHTTPS
}
for _, tmplPath := range cfg.templatePaths {
const maxParts = 2
parts := strings.Split(tmplPath, ":")
templatePathBad = templatePathBad || tmplPath == "" || parts[0] == "" || len(parts) > maxParts
}
waitListParts := strings.Fields(cfg.waitList)
for _, url := range waitListParts {
if err := cfg.waitURLs.Set(url); err != nil {
fatalFlagValue("unable to parse URLs list", "wait-list", url)
}
}
for _, u := range cfg.waitURLs {
switch u.Scheme {
case schemeFile, schemeTCP, schemeTCP4, schemeTCP6, schemeUnix:
case schemeHTTP, schemeHTTPS:
waitHTTP = true
case schemeAMQP:
case schemeAMQPS:
waitAMQPS = true
default:
waitBadScheme = true
}
}
switch {
case flag.NArg() == 0 && flag.NFlag() == 0:
flag.Usage()
os.Exit(exitCodeUsage)
case iniURL && !iniHTTP:
fatalFlagValue("scheme must be http/https", "env", cfg.ini.source)
case len(cfg.ini.headers) > 0 && !iniHTTP:
fatalFlagValue("require -env with HTTP url", "env-header", cfg.ini.headers)
case templatePathBad:
fatalFlagValue("require src:dst or src", "template", cfg.templatePaths)
case cfg.template.noOverwrite && len(cfg.templatePaths) == 0:
fatalFlagValue("require -template", "no-overwrite", cfg.template.noOverwrite)
case cfg.template.strict && len(cfg.templatePaths) == 0:
fatalFlagValue("require -template", "template-strict", cfg.template.strict)
case cfg.template.delims[0] != "" && len(cfg.templatePaths) == 0:
fatalFlagValue("require -template", "delims", cfg.template.delims)
case waitBadScheme:
fatalFlagValue("scheme must be file/tcp/tcp4/tcp6/unix/http/https/amqp/amqps", "wait", cfg.waitURLs)
case len(cfg.wait.headers) > 0 && !waitHTTP:
fatalFlagValue("require -wait with HTTP url", "wait-http-header", cfg.wait.headers)
case len(cfg.wait.statusCodes) > 0 && !waitHTTP:
fatalFlagValue("require -wait with HTTP url", "wait-http-status-code", cfg.wait.statusCodes)
case cfg.wait.skipRedirect && !waitHTTP:
fatalFlagValue("require -wait with HTTP url", "wait-http-skip-redirect", cfg.wait.skipRedirect)
case cfg.wait.skipTLSVerify && !iniHTTP && !waitHTTP && !waitAMQPS:
fatalFlagValue("require -wait/-env with HTTP url", "skip-tls-verify", cfg.wait.skipTLSVerify)
case cfg.caCert != "" && !iniHTTP && !waitHTTP && !waitAMQPS:
fatalFlagValue("require -wait/-env with HTTP url", "cacert", cfg.caCert)
case cfg.exec && len(cfg.tailStdout)+len(cfg.tailStderr) > 0:
fatalFlagValue("using -exec with -stdout/-stderr is not supported", "exec", cfg.exec)
case cfg.exec && flag.NArg() == 0:
fatalFlagValue("require command to exec", "exec", cfg.exec)
case cfg.version:
fmt.Println(app, ver, runtime.Version())
os.Exit(0)
}
var err error
cfg.ini.skipTLSVerify = cfg.wait.skipTLSVerify
cfg.wait.ca, err = LoadCACert(cfg.caCert)
if err != nil {
fatalf("Failed to load CA cert: %s", err)
}
cfg.ini.ca = cfg.wait.ca
if cfg.template.delims[0] == "" {
cfg.template.delims = [2]string{"{{", "}}"}
}
defaultEnv, err := loadINISection(cfg.ini)
if err != nil {
fatalf("Failed to load INI: %s.", err)
}
setDefaultEnv(defaultEnv)
cfg.template.data.Env = getEnv()
err = processTemplatePaths(cfg.template, cfg.templatePaths)
if err != nil {
fatalf("Failed to process templates: %s.", err)
}
err = waitForURLs(cfg.wait, cfg.waitURLs)
if err != nil {
fatalf("Failed to wait: %s.", err)
}
for _, tailPath := range cfg.tailStdout {
tailFile(tailPath, os.Stdout)
}
for _, tailPath := range cfg.tailStderr {
tailFile(tailPath, os.Stderr)
}
switch {
case cfg.exec:
arg0, err := exec.LookPath(flag.Arg(0))
if err == nil {
err = syscall.Exec(arg0, flag.Args(), os.Environ()) //nolint:gosec // False positive.
}
if err != nil {
fatalf("Failed to run command: %s.", err)
}
case flag.NArg() > 0:
code, err := runCmd(flag.Arg(0), flag.Args()[1:]...)
if err != nil {
fatalf("Failed to run command: %s.", err)
}
os.Exit(code)
case len(cfg.tailStdout)+len(cfg.tailStderr) > 0:
select {}
}
}
func warnIfFail(f func() error) {
if err := f(); err != nil {
log.Printf("Warning: %s.", err)
}
}
func fatalf(format string, v ...any) {
log.Printf(format, v...)
os.Exit(cfg.exitCodeFatal) //nolint:revive // By design.
}
func usage() {
fmt.Println(`Usage:
dockerize options [ command [ arg ... ] ]
dockerize [ options ] command [ arg ... ]
Utility to simplify running applications in docker containers.
Options:`)
flag.PrintDefaults()
fmt.Println()
fmt.Println(`Example: Generate /etc/nginx/nginx.conf using nginx.tmpl as a template, tail nginx logs, wait for a website to become available on port 8000 and then start nginx.`)
fmt.Println(`
dockerize -template nginx.tmpl:/etc/nginx/nginx.conf \
-stdout /var/log/nginx/access.log \
-stderr /var/log/nginx/error.log \
-wait tcp://web:8000 \
nginx -g 'daemon off;'
`)
fmt.Println(`For more information, see https://github.com/powerman/dockerize`)
}