Documentation
¶
Overview ¶
Package stack deploys Docker Compose stacks to managed machines.
Index ¶
- Constants
- func BuildAdoptScript(o AdoptOptions) (string, error)
- func BuildDeployScript(name string, compose []byte, assets []Asset) (string, error)
- func BuildListScript() string
- func BuildReadScript(paths []string) string
- func BuildRenameScript(oldName, newName string, compose []byte) (string, error)
- func DockerVersionFrom(out string) string
- func NamedVolumes(compose []byte) ([]string, error)
- func ParseFiles(out string) map[string]string
- func ProjectName(compose []byte) string
- func ValidateAssetName(name string) error
- func ValidateName(name string) error
- type AdoptOptions
- type Asset
- type BindMount
- type ContainerState
- type Project
- type PublishedPort
Constants ¶
const ( MarkerStopped = "hlab:stopped" MarkerMoved = "hlab:moved" MarkerRolled = "hlab:rolled-back" )
Markers the adopt script prints so a partial run can be described exactly.
const ( MarkerDockerPresent = "hlab:docker-present" MarkerDockerInstalled = "hlab:docker-installed" )
Docker install markers.
const ( MarkerNoDocker = "hlab:no-docker" MarkerDeployed = "hlab:deployed" )
Markers the remote script prints so the outcome can be reported precisely.
const FileMarker = "##HLAB#FILE#"
FileMarker delimits one file in the read script's output.
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.
const ListScript = `` /* 154-byte string literal not displayed */
ListScript reports the compose projects on a machine. It only reads.
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.
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 ¶
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 ¶
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 ¶
BuildRenameScript renders the script that renames a managed stack.
func DockerVersionFrom ¶
DockerVersionFrom pulls the reported version out of the install output.
func NamedVolumes ¶
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 ¶
ParseFiles splits the read script's output back into path/content pairs.
func ProjectName ¶
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 ¶
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 ¶
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 ¶
BindMount is a host path a stack mounts into a container.
func RelativeBindMounts ¶
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 ¶
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 ¶
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.
type PublishedPort ¶
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.