-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
api.go
631 lines (568 loc) · 20.4 KB
/
api.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
621
622
623
624
625
626
627
628
629
630
631
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"context"
"fmt"
"strings"
"time"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/utils"
)
// Service manages a compose project
type Service interface {
// Build executes the equivalent to a `compose build`
Build(ctx context.Context, project *types.Project, options BuildOptions) error
// Push executes the equivalent to a `compose push`
Push(ctx context.Context, project *types.Project, options PushOptions) error
// Pull executes the equivalent of a `compose pull`
Pull(ctx context.Context, project *types.Project, options PullOptions) error
// Create executes the equivalent to a `compose create`
Create(ctx context.Context, project *types.Project, options CreateOptions) error
// Start executes the equivalent to a `compose start`
Start(ctx context.Context, projectName string, options StartOptions) error
// Restart restarts containers
Restart(ctx context.Context, projectName string, options RestartOptions) error
// Stop executes the equivalent to a `compose stop`
Stop(ctx context.Context, projectName string, options StopOptions) error
// Up executes the equivalent to a `compose up`
Up(ctx context.Context, project *types.Project, options UpOptions) error
// Down executes the equivalent to a `compose down`
Down(ctx context.Context, projectName string, options DownOptions) error
// Logs executes the equivalent to a `compose logs`
Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
// Ps executes the equivalent to a `compose ps`
Ps(ctx context.Context, projectName string, options PsOptions) ([]ContainerSummary, error)
// List executes the equivalent to a `docker stack ls`
List(ctx context.Context, options ListOptions) ([]Stack, error)
// Kill executes the equivalent to a `compose kill`
Kill(ctx context.Context, projectName string, options KillOptions) error
// RunOneOffContainer creates a service oneoff container and starts its dependencies
RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
// Remove executes the equivalent to a `compose rm`
Remove(ctx context.Context, projectName string, options RemoveOptions) error
// Exec executes a command in a running service container
Exec(ctx context.Context, projectName string, options RunOptions) (int, error)
// Attach STDIN,STDOUT,STDERR to a running service container
Attach(ctx context.Context, projectName string, options AttachOptions) error
// Copy copies a file/folder between a service container and the local filesystem
Copy(ctx context.Context, projectName string, options CopyOptions) error
// Pause executes the equivalent to a `compose pause`
Pause(ctx context.Context, projectName string, options PauseOptions) error
// UnPause executes the equivalent to a `compose unpause`
UnPause(ctx context.Context, projectName string, options PauseOptions) error
// Top executes the equivalent to a `compose top`
Top(ctx context.Context, projectName string, services []string) ([]ContainerProcSummary, error)
// Events executes the equivalent to a `compose events`
Events(ctx context.Context, projectName string, options EventsOptions) error
// Port executes the equivalent to a `compose port`
Port(ctx context.Context, projectName string, service string, port uint16, options PortOptions) (string, int, error)
// Publish executes the equivalent to a `compose publish`
Publish(ctx context.Context, project *types.Project, repository string, options PublishOptions) error
// Images executes the equivalent of a `compose images`
Images(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error)
// MaxConcurrency defines upper limit for concurrent operations against engine API
MaxConcurrency(parallel int)
// DryRunMode defines if dry run applies to the command
DryRunMode(ctx context.Context, dryRun bool) (context.Context, error)
// Watch services' development context and sync/notify/rebuild/restart on changes
Watch(ctx context.Context, project *types.Project, services []string, options WatchOptions) error
// Viz generates a graphviz graph of the project services
Viz(ctx context.Context, project *types.Project, options VizOptions) (string, error)
// Wait blocks until at least one of the services' container exits
Wait(ctx context.Context, projectName string, options WaitOptions) (int64, error)
// Scale manages numbers of container instances running per service
Scale(ctx context.Context, project *types.Project, options ScaleOptions) error
}
type ScaleOptions struct {
Services []string
}
type WaitOptions struct {
// Services passed in the command line to be waited
Services []string
// Executes a down when a container exits
DownProjectOnContainerExit bool
}
type VizOptions struct {
// IncludeNetworks if true, network names a container is attached to should appear in the graph node
IncludeNetworks bool
// IncludePorts if true, ports a container exposes should appear in the graph node
IncludePorts bool
// IncludeImageName if true, name of the image used to create a container should appear in the graph node
IncludeImageName bool
// Indentation string to be used to indent graphviz code, e.g. "\t", " "
Indentation string
}
// WatchLogger is a reserved name to log watch events
const WatchLogger = "#watch"
// WatchOptions group options of the Watch API
type WatchOptions struct {
Build *BuildOptions
LogTo LogConsumer
}
// BuildOptions group options of the Build API
type BuildOptions struct {
// Pull always attempt to pull a newer version of the image
Pull bool
// Push pushes service images
Push bool
// Progress set type of progress output ("auto", "plain", "tty")
Progress string
// Args set build-time args
Args types.MappingWithEquals
// NoCache disables cache use
NoCache bool
// Quiet make the build process not output to the console
Quiet bool
// Services passed in the command line to be built
Services []string
// Deps also build selected services dependencies
Deps bool
// Ssh authentications passed in the command line
SSHs []types.SSHKey
// Memory limit for the build container
Memory int64
// Builder name passed in the command line
Builder string
}
// Apply mutates project according to build options
func (o BuildOptions) Apply(project *types.Project) error {
platform := project.Environment["DOCKER_DEFAULT_PLATFORM"]
for name, service := range project.Services {
if service.Image == "" && service.Build == nil {
return fmt.Errorf("invalid service %q. Must specify either image or build", name)
}
if service.Build == nil {
continue
}
if platform != "" {
if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, platform) {
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, platform)
}
service.Platform = platform
}
if service.Platform != "" {
if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, service.Platform) {
return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)
}
}
service.Build.Pull = service.Build.Pull || o.Pull
service.Build.NoCache = service.Build.NoCache || o.NoCache
project.Services[name] = service
}
return nil
}
// CreateOptions group options of the Create API
type CreateOptions struct {
Build *BuildOptions
// Services defines the services user interacts with
Services []string
// Remove legacy containers for services that are not defined in the project
RemoveOrphans bool
// Ignore legacy containers for services that are not defined in the project
IgnoreOrphans bool
// Recreate define the strategy to apply on existing containers
Recreate string
// RecreateDependencies define the strategy to apply on dependencies services
RecreateDependencies string
// Inherit reuse anonymous volumes from previous container
Inherit bool
// Timeout set delay to wait for container to gracelfuly stop before sending SIGKILL
Timeout *time.Duration
// QuietPull makes the pulling process quiet
QuietPull bool
}
// StartOptions group options of the Start API
type StartOptions struct {
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
Project *types.Project
// Attach to container and forward logs if not nil
Attach LogConsumer
// AttachTo set the services to attach to
AttachTo []string
// CascadeStop stops the application when a container stops
CascadeStop bool
// ExitCodeFrom return exit code from specified service
ExitCodeFrom string
// Wait won't return until containers reached the running|healthy state
Wait bool
WaitTimeout time.Duration
// Services passed in the command line to be started
Services []string
Watch bool
}
// RestartOptions group options of the Restart API
type RestartOptions struct {
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
Project *types.Project
// Timeout override container restart timeout
Timeout *time.Duration
// Services passed in the command line to be restarted
Services []string
// NoDeps ignores services dependencies
NoDeps bool
}
// StopOptions group options of the Stop API
type StopOptions struct {
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
Project *types.Project
// Timeout override container stop timeout
Timeout *time.Duration
// Services passed in the command line to be stopped
Services []string
}
// UpOptions group options of the Up API
type UpOptions struct {
Create CreateOptions
Start StartOptions
}
// DownOptions group options of the Down API
type DownOptions struct {
// RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
RemoveOrphans bool
// Project is the compose project used to define this app. Might be nil if user ran `down` just with project name
Project *types.Project
// Timeout override container stop timeout
Timeout *time.Duration
// Images remove image used by services. 'all': Remove all images. 'local': Remove only images that don't have a tag
Images string
// Volumes remove volumes, both declared in the `volumes` section and anonymous ones
Volumes bool
// Services passed in the command line to be stopped
Services []string
}
// ConfigOptions group options of the Config API
type ConfigOptions struct {
// Format define the output format used to dump converted application model (json|yaml)
Format string
// Output defines the path to save the application model
Output string
// Resolve image reference to digests
ResolveImageDigests bool
}
// PushOptions group options of the Push API
type PushOptions struct {
Quiet bool
IgnoreFailures bool
}
// PullOptions group options of the Pull API
type PullOptions struct {
Quiet bool
IgnoreFailures bool
IgnoreBuildable bool
}
// ImagesOptions group options of the Images API
type ImagesOptions struct {
Services []string
}
// KillOptions group options of the Kill API
type KillOptions struct {
// RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
RemoveOrphans bool
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
Project *types.Project
// Services passed in the command line to be killed
Services []string
// Signal to send to containers
Signal string
}
// RemoveOptions group options of the Remove API
type RemoveOptions struct {
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
Project *types.Project
// Stop option passed in the command line
Stop bool
// Volumes remove anonymous volumes
Volumes bool
// Force don't ask to confirm removal
Force bool
// Services passed in the command line to be removed
Services []string
}
// RunOptions group options of the Run API
type RunOptions struct {
Build *BuildOptions
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
Project *types.Project
Name string
Service string
Command []string
Entrypoint []string
Detach bool
AutoRemove bool
Tty bool
Interactive bool
WorkingDir string
User string
Environment []string
CapAdd []string
CapDrop []string
Labels types.Labels
Privileged bool
UseNetworkAliases bool
NoDeps bool
// QuietPull makes the pulling process quiet
QuietPull bool
// used by exec
Index int
}
// AttachOptions group options of the Attach API
type AttachOptions struct {
Project *types.Project
Service string
Index int
DetachKeys string
NoStdin bool
Proxy bool
}
// EventsOptions group options of the Events API
type EventsOptions struct {
Services []string
Consumer func(event Event) error
}
// Event is a container runtime event served by Events API
type Event struct {
Timestamp time.Time
Service string
Container string
Status string
Attributes map[string]string
}
// PortOptions group options of the Port API
type PortOptions struct {
Protocol string
Index int
}
// OCIVersion controls manifest generation to ensure compatibility
// with different registries.
//
// Currently, this is not exposed as an option to the user – Compose uses
// OCI 1.0 mode automatically for ECR registries based on domain and OCI 1.1
// for all other registries.
//
// There are likely other popular registries that do not support the OCI 1.1
// format, so it might make sense to expose this as a CLI flag or see if
// there's a way to generically probe the registry for support level.
type OCIVersion string
const (
OCIVersion1_0 OCIVersion = "1.0"
OCIVersion1_1 OCIVersion = "1.1"
)
// PublishOptions group options of the Publish API
type PublishOptions struct {
ResolveImageDigests bool
OCIVersion OCIVersion
}
func (e Event) String() string {
t := e.Timestamp.Format("2006-01-02 15:04:05.000000")
var attr []string
for k, v := range e.Attributes {
attr = append(attr, fmt.Sprintf("%s=%s", k, v))
}
return fmt.Sprintf("%s container %s %s (%s)\n", t, e.Status, e.Container, strings.Join(attr, ", "))
}
// ListOptions group options of the ls API
type ListOptions struct {
All bool
}
// PsOptions group options of the Ps API
type PsOptions struct {
Project *types.Project
All bool
Services []string
}
// CopyOptions group options of the cp API
type CopyOptions struct {
Source string
Destination string
All bool
Index int
FollowLink bool
CopyUIDGID bool
}
// PortPublisher hold status about published port
type PortPublisher struct {
URL string
TargetPort int
PublishedPort int
Protocol string
}
// ContainerSummary hold high-level description of a container
type ContainerSummary struct {
ID string
Name string
Names []string
Image string
Command string
Project string
Service string
Created int64
State string
Status string
Health string
ExitCode int
Publishers PortPublishers
Labels map[string]string
SizeRw int64 `json:",omitempty"`
SizeRootFs int64 `json:",omitempty"`
Mounts []string
Networks []string
LocalVolumes int
}
// PortPublishers is a slice of PortPublisher
type PortPublishers []PortPublisher
// Len implements sort.Interface
func (p PortPublishers) Len() int {
return len(p)
}
// Less implements sort.Interface
func (p PortPublishers) Less(i, j int) bool {
left := p[i]
right := p[j]
if left.URL != right.URL {
return left.URL < right.URL
}
if left.TargetPort != right.TargetPort {
return left.TargetPort < right.TargetPort
}
if left.PublishedPort != right.PublishedPort {
return left.PublishedPort < right.PublishedPort
}
return left.Protocol < right.Protocol
}
// Swap implements sort.Interface
func (p PortPublishers) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
// ContainerProcSummary holds container processes top data
type ContainerProcSummary struct {
ID string
Name string
Processes [][]string
Titles []string
}
// ImageSummary holds container image description
type ImageSummary struct {
ID string
ContainerName string
Repository string
Tag string
Size int64
}
// ServiceStatus hold status about a service
type ServiceStatus struct {
ID string
Name string
Replicas int
Desired int
Ports []string
Publishers []PortPublisher
}
// LogOptions defines optional parameters for the `Log` API
type LogOptions struct {
Project *types.Project
Index int
Services []string
Tail string
Since string
Until string
Follow bool
Timestamps bool
}
// PauseOptions group options of the Pause API
type PauseOptions struct {
// Services passed in the command line to be started
Services []string
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
Project *types.Project
}
const (
// STARTING indicates that stack is being deployed
STARTING string = "Starting"
// RUNNING indicates that stack is deployed and services are running
RUNNING string = "Running"
// UPDATING indicates that some stack resources are being recreated
UPDATING string = "Updating"
// REMOVING indicates that stack is being deleted
REMOVING string = "Removing"
// UNKNOWN indicates unknown stack state
UNKNOWN string = "Unknown"
// FAILED indicates that stack deployment failed
FAILED string = "Failed"
)
const (
// RecreateDiverged to recreate services which configuration diverges from compose model
RecreateDiverged = "diverged"
// RecreateForce to force service container being recreated
RecreateForce = "force"
// RecreateNever to never recreate existing service containers
RecreateNever = "never"
)
// Stack holds the name and state of a compose application/stack
type Stack struct {
ID string
Name string
Status string
ConfigFiles string
Reason string
}
// LogConsumer is a callback to process log messages from services
type LogConsumer interface {
Log(containerName, message string)
Err(containerName, message string)
Status(container, msg string)
Register(container string)
}
// ContainerEventListener is a callback to process ContainerEvent from services
type ContainerEventListener func(event ContainerEvent)
// ContainerEvent notify an event has been collected on source container implementing Service
type ContainerEvent struct {
Type int
// Container is the name of the container _without the project prefix_.
//
// This is only suitable for display purposes within Compose, as it's
// not guaranteed to be unique across services.
Container string
ID string
Service string
Line string
// ContainerEventExit only
ExitCode int
Restarting bool
}
const (
// ContainerEventLog is a ContainerEvent of type log on stdout. Line is set
ContainerEventLog = iota
// ContainerEventErr is a ContainerEvent of type log on stderr. Line is set
ContainerEventErr
// ContainerEventAttach is a ContainerEvent of type attach. First event sent about a container
ContainerEventAttach
// ContainerEventStopped is a ContainerEvent of type stopped.
ContainerEventStopped
// ContainerEventRecreated let consumer know container stopped but his being replaced
ContainerEventRecreated
// ContainerEventExit is a ContainerEvent of type exit. ExitCode is set
ContainerEventExit
// UserCancel user cancelled compose up, we are stopping containers
UserCancel
)
// Separator is used for naming components
var Separator = "-"
// GetImageNameOrDefault computes the default image name for a service, used to tag built images
func GetImageNameOrDefault(service types.ServiceConfig, projectName string) string {
imageName := service.Image
if imageName == "" {
imageName = projectName + Separator + service.Name
}
return imageName
}