Documentation
¶
Overview ¶
Package stacks finds compose stacks and computes their status (#24, ADR-0008). A stack is a compose project: a directory with a compose file, either inside --stacks-dir ("managed") or registered in place from elsewhere ("external", #25). Its containers carry the com.docker.compose.project label.
Index ¶
- Constants
- Variables
- func ComposeFile(dir string) string
- func FilePath(def Definition, kind string) (name, path string, err error)
- func Unmanaged(defs []Definition, containers []runtime.Container) []string
- func Version(content []byte) string
- type AdoptRequest
- type Candidate
- type Definition
- type Describer
- type Registry
- type Service
- func (s *Service) Adopt(ctx context.Context, d Describer, req AdoptRequest) (Definition, error)
- func (s *Service) Create(ctx context.Context, name string, compose []byte) (Definition, error)
- func (s *Service) Definitions(ctx context.Context) ([]Definition, error)
- func (s *Service) Discover(ctx context.Context, d Describer) ([]Candidate, error)
- func (s *Service) Find(ctx context.Context, name string) (Definition, error)
- func (s *Service) Forget(ctx context.Context, name string) error
- type Stack
- type StackContainer
- type StackFile
- type StackService
- type Writer
Constants ¶
const ( // AdoptInPlace registers the directory where it is; files stay there. AdoptInPlace = "in-place" // AdoptCopy copies the compose and .env files into the stacks directory. AdoptCopy = "copy" )
Adoption modes (#25, ADR-0008).
const ( FileCompose = "compose" FileEnv = "env" )
Editable files of a stack (#26).
const ( ModeManaged = "managed" ModeExternal = "external" // ModeUnmanaged is a compose project seen only through container labels: nerdui does not // know its files. ModeUnmanaged = "unmanaged" )
Modes of a stack.
const ( StatusRunning = "running" // every service runs StatusPartial = "partial" // some services run StatusStopped = "stopped" // nothing runs StatusUnknown = "unknown" // no readable compose file )
Statuses of a stack.
const DefaultCompose = `services:
app:
image: docker.io/library/nginx:1.27
ports:
- "8080:80"
restart: unless-stopped
`
DefaultCompose is the compose file of a new stack.
const MaxFileSize = 1 << 20
MaxFileSize bounds an edited file.
Variables ¶
var ComposeFileNames = []string{"compose.yaml", "compose.yml", "docker-compose.yml", "docker-compose.yaml"}
ComposeFileNames are looked up in this order, as compose-go does (cli.DefaultFileNames).
Functions ¶
func ComposeFile ¶
ComposeFile returns the compose file of dir, or "" when it has none.
func FilePath ¶
func FilePath(def Definition, kind string) (name, path string, err error)
FilePath returns the name and path of an editable file of a stack.
Types ¶
type AdoptRequest ¶
type AdoptRequest struct {
Name string `json:"name"`
Path string `json:"path"`
Mode string `json:"mode"`
}
AdoptRequest adopts an existing compose project as a stack.
type Candidate ¶
type Candidate struct {
Name string `json:"name"`
Path string `json:"path"`
ComposeFile string `json:"composeFile"`
Services []string `json:"services"`
// LocalPaths are files of the directory the project uses: it can only be adopted in place.
LocalPaths []string `json:"localPaths,omitempty"`
// Error explains why the compose file could not be read.
Error string `json:"error,omitempty"`
// Taken is true when a stack of that name exists already.
Taken bool `json:"taken"`
}
Candidate is a compose project found in a scanned directory and not yet a stack.
type Definition ¶
type Definition struct {
Name string
Dir string
// File is the compose file inside Dir ("" when none is found).
File string
Mode string
}
Definition is a stack nerdui knows the files of.
type Describer ¶
type Describer interface {
Describe(ctx context.Context, dir, file string) (runtime.ComposeProject, error)
}
Describer loads a compose project from disk; runtime.Compose implements it.
type Service ¶
type Service struct {
// Dir is --stacks-dir; each subdirectory with a compose file is a managed stack.
Dir string
Registry Registry
// Writer registers external stacks (#25); nil makes adoption unavailable.
Writer Writer
// ScanDirs are searched for compose projects to adopt (--stacks-scan-dirs, #25).
ScanDirs []string
}
Service finds the stack definitions of a host.
func (*Service) Adopt ¶
func (s *Service) Adopt(ctx context.Context, d Describer, req AdoptRequest) (Definition, error)
Adopt makes an existing compose project a stack (#25): in place (the directory is registered) or by copying its compose and .env files into the stacks directory. Copying is refused when the project uses other files of its directory, which would not follow it.
func (*Service) Create ¶
Create makes a new managed stack: <stacks-dir>/<name>/compose.yaml with compose (#26). The content must have been validated by the caller.
func (*Service) Definitions ¶
func (s *Service) Definitions(ctx context.Context) ([]Definition, error)
Definitions returns the managed and external stacks sorted by name. A missing stacks directory has no stacks.
func (*Service) Discover ¶
Discover scans the scan directories for compose projects that are not stacks yet (#25).
type Stack ¶
type Stack struct {
Name string `json:"name"`
Mode string `json:"mode"`
// Path is the stack directory and ComposeFile the file name in it (empty when unmanaged).
Path string `json:"path,omitempty"`
ComposeFile string `json:"composeFile,omitempty"`
Status string `json:"status"`
// Error explains why the compose file could not be read.
Error string `json:"error,omitempty"`
Services []StackService `json:"services"`
// Containers and Running count all containers of the project, including ones of services
// no longer in the compose file.
Containers int `json:"containers"`
Running int `json:"running"`
}
Stack is a stack with its status in a namespace. Keep in sync with api/openapi.yaml.
type StackContainer ¶
type StackContainer struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
State string `json:"state"`
}
StackContainer is a container of a service.
type StackFile ¶
type StackFile struct {
Kind string `json:"kind"`
Name string `json:"name"`
Content string `json:"content"`
Version string `json:"version"`
Exists bool `json:"exists"`
}
StackFile is the content of an editable file. Version identifies the content for optimistic concurrency; it is empty when the file does not exist. Keep in sync with api/openapi.yaml.
func ReadFile ¶
func ReadFile(def Definition, kind string) (StackFile, error)
ReadFile reads an editable file; a missing .env is empty.
type StackService ¶
type StackService struct {
Name string `json:"name"`
Defined bool `json:"defined"`
Running int `json:"running"`
Containers []StackContainer `json:"containers"`
}
StackService is a service of a stack. Defined is false for a service that has containers but is not in the compose file (removed or behind a profile).