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"), registered in place from elsewhere ("external", #25) or cloned from a Git repository ("git", #37). Its containers carry the com.docker.compose.project label.
Index ¶
- Constants
- Variables
- func CleanPath(p string) (string, error)
- func ComposeFile(dir string) string
- func DeletePath(def Definition, p, version string) error
- 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 GitInfo
- type GitRegistry
- type PathFile
- 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
- func (s *Service) Remove(ctx context.Context, name string) error
- type Stack
- type StackContainer
- type StackFile
- type StackService
- type Tree
- type TreeEntry
- 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" // ModeGit is a stack cloned from a Git repository (#37, ADR-0039): its compose file comes // from the repository and is read-only; .env is local. ModeGit = "git" // 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.
const MaxTreeEntries = 2000
MaxTreeEntries bounds the file tree of a stack; a larger tree is cut off.
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 CleanPath ¶ added in v0.2.0
CleanPath checks a path relative to a stack's directory: slash-separated, no "." or ".." elements, not into skipped directories.
func ComposeFile ¶
ComposeFile returns the compose file of dir, or "" when it has none.
func DeletePath ¶ added in v0.2.0
func DeletePath(def Definition, p, version string) error
DeletePath removes a file, or an empty directory, of a stack's directory. A file is removed only if its version is still version; the compose file cannot be removed.
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
// Git describes the repository of a Git stack.
Git *GitInfo
}
Definition is a stack nerdui knows the files of.
func GitDefinition ¶ added in v0.2.0
func GitDefinition(gitDir string, r db.GitStack) Definition
GitDefinition is the definition of a stored Git stack cloned into gitDir/<name>.
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 GitInfo ¶ added in v0.2.0
type GitInfo struct {
URL string `json:"url"`
// Ref as entered (empty: the default branch) and FullRef as resolved.
Ref string `json:"ref"`
FullRef string `json:"fullRef"`
ComposePath string `json:"composePath"`
CredentialID *int64 `json:"credentialId,omitempty"`
Commit gitrepo.Commit `json:"commit"`
PulledAt time.Time `json:"pulledAt"`
// LastError of the newest failed pull or check; the checked-out commit stays.
LastError string `json:"lastError,omitempty"`
// AutoUpdate says what happens on a new commit found by polling or the webhook (#38, #39):
// off, notify or deploy. PollInterval is in seconds, 0 without polling.
AutoUpdate string `json:"autoUpdate"`
PollInterval int64 `json:"pollInterval"`
// Webhook is true when the stack has a webhook token.
Webhook bool `json:"webhook"`
// CheckedAt is the last check of the remote; RemoteCommit the newest remote commit when it
// is not checked out.
CheckedAt *time.Time `json:"checkedAt,omitempty"`
RemoteCommit string `json:"remoteCommit,omitempty"`
}
GitInfo is the source of a Git stack. Keep in sync with api/openapi.yaml.
func GitInfoFrom ¶ added in v0.2.0
GitInfoFrom converts a stored Git stack.
type GitRegistry ¶ added in v0.2.0
GitRegistry lists Git stacks; *db.Queries implements it.
type PathFile ¶ added in v0.2.0
type PathFile struct {
Path string `json:"path"`
Content string `json:"content"`
Version string `json:"version"`
Exists bool `json:"exists"`
Size int64 `json:"size"`
Binary bool `json:"binary,omitempty"`
TooBig bool `json:"tooBig,omitempty"`
}
PathFile is the content of a file of a stack's directory, by its path (#27). Binary files and files larger than MaxFileSize have no content and cannot be edited. Keep in sync with api/openapi.yaml.
func ReadPath ¶ added in v0.2.0
func ReadPath(def Definition, p string) (PathFile, error)
ReadPath reads a file of a stack's directory; a missing file is returned with Exists false.
func WritePath ¶ added in v0.2.0
WritePath creates or replaces a text file of a stack's directory if its version is still version (empty: the file must not exist), creating parent directories. The compose file and .env go through WriteFile instead, which keeps a backup. New files are readable by the containers they are mounted into, except .env files.
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
// Git lists Git stacks cloned into GitDir (#37); nil disables them.
Git GitRegistry
GitDir 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"`
// Git is set for a stack cloned from a Git repository.
Git *GitInfo `json:"git,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).
type Tree ¶ added in v0.2.0
type Tree struct {
Entries []TreeEntry `json:"entries"`
Truncated bool `json:"truncated"`
ReadOnly bool `json:"readOnly"`
}
Tree is the file tree of a stack. ReadOnly is set for Git stacks, whose files come from the repository. Keep in sync with api/openapi.yaml.
func ReadTree ¶ added in v0.2.0
func ReadTree(def Definition) (Tree, error)
ReadTree lists the files and directories of a stack's directory, directories first, up to MaxTreeEntries. Symbolic links and other special files are left out.
type TreeEntry ¶ added in v0.2.0
type TreeEntry struct {
Path string `json:"path"`
Dir bool `json:"dir"`
Size int64 `json:"size"`
Protected bool `json:"protected,omitempty"`
}
TreeEntry is a file or directory of a stack's directory (#27). Path is relative to the directory, with slashes. Protected files cannot be deleted: the stack's compose file. Keep in sync with api/openapi.yaml.