planstore

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 16 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 planstore keeps the plans a task captured until an apply task takes one of them, exactly once. Each plan is a directory under <root>/.rune/plans with a record, the artifact and its summary. The state of a plan is derived from the files of that directory, so separate processes agree on it without sharing a lock.

Index

Constants

View Source
const (
	EventPlanCreated       = "plan-created"
	EventPlanReplaced      = "plan-replaced"
	EventApprovalRequested = "approval-requested"
	EventApprovalGiven     = "approval-given"
	EventApprovalNotGiven  = "approval-not-given"
	EventApplyRefused      = "apply-refused"
	EventApplyStarted      = "apply-started"
	EventApplyFinished     = "apply-finished"
)

The events of the journal.

View Source
const (
	MaxArtifactBytes int64 = 256 << 20
	MaxSummaryBytes  int64 = 1 << 20
	Retention              = 7 * 24 * time.Hour
	UnfinishedGrace        = 24 * time.Hour
	MaxJournalBytes  int64 = 1 << 20
)

The limits of the store.

Variables

View Source
var (
	ErrClaimed  = errors.New("plan was already claimed")
	ErrReplaced = errors.New("plan was replaced by a newer one")
	ErrExpired  = errors.New("plan has expired")
)

Errors of Claim for a plan that is not ready.

Functions

This section is empty.

Types

type ArtifactError

type ArtifactError struct {
	What   string
	Detail string
}

ArtifactError reports a stored file that is not what its record says. What is "artifact" or "summary".

func (*ArtifactError) Error

func (e *ArtifactError) Error() string

type Attempt

type Attempt struct {
	ID     string
	Path   string // the private copy of the artifact
	Record Record
	// contains filtered or unexported fields
}

Attempt is a claimed plan: the one run that may apply it.

func (*Attempt) Finish

func (a *Attempt) Finish(exit int) (tampered bool, err error)

Finish records how the apply ended and removes the artifact, the summary and the private copy. The copy is hashed first: a copy that changed makes the attempt failed whatever the exit code. A second call returns the result of the first, because by then the copy is gone and would count as changed.

type AttemptInfo

type AttemptInfo struct {
	Plan      string
	Attempt   string
	ApplyTask string
	Surface   string
	Started   time.Time // from the claim, or the claim file's modification time when it cannot be read
	Finished  time.Time // zero when unfinished
	Exit      int
	State     State // StateApplied, StateFailed or StateUnfinished
	Tampered  bool
}

AttemptInfo is the one attempt to apply a plan.

type Binding

type Binding struct {
	WorkDir    string `json:"workDir"`
	Runefile   string `json:"runefile"`
	Config     string `json:"config"`
	Tool       string `json:"tool,omitempty"`
	ToolPath   string `json:"toolPath,omitempty"`
	ToolDigest string `json:"toolDigest,omitempty"`
	Host       string `json:"host"`
}

Binding is what a plan was made with. The engine compares it before an apply; the store only keeps it.

type Digest

type Digest struct {
	SHA256 string `json:"sha256"`
	Size   int64  `json:"size"`
}

Digest identifies the bytes of a stored file.

type Event

type Event struct {
	Time       time.Time `json:"time"`
	Event      string    `json:"event"`
	Surface    string    `json:"surface"`
	PlanTask   string    `json:"planTask"`
	Plan       string    `json:"plan,omitempty"`
	Digest     string    `json:"digest,omitempty"`
	Args       string    `json:"args"`
	ApplyTask  string    `json:"applyTask,omitempty"`
	Attempt    string    `json:"attempt,omitempty"`
	ReplacedBy string    `json:"replacedBy,omitempty"`
	Reason     string    `json:"reason,omitempty"`
	Exit       *int      `json:"exit,omitempty"`
	State      string    `json:"state,omitempty"`
	Tampered   bool      `json:"tampered,omitempty"`
	// Invocation is the id of the agent's call the event happened in, as
	// the audit log records it (spec 052). Empty on the command line.
	Invocation string `json:"invocation,omitempty"`
}

Event is one line of the journal.

type Git

type Git struct {
	Commit string `json:"commit,omitempty"`
	Dirty  *bool  `json:"dirty,omitempty"`
}

Git is the repository state a plan was made in, as far as it was known.

type Record

