reset

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 a first start.

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

View Source
const (
	ActionContentCleared = "system.content_cleared"
	ActionFactoryReset   = "system.factory_reset"
	ActionResetDenied    = "system.reset_denied"
)

Audit actions. Details never carry usernames.

View Source
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"
	StepRestart         = "restart"
)

Step names, as journaled in the marker.

View Source
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.

View Source
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

View Source
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 Preview

func Preview(ctx context.Context, db *store.DB) (protocol.ResetPreview, error)

Preview counts what a reset would delete.

func RemoveMarker

func RemoveMarker(dataDir string) error

RemoveMarker deletes the marker; a missing marker is not an error.

func SweepTrash

func SweepTrash(ctx context.Context, dataDir string) (int, error)

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

func TrashFiles(dataDir string) error

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

func WriteMarker(dataDir string, m *Marker) error

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

func ReadMarker(dataDir string) (*Marker, error)

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
	// AdminPassword is the password of the new admin account (factory). The
	// reset itself leaves it empty: the server fills it in after its
	// rebuild bootstrapped the account.
	AdminPassword string
	// CARotated reports whether the CA was replaced (factory).
	CARotated bool
}

Result describes a completed reset.

func Recover

func Recover(ctx context.Context, dataDir string, opts RecoverOptions) (*Result, error)

Recover completes the reset journaled in dataDir, if any, with nothing else running (call it before opening the database and the listeners). It returns nil when there was nothing to do.

func Run

func Run(ctx context.Context, dataDir string, req Request, h Hooks) (*Result, error)

Run performs req on dataDir. It refuses with ErrPending while another reset's marker is present. On error the marker stays: Recover (at the next start) completes the reset.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL