Documentation
¶
Overview ¶
Package reset implements the two destructive lifecycle operations of a bubbles server (pipeline steps 19 and 20): clear content, which deletes every message, file, alert and event but keeps accounts, channels, apps and settings, and factory reset, which wipes the data directory back to the setup wizard.
A reset is an ordered list of idempotent steps journaled in <data>/reset-pending.json: the marker is written before the first step, rewritten after each one and removed last. Recover, run at server startup before any listener opens, completes a reset that a crash or power loss cut short. The same steps run inside a live server (internal/app supplies Hooks that enter maintenance mode, close sockets and tear down and rebuild the services) and from the console with the server stopped (`bubbles reset`).
Index ¶
- Constants
- Variables
- func AuditDenied(ctx context.Context, db *store.DB, actor audit.Actor, mode Mode, reason string) error
- func AwaitingSetup(ctx context.Context, db *store.DB) (bool, error)
- func Preview(ctx context.Context, db *store.DB) (protocol.ResetPreview, error)
- func RemoveMarker(dataDir string) error
- func SweepTrash(ctx context.Context, dataDir string) (int, error)
- func TrashFiles(dataDir string) error
- func WriteMarker(dataDir string, m *Marker) error
- type Actor
- type Hooks
- type Marker
- type Mode
- type RecoverOptions
- type Request
- type Result
Constants ¶
const ( ActionContentCleared = "system.content_cleared" ActionFactoryReset = "system.factory_reset" ActionResetDenied = "system.reset_denied" )
Audit actions. Details never carry usernames.
const ( StepAudit = "audit" StepQuiesce = "quiesce" StepClearContent = "clear-content" StepVacuum = "vacuum" StepTrashFiles = "trash-files" StepWipeTmp = "wipe-tmp" StepDeleteSnapshots = "delete-snapshots" StepRelease = "release" StepDeleteDB = "delete-db" StepDeleteSecrets = "delete-secrets" StepWipeLogs = "wipe-logs" StepWipeBackups = "wipe-backups" StepRotateCA = "rotate-ca" StepOpenDB = "open-db" StepRestoreSettings = "restore-settings" StepVAPID = "vapid" StepSetupToken = "setup-token" StepRestart = "restart" )
Step names, as journaled in the marker.
const MarkerFile = "reset-pending.json"
MarkerFile is the reset journal in the data directory. It is written before a reset touches anything, rewritten after every completed step and removed last, so a server that finds it at startup knows a reset was cut short and completes it before opening its listeners.
const TrashPrefix = "trash-"
TrashPrefix starts the name of a files directory moved aside by a reset (trash-<ulid>). Trash directories are deleted in the background and swept at every server start.
Variables ¶
var ( // ErrDenied is a reset refused because the typed confirmation or the // password was wrong (see AuditDenied). ErrDenied = errors.New("reset not confirmed") // ErrConfirm is ErrDenied for a confirmation that is not the instance // name. ErrConfirm = fmt.Errorf("%w: the confirmation does not match the instance name", ErrDenied) // ErrPassword is ErrDenied for a wrong password. ErrPassword = fmt.Errorf("%w: wrong password", ErrDenied) // ErrPending means an unfinished reset is journaled in the data // directory; Recover completes it. ErrPending = errors.New("an unfinished reset is pending: restart the server to complete it") // ErrInProgress means another reset is running right now. ErrInProgress = errors.New("a reset is already in progress") )
Functions ¶
func AuditDenied ¶
func AuditDenied(ctx context.Context, db *store.DB, actor audit.Actor, mode Mode, reason string) error
AuditDenied records a refused reset attempt (reason "confirm" or "password").
func AwaitingSetup ¶
AwaitingSetup reports whether db was recreated by a factory reset and no one has completed setup since: the server then keeps the setup token the reset issued instead of replacing it.
func RemoveMarker ¶
RemoveMarker deletes the marker; a missing marker is not an error.
func SweepTrash ¶
SweepTrash deletes every trash directory of dataDir and returns how many it removed. It stops early when ctx is done (the rest waits for the next sweep). Entries that vanish meanwhile are not errors.
func TrashFiles ¶
TrashFiles moves <dataDir>/files aside to <dataDir>/trash-<ulid> and recreates an empty files directory. The rename is instant however many gigabytes the directory holds; SweepTrash deletes the contents later.
func WriteMarker ¶
WriteMarker replaces the marker of dataDir atomically (temporary file, fsync, rename) with owner-only permissions.
Types ¶
type Actor ¶
type Actor struct {
ID string `json:"id,omitempty"`
Kind string `json:"kind"`
IP string `json:"ip,omitempty"`
}
Actor is the audit identity recorded in the marker.
type Hooks ¶
type Hooks struct {
// DB is the live database of a running server. The steps use it until
// Release and never close it.
DB *store.DB
// Quiesce enters maintenance mode, closes every WebSocket with the reset
// close code and waits for requests in flight.
Quiesce func(ctx context.Context) error
// Release stops everything that holds the database and closes it
// (factory, before any file is deleted: Windows cannot delete open files).
Release func(ctx context.Context) error
// ReopenLogs closes the live log file, runs clear and opens a fresh file.
// Nil: clear runs directly.
ReopenLogs func(clear func() error) error
// NewCA creates the new CA and leaf after tls/ was emptied (factory with
// RotateCA) and starts serving them. Nil: the next server start creates
// them.
NewCA func() error
// Restart runs last, before the marker is removed: it rebuilds the
// services on the new database (factory) or drops caches (content).
Restart func(ctx context.Context) error
// Now and Logger are handed to the database the steps open themselves.
Now func() time.Time
Logger *slog.Logger
// contains filtered or unexported fields
}
Hooks connect the steps to a running server. The zero value runs offline: the console with the server stopped, or startup recovery.
type Marker ¶
type Marker struct {
Version int `json:"version"`
ID string `json:"id"`
Mode Mode `json:"mode"`
RotateCA bool `json:"rotate_ca"`
KeepSettings bool `json:"keep_settings"`
// Settings is the snapshot of the settings table a factory reset with
// KeepSettings carries over.
Settings map[string]json.RawMessage `json:"settings,omitempty"`
// Epoch is the instance epoch clear content moves to (current + 1,
// decided before the first step so a resumed step cannot bump it twice).
Epoch int64 `json:"epoch,omitempty"`
Actor Actor `json:"actor"`
StartedAt int64 `json:"started_at"`
// Done lists the completed steps in order.
Done []string `json:"done"`
}
Marker is the content of MarkerFile.
func ReadMarker ¶
ReadMarker returns the pending reset of dataDir, or nil when there is none.
type Mode ¶
type Mode string
Mode selects what a reset deletes.
const ( ModeContent Mode = protocol.ResetContent ModeFactory Mode = protocol.ResetFactory )
Reset modes.
type RecoverOptions ¶
type RecoverOptions struct {
// ReopenLogs is Hooks.ReopenLogs: the caller may already be logging to
// the log directory a factory reset wipes.
ReopenLogs func(clear func() error) error
Now func() time.Time
Logger *slog.Logger
}
RecoverOptions tune Recover; the zero value is ready to use.
type Request ¶
type Request struct {
Mode Mode
// RotateCA destroys the CA and creates a new one (factory only).
RotateCA bool
// KeepSettings carries the settings table over (factory only).
KeepSettings bool
// Actor is recorded in the audit row (factory keeps its kind and
// address only: the account it names is gone).
Actor audit.Actor
}
Request is one reset to run.
type Result ¶
type Result struct {
Mode Mode
// Epoch is the new instance epoch (content).
Epoch int64
// SetupToken is the new one-time setup token (factory).
SetupToken string
// CARotated reports whether the CA was replaced (factory).
CARotated bool
}
Result describes a completed reset.