- hamlab install / uninstall (internal/setup): systemd or OpenRC, elevates through sudo or doas, runs as the invoking user, sets or generates the password, names only journal groups that exist, waits for /healthz, reports an active firewalld/ufw. --purge and the data chown never touch a directory that isn't Ham Lab's alone. - install.sh: curl-able bootstrap (arch detection, SHA256SUMS, offline beside the binaries, noexec /tmp fallback). make release builds dist/ for amd64, arm64, arm (ARMv6), 386, riscv64. Every build is CGO_ENABLED=0; the native one used to link glibc. - internal/priv replaces every literal `sudo -n`: uid 0, sudo or doas. - Updates acts on dnf, zypper, pacman and apk as well as apt; dnf shows installed versions. - Services falls back to the plain systemctl table before systemd 246 and says so on hosts without systemd. Disks falls back to /sys/block without lsblk. - Listener binds with IP_FREEBIND, so a LAN address that isn't up at boot is no failure. - Self-update: the new build must pass `check` against the live config, is put in place by atomic rename, and the daemon re-execs itself (same under any supervisor). - State and config writes are fsynced; a corrupt state.json is set aside, not fatal. - Terminal shutdown no longer empties the saved list, so names come back after a restart by design rather than by winning a race. - Config default is ~/.config/hamlab; an existing ~/apps/hamlab is still found. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
// Package store persists small runtime state (terminal names, login sessions)
|
|
// to a JSON file with atomic writes.
|
|
package store
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"log"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type Terminal struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type State struct {
|
|
Terminals []Terminal `json:"terminals"`
|
|
Sessions map[string]time.Time `json:"sessions"` // token -> expiry
|
|
Dashboard json.RawMessage `json:"dashboard,omitempty"` // UI layout prefs, opaque to the server
|
|
UI json.RawMessage `json:"ui,omitempty"` // theme etc., opaque to the server
|
|
}
|
|
|
|
type Store struct {
|
|
mu sync.Mutex
|
|
path string
|
|
state State
|
|
}
|
|
|
|
func Open(path string) (*Store, error) {
|
|
s := &Store{path: path}
|
|
b, err := os.ReadFile(path)
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return nil, err
|
|
}
|
|
if err == nil {
|
|
// State is conveniences (sessions, terminal names, layout). A file mangled by a power
|
|
// cut must not keep the console down: set it aside and start clean.
|
|
if err := json.Unmarshal(b, &s.state); err != nil {
|
|
s.state = State{}
|
|
_ = os.Rename(path, path+".corrupt")
|
|
log.Printf("state: %s unreadable (%v); kept as %s.corrupt, starting fresh", path, err, path)
|
|
}
|
|
}
|
|
if s.state.Sessions == nil {
|
|
s.state.Sessions = map[string]time.Time{}
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// Update applies fn under the lock and writes the result to disk.
|
|
func (s *Store) Update(fn func(*State)) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
fn(&s.state)
|
|
return s.save()
|
|
}
|
|
|
|
// View reads state under the lock.
|
|
func (s *Store) View(fn func(*State)) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
fn(&s.state)
|
|
}
|
|
|
|
func (s *Store) save() error {
|
|
b, err := json.MarshalIndent(&s.state, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return WriteFileAtomic(s.path, b, 0o600)
|
|
}
|
|
|
|
// WriteFileAtomic writes beside path, syncs, then renames: after a crash the file is the
|
|
// old version or the new one, never empty or half-written.
|
|
func WriteFileAtomic(path string, data []byte, perm os.FileMode) error {
|
|
tmp := path + ".tmp"
|
|
f, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = f.Write(data)
|
|
if err == nil {
|
|
err = f.Sync()
|
|
}
|
|
if cerr := f.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
if err != nil {
|
|
os.Remove(tmp)
|
|
return err
|
|
}
|
|
return os.Rename(tmp, path)
|
|
}
|