| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 | /*   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 apiimport (	"context"	"fmt"	"io"	"strings"	"time"	"github.com/compose-spec/compose-go/types")// Service manages a compose projecttype Service interface {	// Build executes the equivalent to a `compose build`	Build(ctx context.Context, project *types.Project, options BuildOptions) error	// Push executes the equivalent ot 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, opts PullOptions) error	// Create executes the equivalent to a `compose create`	Create(ctx context.Context, project *types.Project, opts CreateOptions) error	// Start executes the equivalent to a `compose start`	Start(ctx context.Context, project *types.Project, options StartOptions) error	// Restart restarts containers	Restart(ctx context.Context, project *types.Project, options RestartOptions) error	// Stop executes the equivalent to a `compose stop`	Stop(ctx context.Context, project *types.Project, 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)	// Convert translate compose model into backend's native format	Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)	// Kill executes the equivalent to a `compose kill`	Kill(ctx context.Context, project *types.Project, 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, project *types.Project, options RemoveOptions) error	// Exec executes a command in a running service container	Exec(ctx context.Context, project string, opts RunOptions) (int, error)	// Copy copies a file/folder between a service container and the local filesystem	Copy(ctx context.Context, project string, options CopyOptions) error	// Pause executes the equivalent to a `compose pause`	Pause(ctx context.Context, project string, options PauseOptions) error	// UnPause executes the equivalent to a `compose unpause`	UnPause(ctx context.Context, project 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, project string, options EventsOptions) error	// Port executes the equivalent to a `compose port`	Port(ctx context.Context, project string, service string, port int, options PortOptions) (string, int, error)	// Images executes the equivalent of a `compose images`	Images(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error)}// BuildOptions group options of the Build APItype BuildOptions struct {	// Pull always attempt to pull a newer version of the image	Pull 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}// CreateOptions group options of the Create APItype CreateOptions struct {	// 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 APItype StartOptions struct {	// 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}// RestartOptions group options of the Restart APItype RestartOptions struct {	// Timeout override container restart timeout	Timeout *time.Duration	// Services passed in the command line to be restarted	Services []string}// StopOptions group options of the Stop APItype StopOptions struct {	// 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 APItype UpOptions struct {	Create CreateOptions	Start  StartOptions}// DownOptions group options of the Down APItype 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}// ConvertOptions group options of the Convert APItype ConvertOptions 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}// PushOptions group options of the Push APItype PushOptions struct {	IgnoreFailures bool}// PullOptions group options of the Pull APItype PullOptions struct {	Quiet          bool	IgnoreFailures bool}// ImagesOptions group options of the Images APItype ImagesOptions struct {	Services []string}// KillOptions group options of the Kill APItype KillOptions struct {	// Services passed in the command line to be killed	Services []string	// Signal to send to containers	Signal string}// RemoveOptions group options of the Remove APItype RemoveOptions struct {	// DryRun just list removable resources	DryRun 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 APItype RunOptions struct {	Name              string	Service           string	Command           []string	Entrypoint        []string	Detach            bool	AutoRemove        bool	Stdin             io.ReadCloser	Stdout            io.WriteCloser	Stderr            io.WriteCloser	Tty               bool	WorkingDir        string	User              string	Environment       []string	Labels            types.Labels	Privileged        bool	UseNetworkAliases bool	NoDeps            bool	// QuietPull makes the pulling process quiet	QuietPull bool	// used by exec	Index int}// EventsOptions group options of the Events APItype EventsOptions struct {	Services []string	Consumer func(event Event) error}// Event is a container runtime event served by Events APItype Event struct {	Timestamp  time.Time	Service    string	Container  string	Status     string	Attributes map[string]string}// PortOptions group options of the Port APItype PortOptions struct {	Protocol string	Index    int}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 APItype ListOptions struct {	All bool}// PsOptions group options of the Ps APItype PsOptions struct {	All      bool	Services []string}// CopyOptions group options of the cp APItype CopyOptions struct {	Source      string	Destination string	All         bool	Index       int	FollowLink  bool	CopyUIDGID  bool}// PortPublisher hold status about published porttype PortPublisher struct {	URL           string	TargetPort    int	PublishedPort int	Protocol      string}// ContainerSummary hold high-level description of a containertype ContainerSummary struct {	ID         string	Name       string	Command    string	Project    string	Service    string	State      string	Health     string	ExitCode   int	Publishers PortPublishers}// PortPublishers is a slice of PortPublishertype PortPublishers []PortPublisher// Len implements sort.Interfacefunc (p PortPublishers) Len() int {	return len(p)}// Less implements sort.Interfacefunc (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.Interfacefunc (p PortPublishers) Swap(i, j int) {	p[i], p[j] = p[j], p[i]}// ContainerProcSummary holds container processes top datatype ContainerProcSummary struct {	ID        string	Name      string	Processes [][]string	Titles    []string}// ImageSummary holds container image descriptiontype ImageSummary struct {	ID            string	ContainerName string	Repository    string	Tag           string	Size          int64}// ServiceStatus hold status about a servicetype ServiceStatus struct {	ID         string	Name       string	Replicas   int	Desired    int	Ports      []string	Publishers []PortPublisher}// LogOptions defines optional parameters for the `Log` APItype LogOptions struct {	Services   []string	Tail       string	Since      string	Until      string	Follow     bool	Timestamps bool}// PauseOptions group options of the Pause APItype PauseOptions struct {	// Services passed in the command line to be started	Services []string}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/stacktype Stack struct {	ID     string	Name   string	Status string	Reason string}// LogConsumer is a callback to process log messages from servicestype LogConsumer interface {	Log(service, container, message string)	Status(container, msg string)	Register(container string)}// ContainerEventListener is a callback to process ContainerEvent from servicestype ContainerEventListener func(event ContainerEvent)// ContainerEvent notify an event has been collected on source container implementing Servicetype ContainerEvent struct {	Type      int	Container string	Service   string	Line      string	// ContainerEventExit only	ExitCode   int	Restarting bool}const (	// ContainerEventLog is a ContainerEvent of type log. Line is set	ContainerEventLog = iota	// ContainerEventAttach is a ContainerEvent of type attach. First event sent about a container	ContainerEventAttach	// ContainerEventStopped is a ContainerEvent of type stopped.	ContainerEventStopped	// ContainerEventExit is a ContainerEvent of type exit. ExitCode is set	ContainerEventExit	// UserCancel user cancelled compose up, we are stopping containers	UserCancel)
 |