-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathadapter.go
211 lines (167 loc) · 4.87 KB
/
adapter.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 ephemerald
import (
"context"
"io"
"io/ioutil"
"strconv"
"github.com/Sirupsen/logrus"
"github.com/boz/ephemerald/config"
"github.com/boz/ephemerald/params"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/docker/registry"
"github.com/docker/go-connections/nat"
)
type dockerAdapter interface {
imageReference() reference.Named
ensureImage() error
createContainer() (string, error)
containerStart(id string, options types.ContainerStartOptions) error
containerInspect(id string) (types.ContainerJSON, error)
containerKill(id string, signal string) error
containerEvents(options types.EventsOptions) (<-chan events.Message, <-chan error)
containerLogs(id string, options types.ContainerLogsOptions) (io.ReadCloser, error)
makeParams(StatusItem) (params.Params, error)
logger() logrus.FieldLogger
}
type dadapter struct {
config *config.Config
// docker image reference
ref reference.Named
info *registry.RepositoryInfo
// docker client
client *client.Client
ctx context.Context
log logrus.FieldLogger
}
func newDockerAdapter(config *config.Config) (dockerAdapter, error) {
log := config.Log().WithField("component", "adapter")
ref, err := reference.ParseNormalizedNamed(config.Image)
if err != nil {
log.WithError(err).
Errorf("Unable to parse image '%s'", config.Image)
return nil, err
}
log = log.WithField("image", ref.String())
info, err := registry.ParseRepositoryInfo(ref)
if err != nil {
log.WithError(err).Error("unable to parse repository info")
return nil, err
}
client, err := client.NewEnvClient()
if err != nil {
log.WithError(err).Error("unable to crate docker client")
return nil, err
}
return &dadapter{
config: config,
ref: ref,
info: info,
client: client,
log: log,
ctx: context.Background(),
}, nil
}
func (a *dadapter) imageReference() reference.Named {
return a.ref
}
func (a *dadapter) ensureImage() error {
exists, err := a.imageExists()
if err != nil {
return err
}
if exists {
a.log.Info("found image")
return nil
}
a.log.Warn("image not present")
return a.imagePull()
}
func (a *dadapter) imageExists() (bool, error) {
_, _, err := a.client.ImageInspectWithRaw(a.ctx, a.ref.Name())
switch {
case err == nil:
return true, nil
case client.IsErrImageNotFound(err):
return false, nil
default:
a.log.WithError(err).
Errorf("error inspecting image")
return false, err
}
}
func (a *dadapter) imagePull() error {
a.log.Infof("pulling image...")
body, err := a.client.ImageCreate(a.ctx, a.ref.String(), types.ImageCreateOptions{})
if err != nil {
a.log.WithError(err).
Error("error pulling image")
return err
}
defer body.Close()
_, err = io.Copy(ioutil.Discard, body)
if err != nil {
a.log.WithError(err).
Error("error while pulling image")
return err
}
a.log.Info("done pulling image")
return nil
}
func (a *dadapter) createContainer() (string, error) {
dconfig := &container.Config{
Image: a.ref.Name(),
Cmd: a.config.Container.Cmd,
Env: a.config.Container.Env,
Volumes: a.config.Container.Volumes,
Labels: a.config.Container.Labels,
AttachStdin: false,
AttachStdout: false,
AttachStderr: false,
ExposedPorts: nat.PortSet{
nat.Port(strconv.Itoa(a.config.Port)): struct{}{},
},
}
hconfig := &container.HostConfig{
AutoRemove: true,
PublishAllPorts: true,
RestartPolicy: container.RestartPolicy{},
}
nconfig := &network.NetworkingConfig{}
name := ""
container, err := a.client.ContainerCreate(a.ctx, dconfig, hconfig, nconfig, name)
if err != nil {
a.log.WithError(err).Error("can't create container")
return "", err
}
lcid(a.log, container.ID).Infof("container created")
for _, w := range container.Warnings {
lcid(a.log, container.ID).Warn(w)
}
return container.ID, nil
}
func (a *dadapter) containerStart(id string, options types.ContainerStartOptions) error {
return a.client.ContainerStart(a.ctx, id, options)
}
func (a *dadapter) containerInspect(id string) (types.ContainerJSON, error) {
return a.client.ContainerInspect(a.ctx, id)
}
func (a *dadapter) containerKill(id string, signal string) error {
return a.client.ContainerKill(a.ctx, id, signal)
}
func (a *dadapter) containerEvents(options types.EventsOptions) (<-chan events.Message, <-chan error) {
return a.client.Events(a.ctx, options)
}
func (a *dadapter) containerLogs(id string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
return a.client.ContainerLogs(a.ctx, id, options)
}
func (a *dadapter) makeParams(c StatusItem) (params.Params, error) {
return a.config.Params.ParamsFor(c.ID(), c.Status(), a.config.Port)
}
func (a *dadapter) logger() logrus.FieldLogger {
return a.log
}