type Record struct {
	ID          string    `json:"id"`
	Key         string    `json:"key"`
	Created     time.Time `json:"created"`
	Expires     time.Time `json:"expires"`
	PlanTask    string    `json:"planTask"`
	ApplyTasks  []string  `json:"applyTasks"`
	Args        string    `json:"args"`
	Binding     Binding   `json:"binding"`
	Artifact    Digest    `json:"artifact"`
	Summary     Digest    `json:"summary"`
	RuneVersion string    `json:"runeVersion,omitempty"`
	Git         Git       `json:"git"`
	Surface     string    `json:"surface"`
}

Record describes one stored plan. It never holds plan or summary bytes.

type SaveInput

type SaveInput struct {
	Record       Record
	Expiry       time.Duration
	ArtifactPath string
	SummaryPath  string
	MaxArtifact  int64 // zero means MaxArtifactBytes
	MaxSummary   int64 // zero means MaxSummaryBytes
}

SaveInput is a plan to store. Record carries the fields the caller computed: Key, PlanTask, ApplyTasks, Args, Binding, RuneVersion, Git and Surface. Save fills in the rest.

type SourceError

type SourceError struct {
	Path   string
	Detail string
}

SourceError reports a file that Save cannot take.

func (*SourceError) Error

func (e *SourceError) Error() string

type State

type State int

State is what the files of a plan's directory say about it.

const (
	StateReady State = iota
	StateUnfinished
	StateApplied
	StateFailed
	StateReplaced
	StateExpired
)

The states of a plan. Only a ready plan can be applied.

func (State) String

func (s State) String() string

type Store

type Store struct {
	// Now is the clock for every time the store writes or compares. Nil means
	// time.Now.
	Now func() time.Time
	// contains filtered or unexported fields
}

Store is the plan store of one Runefile directory.

func Open

func Open(root string) *Store

Open returns the store under root. It does not touch the file system: the store is created by the first Save.

func (*Store) AttemptOf

func (s *Store) AttemptOf(rec Record) (AttemptInfo, bool)

AttemptOf returns the claim of rec and its outcome. It reports false for a record that was never claimed.

func (*Store) Claim

func (s *Store) Claim(rec Record, applyTask, surface string) (*Attempt, error)

Claim takes rec for one apply and returns a private copy of its artifact with the recorded digest. Of any number of callers one succeeds. A Claim that fails leaves the record as it was.

func (*Store) Dir

func (s *Store) Dir() string

Dir is the directory the store keeps its plans in.

func (*Store) LastAttempt

func (s *Store) LastAttempt(key, exceptID string) (AttemptInfo, bool)

LastAttempt returns the attempt of the newest claimed record of key, other than the record exceptID.

func (*Store) Latest

func (s *Store) Latest(key string) (Record, State, bool, error)

Latest returns the newest record of key and its state. A store that does not exist holds no record, and is not created.

func (*Store) Log

func (s *Store) Log(e Event) error

Log appends e to the journal as one line, with the clock's time when e has none. Writers take a lock file in turn, so two processes neither tear a line nor lose one while the journal is cut to its limit.

func (*Store) Prune

func (s *Store) Prune()

Prune removes what the store no longer needs: the bytes of every plan that cannot be applied any more, the copies made of them, and records past their retention. It also marks the older unclaimed records of a key that a concurrent Save left unmarked. It reports nothing: a store it cannot clean now is cleaned by a later call.

func (*Store) Save

func (s *Store) Save(in SaveInput) (rec Record, replaced []Record, err error)

Save stores the two source files as a new plan and marks every older, unclaimed plan of the same key as replaced. It returns the records it marked.

func (*Store) StateOf

func (s *Store) StateOf(rec Record) State

StateOf returns the state of a record as its files say now. A caller that looked a plan up earlier uses it to tell a plan another call has used since from one whose stored bytes went missing.

func (*Store) Summary

func (s *Store) Summary(rec Record) (string, error)

Summary returns the stored summary of rec, read through the same check as Verify.

func (*Store) Verify

func (s *Store) Verify(rec Record) error

Verify compares the stored artifact and summary of rec with the record.

type UnusableError

type UnusableError struct {
	Path   string
	Reason string
}

UnusableError reports a store directory that is a link, is not a directory or belongs to another user.

func (*UnusableError) Error

func (e *UnusableError) Error() string

Jump to

Keyboard shortcuts

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