This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
/
service.go
266 lines (231 loc) · 7.23 KB
/
service.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
package docker
import (
"fmt"
"strings"
"github.com/rancher/os/config"
"github.com/rancher/os/pkg/log"
"github.com/rancher/os/pkg/util/network"
"github.com/docker/docker/layer"
dockerclient "github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
composeConfig "github.com/docker/libcompose/config"
"github.com/docker/libcompose/docker"
"github.com/docker/libcompose/project"
"github.com/docker/libcompose/project/options"
"golang.org/x/net/context"
)
type Service struct {
*docker.Service
deps map[string][]string
context *docker.Context
project *project.Project
}
func NewService(factory *ServiceFactory, name string, serviceConfig *composeConfig.ServiceConfig, context *docker.Context, project *project.Project) *Service {
return &Service{
Service: docker.NewService(name, serviceConfig, context),
deps: factory.Deps,
context: context,
project: project,
}
}
func (s *Service) DependentServices() []project.ServiceRelationship {
rels := s.Service.DependentServices()
for _, dep := range s.deps[s.Name()] {
rels = appendLink(rels, dep, true, s.project)
}
if s.requiresSyslog() {
rels = appendLink(rels, "syslog", false, s.project)
}
if s.requiresUserDocker() {
rels = appendLink(rels, "docker", false, s.project)
} else if s.missingImage() {
rels = appendLink(rels, "network", false, s.project)
}
return rels
}
func (s *Service) missingImage() bool {
image := s.Config().Image
if image == "" {
return false
}
client := s.context.ClientFactory.Create(s)
// If it is already built-in, we should use tag image
// use case: open-vmtools with another REGISTRY_DOMAIN setting
registryDomain := config.LoadConfig().Rancher.Environment["REGISTRY_DOMAIN"]
if registryDomain != "docker.io" && strings.Index(image, registryDomain) >= 0 {
orginImage := strings.SplitN(image, "/", 2)[1]
_, _, err := client.ImageInspectWithRaw(context.Background(), orginImage, false)
if err == nil {
log.Infof("Will tag image %s to %s", orginImage, image)
options := types.ImageTagOptions{
ImageID: orginImage,
RepositoryName: strings.SplitN(image, ":", 2)[0],
Tag: strings.SplitN(image, ":", 2)[1],
Force: false,
}
if err := client.ImageTag(context.Background(), options); err != nil {
log.Warnf("Failed to tag image from %s to %s: %v", orginImage, image, err)
}
}
}
_, _, err := client.ImageInspectWithRaw(context.Background(), image, false)
return err != nil
}
func (s *Service) requiresSyslog() bool {
return s.Config().Logging.Driver == "syslog"
}
func (s *Service) requiresUserDocker() bool {
return s.Config().Labels[config.ScopeLabel] != config.System
}
func appendLink(deps []project.ServiceRelationship, name string, optional bool, p *project.Project) []project.ServiceRelationship {
if _, ok := p.ServiceConfigs.Get(name); !ok {
return deps
}
rel := project.NewServiceRelationship(name, project.RelTypeLink)
rel.Optional = optional
return append(deps, rel)
}
func (s *Service) shouldRebuild(ctx context.Context) (bool, error) {
containers, err := s.Containers(ctx)
if err != nil {
return false, err
}
cfg := config.LoadConfig()
for _, c := range containers {
outOfSync, err := c.(*docker.Container).OutOfSync(ctx, s.Service.Config().Image)
if err != nil {
return false, err
}
_, containerInfo, err := s.getContainer(ctx)
if err != nil {
return false, err
}
name := containerInfo.Name[1:]
origRebuildLabel := containerInfo.Config.Labels[config.RebuildLabel]
newRebuildLabel := s.Config().Labels[config.RebuildLabel]
rebuildLabelChanged := newRebuildLabel != origRebuildLabel
log.WithFields(log.Fields{
"origRebuildLabel": origRebuildLabel,
"newRebuildLabel": newRebuildLabel,
"rebuildLabelChanged": rebuildLabelChanged,
"outOfSync": outOfSync}).Debug("Rebuild values")
if newRebuildLabel == "always" {
return true, nil
}
if s.Name() == "console" && cfg.Rancher.ForceConsoleRebuild {
if err := config.Set("rancher.force_console_rebuild", false); err != nil {
return false, err
}
return true, nil
}
if outOfSync {
if s.Name() == "console" {
origConsoleLabel := containerInfo.Config.Labels[config.ConsoleLabel]
newConsoleLabel := s.Config().Labels[config.ConsoleLabel]
if newConsoleLabel != origConsoleLabel {
return true, nil
}
} else if rebuildLabelChanged || origRebuildLabel != "false" {
return true, nil
} else {
log.Warnf("%s needs rebuilding", name)
}
}
}
return false, nil
}
func (s *Service) Up(ctx context.Context, options options.Up) error {
labels := s.Config().Labels
// wait for networking if necessary
if after := labels["io.rancher.os.after"]; after == "network" {
if err := network.AllDefaultGWOK(network.DefaultRoutesCheckTimeout); err != nil {
log.Warnf("Timeout to wait for the networking ready: %v", err)
}
}
if err := s.Service.Create(ctx, options.Create); err != nil {
return err
}
shouldRebuild, err := s.shouldRebuild(ctx)
if err != nil {
return err
}
if shouldRebuild {
log.Infof("Rebuilding %s", s.Name())
cs, err := s.Service.Containers(ctx)
if err != nil {
return err
}
for _, c := range cs {
if _, err := c.(*docker.Container).Recreate(ctx, s.Config().Image); err != nil {
// sometimes we can get ErrMountNameConflict when booting on RPi
// ignore this error so that ros can boot success, otherwise it will hang forever
if strings.Contains(err.Error(), layer.ErrMountNameConflict.Error()) {
log.Warn(err)
} else {
return err
}
}
}
if err = s.rename(ctx); err != nil {
return err
}
}
if labels[config.CreateOnlyLabel] == "true" {
return s.checkReload(labels)
}
if err := s.Service.Up(ctx, options); err != nil {
return err
}
if labels[config.DetachLabel] == "false" {
if err := s.wait(ctx); err != nil {
return err
}
}
return s.checkReload(labels)
}
func (s *Service) checkReload(labels map[string]string) error {
if labels[config.ReloadConfigLabel] == "true" {
return project.ErrRestart
}
return nil
}
func (s *Service) Create(ctx context.Context, options options.Create) error {
return s.Service.Create(ctx, options)
}
func (s *Service) getContainer(ctx context.Context) (dockerclient.APIClient, types.ContainerJSON, error) {
containers, err := s.Service.Containers(ctx)
if err != nil {
return nil, types.ContainerJSON{}, err
}
if len(containers) == 0 {
return nil, types.ContainerJSON{}, fmt.Errorf("No containers found for %s", s.Name())
}
id, err := containers[0].ID()
if err != nil {
return nil, types.ContainerJSON{}, err
}
client := s.context.ClientFactory.Create(s)
info, err := client.ContainerInspect(context.Background(), id)
return client, info, err
}
func (s *Service) wait(ctx context.Context) error {
client, info, err := s.getContainer(ctx)
if err != nil {
return err
}
if _, err := client.ContainerWait(context.Background(), info.ID); err != nil {
return err
}
return nil
}
func (s *Service) rename(ctx context.Context) error {
client, info, err := s.getContainer(ctx)
if err != nil {
return err
}
if len(info.Name) > 0 && info.Name[1:] != s.Name() {
log.Debugf("Renaming container %s => %s", info.Name[1:], s.Name())
return client.ContainerRename(context.Background(), info.ID, s.Name())
}
return nil
}