prepare

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 9 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 prepare provisions a server for NextDeploy from the daemon's own static binary — no Python, no Ansible, nothing pre-installed on the target beyond a shell.

Ansible needed Python on the managed host because Ansible ships Python modules there and executes them; the provisioning work itself never did. On a small server that dependency is real cost, and a Python version mismatch between the control node and the target fails the run with an error that looks like a NextDeploy bug. nextdeployd is already a static Go binary that generates systemd units, writes Caddy config and sets socket permissions — this package moves provisioning into the language the system already speaks.

The trade Ansible gave us for free was check-before-change. Here every step declares its own Done predicate, and the runner skips satisfied steps, so a re-run converges instead of redoing work.

Index

Constants

View Source
const (
	AppRoot    = "/opt/nextdeploy"
	AppsDir    = AppRoot + "/apps"
	UploadsDir = AppRoot + "/uploads"
	TmpDir     = AppRoot + "/tmp"

	ServiceUser  = "nextdeploy"
	ServiceGroup = "nextdeploy"

	CaddyLogDir      = "/var/log/caddy"
	CaddyFragmentDir = "/etc/caddy/nextdeploy.d"

	// BinDir is where systemd ExecStart lines look for runtimes. Units do not
	// inherit a login PATH, so resolveBinary hard-codes this prefix — every
	// runtime we install has to be reachable here.
	BinDir = "/usr/local/bin"

	// NodeMajor is the Node major installed from NodeSource. Pinned so a
	// re-prepare six months from now provisions the same runtime.
	NodeMajor = "22"
)

Paths and identities the daemon assumes at runtime. Changing one here without changing the daemon is how a "successful" prepare produces a server the daemon can't use, so they are named once and shared.

View Source
const (
	CaddyfilePath  = "/etc/caddy/Caddyfile"
	CaddyUnitPath  = "/etc/systemd/system/caddy.service"
	DaemonConfDir  = "/etc/nextdeployd"
	DaemonConfPath = DaemonConfDir + "/config.json"
	DaemonUnitPath = "/etc/systemd/system/nextdeployd.service"
	DaemonSockDir  = "/run/nextdeployd"
	DaemonSockPath = DaemonSockDir + "/nextdeployd.sock"
)

Paths for the edge (Caddy) and control-plane (nextdeployd) layers. Same rule as the constants in plan.go: the daemon assumes these at runtime, so they are named once and shared rather than spelled out per step.

Variables

This section is empty.

Functions

func RequireRoot

func RequireRoot() error

RequireRoot reports an actionable error when prepare isn't running with the privileges it needs. Every step writes under /opt, /etc or /usr/local and creates a system user, so failing here beats failing halfway through with a pile of permission errors.

Types

type Host

type Host interface {
	// Run executes a command and returns its combined output.
	Run(name string, args ...string) (string, error)
	// RunShell executes a shell script (for pipelines the package managers need).
	RunShell(script string) (string, error)
	// LookPath reports whether an executable is resolvable, and where.
	LookPath(name string) (string, bool)
	// Exists reports whether a path exists.
	Exists(path string) bool
	// ReadFile / WriteFile / MkdirAll mirror the os equivalents.
	ReadFile(path string) ([]byte, error)
	WriteFile(path string, data []byte, perm os.FileMode) error
	MkdirAll(path string, perm os.FileMode) error
	// Chown sets ownership by name, resolving uid/gid on the host.
	Chown(path, owner, group string) error
	// GroupExists / UserExists gate the create steps.
	GroupExists(name string) bool
	UserExists(name string) bool
	// UserInGroup reports secondary-group membership. Caddy serves assets out
	// of /opt/nextdeploy, which is 0750 nextdeploy:nextdeploy — group access is
	// the only way in.
	UserInGroup(username, group string) bool
}

Host is every effect a step can have on the machine. Steps take a Host rather than calling os/exec directly so the whole provisioning plan is unit-testable against a fake — the thing hand-written idempotency most needs.

func NewHost

func NewHost() Host

NewHost returns a Host backed by the real machine.

type Options

type Options struct {
	// SkipRuntimes leaves Node/Corepack alone. Useful when the host is
	// managed separately and prepare should only own the NextDeploy layer.
	SkipRuntimes bool
	// SkipHardening leaves fail2ban and logrotate alone.
	SkipHardening bool
}

Options tunes what a run provisions.

type PackageManager

type PackageManager string

PackageManager identifies the host's package manager.

const (
	PkgApt     PackageManager = "apt"
	PkgDnf     PackageManager = "dnf"
	PkgYum     PackageManager = "yum"
	PkgUnknown PackageManager = ""
)

func DetectPackageManager

func DetectPackageManager(h Host) PackageManager

DetectPackageManager picks the host's package manager, preferring dnf over yum (dnf is yum's successor and present alongside it on modern RHEL).

type Report

type Report struct {
	Results []Result
}

Report is the outcome of a whole run.

func Run

func Run(h Host, opts Options, out io.Writer) *Report

Run provisions this machine and returns the report. It never panics on a failed step — the report carries the outcome so the caller can decide the exit code and print a summary.

func RunSteps

func RunSteps(h Host, steps []Step, out io.Writer) *Report

RunSteps executes steps in order, skipping satisfied ones and stopping at the first required failure. Optional failures are recorded and the run continues.

Stopping at the first required failure is deliberate: later steps assume earlier ones landed (the app user must exist before its directories are chowned), so pressing on would turn one clear error into a cascade of confusing ones.

func (*Report) Counts

func (r *Report) Counts() (ok, skipped, warned, failed int)

Counts summarizes the run for a one-line ending.

func (*Report) Failed

func (r *Report) Failed() bool

Failed reports whether any required step failed.

func (*Report) Summary

func (r *Report) Summary() string

Summary renders the run as a human-readable block.

type Result

type Result struct {
	Name   string
	Status Status
	Err    error
}

Result records what happened to one step.

type Status

type Status string

Status is the outcome of one step.

const (
	StatusOK      Status = "ok"      // applied successfully
	StatusSkipped Status = "skipped" // already satisfied
	StatusWarned  Status = "warn"    // optional step failed
	StatusFailed  Status = "failed"  // required step failed
)

type Step

type Step struct {
	Name string
	// Done reports the step's effect is already in place. nil means "always
	// apply" (only correct for steps that are inherently idempotent).
	Done func(Host) bool
	// Apply performs the change.
	Apply func(Host) error
	// Optional marks a step whose failure degrades the install but must not
	// abort it (e.g. fail2ban hardening on a host without fail2ban).
	Optional bool
}

Step is one idempotent unit of provisioning.

Done is the check-before-change Ansible gave us for free and we now write by hand: it must be cheap, side-effect free, and honest. A Done that returns true when the state is only partially there is worse than no Done at all — the runner will skip the repair.

func Plan

func Plan(h Host, opts Options) []Step

Plan builds the ordered provisioning steps for this host.

Order matters and encodes real dependencies: the group must exist before the user that joins it, the user before the directories it owns, Node before the Corepack shims that Node provides.

Jump to

Keyboard shortcuts

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