/
boot.go
225 lines (180 loc) · 4.99 KB
/
boot.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
package main
import (
"bufio"
"bytes"
"net"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"github.com/ActiveState/tail"
"github.com/Sirupsen/logrus"
"github.com/coreos/go-etcd/etcd"
"github.com/deis/deis/router/logger"
)
var log = logrus.New()
const (
timeout time.Duration = 10 * time.Second
ttl time.Duration = timeout * 2
gitLogFile string = "/opt/nginx/logs/git.log"
nginxAccessLog string = "/opt/nginx/logs/access.log"
nginxErrorLog string = "/opt/nginx/logs/error.log"
)
func main() {
log.Formatter = new(logger.StdOutFormatter)
logLevel := getopt("LOG", "info")
if level, err := logrus.ParseLevel(logLevel); err == nil {
log.Level = level
}
log.Debug("reading environment variables...")
host := getopt("HOST", "127.0.0.1")
etcdPort := getopt("ETCD_PORT", "4001")
etcdPath := getopt("ETCD_PATH", "/deis/router")
hostEtcdPath := getopt("HOST_ETCD_PATH", "/deis/router/hosts/"+host)
externalPort := getopt("EXTERNAL_PORT", "80")
client := etcd.NewClient([]string{"http://" + host + ":" + etcdPort})
// wait until etcd has discarded potentially stale values
time.Sleep(timeout + 1)
log.Debug("creating required defaults in etcd...")
mkdirEtcd(client, "/deis/controller")
mkdirEtcd(client, "/deis/services")
mkdirEtcd(client, "/deis/domains")
mkdirEtcd(client, "/deis/builder")
mkdirEtcd(client, "/deis/certs")
mkdirEtcd(client, "/deis/router/hosts")
setDefaultEtcd(client, etcdPath+"/gzip", "on")
log.Info("Starting Nginx...")
// tail the logs
go tailFile(nginxAccessLog)
go tailFile(nginxErrorLog)
go tailFile(gitLogFile)
nginxChan := make(chan bool)
go launchNginx(nginxChan)
<-nginxChan
// FIXME: have to launch cron first so generate-certs will generate the files nginx requires
go launchCron()
waitForInitialConfd(host+":"+etcdPort, timeout)
go launchConfd(host + ":" + etcdPort)
go publishService(client, hostEtcdPath, host, externalPort, uint64(ttl.Seconds()))
log.Info("deis-router running...")
exitChan := make(chan os.Signal, 2)
signal.Notify(exitChan, syscall.SIGTERM, syscall.SIGINT)
go cleanOnExit(exitChan)
<-exitChan
}
func launchCron() {
// edit crontab
crontab := `(echo "* * * * * generate-certs >> /dev/stdout") | crontab -`
cmd := exec.Command("bash", "-c", crontab)
if err := cmd.Run(); err != nil {
log.Fatalf("could not write to crontab: %v", err)
}
// run cron
cmd = exec.Command("cron")
if err := cmd.Run(); err != nil {
log.Fatalf("cron terminated by error: %v", err)
}
}
func cleanOnExit(exitChan chan os.Signal) {
for _ = range exitChan {
tail.Cleanup()
}
}
// Wait until the compilation of the templates
func waitForInitialConfd(etcd string, timeout time.Duration) {
for {
var buffer bytes.Buffer
output := bufio.NewWriter(&buffer)
log.Debugf("Connecting to etcd server %s", etcd)
cmd := exec.Command("confd", "-node", etcd, "--confdir", "/app", "-onetime", "--log-level", "error")
cmd.Stdout = output
cmd.Stderr = output
err := cmd.Run()
output.Flush()
if err == nil {
break
}
log.Info("waiting for confd to write initial templates...")
log.Debugf("\n%s", buffer.String())
time.Sleep(timeout)
}
}
func launchConfd(etcd string) {
cmd := exec.Command("confd", "-node", etcd, "--confdir", "/app", "--log-level", "error", "--interval", "5")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
log.Warn("confd terminated by error: %v", err)
}
}
func launchNginx(nginxChan chan bool) {
cmd := exec.Command("/opt/nginx/sbin/nginx", "-c", "/opt/nginx/conf/nginx.conf")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
log.Warn("Nginx terminated by error: %v", err)
}
// Wait until the nginx is available
for {
_, err := net.DialTimeout("tcp", "127.0.0.1:80", timeout)
if err == nil {
nginxChan <- true
break
}
}
if err := cmd.Wait(); err != nil {
log.Warnf("Nginx terminated by error: %v", err)
} else {
log.Info("Reloading nginx (change in configuration)...")
}
}
func tailFile(path string) {
mkfifo(path)
t, _ := tail.TailFile(path, tail.Config{Follow: true})
for line := range t.Lines {
log.Info(line.Text)
}
}
func mkfifo(path string) {
os.Remove(path)
if err := syscall.Mkfifo(path, syscall.S_IFIFO|0666); err != nil {
log.Fatalf("%v", err)
}
}
func publishService(
client *etcd.Client,
etcdPath string,
host string,
externalPort string,
ttl uint64) {
for {
setEtcd(client, etcdPath, host+":"+externalPort, ttl)
time.Sleep(timeout)
}
}
func setEtcd(client *etcd.Client, key, value string, ttl uint64) {
_, err := client.Set(key, value, ttl)
if err != nil {
log.Warn(err)
}
}
func setDefaultEtcd(client *etcd.Client, key, value string) {
_, err := client.Set(key, value, 0)
if err != nil {
log.Warn(err)
}
}
func mkdirEtcd(client *etcd.Client, path string) {
_, err := client.CreateDir(path, 0)
if err != nil {
log.Warn(err)
}
}
func getopt(name, dfault string) string {
value := os.Getenv(name)
if value == "" {
value = dfault
}
return value
}