-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathentrypoint.go
228 lines (198 loc) · 5.37 KB
/
entrypoint.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
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"
)
// Replacement represents a key to find and value to replace it with
type Replacement struct {
Key string
Value string
}
// EnvVar represents expected environment variables, whether they are required, and a description for error reporting
type EnvVar struct {
Name string
Required bool
Desc string
Default string
}
func main() {
var configFile string
flag.StringVar(&configFile, "c", "/etc/traefik/traefik.toml", "Traefik config file to use, default: /etc/traefik/traefik.toml")
flag.Parse()
if _, err := os.Stat(configFile); err != nil {
fmt.Println("Config file", configFile, "not found")
os.Exit(1)
}
if len(os.Args) <= 1 {
fmt.Println("You must provide a command to run after entrypoint process completes. You probably want: /traefik")
}
replacements, err := BuildReplacementsFromEnv()
handleError(err)
configToml, err := ReadTraefikToml(configFile)
handleError(err)
configToml = UpdateConfigContent(configToml, replacements)
err = WriteTraefikToml(configFile, configToml)
handleError(err)
runCmd()
os.Exit(0)
}
// Run CMD specified in Dockerfile or runtime and send output to stdout
func runCmd() {
executable := os.Args[1]
args := os.Args[2:]
cmd := exec.Command(executable, args...)
cmdStdout, err := cmd.StdoutPipe()
handleError(err)
scanner := bufio.NewScanner(cmdStdout)
go func() {
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}()
err = cmd.Start()
handleError(err)
err = cmd.Wait()
handleError(err)
}
func handleError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// ReadTraefikToml reads the Traefik config file from filesystem and returns as byte array
func ReadTraefikToml(filename string) ([]byte, error) {
file, err := ioutil.ReadFile(filename)
if err != nil {
return []byte{}, fmt.Errorf("Unable to read config file at %s", filename)
}
return file, nil
}
// WriteTraefikToml writes updated Traefix config to filesystem
func WriteTraefikToml(filename string, contents []byte) error {
err := ioutil.WriteFile(filename, contents, 0644)
if err != nil {
return err
}
return nil
}
// UpdateConfigContent replaces placeholders with values from environment variables
func UpdateConfigContent(config []byte, replacements []Replacement) []byte {
for _, rep := range replacements {
regex := regexp.MustCompile(rep.Key)
config = regex.ReplaceAll(config, []byte(rep.Value))
}
return config
}
// BuildReplacementsFromEnv Build []Replacement from env vars
func BuildReplacementsFromEnv() ([]Replacement, error) {
var configReplacements []Replacement
envVars := GetEnvVarModels()
for _, envvar := range envVars {
value := os.Getenv(envvar.Name)
if value == "" && envvar.Required {
return configReplacements, fmt.Errorf("Missing required env var: %s. Description: %s", envvar.Name, envvar.Desc)
}
if value != "" {
if envvar.Name == "LETS_ENCRYPT_CA" {
if value == "staging" {
value = "https://acme-staging.api.letsencrypt.org/directory"
} else if value == "production" {
value = "https://acme-v01.api.letsencrypt.org/directory"
}
} else if envvar.Name == "SANS" {
sans := strings.Split(value, ",")
value = ""
for _, san := range sans {
value += "\"" + san + "\", "
}
value = strings.TrimRight(value, ", ")
}
configReplacements = append(configReplacements, Replacement{
Key: envvar.Name,
Value: value,
})
} else if value == "" && !envvar.Required {
value = envvar.Default
}
}
return configReplacements, nil
}
// GetEnvVarModels returns an array of EnvVar objects
func GetEnvVarModels() []EnvVar {
envVars := []EnvVar{
{
Name: "LETS_ENCRYPT_EMAIL",
Required: true,
Desc: "An email address is required for LETS_ENCRYPT_EMAIL",
Default: "",
},
{
Name: "LETS_ENCRYPT_CA",
Required: true,
Desc: "Which CA to use, either staging or production. Default: staging",
Default: "staging",
},
{
Name: "TLD",
Required: true,
Desc: "TLD is required for use as main domain on certificate, ex: domain.com",
Default: "",
},
{
Name: "SANS",
Required: true,
Desc: "SANS is required as comma separated list of FQDNs to list on SAN certificate, ex: app.domain.com,other.domain.com",
Default: "",
},
{
Name: "DNS_PROVIDER",
Required: false,
Desc: "Which supported DNS provider to use with Lets Encrypt for validation. You must also set env vars for any other values the DNS provider needs",
Default: "cloudflare",
},
{
Name: "BACKEND1_URL",
Required: true,
Desc: "Url to first backend, ex: http://app:80",
Default: "",
},
{
Name: "FRONTEND1_DOMAIN",
Required: true,
Desc: "Domain for first frontend, ex: app.domain.com",
Default: "",
},
{
Name: "BACKEND2_URL",
Required: false,
Desc: "Url to second backend, ex: http://other:80",
Default: "",
},
{
Name: "FRONTEND2_DOMAIN",
Required: false,
Desc: "Domain for second frontend, ex: otherapp.domain.com",
Default: "",
},
{
Name: "BACKEND3_URL",
Required: false,
Desc: "Url to third backend, ex: http://third:80",
Default: "",
},
{
Name: "FRONTEND3_DOMAIN",
Required: false,
Desc: "Domain for third frontend, ex: thirdapp.domain.com",
Default: "",
},
}
return envVars
}