stacks

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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).

View Source
const (
	FileCompose = "compose"
	FileEnv     = "env"
)

Editable files of a stack (#26).

View Source
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.

View Source
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.

View Source
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.

View Source
const MaxFileSize = 1 << 20

MaxFileSize bounds an edited file.

Variables

View Source
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

func ComposeFile(dir string) string

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.

func Unmanaged

func Unmanaged(defs []Definition, containers []runtime.Container) []string

Unmanaged returns the compose projects of containers that match no definition, sorted.

func Version

func Version(content []byte) string

Version returns the version of content.

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 Registry

type Registry interface {
	ListStacks(ctx context.Context) ([]db.Stack, error)
}

Registry stores external stacks; *db.Queries 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

func (s *Service) Create(ctx context.Context, name string, compose []byte) (Definition, error)

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

func (s *Service) Discover(ctx context.Context, d Describer) ([]Candidate, error)

Discover scans the scan directories for compose projects that are not stacks yet (#25).

func (*Service) Find

func (s *Service) Find(ctx context.Context, name string) (Definition, error)

Find returns the definition of a stack nerdui knows the files of.

func (*Service) Forget

func (s *Service) Forget(ctx context.Context, name string) error

Forget removes the registration of an external stack; its files stay where they are.

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.

func Summarize

func Summarize(def Definition, services []string, loadErr error, containers []runtime.Container) Stack

Summarize computes a stack's status from its compose services (nil with loadErr when the file could not be read) and the containers of its project.

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.

func WriteFile

func WriteFile(def Definition, kind string, content []byte, version string) (StackFile, error)

WriteFile replaces an editable file if its version is still version (empty: the file must not exist), keeping the previous content as <name>.bak. The write is atomic: a temporary file in the same directory is renamed over the file, with the file's permissions.

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 Writer

type Writer interface {
	RegisterStack(ctx context.Context, arg db.RegisterStackParams) error
	DeleteStack(ctx context.Context, name string) (int64, error)
}

Writer registers and forgets external stacks; *db.Queries implements it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL