-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
122 lines (105 loc) · 2.84 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
package main
import (
"context"
"github.com/docker/docker/api/types/events"
"log"
"os"
"os/signal"
"regexp"
"runtime"
"syscall"
"time"
"github.com/docker/docker/client"
)
const (
programName = "container-hoster"
programURL = "github.com/wollomatic/container-hoster"
)
var (
refreshHostsfileNeeded = true
networkRegexpCompiled *regexp.Regexp
version = "develop" // will be set in Github Action
)
func main() {
log.Printf("--- Starting %s %s (%s, %s, %s) %s ---\n", programName, version, runtime.GOOS, runtime.GOARCH, runtime.Version(), programURL)
conf.getFromENV()
conf.logConfig()
networkRegexpCompiled = regexp.MustCompile(conf.networkRegexp)
// check if hostsfile is writable
_, err := os.OpenFile(conf.hostsfile, os.O_WRONLY, 0644) // #nosec G302 -- hostsfile needs 644 permissions
if err != nil {
log.Fatalf("Error: Hostsfile %s ist not writable: %s", conf.hostsfile, err)
}
// stop signal listener
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// create docker client
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
defer func(cli *client.Client) {
err := cli.Close()
if err != nil {
log.Println("Error closing docker client:", err)
}
}(cli)
// check if docker is running
_, err = cli.Ping(context.Background())
if err != nil {
log.Fatalf("Docker error: %v", err)
}
// create background job for refreshing the hosts file
fch := make(chan error)
go refreshHostsfileJob(fch, cli)
// create listener for docker events
ch, ech := cli.Events(context.Background(), events.ListOptions{})
log.Println("everything seems okay, waiting for things to happen")
// wait for things to happen
for {
select {
case event := <-ch:
switch event.Action {
case "start", "stop", "die", "destroy", "rename":
if conf.logEvents {
log.Println(event.Action, event.Actor.Attributes["name"])
}
refreshHostsfileNeeded = true
}
case err := <-ech:
log.Println("Error updating hostsfile:", err)
gracefulShutdown(1)
case err := <-fch:
log.Println("Docker event Error:", err)
gracefulShutdown(2)
case sig := <-done: // graceful shutdown
log.Println("Received stop signal: ", sig)
gracefulShutdown(0)
}
}
}
// refreshHostsfileJob is a background job for refreshing the hosts file
func refreshHostsfileJob(ech chan<- error, cli *client.Client) {
for {
if refreshHostsfileNeeded {
refreshHostsfileNeeded = false
if conf.logEvents {
log.Println("writing hosts file")
}
err := refreshHostsfile(cli)
if err != nil {
ech <- err
}
}
time.Sleep(conf.refreshHostsfileInterval)
}
}
// gracefulShutdown stops the program
func gracefulShutdown(i int) {
err := writeHostsfile([]byte{})
if err != nil {
log.Fatalf("Error: %v", err)
}
log.Println("exit with code", i)
os.Exit(i)
}