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"
|
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`
|
|
|
|
Build(ctx context.Context, project *types.Project) 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`
|
2020-12-07 09:09:12 +01:00
|
|
|
Start(ctx context.Context, project *types.Project, consumer LogConsumer) 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`
|
2020-12-07 14:46:36 +01:00
|
|
|
Ps(ctx context.Context, projectName string) ([]ContainerSummary, error)
|
2020-09-04 13:20:11 +02:00
|
|
|
// List executes the equivalent to a `docker stack ls`
|
|
|
|
List(ctx context.Context, projectName string) ([]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)
|
2020-12-16 15:25:31 +01:00
|
|
|
// RunOneOffContainer creates a service oneoff container and starts its dependencies
|
2020-12-16 18:50:03 +01:00
|
|
|
RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) error
|
2020-12-16 16:16:39 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 14:23:37 +01:00
|
|
|
// CreateOptions group options of the Create API
|
|
|
|
type CreateOptions struct {
|
|
|
|
// Remove legacy containers for services that are not defined in the project
|
|
|
|
RemoveOrphans bool
|
|
|
|
}
|
|
|
|
|
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
|
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
|
2020-05-05 10:58:24 +02:00
|
|
|
}
|
2020-08-17 17:48:52 +02:00
|
|
|
|
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 {
|
2020-12-29 16:22:57 -03:00
|
|
|
Service string
|
2020-12-16 18:34:14 +01:00
|
|
|
Command []string
|
|
|
|
Detach bool
|
|
|
|
AutoRemove bool
|
2020-12-16 18:50:03 +01:00
|
|
|
Writer io.Writer
|
|
|
|
Reader io.Reader
|
2020-12-03 09:24:15 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 {
|
|
|
|
Services []string
|
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
Log(service, container, message string)
|
|
|
|
}
|