-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.go
191 lines (156 loc) · 4.3 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
// Icinga 2 Docker image | (c) 2020 Icinga GmbH | GPLv2+
package main
import (
"bufio"
"fmt"
"github.com/otiai10/copy"
"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"syscall"
"time"
)
const ca = "/var/lib/icinga2/certs/ca.crt"
const crtMode = 0640
const mSmtpRc = "/var/lib/icinga2/.msmtprc"
func main() {
if err := entrypoint(); err != nil {
logf(critical, "%s", err.Error())
os.Exit(1)
}
}
func entrypoint() error {
command := os.Args[1:]
if len(command) < 1 {
logf(warning, "Nothing to do.")
return nil
}
if os.Getpid() == 1 {
logf(info, "Initializing /data as we're the init process (PID 1)")
for _, dir := range []string{"etc", "var/cache", "var/lib", "var/log", "var/run", "var/spool"} {
dest := path.Join("/data", dir, "icinga2")
logf(info, "Checking %#v", dest)
if _, errSt := os.Stat(dest); errSt != nil {
if os.IsNotExist(errSt) {
src := path.Join("/data-init", dir, "icinga2")
logf(info, "Copying %#v to %#v", src, dest)
if errMA := os.MkdirAll(path.Dir(dest), 0755); errMA != nil {
return errMA
}
if errCp := copy.Copy(src, dest); errCp != nil {
return errCp
}
} else {
return errSt
}
}
}
logf(info, "Checking %#v", ca)
if _, errSt := os.Stat(ca); errSt != nil {
if os.IsNotExist(errSt) {
nodeSetup := []string{"node", "setup"}
runNodeSetup := false
for _, env := range os.Environ() {
if kv := strings.SplitN(env, "=", 2); len(kv) == 2 {
if strings.HasPrefix(kv[0], "ICINGA_") {
switch kv[0] = strings.ToLower(strings.TrimPrefix(kv[0], "ICINGA_")); kv[0] {
case "accept_commands", "accept_config", "disable_confd", "master":
runNodeSetup = true
if kv[1] == "1" {
nodeSetup = append(nodeSetup, "--"+strings.ReplaceAll(kv[0], "_", "-"))
}
case "cn", "endpoint", "global_zones", "listen",
"parent_host", "parent_zone", "ticket", "zone":
runNodeSetup = true
nodeSetup = append(nodeSetup, "--"+kv[0], kv[1])
case "trustedcert":
logf(info, "Writing trusted certificate")
runNodeSetup = true
dir, errTD := ioutil.TempDir("", "")
if errTD != nil {
return errTD
}
file := path.Join(dir, "trusted.crt")
if errWF := ioutil.WriteFile(file, []byte(kv[1]), crtMode); errWF != nil {
return errWF
}
nodeSetup = append(nodeSetup, "--"+kv[0], file)
case "cacert":
logf(info, "Writing CA certificate")
runNodeSetup = true
if errWF := ioutil.WriteFile(ca, []byte(kv[1]), crtMode); errWF != nil {
return errWF
}
}
}
}
}
if runNodeSetup {
logf(info, "Running 'node setup'")
cmd := exec.Command("icinga2", nodeSetup...)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if errRn := cmd.Run(); errRn != nil {
return errRn
}
}
} else {
return errSt
}
}
if mSmtpCfg, ok := os.LookupEnv("MSMTPRC"); ok {
logf(info, "Writing %#v", mSmtpRc)
if errWF := ioutil.WriteFile(mSmtpRc, []byte(mSmtpCfg), 0644); errWF != nil {
return errWF
}
}
command = append([]string{"dumb-init", "-c", "--"}, command...)
}
path := command[0]
if filepath.Base(path) == path {
logf(info, "Looking up %#v in $PATH", path)
abs, errLP := exec.LookPath(path)
if errLP != nil {
return errLP
}
path = abs
}
logf(info, "Running %#v", path)
return syscall.Exec(path, command, os.Environ())
}
type logSeverity uint8
const (
info logSeverity = iota
warning
critical
)
var out = bufio.NewWriter(os.Stderr)
var isTerminal = terminal.IsTerminal(int(os.Stderr.Fd()))
func logf(severity logSeverity, format string, a ...interface{}) {
var color, colorOff, seeverity string
switch severity {
case info:
color = "\x1b[32m"
seeverity = "information"
case warning:
color = "\x1b[33m\x1b[1m"
seeverity = "warning"
case critical:
color = "\x1b[31m\x1b[1m"
seeverity = "critical"
}
if isTerminal {
colorOff = "\x1b[0m"
} else {
color = ""
}
_, _ = fmt.Fprintf(out, "[%s] ", time.Now().Format("2006-01-02 15:04:05 -0700"))
_, _ = fmt.Fprintf(out, "%s%s%s/DockerEntrypoint: ", color, seeverity, colorOff)
_, _ = fmt.Fprintf(out, format, a...)
_, _ = fmt.Fprintln(out)
_ = out.Flush()
}