stacks

package
v0.2.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: AGPL-3.0 Imports: 20 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"), 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

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"
	// 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.

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.

View Source
const MaxTreeEntries = 2000

MaxTreeEntries bounds the file tree of a stack; a larger tree is cut off.

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 CleanPath added in v0.2.0

func CleanPath(p string) (string, error)

CleanPath checks a path relative to a stack's directory: slash-separated, no "." or ".." elements, not into skipped directories.

func ComposeFile

func ComposeFile(dir string) string

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.

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
	// 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

func GitInfoFrom(r db.GitStack) *GitInfo

GitInfoFrom converts a stored Git stack.

type GitRegistry added in v0.2.0

type GitRegistry interface {
	ListGitStacks(ctx context.Context) ([]db.GitStack, error)
}

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

func WritePath(def Definition, p string, content []byte, version string) (PathFile, error)

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 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
	// 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

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.

func (*Service) Remove added in v0.2.0

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

Remove deletes the directory of a managed stack with all its files (#248). The caller takes the stack's containers down first.

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.

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

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