hooks

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2021 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package hooks implements pre- and post snapshot hooks.

Plan is a generic executor for ExpectStepReports before and after an activity specified in a callback. It provides a reporting facility that can be polled while the plan is executing to gather progress information.

This package also provides all supported hook type implementations and abstractions around them.

Use For Other Kinds Of ExpectStepReports

This package REQUIRES REFACTORING before it can be used for other activities than snapshots, e.g. pre- and post-replication:

The Hook interface requires a hook to provide a Filesystems() filter, which doesn't make sense for all kinds of activities.

The hook implementations should move out of this package. However, there is a lot of tight coupling which to untangle isn't worth it ATM.

How This Package Is Used By Package Snapper

Deserialize a config.List using ListFromConfig(). Then it MUST filter the list to only contain hooks for a particular filesystem using hooksList.CopyFilteredForFilesystem(fs).

Then create a CallbackHook using NewCallbackHookForFilesystem().

Pass all of the above to NewPlan() which provides a Report() and Run() method:

Plan.Run(ctx context.Context,dryRun bool) executes the plan and take a context as argument that should contain a logger added using hooks.WithLogger()). The value of dryRun is passed through to the hooks' Run() method. Command hooks make it available in the environment variable ZREPL_DRYRUN.

Plan.Report() can be called while Plan.Run() is executing to give an overview of plan execution progress (future use in "zrepl status").

Index

Constants

View Source
const (
	PhaseSnapshot = Phase("snapshot")
	PhaseTesting  = Phase("testing")
)
View Source
const (
	Pre = Edge(1 << iota)
	Callback
	Post
)
View Source
const MAX_HOOK_LOG_SIZE_DEFAULT int = 1 << 20

Variables

This section is empty.

Functions

func NewLogWriter

func NewLogWriter(mtx *sync.Mutex, logger Logger, level logger.Level, field string) *logWriter

Types

type CallbackHook

type CallbackHook struct {
	// contains filtered or unexported fields
}

func NewCallbackHook

func NewCallbackHook(displayString string, cb HookJobCallback, filter Filter) *CallbackHook

func NewCallbackHookForFilesystem

func NewCallbackHookForFilesystem(displayString string, fs *zfs.DatasetPath, cb HookJobCallback) *CallbackHook

func (*CallbackHook) ErrIsFatal

func (h *CallbackHook) ErrIsFatal() bool

func (*CallbackHook) Filesystems

func (h *CallbackHook) Filesystems() Filter

func (*CallbackHook) Run

func (h *CallbackHook) Run(ctx context.Context, edge Edge, phase Phase, dryRun bool, extra Env, state map[interface{}]interface{}) HookReport

func (*CallbackHook) String

func (h *CallbackHook) String() string

type CallbackHookReport

type CallbackHookReport struct {
	Name string
	Err  error
}

func (*CallbackHookReport) Error

func (r *CallbackHookReport) Error() string

func (*CallbackHookReport) HadError

func (r *CallbackHookReport) HadError() bool

func (*CallbackHookReport) String

func (r *CallbackHookReport) String() string

type CommandHook

type CommandHook struct {
	// contains filtered or unexported fields
}

func NewCommandHook

func NewCommandHook(in *config.HookCommand) (r *CommandHook, err error)

func (*CommandHook) ErrIsFatal

func (h *CommandHook) ErrIsFatal() bool

func (*CommandHook) Filesystems

func (h *CommandHook) Filesystems() Filter

func (*CommandHook) Run

func (h *CommandHook) Run(ctx context.Context, edge Edge, phase Phase, dryRun bool, extra Env, state map[interface{}]interface{}) HookReport

func (*CommandHook) String

func (h *CommandHook) String() string

type CommandHookReport

type CommandHookReport struct {
	Command                      string
	Args                         []string // currently always empty
	Env                          Env
	Err                          error
	CapturedStdoutStderrCombined []byte
}

func (*CommandHookReport) Error

func (r *CommandHookReport) Error() string

func (*CommandHookReport) HadError

func (r *CommandHookReport) HadError() bool

func (*CommandHookReport) String

func (r *CommandHookReport) String() string

type Edge

type Edge uint

func (Edge) String

func (i Edge) String() string

func (Edge) StringForPhase

func (e Edge) StringForPhase(phase Phase) string

type Env

type Env map[HookEnvVar]string

func NewHookEnv

func NewHookEnv(edge Edge, phase Phase, dryRun bool, timeout time.Duration, extra Env) Env

type Filter

type Filter zfs.DatasetFilter

Re-export type here so that every file in package hooks doesn't have to import github.com/zrepl/zrepl/zfs

type Hook

type Hook interface {
	Filesystems() Filter

	// If true and the Pre edge invocation of Run fails, Post edge will not run and other Pre edges will not run.
	ErrIsFatal() bool

	// Run is invoked by HookPlan for a Pre edge.
	// If HookReport.HadError() == false, the Post edge will be invoked, too.
	Run(ctx context.Context, edge Edge, phase Phase, dryRun bool, extra Env, state map[interface{}]interface{}) HookReport

	String() string
}

func HookFromConfig

func HookFromConfig(in config.HookEnum) (Hook, error)

type HookEnvVar

