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 ¶
- Variables
- func Ask(in *bufio.Reader, out io.Writer, label, fallback string) (string, error)
- func AskOptional(in *bufio.Reader, out io.Writer, label string) string
- func RandomPassword() string
- func RenderCloudInit(a Answers) (string, error)
- func RenderWorkerCloudInit(a WorkerAnswers) (string, error)
- type Answers
- type Error
- type WorkerAnswers
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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.
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" or "advanced".
UI string
// ConfigRepo is an optional git URL cloned to /opt/preview/config for driving the CLI from the host.
ConfigRepo 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
}
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.