container

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT 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 container runs a local pool's command in a container (docs/containers.md): the docker run argv, the labels that tie a container to the ticket that started it, the guard process that removes a container when bitc is killed, and the sweep that removes orphans by label.

Index

Constants

View Source
const (
	LabelJob    = "bitc.job"
	LabelPool   = "bitc.pool"
	LabelState  = "bitc.state"
	LabelTicket = "bitc.ticket"
)

The labels on every container bitc starts. bitc.state scopes a sweep to one state dir; bitc.ticket (the ticket file name without .json) is what tells a live run from an orphan.

View Source
const LifelineFD = 3

LifelineFD is the descriptor the guard finds the read end of bitc's lifeline pipe on.

Variables

View Source
var ErrNotFound = errors.New("not found on PATH")

ErrNotFound is a runtime that is not on PATH.

Functions

func GuardMain

func GuardMain(args []string, life *os.File) int

GuardMain is the body of `bitc __container-guard <runtime> <cidfile> <envfile>`: it waits on life; "done" means bitc cleaned up, and anything else (EOF) means bitc died, so it removes the container whose id is in the cidfile (if there is one) and the cidfile and the env file.

func Lookup

func Lookup(name string, env []string) (string, error)

Lookup finds the runtime's CLI on the PATH of env (the run's environment, last PATH wins).

func Name

func Name(ticket string) string

Name is the container's name, unique per run: bitc-<ticket id>. The job name is only a label (bitc.job): --name is the caller's and may be reused, even by runs going at the same time, and a removal by a name two runs share would remove the other run's container.

func ReadID

func ReadID(cidfile string) string

ReadID is the container id docker wrote to the cidfile, and only a full one (64 hex digits): "" if there is no file, nothing in it, or anything else. No id means docker never created a container for this run, and nothing is removed: a short id would be resolved by the daemon as a name first and a prefix of another container's id next, which is not "this run's container".

func Remove

func Remove(ctx context.Context, runtime, id string, env []string) error

Remove force-removes the container with this id (`rm -f`). It is by id and never by name: an id is the one container this run (or the listing) saw, a name can be another run's. A container that is already gone is not an error: the run's own --rm, the guard and the sweep may all get there first.

func RemoveArgv

func RemoveArgv(runtime, id string) []string

RemoveArgv is the forced removal of the container with this id.

func RunArgv

func RunArgv(s Spec) []string

RunArgv is the whole `docker run` command line, program first.

func StateHash

func StateHash(stateDir string) string

StateHash is the bitc.state label: the first 12 hex digits of the sha256 of the state dir path.

func Sweep

func Sweep(ctx context.Context, runtime string, l state.Layout, env []string, dry bool, removed, kept func(Found)) error

Sweep removes this state dir's orphaned containers (a label match whose ticket is gone or unheld), by the id the listing gave, and then the cidfiles and env files of dead runs. removed is called for each orphan, after its removal, or instead of it when dry; kept for each whose holder this host cannot tell (never removed on a guess, #24). A live run's container is never reported.

Any error aborts the sweep: the tickets cannot be read, the listing fails, a removal fails. It returns the error and removes nothing more: containers it removed before a failing removal stay removed, and the files of dead runs are touched only after every removal succeeded. A sweep that cannot see what it is doing (the tickets, the listing) deletes nothing at all.

func TicketID

func TicketID(ticketFile string) string

TicketID is a ticket file's name without ".json".

func WriteEnvFile

func WriteEnvFile(path string, env []string) error

WriteEnvFile writes env (NAME=VALUE entries) one per line to path, mode 0600, for --env-file. It replaces a leftover of the same name, and writes nothing if an entry cannot be one line of the file (no name, no =, a newline or a NUL in it). The state dir around it is 0700 as well.

Types

type Found

type Found struct {
	ID     string // the container id: what a removal goes by
	Name   string
	Pool   string
	Job    string // the bitc.job label
	Ticket string
	Live   lock.State // StateFree: the ticket is gone or nobody holds it (an orphan); StateHeld: a run owns it; StateUnknown: this host cannot tell
	Why    string     // with StateUnknown: why (the error looking for the ticket, or the lock this host cannot inspect)
}

Found is a container of this state dir and what its ticket says about its run.

type Guard

type Guard struct {
	// contains filtered or unexported fields
}

Guard is the process that removes a container when bitc dies without cleaning up. bitc holds the write end of a pipe whose read end is the guard's fd 3; the kernel closes it when bitc ends, even by SIGKILL, and the guard reads EOF. It runs in its own session, so a signal to bitc or its process group does not reach it. This is the same lifeline idea as the macOS plugin shim, and it works the same on Linux, where the Pdeathsig of the docker CLI child kills the CLI but never the container.

func StartGuard

func StartGuard(exe string, exeArgs []string, runtime, cidfile, envfile string, env []string) (*Guard, error)

StartGuard starts `exe exeArgs... <runtime> <cidfile> <envfile>` as the guard. Production passes bitc's own binary and "__container-guard". The guard knows the run's container only through its cidfile, the id docker wrote there: with no id there is no container this run created, and the guard removes none (a name it could remove by might be another run's).

func (*Guard) Done

func (g *Guard) Done()

Done tells the guard the run is over (the container is removed) and lets it exit.

type Spec

type Spec struct {
	Runtime string // the CLI's path (Lookup)
	Image   string
	Job     string
	Pool    string
	State   string // StateHash
	Ticket  string // TicketID
	CID     string // --cidfile path
	Mounts  []config.Mount
	Dir     string // --workdir
	CPUs    string // "" = no limit
	Memory  config.Size
	EnvFile string // --env-file path (WriteEnvFile): the container's environment, never on argv
	TTY     bool
	Argv    []string // the command
}

Spec is one container run.

Jump to

Keyboard shortcuts

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