-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (65 loc) · 1.54 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
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() {
since := time.Now()
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "usage: %s [flags] [container_id_or_name ...]\n\nflags:\n", os.Args[0])
flag.PrintDefaults()
}
failOnUnhealthy := flag.Bool("fail-on-unhealthy", false, "fail on unhealthy")
timeout := flag.Duration("timeout", time.Hour, "timeout after waiting")
flag.Parse()
cli, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
fail(err)
}
c := Containers{}
for _, container := range flag.Args() {
ID, container, err := containerInfo(container, cli, since)
if err != nil {
fail(err)
}
c.Add(ID, *container)
}
if c.Healthy() {
os.Exit(0)
}
if err := c.Unhealthy(); err != nil && *failOnUnhealthy {
fail(err)
}
if _, err := listen(c, since, *timeout, *failOnUnhealthy); err != nil {
fail(err)
}
}
func containerInfo(containerID string, cli *client.Client, since time.Time) (string, *Container, error) {
info, err := cli.ContainerInspect(context.Background(), containerID)
if err != nil {
return "", nil, err
}
state := types.NoHealthcheck
if info.State.Health != nil {
state = info.State.Health.Status
}
c := &Container{
Status: state,
Changed: since,
Name: strings.TrimLeft(info.Name, "/"),
}
return info.ID, c, nil
}
func fail(err error) {
fmt.Fprintf(flag.CommandLine.Output(), "%s", err)
os.Exit(1)
}