stack

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: GPL-3.0 Imports: 8 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 stack deploys Docker Compose stacks to managed machines.

Index

Constants

View Source
const (
	MarkerStopped = "hlab:stopped"
	MarkerMoved   = "hlab:moved"
	MarkerRolled  = "hlab:rolled-back"
)

Markers the adopt script prints so a partial run can be described exactly.

View Source
const (
	MarkerDockerPresent   = "hlab:docker-present"
	MarkerDockerInstalled = "hlab:docker-installed"
)

Docker install markers.

View Source
const (
	MarkerNoDocker = "hlab:no-docker"
	MarkerDeployed = "hlab:deployed"
)

Markers the remote script prints so the outcome can be reported precisely.

View Source
const FileMarker = "##HLAB#FILE#"

FileMarker delimits one file in the read script's output.

View Source
const InstallDockerScript = `
set -eu

if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then
  echo '` + MarkerDockerPresent + `'
  docker --version
  exit 0
fi

if ! command -v curl >/dev/null 2>&1; then
  echo 'hlab:error curl is not installed' >&2
  exit 10
fi

TMP=$(mktemp)
trap 'rm -f "$TMP"' EXIT

curl -fsSL https://get.docker.com -o "$TMP"

# A truncated download would otherwise run as far as it got.
if [ ! -s "$TMP" ] || ! tail -n 1 "$TMP" >/dev/null 2>&1; then
  echo 'hlab:error the installer download was empty' >&2
  exit 11
fi
if ! grep -q 'docker' "$TMP"; then
  echo 'hlab:error the installer download does not look like the docker script' >&2
  exit 12
fi

sh "$TMP"

if ! command -v docker >/dev/null 2>&1; then
  echo 'hlab:error docker is still not present after installation' >&2
  exit 13
fi

systemctl enable --now docker >/dev/null 2>&1 || true

echo '` + MarkerDockerInstalled + `'
docker --version
`

InstallDockerScript installs Docker using the vendor's convenience script.

get.docker.com pipes a remote script into a root shell, which is a real supply-chain exposure — but it is Docker's own recommendation, it configures the official apt repository rather than a distribution snapshot, and the alternative of hand-rolling the repository and key is more code to get wrong. The script is downloaded and checked before it is run, rather than piped straight into sh, so a truncated or empty download cannot half-execute.

View Source
const ListScript = `` /* 154-byte string literal not displayed */

ListScript reports the compose projects on a machine. It only reads.

View Source
const LocalDirName = "stacks"

LocalDirName is the directory under hlab's home where pulled stacks are kept. Never the working directory: a compose file routinely carries secrets and hlab is routinely run from inside a checkout.

View Source
const RemoteDir = "/opt/hlab/stacks"

RemoteDir is where stacks live on a managed machine.

Variables

This section is empty.

Functions

func BuildAdoptScript

func BuildAdoptScript(o AdoptOptions) (string, error)

BuildAdoptScript renders the script that relocates a project and redeploys it.

func BuildDeployScript

func BuildDeployScript(name string, compose []byte, assets []Asset) (string, error)

BuildDeployScript renders the script that installs and starts a stack, writing any assets alongside the compose file before bringing it up.

func BuildListScript

func BuildListScript() string

BuildListScript renders the project listing script.

func BuildReadScript

func BuildReadScript(paths []string) string

BuildReadScript emits the contents of the given paths, each behind a marker.

Paths come from docker's own project listing rather than from the operator, but they are quoted regardless: this runs as root, and a path is exactly the sort of value that stops being trustworthy the moment anything else can influence it.

func BuildRenameScript

func BuildRenameScript(oldName, newName string, compose []byte) (string, error)

BuildRenameScript renders the script that renames a managed stack.

func DockerVersionFrom

func DockerVersionFrom(out string) string

DockerVersionFrom pulls the reported version out of the install output.

func NamedVolumes

func NamedVolumes(compose []byte) ([]string, error)

NamedVolumes reports the named volumes a compose file declares.

