-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (80 loc) · 2.16 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
package main
import (
"context"
"flag"
"log"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/project0/cronify"
)
func main() {
DockerLabelFilter := flag.String("label", "cronify=true", "Discovery only container attached with this label")
flag.Parse()
// enforce older api version
// os.Setenv("DOCKER_API_VERSION", "1.26")
cl, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
cron := cronify.Cronify{
DockerClient: cl,
}
containerFilterArgs := filters.NewArgs()
if *DockerLabelFilter != "" {
containerFilterArgs.Add("label", *DockerLabelFilter)
}
log.Println("Synchronize with docker host")
container, err := cl.ContainerList(context.Background(), types.ContainerListOptions{
Filters: containerFilterArgs,
All: true,
})
if err != nil {
panic(err)
}
for _, c := range container {
job := cronify.ParseJobs(c.Labels, c.ID)
for _, j := range job {
log.Printf("Add job for container %s\n", c.ID)
cron.AddJob(j)
}
}
stopChan := cron.Start()
ctx := context.Background()
containerFilterArgs.Add("event", "create")
containerFilterArgs.Add("event", "update")
containerFilterArgs.Add("event", "destroy")
// watch rename?
containerFilterArgs.Add("type", "container")
chanEvents, chanErrors := cl.Events(ctx, types.EventsOptions{
Filters: containerFilterArgs,
})
log.Println("Watch for container events")
for {
select {
case event := <-chanEvents:
switch event.Action {
case "create":
info, err := cl.ContainerInspect(context.Background(), event.Actor.ID)
if err != nil {
log.Println("failed to inspect")
continue
}
job := cronify.ParseJobs(info.Config.Labels, info.ID)
for _, j := range job {
log.Printf("Add job for container %s\n", event.Actor.ID)
cron.AddJob(j)
}
case "destroy":
log.Printf("Remove jobs for container %s\n", event.Actor.ID)
cron.RemoveJobsByContainerID(event.Actor.ID)
default:
log.Printf("Unhandled event action: %s", event.Action)
}
case errors := <-chanErrors:
stopChan <- true
log.Panic(errors.Error())
return
}
}
}