-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlistener.go
67 lines (54 loc) · 1.18 KB
/
listener.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
package main
import (
"io"
"github.com/spf13/afero"
)
// Listener holds config for an event listener and is used to listen for container events
type Listener struct {
Client *Client
Fs afero.Fs
}
// NewListener returns a new Listener
func NewListener() (*Listener, error) {
client, err := NewClient()
if err != nil {
log.Errorf("Unable to connect to Docker: %s", err)
return nil, err
}
listener := &Listener{
Client: client,
Fs: afero.NewOsFs(),
}
return listener, nil
}
// Revive revives tunnels for currently running containers
func (l *Listener) Revive() error {
handler := NewHandler(l.Client)
containers, err := l.Client.ListContainers()
if err != nil {
return err
}
for _, c := range containers {
err := handler.HandleContainer(c.ID)
if err != nil {
return err
}
}
return nil
}
// Listen listens for container events to be handled
func (l *Listener) Listen() {
log.Info("Hera is listening")
handler := NewHandler(l.Client)
messages, errs := l.Client.Events()
for {
select {
case event := <-messages:
handler.HandleEvent(event)
case err := <-errs:
if err != nil && err != io.EOF {
log.Error(err.Error())
}
}
}
}