forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.go
157 lines (148 loc) · 4.43 KB
/
docker.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
// Copyright 2013 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package docker
import (
"crypto"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"time"
"github.com/pkg/errors"
"github.com/tsuru/config"
"github.com/tsuru/docker-cluster/cluster"
"github.com/tsuru/docker-cluster/storage/mongodb"
"github.com/tsuru/tsuru/action"
"github.com/tsuru/tsuru/app/image"
"github.com/tsuru/tsuru/event"
"github.com/tsuru/tsuru/log"
"github.com/tsuru/tsuru/provision"
"github.com/tsuru/tsuru/provision/docker/clusterclient"
"github.com/tsuru/tsuru/provision/docker/container"
"github.com/tsuru/tsuru/provision/dockercommon"
)
func buildClusterStorage() (cluster.Storage, error) {
mongoURL, _ := config.GetString("docker:cluster:mongo-url")
mongoDatabase, _ := config.GetString("docker:cluster:mongo-database")
if mongoURL == "" || mongoDatabase == "" {
return nil, errors.Errorf("Cluster Storage: docker:cluster:{mongo-url,mongo-database} must be set.")
}
storage, err := mongodb.Mongodb(mongoURL, mongoDatabase)
if err != nil {
return nil, errors.Errorf("Cluster Storage: Unable to connect to mongodb: %s (docker:cluster:mongo-url = %q; docker:cluster:mongo-database = %q)",
err.Error(), mongoURL, mongoDatabase)
}
return storage, nil
}
func randomString() string {
h := crypto.MD5.New()
h.Write([]byte(time.Now().Format(time.RFC3339Nano)))
io.CopyN(h, rand.Reader, 10)
return fmt.Sprintf("%x", h.Sum(nil))[:20]
}
func (p *dockerProvisioner) deployPipeline(app provision.App, imageID string, commands []string, evt *event.Event) (string, error) {
actions := []*action.Action{
&insertEmptyContainerInDB,
&createContainer,
&setContainerID,
&startContainer,
&updateContainerInDB,
&followLogsAndCommit,
}
pipeline := action.NewPipeline(actions...)
deployImage, err := image.AppNewImageName(app.GetName())
if err != nil {
return "", log.WrapError(errors.Errorf("error getting new image name for app %s", app.GetName()))
}
var writer io.Writer = evt
if evt == nil {
writer = ioutil.Discard
}
args := runContainerActionsArgs{
app: app,
imageID: imageID,
commands: commands,
writer: writer,
isDeploy: true,
buildingImage: deployImage,
provisioner: p,
event: evt,
}
err = container.RunPipelineWithRetry(pipeline, args)
if err != nil {
log.Errorf("error on execute deploy pipeline for app %s - %s", app.GetName(), err)
return "", err
}
return deployImage, nil
}
func (p *dockerProvisioner) start(oldContainer *container.Container, app provision.App, imageID string, w io.Writer, exposedPort string, destinationHosts ...string) (*container.Container, error) {
commands, processName, err := dockercommon.LeanContainerCmds(oldContainer.ProcessName, imageID, app)
if err != nil {
return nil, err
}
var actions []*action.Action
if oldContainer != nil && oldContainer.Status == provision.StatusStopped.String() {
actions = []*action.Action{
&insertEmptyContainerInDB,
&createContainer,
&setContainerID,
&stopContainer,
&updateContainerInDB,
&setNetworkInfo,
}
} else {
actions = []*action.Action{
&insertEmptyContainerInDB,
&createContainer,
&setContainerID,
&startContainer,
&updateContainerInDB,
&setNetworkInfo,
}
}
pipeline := action.NewPipeline(actions...)
args := runContainerActionsArgs{
app: app,
processName: processName,
imageID: imageID,
commands: commands,
destinationHosts: destinationHosts,
provisioner: p,
exposedPort: exposedPort,
}
err = container.RunPipelineWithRetry(pipeline, args)
if err != nil {
return nil, err
}
c := pipeline.Result().(*container.Container)
err = c.SetImage(p.ClusterClient(), imageID)
if err != nil {
return nil, err
}
return c, nil
}
func (p *dockerProvisioner) ClusterClient() provision.BuilderDockerClient {
return &clusterclient.ClusterClient{
Cluster: p.Cluster(),
Collection: p.Collection,
Limiter: p.ActionLimiter(),
}
}
func (p *dockerProvisioner) GetClient(app provision.App) (provision.BuilderDockerClient, error) {
cli := &clusterclient.ClusterClient{
Cluster: p.Cluster(),
Collection: p.Collection,
Limiter: p.ActionLimiter(),
}
if app != nil {
appNodes, err := p.Nodes(app)
if err != nil {
return nil, err
}
for _, n := range appNodes {
cli.PossibleNodes = append(cli.PossibleNodes, n.Address)
}
}
return cli, nil
}