cloudinit

package
v0.46.1 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT, MIT Imports: 12 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 cloudinit is `pstack cloud-init` — fill the cloud-config template and print a ready-to-boot user-data file.

The template is embedded at build time (like the control-stack compose file), so this works from a global install with no checkout — the same property `build-image` needed.

Placeholders are `{{NAME}}`, deliberately NOT `${NAME}`: the template contains real shell that must survive rendering untouched (`$(dpkg --print-architecture)`, `${STACK}`, `${DOMAIN}` for Compose). Using the same syntax for both would mean the generator eating lines meant for bash.

`{{...}}` does collide with one other thing in the file — Docker's Go templates, e.g. `docker inspect -f '{{.State.Health.Status}}'`. They coexist because the pattern here is `[A-Z_]+` only: a Go template carries dots and lower case, so neither the substitution nor the leftover-check below can touch it. Keep placeholder names SHOUT_CASE and that stays true.

Substitution is strings.ReplaceAll over an ORDERED list (rule 8) — the TS did `out.split('{{K}}').join(v)`, which has none of String.replace's `$` magic, and neither does this.

Index

Constants

This section is empty.

Variables

View Source
var Distros = []string{"ubuntu", "debian", "fedora", "suse", "arch", "alpine"}

Distros are the targets the generator can render for.

What actually differs is smaller than it looks: how Docker is installed and how its service is enabled. Everything else — cloud-init's own `users`/`groups`/`packages` stages, the Bun installer, pstack, the compose files — is identical across them. So a distro is a table row, not a template.

Docker sources, and why they differ:

  • ubuntu/debian/fedora: Docker's OWN repositories, which is the vendor-recommended path and what the original Ubuntu-only template already did. Fedora avoids `dnf config-manager` on purpose — its syntax CHANGED between dnf4 and dnf5 (Fedora ≥41), and downloading the .repo file directly sidesteps the divergence entirely.
  • suse/arch/alpine: Docker publishes no repository for these; the distro's own packages are the supported path. Their compose package may ship a standalone `docker-compose` binary rather than the CLI plugin, which is why the template carries a plugin-symlink fallback after this block.

Alpine is the odd one out twice: OpenRC instead of systemd, and no bash in the base image — the template's own runcmd lines use `bash -c`, so bash (and sudo, for the `preview` user's sudoers entry) ride in through extraPackages.

Functions

func Ask

func Ask(in *bufio.Reader, out io.Writer, label, fallback string) (string, error)

Ask asks for anything not already supplied: prints `<label>[ [fallback]]:` and reads a line.

EOF (`prompt` returned null in the TS) is an error, so a piped/CI invocation with missing answers fails loudly here instead of silently rendering an empty domain.

func AskOptional

func AskOptional(in *bufio.Reader, out io.Writer, label string) string

AskOptional asks for something the answer may legitimately be "nothing".

Separate from Ask because EOF must mean "skip it", not an error: a piped or CI invocation that runs out of input should still produce a file, and an OPTIONAL field is exactly the one place where no answer is a valid answer.

func RandomPassword

func RandomPassword() string

RandomPassword is hex, because a `$` in a password would break the single-quoted shell that hashes it.

func RenderCloudInit

func RenderCloudInit(a Answers) (string, error)

RenderCloudInit renders the template. Pure: no prompting, no filesystem — so it is trivially testable.

func RenderWorkerCloudInit

func RenderWorkerCloudInit(a WorkerAnswers) (string, error)

RenderWorkerCloudInit is the user-data for a swarm WORKER: install Docker, join the manager. Nothing else — no Bun, no pstack, no control stack; a worker runs tasks the manager schedules and is managed from the manager.

The one optional step is Logging's loki plugin, between Docker and the join. Its section is UNNUMBERED so the logging-off file stays byte-identical, and it is a `|` literal block because the line starts with `{` and its echo carries `: ` — as a plain scalar YAML reads a flow mapping. `{ …; } || echo` so a failed install is a line in cloud-init's log, never a worker that did not join.

Its own small template rather than a third of the manager's with conditionals: the generator has no conditional syntax (deliberately — see the header), and the two files share only the Docker install fragments, which are reused here verbatim.

