forked from deis/deis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattacher.go
211 lines (196 loc) · 4.46 KB
/
attacher.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
210
211
package main
import (
"bufio"
"io"
"log"
"strings"
"sync"
"github.com/fsouza/go-dockerclient"
)
type AttachManager struct {
sync.Mutex
attached map[string]*LogPump
channels map[chan *AttachEvent]struct{}
client *docker.Client
}
func NewAttachManager(client *docker.Client) *AttachManager {
m := &AttachManager{
attached: make(map[string]*LogPump),
channels: make(map[chan *AttachEvent]struct{}),
client: client,
}
containers, err := client.ListContainers(docker.ListContainersOptions{})
assert(err, "attacher")
for _, listing := range containers {
m.attach(listing.ID[:12])
}
go func() {
events := make(chan *docker.APIEvents)
assert(client.AddEventListener(events), "attacher")
for msg := range events {
debug("event:", msg.ID[:12], msg.Status)
if msg.Status == "start" {
go m.attach(msg.ID[:12])
}
}
log.Fatal("ruh roh") // todo: loop?
}()
return m
}
func (m *AttachManager) attach(id string) {
container, err := m.client.InspectContainer(id)
assert(err, "attacher")
name := container.Name[1:]
success := make(chan struct{})
failure := make(chan error)
outrd, outwr := io.Pipe()
errrd, errwr := io.Pipe()
go func() {
err := m.client.AttachToContainer(docker.AttachToContainerOptions{
Container: id,
OutputStream: outwr,
ErrorStream: errwr,
Stdin: false,
Stdout: true,
Stderr: true,
Stream: true,
Success: success,
})
outwr.Close()
errwr.Close()
debug("attach:", id, "finished")
if err != nil {
close(success)
failure <- err
}
m.send(&AttachEvent{Type: "detach", ID: id, Name: name})
m.Lock()
delete(m.attached, id)
m.Unlock()
}()
_, ok := <-success
if ok {
m.Lock()
m.attached[id] = NewLogPump(outrd, errrd, id, name)
m.Unlock()
success <- struct{}{}
m.send(&AttachEvent{ID: id, Name: name, Type: "attach"})
debug("attach:", id, name, "success")
return
}
debug("attach:", id, "failure:", <-failure)
}
func (m *AttachManager) send(event *AttachEvent) {
m.Lock()
defer m.Unlock()
for ch, _ := range m.channels {
// TODO: log err after timeout and continue
ch <- event
}
}
func (m *AttachManager) addListener(ch chan *AttachEvent) {
m.Lock()
defer m.Unlock()
m.channels[ch] = struct{}{}
go func() {
for id, pump := range m.attached {
ch <- &AttachEvent{ID: id, Name: pump.Name, Type: "attach"}
}
}()
}
func (m *AttachManager) removeListener(ch chan *AttachEvent) {
m.Lock()
defer m.Unlock()
delete(m.channels, ch)
}
func (m *AttachManager) Get(id string) *LogPump {
m.Lock()
defer m.Unlock()
return m.attached[id]
}
func (m *AttachManager) Listen(source *Source, logstream chan *Log, closer <-chan bool) {
if source == nil {
source = new(Source)
}
depth := len(m.attached)
if depth == 0 {
depth = 1
}
events := make(chan *AttachEvent, depth)
m.addListener(events)
defer m.removeListener(events)
for {
select {
case event := <-events:
if event.Type == "attach" && (source.All() ||
(source.ID != "" && strings.HasPrefix(event.ID, source.ID)) ||
(source.Name != "" && event.Name == source.Name) ||
(source.Filter != "" && strings.Contains(event.Name, source.Filter))) {
pump := m.Get(event.ID)
pump.AddListener(logstream)
defer func() {
if pump != nil {
pump.RemoveListener(logstream)
}
}()
} else if source.ID != "" && event.Type == "detach" &&
strings.HasPrefix(event.ID, source.ID) {
return
}
case <-closer:
return
}
}
}
type LogPump struct {
sync.Mutex
ID string
Name string
channels map[chan *Log]struct{}
}
func NewLogPump(stdout, stderr io.Reader, id, name string) *LogPump {
obj := &LogPump{
ID: id,
Name: name,
channels: make(map[chan *Log]struct{}),
}
pump := func(typ string, source io.Reader) {
buf := bufio.NewReader(source)
for {
data, err := buf.ReadBytes('\n')
if err != nil {
if err != io.EOF {
debug("pump:", id, typ+":", err)
}
return
}
obj.send(&Log{
Data: strings.TrimSuffix(string(data), "\n"),
ID: id,
Name: name,
Type: typ,
})
}
}
go pump("stdout", stdout)
go pump("stderr", stderr)
return obj
}
func (o *LogPump) send(log *Log) {
o.Lock()
defer o.Unlock()
for ch, _ := range o.channels {
// TODO: log err after timeout and continue
ch <- log
}
}
func (o *LogPump) AddListener(ch chan *Log) {
o.Lock()
defer o.Unlock()
o.channels[ch] = struct{}{}
}
func (o *LogPump) RemoveListener(ch chan *Log) {
o.Lock()
defer o.Unlock()
delete(o.channels, ch)
}