From 9643c41c5347df10a37db8cb13b3ef8bab26529e Mon Sep 17 00:00:00 2001 From: John Coffey Date: Fri, 13 Nov 2026 08:10:24 -0700 Subject: [PATCH] Add install --answers for an unattended install from a file --- README.md | 19 +++++++++ cmd/inbuxa/answers_test.go | 83 ++++++++++++++++++++++++++++++++++++++ cmd/inbuxa/main.go | 83 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 cmd/inbuxa/answers_test.go diff --git a/README.md b/README.md index def174b..b6815e4 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,14 @@ This is early. What is built: rest up without it, exempts the front ends from the auto-ban, creates the first mailbox, writes `credentials.txt` and `dns.zone`, and then checks that all three answer. +- **`install --answers FILE`** -- the install options from a JSON file, for + an unattended install driven by cloud-init, Ansible or a kickstart rather + than a long command line. The keys are the flag names (`domain`, + `mail-host`, `server`, `console`, `webmail`, `proxy`, `local`, and the + rest); a mistyped key is refused rather than ignored. A flag given on the + command line still wins over the file, so one answers file can be a base + that a flag overrides. It carries no secrets -- the API key stays on + `--key`/`INBUXA_API_KEY` -- and still needs `--yes` to carry the plan out. - **`install --server host`** -- the same, with the mail server as the release binary under systemd instead of a container, checked against a checksum in the source. It gets a user of its own, its configuration and @@ -196,6 +204,17 @@ DNS and no certificates, on a machine that starts with nothing. Without certificates -- which `e2e/cases/install-public.sh` proves against a private CA, with no internet and no public name involved. +For an unattended install, put the options in a file and point `--answers` at +it: + + cat > answers.json <<'JSON' + { "domain": "example.com", "email": "postmaster@example.com", "proxy": "caddy" } + JSON + inbuxa install --answers answers.json --yes + +A flag still wins over the file (`--answers answers.json --server host`), so +one file can be a base a flag overrides. + ## Download Each release has static binaries for linux/amd64 and linux/arm64, and a diff --git a/cmd/inbuxa/answers_test.go b/cmd/inbuxa/answers_test.go new file mode 100644 index 0000000..091b15d --- /dev/null +++ b/cmd/inbuxa/answers_test.go @@ -0,0 +1,83 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package main + +import ( + "testing" + + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/plan" +) + +func TestParseAnswersReadsFieldsAndRejectsTypos(t *testing.T) { + a, err := parseAnswers([]byte(`{"domain":"example.com","server":"host","local":false,"install-deps":true}`)) + if err != nil { + t.Fatalf("valid answers should parse: %v", err) + } + if a.Domain == nil || *a.Domain != "example.com" { + t.Errorf("domain not read: %+v", a.Domain) + } + if a.Server == nil || *a.Server != "host" { + t.Errorf("server not read: %+v", a.Server) + } + if a.Local == nil || *a.Local != false { + t.Errorf("an explicit false should be read, not left nil: %+v", a.Local) + } + if a.InstallDeps == nil || *a.InstallDeps != true { + t.Errorf("install-deps not read: %+v", a.InstallDeps) + } + if a.MailHost != nil { + t.Errorf("a key left out should stay nil, got %+v", a.MailHost) + } + + if _, err := parseAnswers([]byte(`{"domян":"typo"}`)); err == nil { + t.Error("an unknown key should be rejected") + } + if _, err := parseAnswers([]byte(`{"domain":`)); err == nil { + t.Error("malformed JSON should error") + } +} + +func TestApplyAnswersFillsUnsetOptions(t *testing.T) { + a, err := parseAnswers([]byte(`{"domain":"example.com","mail-host":"mx.example.com","server":"host","local":true}`)) + if err != nil { + t.Fatal(err) + } + var o plan.Options + server, console, webmail := "container", "container", "container" + applyAnswers(a, map[string]bool{}, &o, &server, &console, &webmail) + if o.Domain != "example.com" { + t.Errorf("domain not applied: %q", o.Domain) + } + if o.MailHost != "mx.example.com" { + t.Errorf("mail-host not applied: %q", o.MailHost) + } + if server != "host" { + t.Errorf("server shape not applied: %q", server) + } + if !o.Local { + t.Errorf("local not applied") + } + if console != "container" { + t.Errorf("a key left out must not change a default: console=%q", console) + } +} + +func TestApplyAnswersFlagsWinOverFile(t *testing.T) { + a, err := parseAnswers([]byte(`{"domain":"from-file.example","server":"host"}`)) + if err != nil { + t.Fatal(err) + } + var o plan.Options + o.Domain = "from-flag.example" // as if set on the command line + server := "skip" // as if --server skip was given + console, webmail := "container", "container" + // Both "domain" and "server" were set on the command line. + applyAnswers(a, map[string]bool{"domain": true, "server": true}, &o, &server, &console, &webmail) + if o.Domain != "from-flag.example" { + t.Errorf("a command-line flag must win over the file: domain=%q", o.Domain) + } + if server != "skip" { + t.Errorf("a command-line shape must win over the file: server=%q", server) + } +} diff --git a/cmd/inbuxa/main.go b/cmd/inbuxa/main.go index 0d02140..7434bee 100644 --- a/cmd/inbuxa/main.go +++ b/cmd/inbuxa/main.go @@ -104,6 +104,10 @@ install flags: nginx passed the server's names through to it; "-" reads it from standard input, and INBUXA_API_KEY works too + --answers FILE read these options from a JSON file, for an + unattended install (cloud-init, Ansible); keys + are the flag names, and a flag given here still + wins over the file --dry-run print the plan and stop --yes do not ask for confirmation @@ -242,6 +246,69 @@ func survey() int { return 0 } +// answersFile carries install options from a file, for an unattended install +// driven by cloud-init, Ansible or a kickstart rather than a long command +// line. Its keys are the install flags. Every field is a pointer, so a key +// left out keeps the default and an explicit `"local": false` is told apart +// from an absent one. A command-line flag always overrides the file. +type answersFile struct { + Domain *string `json:"domain"` + MailHost *string `json:"mail-host"` + ConsoleHost *string `json:"console-host"` + WebmailHost *string `json:"webmail-host"` + Email *string `json:"email"` + Proxy *string `json:"proxy"` + Dir *string `json:"dir"` + Local *bool `json:"local"` + InstallDeps *bool `json:"install-deps"` + ACMEDirectory *string `json:"acme-directory"` + ACMECARoot *string `json:"acme-ca-root"` + Server *string `json:"server"` + Console *string `json:"console"` + Webmail *string `json:"webmail"` +} + +// parseAnswers reads an answers file as JSON, rejecting unknown keys so a +// mistyped field name is caught rather than quietly ignored. +func parseAnswers(b []byte) (answersFile, error) { + var a answersFile + dec := json.NewDecoder(strings.NewReader(string(b))) + dec.DisallowUnknownFields() + if err := dec.Decode(&a); err != nil { + return answersFile{}, err + } + return a, nil +} + +// applyAnswers fills the options the command line did not set (those are in +// set) from the answers file; a flag given on the command line always wins. +func applyAnswers(a answersFile, set map[string]bool, o *plan.Options, server, console, webmail *string) { + str := func(name string, dst, v *string) { + if v != nil && !set[name] { + *dst = *v + } + } + boolean := func(name string, dst, v *bool) { + if v != nil && !set[name] { + *dst = *v + } + } + str("domain", &o.Domain, a.Domain) + str("mail-host", &o.MailHost, a.MailHost) + str("console-host", &o.ConsoleHost, a.ConsoleHost) + str("webmail-host", &o.WebmailHost, a.WebmailHost) + str("email", &o.ACMEEmail, a.Email) + str("proxy", &o.Proxy, a.Proxy) + str("dir", &o.Dir, a.Dir) + str("acme-directory", &o.ACMEDirectory, a.ACMEDirectory) + str("acme-ca-root", &o.ACMECARoot, a.ACMECARoot) + str("server", server, a.Server) + str("console", console, a.Console) + str("webmail", webmail, a.Webmail) + boolean("local", &o.Local, a.Local) + boolean("install-deps", &o.InstallDeps, a.InstallDeps) +} + func install(args []string) int { fs := flag.NewFlagSet("install", flag.ContinueOnError) fs.Usage = func() { fmt.Print(usage) } @@ -253,6 +320,7 @@ func install(args []string) int { dryRun = fs.Bool("dry-run", false, "") yes = fs.Bool("yes", false, "") key = fs.String("key", os.Getenv("INBUXA_API_KEY"), "") + answers = fs.String("answers", "", "") ) fs.StringVar(&o.Domain, "domain", "", "") fs.StringVar(&o.MailHost, "mail-host", "", "") @@ -268,6 +336,21 @@ func install(args []string) int { if err := fs.Parse(args); err != nil { return 2 } + if *answers != "" { + set := map[string]bool{} + fs.Visit(func(fl *flag.Flag) { set[fl.Name] = true }) + b, err := os.ReadFile(*answers) + if err != nil { + fmt.Fprintln(os.Stderr, "reading --answers: "+err.Error()) + return 2 + } + a, err := parseAnswers(b) + if err != nil { + fmt.Fprintf(os.Stderr, "--answers %s: %v\n", *answers, err) + return 2 + } + applyAnswers(a, set, &o, server, console, webmail) + } for _, c := range []struct { comp plan.Component val string -- 2.54.0