-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
73 lines (59 loc) · 1.42 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
package main
import (
"context"
"flag"
"os"
"os/signal"
"time"
log "github.com/sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)
// Config for autoheal options
type Config struct {
Interval time.Duration
Actions []Action
}
func main() {
cfg := Config{Actions: defaultActions()}
flag.DurationVar(&cfg.Interval, "interval", 5*time.Second, "frequency interval")
flag.Parse()
log.SetLevel(log.DebugLevel)
serve(&cfg)
}
func serve(config *Config) {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
defer signal.Stop(sigint)
cli, err := client.NewClientWithOpts(client.WithVersion("1.37"))
if err != nil {
panic(err)
}
unhealthyFilter := filters.NewArgs(filters.KeyValuePair{Key: "health", Value: "unhealthy"})
healContainers := func(containers []types.Container, actions []Action) {
for _, container := range containers {
for _, action := range config.Actions {
if err := action(&container); err != nil {
log.Errorf(container.ID)
}
}
}
}
for {
log.Debug("check...")
unhealthy, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
Filters: unhealthyFilter,
})
if err != nil {
panic(err)
}
healContainers(unhealthy, config.Actions)
select {
case <-time.After(config.Interval):
case <-sigint:
log.Println("cleanup...")
os.Exit(0)
}
}
}