/
docker.go
620 lines (498 loc) · 17.1 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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package docker
import (
"context"
_ "embed"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"github.com/YuukanOO/seelf/internal/deployment/domain"
"github.com/YuukanOO/seelf/pkg/log"
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/flags"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/compose"
dockertypes "github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
)
var (
ErrLoadProjectFailed = errors.New("compose_file_malformed")
ErrOpenComposeFileFailed = errors.New("compose_file_open_failed")
ErrComposeFailed = errors.New("compose_failed")
_ domain.Backend = (*Docker)(nil) // Make sure docker implements the Backend interface
)
const (
balancerProjectName = "seelf-internal"
balancerServiceName = "balancer"
certResolverName = "seelfresolver"
publicNetworkName = "seelf-public"
AppLabel = "app.seelf.application"
EnvironmentLabel = "app.seelf.environment"
)
type (
Options interface {
Domain() domain.Url
AcmeEmail() string
}
DockerOptions func(*Docker)
Docker struct {
cli command.Cli // Docker cli to use, if nil, a new one will be created per deployment task
compose api.Service // Docker compose service to use, if nil, a new one will be created per deployment task
options Options
logger log.Logger
}
)
// Creates a docker backend with given options. The configuration is mostly used to
// ease the testing of some internals.
func New(options Options, logger log.Logger, configuration ...DockerOptions) *Docker {
d := &Docker{
options: options,
logger: logger,
}
for _, opt := range configuration {
opt(d)
}
return d
}
// Use the given compose service and cli instead of creating new ones. Used for testing.
func WithDockerAndCompose(cli command.Cli, composeService api.Service) DockerOptions {
return func(d *Docker) {
d.cli = cli
d.compose = composeService
}
}
func (d *Docker) Setup() error {
cli, compose, err := d.instantiateClientAndCompose(nil)
if err != nil {
return err
}
defer cli.Client().Close()
d.logger.Info("deploying traefik balancer service, it could take a while if it's the first time...")
project := &types.Project{
Name: balancerProjectName,
Services: []types.ServiceConfig{
{
Name: balancerServiceName,
Image: "traefik:v2.6",
Restart: types.RestartPolicyUnlessStopped,
Command: []string{
"--providers.docker",
fmt.Sprintf("--providers.docker.network=%s", publicNetworkName),
"--providers.docker.exposedbydefault=false",
},
Ports: []types.ServicePortConfig{
{Target: 80, Published: "80"},
},
Volumes: []types.ServiceVolumeConfig{
{Type: types.VolumeTypeBind, Source: "/var/run/docker.sock", Target: "/var/run/docker.sock"},
},
CustomLabels: getProjectCustomLabels(balancerProjectName, balancerServiceName, ""),
},
},
Networks: types.Networks{
"default": types.NetworkConfig{
Name: publicNetworkName,
},
},
}
if d.options.Domain().UseSSL() {
project.Services[0].Command = append(project.Services[0].Command,
"--entrypoints.web.address=:80",
"--entrypoints.web.http.redirections.entryPoint.to=websecure",
"--entrypoints.web.http.redirections.entryPoint.scheme=https",
"--entrypoints.websecure.address=:443",
fmt.Sprintf("--certificatesresolvers.%s.acme.tlschallenge=true", certResolverName),
fmt.Sprintf("--certificatesresolvers.%s.acme.email=%s", certResolverName, d.options.AcmeEmail()),
fmt.Sprintf("--certificatesresolvers.%s.acme.storage=/letsencrypt/acme.json", certResolverName),
)
project.Services[0].Ports = append(project.Services[0].Ports, types.ServicePortConfig{
Target: 443, Published: "443",
})
project.Services[0].Volumes = append(project.Services[0].Volumes, types.ServiceVolumeConfig{
Type: types.VolumeTypeVolume,
Source: "letsencrypt",
Target: "/letsencrypt",
})
project.Volumes = types.Volumes{
"letsencrypt": types.VolumeConfig{},
}
}
loader.Normalize(project)
return compose.Up(context.Background(), project, api.UpOptions{
Create: api.CreateOptions{
RemoveOrphans: true,
QuietPull: true,
},
Start: api.StartOptions{
Wait: true,
},
})
}
func (d *Docker) Run(ctx context.Context, dir string, logger domain.DeploymentLogger, depl domain.Deployment) (domain.Services, error) {
cli, compose, err := d.instantiateClientAndCompose(logger)
if err != nil {
return nil, err
}
defer cli.Client().Close()
logger.Stepf("configuring seelf docker project for environment: %s", depl.Config().Environment())
project, services, err := d.generateProject(depl, dir, logger)
if err != nil {
return nil, err
}
logger.Stepf("launching docker compose project (pulling, building and running)")
if err = compose.Up(ctx, project, api.UpOptions{
Create: api.CreateOptions{
Build: &api.BuildOptions{},
RemoveOrphans: true,
},
Start: api.StartOptions{
Wait: true,
},
}); err != nil {
logger.Error(err)
return nil, ErrComposeFailed
}
if d.options.Domain().UseSSL() {
logger.Infof("you may have to wait for certificates to be generated before your app is available")
}
// Remove dangling images
pruneResult, err := cli.Client().ImagesPrune(ctx, filters.NewArgs(
filters.Arg("dangling", "true"),
filters.Arg("label", fmt.Sprintf("%s=%s", AppLabel, depl.ID().AppID())),
filters.Arg("label", fmt.Sprintf("%s=%s", EnvironmentLabel, depl.Config().Environment())),
))
if err == nil {
prunedCount := len(pruneResult.ImagesDeleted)
if prunedCount > 0 {
logger.Infof("pruned %d dangling image(s)", prunedCount)
}
} else {
// If there's an error, we just log it and go on since it's not a critical one
logger.Warnf(err.Error())
}
return services, nil
}
func (d *Docker) Cleanup(ctx context.Context, app domain.App) error {
cli, _, err := d.instantiateClientAndCompose(nil)
if err != nil {
return err
}
client := cli.Client()
defer client.Close()
appFilters := filters.NewArgs(
filters.Arg("label", fmt.Sprintf("%s=%s", AppLabel, app.ID())),
)
// List and stop all containers related to this application
containers, err := client.ContainerList(ctx, dockertypes.ContainerListOptions{
All: true,
Filters: appFilters,
})
if err != nil {
return err
}
// Before removing containers, make sure everything is stopped
for _, container := range containers {
d.logger.Debugw("stopping container", "id", container.ID)
if err = client.ContainerStop(ctx, container.ID, dockercontainer.StopOptions{}); err != nil {
return err
}
}
for _, container := range containers {
d.logger.Debugw("removing container", "id", container.ID)
if err = client.ContainerRemove(ctx, container.ID, dockertypes.ContainerRemoveOptions{}); err != nil {
return err
}
}
// List and remove all volumes
volumes, err := client.VolumeList(ctx, volume.ListOptions{
Filters: appFilters,
})
if err != nil {
return err
}
for _, volume := range volumes.Volumes {
d.logger.Debugw("removing volume", "name", volume.Name)
if err = client.VolumeRemove(ctx, volume.Name, true); err != nil {
return err
}
}
// List and remove all networks
networks, err := client.NetworkList(ctx, dockertypes.NetworkListOptions{
Filters: appFilters,
})
if err != nil {
return err
}
for _, network := range networks {
d.logger.Debugw("removing network", "id", network.ID)
if err = client.NetworkRemove(ctx, network.ID); err != nil {
return err
}
}
// List and remove all images
images, err := client.ImageList(ctx, dockertypes.ImageListOptions{
All: true,
Filters: appFilters,
})
if err != nil {
return err
}
for _, image := range images {
d.logger.Debugw("removing image", "id", image.ID)
if _, err = client.ImageRemove(ctx, image.ID, dockertypes.ImageRemoveOptions{
Force: true,
PruneChildren: true,
}); err != nil {
return err
}
}
return nil
}
// Initialize a new docker client and compose service. You MUST close the command.Cli once done.
func (d *Docker) instantiateClientAndCompose(logger domain.DeploymentLogger) (command.Cli, api.Service, error) {
if d.compose != nil && d.cli != nil {
return d.cli, d.compose, nil
}
stream := io.Discard
if logger != nil {
stream = logger
}
dockerCli, err := command.NewDockerCli(command.WithCombinedStreams(stream))
if err != nil {
return nil, nil, err
}
if err = dockerCli.Initialize(flags.NewClientOptions()); err != nil {
return nil, nil, err
}
ping, err := dockerCli.Client().Ping(context.Background())
if err != nil {
return nil, nil, err
}
if logger != nil {
logger.Stepf("connected to docker with version %s", ping.APIVersion)
}
return dockerCli, compose.NewComposeService(dockerCli), nil
}
// Generate a compose project for a specific app and transform it to make it usable
// by seelf (ie. exposing needed services)
//
// This function use some heuristics to determine what should be exposed and how
// according to the given configuration.
//
// The goal is really for the user to provide a docker-compose file which runs fine locally
// and we should do our best to expose it accordingly without the user providing anything.
func (d *Docker) generateProject(depl domain.Deployment, dir string, logger domain.DeploymentLogger) (*types.Project, domain.Services, error) {
var (
services domain.Services
config = depl.Config()
seelfLabels = types.Labels{
AppLabel: string(depl.ID().AppID()),
EnvironmentLabel: string(config.Environment()),
}
)
composeFilepath, err := findServiceFile(dir, config.Environment())
if err != nil {
logger.Error(err)
return nil, nil, ErrOpenComposeFileFailed
}
logger.Stepf("reading project from %s", composeFilepath)
project, err := loadProject(composeFilepath, config.ProjectName(), config.Environment())
if err != nil {
logger.Error(err)
return nil, nil, ErrLoadProjectFailed
}
disabledServicesCount := len(project.DisabledServices)
if disabledServicesCount > 0 {
disabledServicesNames := make([]string, disabledServicesCount)
for i, service := range project.DisabledServices {
disabledServicesNames[i] = service.Name
}
logger.Infof("some services have been disabled by the %s profile: %s", config.Environment(), strings.Join(disabledServicesNames, ", "))
}
// Sort services by alphabetical order so that we know how where the default subdomain (ie. the one without a service suffix)
// will be.
orderedNames := make([]string, len(project.Services))
namesToIndex := make(map[string]int, len(project.Services))
for i, service := range project.Services {
orderedNames[i] = service.Name
namesToIndex[service.Name] = i
}
slices.Sort(orderedNames)
// Let's transform the project to expose needed services
for _, name := range orderedNames {
var (
i = namesToIndex[name]
service = project.Services[i]
deployedService domain.Service
)
if len(service.Ports) == 0 {
services, deployedService = services.Internal(config, service.Name, service.Image)
} else {
services, deployedService = services.Public(d.options.Domain(), config, service.Name, service.Image)
}
if service.Restart == "" {
logger.Warnf("no restart policy sets for service %s, the service will not be restarted automatically", service.Name)
}
// If there's an image to build, force it (same as --build in the docker compose cli)
if service.Build != nil {
service.Image = deployedService.Image() // Since the image name may have been generated, override it
service.PullPolicy = types.PullPolicyBuild
service.Build.Labels = appendLabels(seelfLabels, service.Build.Labels)
}
// Attach environment variables if any
servicesEnv := config.EnvironmentVariablesFor(deployedService.Name())
if vars, hasVars := servicesEnv.TryGet(); hasVars {
envNames := make([]string, 0, len(vars))
for name, value := range vars {
localValue := value // Copy the value to avoid the loop to use the same pointer
service.Environment[name] = &localValue
envNames = append(envNames, name)
}
logger.Infof("using %s environment variable(s) for service %s", strings.Join(envNames, ", "), deployedService.Name())
}
service.Labels = appendLabels(seelfLabels, service.Labels)
for _, volume := range service.Volumes {
if volume.Type == types.VolumeTypeBind {
logger.Warnf("bind mount detected for service %s, this is not supported and your data are not guaranteed to be preserved, use docker volumes instead", deployedService.Name())
}
}
// Not exposed, no need to go further
if !deployedService.IsExposed() {
project.Services[i] = service
continue
}
url := deployedService.Url().MustGet()
serviceQualifiedName := deployedService.QualifiedName()
if len(service.Ports) > 1 {
logger.Warnf("service %s exposes multiple ports but seelf only supports one port per service at the moment", deployedService.Name())
}
containerPort := uint64(service.Ports[0].Target)
service.Ports = []types.ServicePortConfig{} // Remove them since traefik will expose this service
if service.Networks == nil {
service.Networks = map[string]*types.ServiceNetworkConfig{}
}
service.Networks[publicNetworkName] = nil // nil here because there's no additional options to give
service.Labels["traefik.enable"] = "true"
service.Labels[fmt.Sprintf("traefik.http.services.%s.loadbalancer.server.port", serviceQualifiedName)] = strconv.FormatUint(containerPort, 10)
service.Labels[fmt.Sprintf("traefik.http.routers.%s.rule", serviceQualifiedName)] =
fmt.Sprintf("Host(`%s`)", url.Host())
if url.UseSSL() {
service.Labels[fmt.Sprintf("traefik.http.routers.%s.tls.certresolver", serviceQualifiedName)] =
certResolverName
}
project.Services[i] = service
}
// Add labels to network and volumes to make it easy to find them
for name, network := range project.Networks {
network.Labels = appendLabels(seelfLabels, network.Labels)
project.Networks[name] = network
}
for name, volume := range project.Volumes {
volume.Labels = appendLabels(seelfLabels, volume.Labels)
project.Volumes[name] = volume
}
// Append the public seelf network to the project
if project.Networks == nil {
project.Networks = types.Networks{}
}
project.Networks[publicNetworkName] = types.NetworkConfig{
Name: publicNetworkName,
External: types.External{
External: true,
},
}
return project, services, nil
}
// add some labels to a given target.
func appendLabels(labelsToAdd types.Labels, target types.Labels) types.Labels {
if target == nil {
target = types.Labels{}
}
for k, v := range labelsToAdd {
target[k] = v
}
return target
}
// Load a project from a given compose path.
func loadProject(composePath, projectName string, env domain.Environment) (*types.Project, error) {
opts, err := cli.NewProjectOptions([]string{composePath},
cli.WithName(projectName),
cli.WithProfiles([]string{string(env)}),
)
if err != nil {
return nil, err
}
project, err := cli.ProjectFromOptions(opts)
if err != nil {
return nil, err
}
for i, s := range project.Services {
s.CustomLabels = getProjectCustomLabels(project.Name, s.Name, project.WorkingDir, project.ComposeFiles...)
project.Services[i] = s
}
return project, nil
}
// Find the service file that should be used based on the given environment.
func findServiceFile(dir string, env domain.Environment) (string, error) {
var (
composeFilepath string
serviceFilesAffinity = []string{
fmt.Sprintf("compose.seelf.%s.yml", env),
fmt.Sprintf("compose.seelf.%s.yaml", env),
fmt.Sprintf("docker-compose.seelf.%s.yml", env),
fmt.Sprintf("docker-compose.seelf.%s.yaml", env),
fmt.Sprintf("compose.%s.yml", env),
fmt.Sprintf("compose.%s.yaml", env),
fmt.Sprintf("docker-compose.%s.yml", env),
fmt.Sprintf("docker-compose.%s.yaml", env),
"compose.seelf.yml",
"compose.seelf.yaml",
"docker-compose.seelf.yml",
"docker-compose.seelf.yaml",
"compose.yml",
"compose.yaml",
"docker-compose.yml",
"docker-compose.yaml",
}
)
// Find the first valid docker-compose file
for _, file := range serviceFilesAffinity {
servicePath := filepath.Join(dir, file)
_, err := os.Stat(servicePath)
if os.IsNotExist(err) {
continue
}
if err == nil {
composeFilepath = servicePath
break
}
return "", err
}
if composeFilepath == "" {
return "", fmt.Errorf("could not find a valid compose file, tried in the following order:\n\t%s", strings.Join(serviceFilesAffinity, "\n\t"))
}
return composeFilepath, nil
}
// Apply common compose labels as per https://github.com/docker/compose/blob/126cb988c6f0c00a2a9887b8a39dc0907daec289/cmd/compose/compose.go#L200
func getProjectCustomLabels(project, service, workingDir string, composeFiles ...string) map[string]string {
labels := map[string]string{
api.ProjectLabel: project,
api.ServiceLabel: service,
api.VersionLabel: api.ComposeVersion,
api.ConfigFilesLabel: strings.Join(composeFiles, ","),
api.OneoffLabel: "False",
}
if workingDir != "" {
labels[api.WorkingDirLabel] = workingDir
}
return labels
}