type HookEnvVar string
const (
	EnvType     HookEnvVar = "ZREPL_HOOKTYPE"
	EnvDryRun   HookEnvVar = "ZREPL_DRYRUN"
	EnvFS       HookEnvVar = "ZREPL_FS"
	EnvSnapshot HookEnvVar = "ZREPL_SNAPNAME"
	EnvTimeout  HookEnvVar = "ZREPL_TIMEOUT"
)

type HookJobCallback

type HookJobCallback func(ctx context.Context) error

type HookReport

type HookReport interface {
	String() string
	HadError() bool
	Error() string
}

type List

type List []Hook

func ListFromConfig

func ListFromConfig(in *config.HookList) (r *List, err error)

func (List) CopyFilteredForFilesystem

func (l List) CopyFilteredForFilesystem(fs *zfs.DatasetPath) (ret List, err error)

type Logger

type Logger = logger.Logger

func GetLogger

func GetLogger(ctx context.Context) Logger

type MyLockTablesReport

type MyLockTablesReport struct {
	What string
	Err  error
}

func (*MyLockTablesReport) Error

func (r *MyLockTablesReport) Error() string

func (*MyLockTablesReport) HadError

func (r *MyLockTablesReport) HadError() bool

func (*MyLockTablesReport) String

func (r *MyLockTablesReport) String() string

type MySQLLockTables

type MySQLLockTables struct {
	// contains filtered or unexported fields
}

https://dev.mysql.com/doc/mysql-backup-excerpt/5.7/en/backup-methods.html

Making Backups Using a File System Snapshot:

 If you are using a Veritas file system, you can make a backup like this:

   	From a client program, execute FLUSH TABLES WITH READ LOCK.
   	From another shell, execute mount vxfs snapshot.
   	From the first client, execute UNLOCK TABLES.
   	Copy files from the snapshot.
   	Unmount the snapshot.

	Similar snapshot capabilities may be available in other file systems, such as LVM or ZFS.

func MyLockTablesFromConfig

func MyLockTablesFromConfig(in *config.HookMySQLLockTables) (*MySQLLockTables, error)

func (*MySQLLockTables) ErrIsFatal

func (h *MySQLLockTables) ErrIsFatal() bool

func (*MySQLLockTables) Filesystems

func (h *MySQLLockTables) Filesystems() Filter

func (*MySQLLockTables) Run

func (h *MySQLLockTables) Run(ctx context.Context, edge Edge, phase Phase, dryRun bool, extra Env, state map[interface{}]interface{}) HookReport

func (*MySQLLockTables) String

func (h *MySQLLockTables) String() string

type PgChkptHook

type PgChkptHook struct {
	// contains filtered or unexported fields
}

func PgChkptHookFromConfig

func PgChkptHookFromConfig(in *config.HookPostgresCheckpoint) (*PgChkptHook, error)

func (*PgChkptHook) ErrIsFatal

func (h *PgChkptHook) ErrIsFatal() bool

func (*PgChkptHook) Filesystems

func (h *PgChkptHook) Filesystems() Filter

func (*PgChkptHook) Run

func (h *PgChkptHook) Run(ctx context.Context, edge Edge, phase Phase, dryRun bool, extra Env, state map[interface{}]interface{}) HookReport

func (*PgChkptHook) String

func (h *PgChkptHook) String() string

type PgChkptHookReport

type PgChkptHookReport struct{ Err error }

func (*PgChkptHookReport) Error

func (r *PgChkptHookReport) Error() string

func (*PgChkptHookReport) HadError

func (r *PgChkptHookReport) HadError() bool

func (*PgChkptHookReport) String

func (r *PgChkptHookReport) String() string

type Phase

type Phase string

func (Phase) String

func (p Phase) String() string

type Plan

type Plan struct {
	// contains filtered or unexported fields
}

func NewPlan

func NewPlan(hooks *List, phase Phase, cb *CallbackHook, extra Env) (*Plan, error)

func (*Plan) Report

func (p *Plan) Report() PlanReport

func (*Plan) Run

func (p *Plan) Run(ctx context.Context, dryRun bool)

type PlanReport

type PlanReport []Step

func (PlanReport) HadError

func (r PlanReport) HadError() bool

func (PlanReport) HadFatalError

func (r PlanReport) HadFatalError() bool

func (PlanReport) String

func (r PlanReport) String() string

type Step

type Step struct {
	Hook       Hook
	Edge       Edge
	Status     StepStatus
	Begin, End time.Time
	// Report may be nil
	// FIXME cannot serialize this for client status, but contains interesting info (like what error happened)
	Report HookReport
	// contains filtered or unexported fields
}

func (Step) String

func (s Step) String() (out string)

type StepStatus

type StepStatus int
const (
	StepPending StepStatus = 1 << iota
	StepExec
	StepOk
	StepErr
	StepSkippedDueToFatalErr
	StepSkippedDueToPreErr
)

func StepStatusString

func StepStatusString(s string) (StepStatus, error)

StepStatusString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func StepStatusValues

func StepStatusValues() []StepStatus

StepStatusValues returns all values of the enum

func (StepStatus) IsAStepStatus

func (i StepStatus) IsAStepStatus() bool

IsAStepStatus returns "true" if the value is listed in the enum definition. "false" otherwise

func (StepStatus) String

func (i StepStatus) String() string

Jump to

Keyboard shortcuts

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