This repository has been archived by the owner on Feb 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockeractd.go
209 lines (180 loc) · 4 KB
/
dockeractd.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
package dockeractd
import (
"bytes"
"encoding/json"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
"time"
dockerapi "github.com/fsouza/go-dockerclient"
)
type Dockeractd struct {
client *dockerapi.Client
endpoint string
retryInterval time.Duration
tlsCaFile string
tlsCertFile string
tlsKeyFile string
tlsVerify bool
cmd string
}
type optionInterface interface {
Cmd() string
Endpoint() string
RetryInterval() time.Duration
TLSVerify() bool
TLSCertFile() string
TLSKeyFile() string
TLSCaFile() string
}
type Options struct {
OptCmd string
OptEndpoint string
OptRetryInterval time.Duration
OptTLSCaFile string
OptTLSCertFile string
OptTLSKeyFile string
OptTLSVerify bool
}
func (o Options) Cmd() string { return o.OptCmd }
func (o Options) Endpoint() string { return o.OptEndpoint }
func (o Options) RetryInterval() time.Duration { return o.OptRetryInterval }
func (o Options) TLSCaFile() string { return o.OptTLSCaFile }
func (o Options) TLSCertFile() string { return o.OptTLSCertFile }
func (o Options) TLSKeyFile() string { return o.OptTLSKeyFile }
func (o Options) TLSVerify() bool { return o.OptTLSVerify }
func MakeDefaultOptions() *Options {
o := &Options{
OptEndpoint: "unix:///var/run/docker.sock",
OptRetryInterval: 5 * time.Second,
}
if v := os.Getenv("DOCKER_HOST"); v != "" {
o.OptEndpoint = v
}
if v := os.Getenv("DOCKER_CERT_PATH"); v != "" {
ok := 0
fn := filepath.Join(v, "cert.pem")
if _, err := os.Stat(fn); err == nil {
o.OptTLSCertFile = fn
ok++
}
fn = filepath.Join(v, "key.pem")
if _, err := os.Stat(fn); err == nil {
o.OptTLSKeyFile = fn
ok++
}
fn = filepath.Join(v, "ca.pem")
if _, err := os.Stat(fn); err == nil {
o.OptTLSCaFile = fn
ok++
}
if ok == 3 {
o.OptTLSVerify = true
}
}
return o
}
func New(o optionInterface) *Dockeractd {
if o == nil {
o = MakeDefaultOptions()
}
return &Dockeractd{
cmd: o.Cmd(),
endpoint: o.Endpoint(),
retryInterval: o.RetryInterval(),
tlsCaFile: o.TLSCaFile(),
tlsCertFile: o.TLSCertFile(),
tlsKeyFile: o.TLSKeyFile(),
tlsVerify: o.TLSVerify(),
}
}
func (d *Dockeractd) Run() error {
loop := true
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
for loop {
select {
case <-sigCh:
loop = false
continue
default:
}
err := d.attachToDocker()
if err != nil {
if t := d.retryInterval; t > 0 {
time.Sleep(t)
}
continue
}
// wait for events
events := make(chan *dockerapi.APIEvents)
if err = d.client.AddEventListener(events); err != nil {
log.Printf("Error receiving events: %s", err)
continue
}
INNER:
for {
select {
case <-sigCh:
loop = false
break INNER
case ev := <-events:
if ev == nil {
loop = false
break INNER
}
if err := d.process(ev); err != nil {
log.Printf("Error executing script: %s", err)
}
}
}
}
return nil
}
func (d *Dockeractd) attachToDocker() error {
var client *dockerapi.Client
var err error
log.Printf("Attaching to %s", d.endpoint)
if d.tlsVerify {
log.Printf("Enabling TLS...")
client, err = dockerapi.NewTLSClient(
d.endpoint,
d.tlsCertFile,
d.tlsKeyFile,
d.tlsCaFile,
)
} else {
client, err = dockerapi.NewClient(d.endpoint)
}
if err != nil {
log.Printf("err =%s\n", err)
return err
}
d.client = client
return nil
}
func (d *Dockeractd) process(ev *dockerapi.APIEvents) error {
// It's okay if we can't get the container
container, _ := d.client.InspectContainer(ev.ID)
payload := struct {
Event *dockerapi.APIEvents
Container *dockerapi.Container
}{
ev,
container,
}
cmd := exec.Command(d.cmd)
buf := &bytes.Buffer{}
cmd.Stdin = buf
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
enc := json.NewEncoder(buf)
enc.Encode(payload)
if err := cmd.Run(); err != nil {
return err
}
return nil
}