-
Notifications
You must be signed in to change notification settings - Fork 22
/
container.go
488 lines (439 loc) · 12.7 KB
/
container.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package container
import (
"bytes"
"fmt"
"io"
"net"
"net/url"
"time"
// "os"
// "encoding/json"
log "github.com/Sirupsen/logrus"
"github.com/fsouza/go-dockerclient"
"github.com/megamsys/libgo/utils"
constants "github.com/megamsys/libgo/utils"
lw "github.com/megamsys/libgo/writer"
"github.com/megamsys/vertice/carton"
"github.com/megamsys/vertice/provision"
"github.com/megamsys/vertice/provision/docker/cluster"
)
const (
portRangeStart = 49153
portRangeEnd = 65535
portAllocMaxTries = 15
)
type DockerProvisioner interface {
Cluster() *cluster.Cluster
PushImage(name, tag string) error
}
type Container struct {
Id string //container id.
BoxId string
AccountId string
CartonId string
Name string
BoxName string
Level provision.BoxLevel
PublicIp string
HostAddr string
HostPort string
PrivateKey string
Version string
Image string
Status utils.Status
State utils.State
BuildingImage string
LastStatusUpdate time.Time
LastSuccessStatusUpdate time.Time
LockedUntil time.Time
Routable bool
Region string
closechan chan bool
}
func (c *Container) ShortId() string {
if len(c.Id) > 10 {
return c.Id[:10]
}
return c.Id
}
func (c *Container) Available() bool {
return c.Status.String() == constants.StatusContainerStarted.String() || c.Status.String() == constants.StatusContainerStarting.String()
}
func (c *Container) Address() *url.URL {
return &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%s", c.HostAddr, c.HostPort),
}
}
type CreateArgs struct {
ImageId string
Box *provision.Box
Deploy bool
Provisioner DockerProvisioner
DestinationHosts []string
}
func (c *Container) Create(args *CreateArgs) error {
config := docker.Config{
Image: args.ImageId,
AttachStdin: false,
AttachStdout: false,
AttachStderr: false,
Memory: int64(args.Box.ConGetMemory()),
MemorySwap: int64(args.Box.ConGetMemory() + args.Box.GetSwap()),
CPUShares: int64(args.Box.GetCpushare()),
Labels: map[string]string{utils.ASSEMBLY_ID: args.Box.CartonId, utils.ASSEMBLY_NAME: c.BoxName,
utils.ASSEMBLIES_ID: args.Box.CartonsId, utils.ACCOUNT_ID: args.Box.AccountId, utils.QUOTA_ID: args.Box.QuotaId},
}
opts := docker.CreateContainerOptions{Name: c.BoxName, Config: &config}
cl := args.Provisioner.Cluster()
cl.VNets = args.Box.Vnets
addr, cont, err := cl.CreateContainerSchedulerOpts(opts)
if err != nil {
log.Errorf("Error on creating container in docker %s - %s", c.BoxName, err)
return err
}
c.Id = cont.ID
c.HostAddr = urlToHost(addr)
return nil
}
func (c *Container) Logs(p DockerProvisioner) error {
var outBuffer bytes.Buffer
var closeChan chan bool
b := &provision.Box{Id: c.Id, Name: c.BoxName, Tosca: "docker"}
logWriter := lw.NewLogWriter(b)
writer := io.MultiWriter(&outBuffer, &logWriter)
logopt := docker.LogsOptions{
Container: c.Id,
OutputStream: writer,
ErrorStream: writer,
Follow: true,
// RawTerminal: true,
Stdout: true,
Stderr: true,
Timestamps: false,
// Tail: "100",
}
cs := make(chan []byte)
go p.Cluster().SetLogs(cs, logopt, closeChan)
go func(closeChan chan bool, logWriter lw.LogWriter) {
select {
case <-closeChan:
logWriter.Close()
default:
}
}(closeChan, logWriter)
var err error
if err != nil {
return err
}
return nil
}
func (c *Container) hostToNodeAddress(p DockerProvisioner, host string) (string, error) {
nodes, err := p.Cluster().Nodes()
if err != nil {
return "", err
}
for _, node := range nodes {
if urlToHost(node.Address) == host {
return node.Address, nil
}
}
return "", fmt.Errorf("Host `%s` not found", host)
}
func (c *Container) SetMileStone(state utils.State) error {
log.Debugf(" set state[%s] of container (%s, %s)", c.BoxId, c.Name, state.String())
if asm, err := carton.NewAssembly(c.CartonId, c.AccountId, ""); err != nil {
return err
} else if err = asm.SetState(state); err != nil {
return err
}
if c.Level == provision.BoxSome {
log.Debugf(" set state[%s] of container (%s, %s)", c.BoxId, c.Name, state.String())
if comp, err := carton.NewComponent(c.BoxId, c.AccountId, ""); err != nil {
return err
} else if err = comp.SetState(state, c.AccountId); err != nil {
return err
}
}
return nil
}
func (c *Container) UpdateContId() error {
log.Debugf(" update Id[%s] of container (%s) in cassandra", c.Id, c.Name)
var ids = make(map[string][]string)
cid := []string{c.Id}
ids[carton.INSTANCE_ID] = cid
if asm, err := carton.NewAssembly(c.CartonId, c.AccountId, ""); err != nil {
return err
} else if err = asm.NukeAndSetOutputs(ids); err != nil {
return err
}
return nil
}
func urlToHost(urlStr string) string {
url, _ := url.Parse(urlStr)
if url == nil || url.Host == "" {
return urlStr
}
host, _, _ := net.SplitHostPort(url.Host)
if host == "" {
return url.Host
}
return host
}
func (c *Container) addEnvsToConfig(args *CreateArgs, cfg *docker.Config) {
/*if !args.Deploy {
for _, envData := range args.Box.Envs() {
cfg.Env = append(cfg.Env, fmt.Sprintf("%s=%s", envData.Name, envData.Value))
}
}*/
}
func (c *Container) Remove(p DockerProvisioner) error {
log.Debugf("Removing container %s from docker", c.BoxName)
//this will be removed. containerID will be stored upon create in riak
id, _ := p.Cluster().PreStopAction(c.BoxName)
c.Id = id
err := c.Stop(p)
if err != nil {
log.Errorf("error on stop unit %s - %s", c.Id, err)
}
err = p.Cluster().RemoveContainer(docker.RemoveContainerOptions{ID: c.Id})
if err != nil {
log.Errorf("Failed to remove container from docker: %s", err)
return err
}
c.SetMileStone(constants.StateDestroyed)
c.SetStatus(constants.StatusDestroyed)
return nil
}
type StartArgs struct {
Provisioner DockerProvisioner
Box *provision.Box
Deploy bool
}
func (c *Container) Start(args *StartArgs) error {
_, err := getPort()
if err != nil {
return err
}
hostConfig := docker.HostConfig{
Memory: int64(args.Box.ConGetMemory()),
MemorySwap: int64(args.Box.ConGetMemory() + args.Box.GetSwap()),
CPUShares: int64(args.Box.GetCpushare()),
}
err = args.Provisioner.Cluster().StartContainer(c.Id, &hostConfig)
if err != nil {
return err
}
initialStatus := constants.StatusContainerStarted
if args.Deploy {
initialStatus = constants.StatusContainerBootstrapping
}
c.SetMileStone(constants.StateRunning)
return c.SetStatus(initialStatus)
}
func (c *Container) Stop(p DockerProvisioner) error {
if c.Status.String() == constants.StatusContainerStopped.String() {
return nil
}
err := p.Cluster().StopContainer(c.Id, 10)
if err != nil {
log.Errorf("error on stop container %s: %s", c.Id, err)
return err
}
c.SetStatus(constants.StatusContainerStopped)
c.SetMileStone(constants.StateStopped)
return nil
}
type waitResult struct {
status int
err error
}
var safeAttachInspectTimeout = 20 * time.Second
func SafeAttachWaitContainer(p DockerProvisioner, opts docker.AttachToContainerOptions) (int, error) {
cluster := p.Cluster()
resultCh := make(chan waitResult, 1)
go func() {
err := cluster.AttachToContainer(opts)
if err != nil {
resultCh <- waitResult{err: err}
return
}
status, err := cluster.WaitContainer(opts.Container)
resultCh <- waitResult{status: status, err: err}
}()
for {
select {
case result := <-resultCh:
return result.status, result.err
case <-time.After(safeAttachInspectTimeout):
}
contData, err := cluster.InspectContainer(opts.Container)
if err != nil {
return 0, err
}
if !contData.State.Running {
return contData.State.ExitCode, nil
}
}
}
func (c *Container) SetStatus(status utils.Status) error {
log.Debugf(" set status[%s] of container (%s, %s)", c.BoxId, c.Name, status.String())
if asm, err := carton.NewAssembly(c.CartonId, c.AccountId, ""); err != nil {
return err
} else if err = asm.SetStatus(status); err != nil {
return err
}
if c.Level == provision.BoxSome {
log.Debugf(" set status[%s] of container (%s, %s)", c.BoxId, c.Name, status.String())
if comp, err := carton.NewComponent(c.BoxId, c.AccountId, ""); err != nil {
return err
} else if err = comp.SetStatus(status, c.AccountId); err != nil {
return err
}
}
return nil
}
type NetworkInfo struct {
HTTPHostPort string
IP string
}
func (c *Container) NetworkInfo(p DockerProvisioner) (NetworkInfo, error) {
var netInfo NetworkInfo
err := p.Cluster().SetNetworkinNode(c.Id, c.CartonId, c.AccountId)
return netInfo, err
}
type Pty struct {
Width int
Height int
Term string
}
func (c *Container) Shell(p DockerProvisioner, stdin io.Reader, stdout, stderr io.Writer, pty Pty) error {
cmds := []string{"/usr/bin/env", "/bin/sh", "-c", "TERM=xterm-256color; export TERM; [ -x /bin/bash ] && ([ -x /usr/bin/script ] && /usr/bin/script -q -c '/bin/bash' /dev/null || exec /bin/bash) || exec /bin/sh"}
execCreateOpts := docker.CreateExecOptions{
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Cmd: cmds,
Container: c.Id,
Tty: true,
}
exec, err := p.Cluster().CreateExec(execCreateOpts, c.Region)
if err != nil {
return err
}
startExecOptions := docker.StartExecOptions{
InputStream: stdin,
OutputStream: stdout,
ErrorStream: stderr,
Tty: true,
RawTerminal: true,
}
errs := make(chan error, 1)
go func() {
errs <- p.Cluster().StartExec(exec.ID, c.Id, startExecOptions, c.Region)
}()
execInfo, err := p.Cluster().InspectExec(exec.ID, c.Id, c.Region)
for !execInfo.Running && err == nil {
select {
case startErr := <-errs:
return startErr
default:
execInfo, err = p.Cluster().InspectExec(exec.ID, c.Id, c.Region)
}
}
if err != nil {
return err
}
p.Cluster().ResizeExecTTY(exec.ID, c.Id, pty.Height, pty.Width, c.Region)
return <-errs
}
type execErr struct {
code int
}
func (e *execErr) Error() string {
return fmt.Sprintf("unexpected exit code: %d", e.code)
}
func (c *Container) Exec(p DockerProvisioner, stdout, stderr io.Writer, cmd string, args ...string) error {
cmds := []string{"/bin/bash", "-lc", cmd}
cmds = append(cmds, args...)
execCreateOpts := docker.CreateExecOptions{
AttachStdin: false,
AttachStdout: true,
AttachStderr: true,
Tty: false,
Cmd: cmds,
Container: c.Id,
}
exec, err := p.Cluster().CreateExec(execCreateOpts, c.Region)
if err != nil {
return err
}
startExecOptions := docker.StartExecOptions{
OutputStream: stdout,
ErrorStream: stderr,
}
err = p.Cluster().StartExec(exec.ID, c.Id, startExecOptions, c.Region)
if err != nil {
return err
}
execData, err := p.Cluster().InspectExec(exec.ID, c.Id, c.Region)
if err != nil {
return err
}
if execData.ExitCode != 0 {
return &execErr{code: execData.ExitCode}
}
return nil
}
/*
// Commits commits the container, creating an image in Docker. It then returns
// the image identifier for usage in future container creation.
func (c *Container) Commit(p DockerProvisioner, writer io.Writer) (string, error) {
log.Debugf("commiting container %s", c.Id)
parts := strings.Split(c.BuildingImage, ":")
if len(parts) < 2 {
return "", log.WrapError(fmt.Errorf("error parsing image name, not enough parts: %s", c.BuildingImage))
}
repository := strings.Join(parts[:len(parts)-1], ":")
tag := parts[len(parts)-1]
opts := docker.CommitContainerOptions{Container: c.Id, Repository: repository, Tag: tag}
image, err := p.Cluster().CommitContainer(opts)
if err != nil {
return "", log.WrapError(fmt.Errorf("error in commit container %s: %s", c.Id, err.Error()))
}
imgData, err := p.Cluster().InspectImage(c.BuildingImage)
imgSize := ""
if err == nil {
imgSize = fmt.Sprintf("(%.02fMB)", float64(imgData.Size)/1024/1024)
}
fmt.Fprintf(writer, " ---> Sending image to repository %s\n", imgSize)
log.Debugf("image %s generated from container %s", image.ID, c.Id)
maxTry, _ := config.GetInt("docker:registry-max-try")
if maxTry <= 0 {
maxTry = 3
}
for i := 0; i < maxTry; i++ {
err = p.PushImage(repository, tag)
if err != nil {
fmt.Fprintf(writer, "Could not send image, trying again. Original error: %s\n", err.Error())
log.Errorf("error in push image %s: %s", c.BuildingImage, err.Error())
time.Sleep(time.Second)
continue
}
break
}
if err != nil {
return "", log.WrapError(fmt.Errorf("error in push image %s: %s", c.BuildingImage, err.Error()))
}
return c.BuildingImage, nil
}
*/
func getPort() (string, error) {
/* port, err := config.Get("docker:run-cmd:port")
if err != nil {
return "", err
}
return fmt.Sprint(port), nil
*/
return "", nil
}