THE TOKEN IS A SECRET: whoever can read this file can add a node that runs any task on the cluster. Treat the rendered file like the manager's `.env`.

Types

type Answers

type Answers struct {
	Domain    string
	AcmeEmail string
	// SSHKey is optional: most providers inject their own key at boot (hcloud --ssh-key).
	SSHKey            string
	DashboardPassword string
	// Challenge is "http01" or "dns01".
	Challenge   string
	DNSProvider string
	// UI is "basic", "advanced" or "hearth".
	UI string
	// AdminUser is the first pstack account, created by the control plane on first boot. Empty (the
	// default) renders no account at all, and the operator makes one afterwards with
	// `POST /api/auth/bootstrap` and the bearer token.
	AdminUser string
	// AdminPassword is that account's password. Only read when AdminUser is set.
	AdminPassword string
	// ExtraDomains are additional hostnames the host answers on from its first boot, passed through
	// to `init` as --extra-domain. The case: the primary's DNS still points at the old box, so its
	// routers wait while one of these is usable now.
	ExtraDomains []string
	// DNSToken is the DNS-01 credential (PSTACK_DNS_TOKEN on the rendered init call). REQUIRED with
	// `dns01`: without it the host boots, renders `dns.env` empty, and every ACME order fails with
	// "some credentials information are missing" — a wildcard host that never gets a certificate,
	// which is not a state worth rendering a file for. It DOES land in instance metadata, so the
	// header says so rather than claiming the file holds no credential.
	DNSToken string
	// Token is PSTACK_TOKEN for the host. Empty (the default) is the SAFER one: `init` generates a
	// token on the box and prints it once into the boot log, so it never exists in instance metadata
	// at all. Set it only when something already holds the token — a CI secret, a second host.
	Token string
	// ConfigRepo is an optional git URL cloned to /opt/preview/config for driving the CLI from the host.
	ConfigRepo string
	// ConfigSealed is the sealed bytes of a `pstack pull config` export, embedded with write_files;
	// ConfigURL is the alternative, fetched by the host at boot. Mutually exclusive, and either one
	// needs ConfigKey. Opaque here on purpose: this package renders text and has no business
	// importing the store that `internal/config` reaches — the CLI unseals the file before calling,
	// which is where a wrong passphrase is caught.
	ConfigSealed []byte
	ConfigURL    string
	// ConfigKey is the passphrase, and it IS embedded in both modes: the box being provisioned has to
	// open the file with no human present. That is the security statement the SECRETS header has to
	// make honestly rather than paper over — with --config the payload sits beside the key in
	// instance metadata, so the seal protects it everywhere except here; with --config-url only the
	// key is here, and opening the export needs the metadata AND access to the URL.
	ConfigKey string
	// Distro decides the Docker install/enable fragments. Default ubuntu.
	Distro string
	// Orchestrator is `swarm` (the default for a new host) or `compose`; passed to
	// `pstack init --orchestrator` only when set.
	Orchestrator string
	// Logging is "none" or "loki"; loki adds `--logging loki` to the init call. No step of its own:
	// init installs the plugin on the manager.
	Logging string
	// PreviewsOn is `workers` or `any`; passed to `pstack init --previews-on` only when set. Swarm only.
	PreviewsOn string
	// Scaler is "on" or "off"; on adds `--scaler on` to the init call. Swarm only. Its settings and
	// credentials are entered on the host afterwards, never here.
	Scaler string
}

Answers are the inputs to RenderCloudInit. Plain strings where the TS had literal unions (`challenge`, `ui`, `orchestrator`): a value that is not one of the expected words renders the default branch, exactly as an untyped JS caller would get.

type Error

type Error struct{ Msg string }

Error is a refused input or a template that did not render. The CLI prints its message and exits 3.

func (*Error) Error

func (e *Error) Error() string

type WorkerAnswers

type WorkerAnswers struct {
	Token       string
	ManagerAddr string
	Distro      string
	SSHKey      string
	// Logging adds the loki plugin step between Docker and the join.
	Logging bool
}

WorkerAnswers are RenderWorkerCloudInit's inputs.

Jump to

Keyboard shortcuts

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