Files
inbuxa-installer/internal/state/state.go
T
jcoffey-dev 6787289bb9 install: run again, it keeps the options it isn't given (#84)
Running install on a machine with an install recorded started from the
defaults, so an option left out changed the install: a missing --server
host asked for a move, a missing --proxy snippets swapped in Caddy, and
renaming meant repeating every option from the first run.

Now every option not on the command line or in --answers is taken from
the install: shapes (a component it doesn't have stays skipped), proxy,
names, dir, ACME contact and CA. The record gains the proxy, the front
ends' names and the ACME settings; for installs from before it, the proxy
is read from the deployment's files and the front ends' names from the
addresses the server is given (INBUXA_ADMIN_URL / INBUXA_WEBMAIL_URL).
The plan lists what it took. Front ends joined to a server elsewhere
inherit nothing.
2026-11-18 09:30:20 -07:00

113 lines
4.7 KiB
Go

// SPDX-FileCopyrightText: 2026 Coffey Labs LLC
// SPDX-License-Identifier: AGPL-3.0-or-later
// Package state is what this machine remembers about what was installed on
// it, so a second run converges instead of building a second one.
//
// It is deliberately small, and deliberately not the truth: the truth is the
// machine, and a plan is built by reading both. What this adds is intent --
// which components this installer put here, in which shape, from which
// topology -- which the machine itself cannot tell you. A container that was
// stopped by hand is still ours; a container we never installed is not.
package state
import (
"encoding/json"
"os"
"path/filepath"
"time"
)
// Path is where it lives. /etc, not the deployment directory: it survives
// the deployment directory being moved or rebuilt, and an operator looking
// for "what did this thing do to my machine" looks in /etc.
const Path = "/etc/inbuxa/install.json"
// State is the file.
type State struct {
Version int `json:"version"`
Machine string `json:"machine"` // its name in the topology
Dir string `json:"dir"` // the deployment directory
Runtime string `json:"runtime,omitempty"` // docker or podman
Domain string `json:"domain,omitempty"` //
MailHost string `json:"mailHost,omitempty"` // the server's name; installs before 2026.10.13 did not record it
Local bool `json:"local,omitempty"` // --local: loopback only, no certificates
Shapes map[string]string `json:"shapes"` // component -> container|host
// The rest of the install's options, so running install again changes
// only what it is given (#84). Installs before 2026.11.18 did not record
// them; install reads them back from the machine instead.
Proxy string `json:"proxy,omitempty"` // caddy | snippets | none
ConsoleHost string `json:"consoleHost,omitempty"` //
WebmailHost string `json:"webmailHost,omitempty"` //
ACMEEmail string `json:"acmeEmail,omitempty"` //
ACMEDirectory string `json:"acmeDirectory,omitempty"` // a private ACME CA, when one was given
ACMECARoot string `json:"acmeCaRoot,omitempty"` // that CA's root
Topology string `json:"topology,omitempty"` // the file this came from, if any
ServerURL string `json:"serverUrl,omitempty"` // the server on another machine these front ends were joined to
Updated time.Time `json:"updated"` //
Installer string `json:"installer,omitempty"` // the version that wrote this
// PassThrough is whether the server's HTTPS listener was fitted to the
// name router --proxy snippets writes for nginx (#48). Installs from
// before it are moved over by a run with --key.
PassThrough bool `json:"passThrough,omitempty"`
// LogFiles is whether the server was given the Log tracer the console's
// Logs page reads (#70). Installs from before it are given it by a run
// with --key.
LogFiles bool `json:"logFiles,omitempty"`
// Clustered is whether `inbuxa cluster join` pointed this node's server at
// a shared store; ClusterRole is the role it was given, empty for the node
// that carries every task.
Clustered bool `json:"clustered,omitempty"`
ClusterRole string `json:"clusterRole,omitempty"`
// RemovedAt is when `inbuxa uninstall` took the programs away and kept
// the data. The shapes stay, so `install` run again picks the data up
// rather than trying a first boot on a server configured long ago.
RemovedAt *time.Time `json:"removedAt,omitempty"`
}
// Removed is whether the programs were uninstalled with the data kept.
func (s *State) Removed() bool { return s.RemovedAt != nil }
const Version = 1
// Load reads the state, returning an empty one when there is none. A machine
// with nothing installed is not an error; it is the ordinary first case.
func Load() (*State, error) {
b, err := os.ReadFile(Path)
if os.IsNotExist(err) {
return &State{Version: Version, Shapes: map[string]string{}}, nil
}
if err != nil {
return nil, err
}
var s State
if err := json.Unmarshal(b, &s); err != nil {
return nil, err
}
if s.Shapes == nil {
s.Shapes = map[string]string{}
}
return &s, nil
}
// Save writes it, creating /etc/inbuxa if it is not there.
func (s *State) Save() error {
s.Version = Version
s.Updated = time.Now().UTC().Truncate(time.Second)
if err := os.MkdirAll(filepath.Dir(Path), 0o755); err != nil {
return err
}
b, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}
return os.WriteFile(Path, append(b, '\n'), 0o644)
}
// Installed is whether anything is recorded here at all.
func (s *State) Installed() bool { return len(s.Shapes) > 0 }