These matter for a rename: compose prefixes a named volume with the project name, so renaming the project leaves the old volume behind and starts the stack against an empty new one. The data is not lost, but the stack comes up as though it were new — which is worse than failing, because it looks like it worked.

func ParseFiles

func ParseFiles(out string) map[string]string

ParseFiles splits the read script's output back into path/content pairs.

func ProjectName

func ProjectName(compose []byte) string

ProjectName reads the name a compose file declares for itself, if any.

A file that states its name keeps it wherever it is moved to. One that does not takes the name from its directory, which is why the destination is named after the project rather than anything else.

func ValidateAssetName

func ValidateAssetName(name string) error

ValidateAssetName rejects anything that would write outside the stack directory. The name becomes a path on a remote machine running the script as root, so this is a security control, not a nicety.

func ValidateName

func ValidateName(name string) error

ValidateName rejects stack names that would escape the stack directory.

Types

type AdoptOptions

type AdoptOptions struct {
	// Name is the compose project name, and the directory hlab will keep it in.
	Name string
	// SourceDir is where the project lives now.
	SourceDir string
	// OriginalFile is the compose file's current base name, kept for rollback.
	OriginalFile string
	// Compose is the reviewed compose file to deploy in its place.
	Compose []byte
	// Assets are configuration files deployed alongside it.
	Assets []Asset
	// CarriesData says the stack keeps data beside its compose file, so the
	// directory itself has to move. When it does not, the directory is left
	// alone — a project defined straight in a home directory is common, and
	// moving that would take the home with it.
	CarriesData bool
}

AdoptOptions describes one project being taken over.

type Asset

type Asset struct {
	// Name is the file's name inside the stack directory, with no path.
	Name string
	// Content is written verbatim.
	Content []byte
}

Asset is a file deployed alongside the compose file, for the configuration a stack mounts rather than embeds — a Caddyfile, a resolver config, a rules file. Compose can carry environment variables, but not a multi-line config, so without these a stack is only half reproducible.

type BindMount

type BindMount struct {
	Service string
	Source  string
}

BindMount is a host path a stack mounts into a container.

func RelativeBindMounts

func RelativeBindMounts(compose []byte) ([]BindMount, error)

RelativeBindMounts reports mounts whose source is relative to the compose file's own directory.

This is what makes adopting a stack more than copying one file: a project that keeps its database in ./appdata carries that directory with it, and a compose file relocated without it comes up empty. Absolute sources (/var/run/docker.sock) and named volumes are not returned — they do not move.

type ContainerState

type ContainerState struct {
	Name    string
	Service string
	State   string
}

ContainerState is one container's state as compose reports it.

func ParseContainerStates

func ParseContainerStates(out string) []ContainerState

ParseContainerStates reads the `compose ps` lines the script emits.

type Project

type Project struct {
	Name        string `json:"Name"`
	Status      string `json:"Status"`
	ConfigFiles string `json:"ConfigFiles"`
}

Project is one Docker Compose project as the machine reports it.

func ParseProjects

func ParseProjects(out string) ([]Project, error)

ParseProjects reads the JSON array `docker compose ls` prints.

Output is taken from the first line that looks like the array, because a sudo banner or a deprecation notice ahead of it is common and would otherwise make an ordinary machine look like it has no stacks.

func (Project) Adopted

func (p Project) Adopted() bool

Adopted reports whether hlab already owns this project.

func (Project) Dir

func (p Project) Dir() string

Dir is the directory the project's first config file lives in, which is what its relative bind mounts resolve against.

func (Project) Files

func (p Project) Files() []string

Files splits the comma-separated config file list compose reports.

type PublishedPort

type PublishedPort struct {
	Port    int
	Service string
}

PublishedPort is a host port a compose file would bind.

func PublishedPorts

func PublishedPorts(compose []byte) ([]PublishedPort, error)

PublishedPorts reports the host ports a compose file would bind.

This exists so a deployment can be checked against what a machine is already listening on: a stack that silently fails to bind because something else holds the port is worse than one that refuses up front.

Jump to

Keyboard shortcuts

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