This repository has been archived by the owner on Dec 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
compose.go
223 lines (201 loc) Β· 6.06 KB
/
compose.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
// Package compose aims to provide simple "helper" methods to ease the use of
// compose (through libcompose) in (integration) tests.
package compose
import (
"fmt"
"regexp"
"strings"
"golang.org/x/net/context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/docker/libcompose/config"
"github.com/docker/libcompose/docker"
"github.com/docker/libcompose/docker/ctx"
"github.com/docker/libcompose/project"
"github.com/docker/libcompose/project/events"
"github.com/docker/libcompose/project/options"
d "github.com/libkermit/docker"
)
// Project holds compose related project attributes
type Project struct {
composeFiles []string
composeProject project.APIProject
name string
listenChan chan events.Event
started chan struct{}
stopped chan struct{}
deleted chan struct{}
client client.APIClient
hasOpenedChan bool
}
// CreateProject creates a compose project with the given name based on the
// specified compose files
func CreateProject(name string, composeFiles ...string) (*Project, error) {
// FIXME(vdemeester) temporarly normalize the project name, should not be needed.
r := regexp.MustCompile("[^a-z0-9]+")
name = r.ReplaceAllString(strings.ToLower(name), "")
apiClient, err := client.NewEnvClient()
if err != nil {
return nil, err
}
ping := types.Ping{APIVersion: d.CurrentAPIVersion}
apiClient.NegotiateAPIVersionPing(ping)
composeProject, err := docker.NewProject(&ctx.Context{
Context: project.Context{
ComposeFiles: composeFiles,
ProjectName: name,
},
}, &config.ParseOptions{
Interpolate: true,
Validate: true,
})
if err != nil {
return nil, err
}
p := &Project{
composeFiles: composeFiles,
composeProject: composeProject,
name: name,
listenChan: make(chan events.Event),
started: make(chan struct{}),
stopped: make(chan struct{}),
deleted: make(chan struct{}),
client: apiClient,
hasOpenedChan: true,
}
// Listen to compose events
go p.startListening()
p.composeProject.AddListener(p.listenChan)
return p, nil
}
// Start creates and starts the compose project.
func (p *Project) Start(services ...string) error {
// If project chan are closed, recreate new compose project
if !p.hasOpenedChan {
newProject, _ := CreateProject(p.name, p.composeFiles...)
*p = *newProject
}
ctx := context.Background()
err := p.composeProject.Create(ctx, options.Create{}, services...)
if err != nil {
return err
}
return p.StartOnly(services...)
}
// StartOnly only starts created services which are stopped.
func (p *Project) StartOnly(services ...string) error {
ctx := context.Background()
err := p.composeProject.Start(ctx, services...)
if err != nil {
return err
}
// Wait for compose to start
<-p.started
return nil
}
// StopOnly only stop services without delete them.
func (p *Project) StopOnly(services ...string) error {
ctx := context.Background()
err := p.composeProject.Stop(ctx, 10, services...)
if err != nil {
return err
}
<-p.stopped
return nil
}
// Stop shuts down and clean the project
func (p *Project) Stop(services ...string) error {
// FIXME(vdemeester) handle timeout
err := p.StopOnly(services...)
if err != nil {
return err
}
err = p.composeProject.Delete(context.Background(), options.Delete{}, services...)
if err != nil {
return err
}
<-p.deleted
existingContainers, err := p.existContainers(project.AnyState)
if err != nil {
return err
}
// Close channels only if there are no running services
if !existingContainers {
p.hasOpenedChan = false
close(p.started)
close(p.stopped)
close(p.deleted)
close(p.listenChan)
}
return nil
}
// Check if containers exist in the desirated state for the given services
func (p *Project) existContainers(stateFiltered project.State, services ...string) (bool, error) {
existingContainers := false
var err error
containersFound, err := p.composeProject.Containers(context.Background(), project.Filter{stateFiltered})
if err == nil && containersFound != nil && len(containersFound) > 0 {
existingContainers = true
}
return existingContainers, err
}
// Scale scale a service up
func (p *Project) Scale(service string, count int) error {
return p.composeProject.Scale(context.Background(), 10, map[string]int{
service: count,
})
}
func (p *Project) startListening() {
for event := range p.listenChan {
// FIXME Add a timeout on event ?
if event.EventType == events.ProjectStartDone {
p.started <- struct{}{}
}
if event.EventType == events.ProjectStopDone {
p.stopped <- struct{}{}
}
if event.EventType == events.ProjectDeleteDone {
p.deleted <- struct{}{}
}
}
}
// Containers lists containers for a given services.
func (p *Project) Containers(service string) ([]types.ContainerJSON, error) {
ctx := context.Background()
containers := []types.ContainerJSON{}
// Let's use engine-api for now as there is nothing really useful in
// libcompose for now.
filter := filters.NewArgs()
filter.Add("label", "com.docker.compose.project="+p.name)
filter.Add("label", "com.docker.compose.service="+service)
containerList, err := p.client.ContainerList(ctx, types.ContainerListOptions{
Filters: filter,
})
if err != nil {
return containers, err
}
for _, c := range containerList {
container, err := p.client.ContainerInspect(ctx, c.ID)
if err != nil {
return containers, err
}
containers = append(containers, container)
}
return containers, nil
}
// Container returns the one and only container for a given services. It returns an error
// if the service has more than one container (in case of scale)
func (p *Project) Container(service string) (types.ContainerJSON, error) {
containers, err := p.Containers(service)
if err != nil {
return types.ContainerJSON{}, err
}
if len(containers) > 1 {
return types.ContainerJSON{}, fmt.Errorf("More than one container are running for '%s' service", service)
}
if len(containers) == 0 {
return types.ContainerJSON{}, fmt.Errorf("No container found for '%s' service", service)
}
return containers[0], nil
}