2020-06-18 16:13:24 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-06-18 16:13:24 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-05-05 10:58:24 +02:00
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-16 18:50:03 +01:00
|
|
|
"io"
|
2021-02-23 10:36:23 +01:00
|
|
|
"strings"
|
2021-02-15 13:54:55 +01:00
|
|
|
"time"
|
2020-07-02 16:05:45 +02:00
|
|
|
|
2020-08-20 16:42:37 +02:00
|
|
|
"github.com/compose-spec/compose-go/types"
|
2020-05-05 10:58:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service manages a compose project
|
|
|
|
type Service interface {
|
2020-11-30 12:03:13 +01:00
|
|
|
// Build executes the equivalent to a `compose build`
|
2021-03-02 09:03:04 +01:00
|
|
|
Build(ctx context.Context, project *types.Project, options BuildOptions) error
|
2020-12-01 11:34:48 +01:00
|
|
|
// Push executes the equivalent ot a `compose push`
|
|
|
|
Push(ctx context.Context, project *types.Project) error
|
2020-12-03 12:22:01 +01:00
|
|
|
// Pull executes the equivalent of a `compose pull`
|
|
|
|
Pull(ctx context.Context, project *types.Project) error
|
2020-12-02 18:23:01 +01:00
|
|
|
// Create executes the equivalent to a `compose create`
|
2021-01-06 14:23:37 +01:00
|
|
|
Create(ctx context.Context, project *types.Project, opts CreateOptions) error
|
2020-12-02 18:23:01 +01:00
|
|
|
// Start executes the equivalent to a `compose start`
|
2021-02-08 11:04:46 +01:00
|
|
|
Start(ctx context.Context, project *types.Project, options StartOptions) error
|
2021-01-20 09:35:06 -03:00
|
|
|
// Stop executes the equivalent to a `compose stop`
|
2021-02-15 13:54:55 +01:00
|
|
|
Stop(ctx context.Context, project *types.Project, options StopOptions) error
|
2020-05-05 10:58:24 +02:00
|
|
|
// Up executes the equivalent to a `compose up`
|
2020-12-16 16:16:39 +01:00
|
|
|
Up(ctx context.Context, project *types.Project, options UpOptions) error
|
2020-05-05 10:58:24 +02:00
|
|
|
// Down executes the equivalent to a `compose down`
|
2020-12-16 16:16:39 +01:00
|
|
|
Down(ctx context.Context, projectName string, options DownOptions) error
|
2020-08-07 10:16:12 +02:00
|
|
|
// Logs executes the equivalent to a `compose logs`
|
2020-12-16 16:58:54 +01:00
|
|
|
Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
|
2020-08-07 10:16:12 +02:00
|
|
|
// Ps executes the equivalent to a `compose ps`
|
2021-01-29 14:41:44 +01:00
|
|
|
Ps(ctx context.Context, projectName string, options PsOptions) ([]ContainerSummary, error)
|
2020-09-04 13:20:11 +02:00
|
|
|
// List executes the equivalent to a `docker stack ls`
|
2021-02-24 12:06:17 +01:00
|
|
|
List(ctx context.Context, options ListOptions) ([]Stack, error)
|
2020-08-18 08:58:37 +02:00
|
|
|
// Convert translate compose model into backend's native format
|
2020-12-16 16:16:39 +01:00
|
|
|
Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
|
2021-01-31 14:42:13 -03:00
|
|
|
// Kill executes the equivalent to a `compose kill`
|
|
|
|
Kill(ctx context.Context, project *types.Project, options KillOptions) error
|
2020-12-16 15:25:31 +01:00
|
|
|
// RunOneOffContainer creates a service oneoff container and starts its dependencies
|
2021-02-12 17:33:59 +01:00
|
|
|
RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
|
2021-02-15 09:13:41 +01:00
|
|
|
// Remove executes the equivalent to a `compose rm`
|
2021-02-15 10:46:56 +01:00
|
|
|
Remove(ctx context.Context, project *types.Project, options RemoveOptions) ([]string, error)
|
2021-02-15 09:56:34 +01:00
|
|
|
// Exec executes a command in a running service container
|
|
|
|
Exec(ctx context.Context, project *types.Project, opts RunOptions) error
|
2021-02-22 10:25:40 +01:00
|
|
|
// Pause executes the equivalent to a `compose pause`
|
|
|
|
Pause(ctx context.Context, project *types.Project) error
|
|
|
|
// UnPause executes the equivalent to a `compose unpause`
|
|
|
|
UnPause(ctx context.Context, project *types.Project) error
|
2020-12-16 16:16:39 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 09:03:04 +01:00
|
|
|
// BuildOptions group options of the Build API
|
|
|
|
type BuildOptions struct {
|
|
|
|
// Pull always attempt to pull a newer version of the image
|
|
|
|
Pull bool
|
2021-03-02 09:14:09 +01:00
|
|
|
// Progress set type of progress output ("auto", "plain", "tty")
|
|
|
|
Progress string
|
2021-03-02 09:03:04 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 14:23:37 +01:00
|
|
|
// CreateOptions group options of the Create API
|
|
|
|
type CreateOptions struct {
|
2021-03-01 17:45:52 +01:00
|
|
|
// Services defines the services user interacts with
|
|
|
|
Services []string
|
2021-01-06 14:23:37 +01:00
|
|
|
// Remove legacy containers for services that are not defined in the project
|
|
|
|
RemoveOrphans bool
|
2021-01-28 15:00:46 +01:00
|
|
|
// Recreate define the strategy to apply on existing containers
|
|
|
|
Recreate string
|
2021-03-01 17:45:52 +01:00
|
|
|
// RecreateDependencies define the strategy to apply on dependencies services
|
|
|
|
RecreateDependencies string
|
2021-03-02 08:38:15 +01:00
|
|
|
// Inherit reuse anonymous volumes from previous container
|
|
|
|
Inherit bool
|
2021-03-02 14:33:26 +01:00
|
|
|
// Timeout set delay to wait for container to gracelfuly stop before sending SIGKILL
|
|
|
|
Timeout *time.Duration
|
2021-01-06 14:23:37 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 11:04:46 +01:00
|
|
|
// StartOptions group options of the Start API
|
|
|
|
type StartOptions struct {
|
2021-02-11 10:45:11 +01:00
|
|
|
// Attach will attach to service containers and send container logs and events
|
2021-02-10 14:20:32 +01:00
|
|
|
Attach ContainerEventListener
|
2021-02-08 11:04:46 +01:00
|
|
|
}
|
|
|
|
|
2021-02-15 13:54:55 +01:00
|
|
|
// StopOptions group options of the Stop API
|
|
|
|
type StopOptions struct {
|
|
|
|
// Timeout override container stop timeout
|
|
|
|
Timeout *time.Duration
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:16:39 +01:00
|
|
|
// UpOptions group options of the Up API
|
|
|
|
type UpOptions struct {
|
|
|
|
// Detach will create services and return immediately
|
|
|
|
Detach bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-01-14 15:59:57 +01:00
|
|
|
// Project is the compose project used to define this app. Might be nil if user ran `down` just with project name
|
|
|
|
Project *types.Project
|
2021-02-15 13:54:55 +01:00
|
|
|
// Timeout override container stop timeout
|
|
|
|
Timeout *time.Duration
|
2021-02-17 15:22:26 +01:00
|
|
|
// 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
|
2020-12-16 16:16:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConvertOptions group options of the Convert API
|
|
|
|
type ConvertOptions struct {
|
|
|
|
// Format define the output format used to dump converted application model (json|yaml)
|
|
|
|
Format string
|
2021-02-05 17:16:20 +01:00
|
|
|
// Output defines the path to save the application model
|
|
|
|
Output string
|
2020-05-05 10:58:24 +02:00
|
|
|
}
|
2020-08-17 17:48:52 +02:00
|
|
|
|
2021-01-31 14:42:13 -03:00
|
|
|
// KillOptions group options of the Kill API
|
|
|
|
type KillOptions struct {
|
|
|
|
// Signal to send to containers
|
|
|
|
Signal string
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:13:41 +01:00
|
|
|
// RemoveOptions group options of the Remove API
|
|
|
|
type RemoveOptions struct {
|
2021-02-15 10:46:56 +01:00
|
|
|
// DryRun just list removable resources
|
|
|
|
DryRun bool
|
2021-02-15 09:13:41 +01:00
|
|
|
// Volumes remove anonymous volumes
|
|
|
|
Volumes bool
|
|
|
|
// Force don't ask to confirm removal
|
|
|
|
Force bool
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:50:03 +01:00
|
|
|
// RunOptions options to execute compose run
|
2020-12-03 09:24:15 +01:00
|
|
|
type RunOptions struct {
|
2021-02-23 10:36:23 +01:00
|
|
|
Name string
|
|
|
|
Service string
|
|
|
|
Command []string
|
|
|
|
Entrypoint []string
|
|
|
|
Detach bool
|
|
|
|
AutoRemove bool
|
|
|
|
Writer io.Writer
|
|
|
|
Reader io.Reader
|
2021-02-15 09:56:34 +01:00
|
|
|
Tty bool
|
|
|
|
WorkingDir string
|
|
|
|
User string
|
|
|
|
Environment []string
|
2021-02-23 10:36:23 +01:00
|
|
|
Labels types.Labels
|
2021-02-15 09:56:34 +01:00
|
|
|
Privileged bool
|
2021-02-23 10:36:23 +01:00
|
|
|
// used by exec
|
|
|
|
Index int
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnvironmentMap return RunOptions.Environment as a MappingWithEquals
|
|
|
|
func (opts *RunOptions) EnvironmentMap() types.MappingWithEquals {
|
|
|
|
environment := types.MappingWithEquals{}
|
|
|
|
for _, s := range opts.Environment {
|
|
|
|
parts := strings.SplitN(s, "=", 2)
|
|
|
|
key := parts[0]
|
|
|
|
switch {
|
|
|
|
case len(parts) == 1:
|
|
|
|
environment[key] = nil
|
|
|
|
default:
|
|
|
|
environment[key] = &parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return environment
|
2020-12-03 09:24:15 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 12:06:17 +01:00
|
|
|
// ListOptions group options of the ls API
|
|
|
|
type ListOptions struct {
|
|
|
|
All bool
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:41:44 +01:00
|
|
|
// PsOptions group options of the Ps API
|
|
|
|
type PsOptions struct {
|
|
|
|
All bool
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:56:42 +02:00
|
|
|
// PortPublisher hold status about published port
|
2020-08-18 11:38:23 +02:00
|
|
|
type PortPublisher struct {
|
2020-08-17 17:48:52 +02:00
|
|
|
URL string
|
|
|
|
TargetPort int
|
|
|
|
PublishedPort int
|
|
|
|
Protocol string
|
|
|
|
}
|
|
|
|
|
2020-12-07 14:46:36 +01:00
|
|
|
// ContainerSummary hold high-level description of a container
|
|
|
|
type ContainerSummary struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Project string
|
|
|
|
Service string
|
|
|
|
State string
|
2021-02-01 16:53:34 +01:00
|
|
|
Health string
|
2020-12-07 14:46:36 +01:00
|
|
|
Publishers []PortPublisher
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:56:42 +02:00
|
|
|
// ServiceStatus hold status about a service
|
2020-08-17 17:48:52 +02:00
|
|
|
type ServiceStatus struct {
|
2020-08-18 16:56:42 +02:00
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Replicas int
|
|
|
|
Desired int
|
|
|
|
Ports []string
|
|
|
|
Publishers []PortPublisher
|
2020-08-18 11:38:23 +02:00
|
|
|
}
|
2020-09-04 13:30:11 +02:00
|
|
|
|
2020-12-16 16:58:54 +01:00
|
|
|
// LogOptions defines optional parameters for the `Log` API
|
|
|
|
type LogOptions struct {
|
2021-02-18 16:50:39 +01:00
|
|
|
Services []string
|
|
|
|
Tail string
|
|
|
|
Follow bool
|
|
|
|
Timestamps bool
|
2020-12-16 16:58:54 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 13:20:11 +02:00
|
|
|
const (
|
2020-09-04 13:30:11 +02:00
|
|
|
// STARTING indicates that stack is being deployed
|
2020-09-04 18:11:02 +02:00
|
|
|
STARTING string = "Starting"
|
2020-09-04 13:30:11 +02:00
|
|
|
// RUNNING indicates that stack is deployed and services are running
|
2020-09-04 18:11:02 +02:00
|
|
|
RUNNING string = "Running"
|
2020-09-04 13:30:11 +02:00
|
|
|
// UPDATING indicates that some stack resources are being recreated
|
2020-09-04 18:11:02 +02:00
|
|
|
UPDATING string = "Updating"
|
2020-09-04 13:30:11 +02:00
|
|
|
// REMOVING indicates that stack is being deleted
|
2020-09-04 18:11:02 +02:00
|
|
|
REMOVING string = "Removing"
|
|
|
|
// UNKNOWN indicates unknown stack state
|
|
|
|
UNKNOWN string = "Unknown"
|
|
|
|
// FAILED indicates that stack deployment failed
|
|
|
|
FAILED string = "Failed"
|
2020-09-04 13:20:11 +02:00
|
|
|
)
|
|
|
|
|
2021-01-28 15:00:46 +01:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2020-09-04 13:30:11 +02:00
|
|
|
// Stack holds the name and state of a compose application/stack
|
2020-09-04 13:20:11 +02:00
|
|
|
type Stack struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
2020-09-04 18:11:02 +02:00
|
|
|
Status string
|
2020-09-30 14:52:53 +02:00
|
|
|
Reason string
|
2020-09-04 13:20:11 +02:00
|
|
|
}
|
2020-12-07 09:09:12 +01:00
|
|
|
|
|
|
|
// LogConsumer is a callback to process log messages from services
|
|
|
|
type LogConsumer interface {
|
2021-02-22 11:40:26 +01:00
|
|
|
Log(name, service, container, message string)
|
2021-02-15 11:14:09 +01:00
|
|
|
Status(name, container, msg string)
|
|
|
|
Register(name string, source string)
|
2021-02-08 11:42:42 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 14:20:32 +01:00
|
|
|
// ContainerEventListener is a callback to process ContainerEvent from services
|
|
|
|
type ContainerEventListener func(event ContainerEvent)
|
|
|
|
|
2021-02-09 16:51:48 +01:00
|
|
|
// ContainerEvent notify an event has been collected on Source container implementing Service
|
|
|
|
type ContainerEvent struct {
|
|
|
|
Type int
|
|
|
|
Source string
|
|
|
|
Service string
|
2021-02-15 11:14:09 +01:00
|
|
|
Name string
|
2021-02-09 16:51:48 +01:00
|
|
|
Line string
|
|
|
|
ExitCode int
|
2020-12-07 09:09:12 +01:00
|
|
|
}
|
2021-02-09 16:51:48 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
// ContainerEventLog is a ContainerEvent of type log. Line is set
|
|
|
|
ContainerEventLog = iota
|
2021-02-12 10:02:05 +01:00
|
|
|
// ContainerEventAttach is a ContainerEvent of type attach. First event sent about a container
|
|
|
|
ContainerEventAttach
|
2021-02-09 16:51:48 +01:00
|
|
|
// ContainerEventExit is a ContainerEvent of type exit. ExitCode is set
|
|
|
|
ContainerEventExit
|
2021-02-23 15:13:51 +01:00
|
|
|
// UserCancel user cancelled compose up, we are stopping containers
|
|
|
|
UserCancel
|
2021-02-09 16:51:48 +01:00
|
|
|
)
|