policy

package
v0.9.5 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2021 License: Apache-2.0 Imports: 16 Imported by: 6

Documentation

Overview

Package policy implements management of snapshot policies.

Index

Constants

View Source
const (
	PolicyTypeLabel = "policyType"

	PolicyTypePath   = "path"
	PolicyTypeGlobal = "global"
	PolicyTypeHost   = "host"
	PolicyTypeUser   = "user"

	PathLabel     = snapshot.PathLabel
	UsernameLabel = snapshot.UsernameLabel
	HostnameLabel = snapshot.HostnameLabel
)

Manifest labels identifying snapshots.

View Source
const ManifestType = "policy"

ManifestType is the type of the manifest that represents policy.

Variables

View Source
var DefaultPolicy = &Policy{
	FilesPolicy:         defaultFilesPolicy,
	RetentionPolicy:     defaultRetentionPolicy,
	CompressionPolicy:   defaultCompressionPolicy,
	ErrorHandlingPolicy: defaultErrorHandlingPolicy,
	SchedulingPolicy:    defaultSchedulingPolicy,
	LoggingPolicy:       defaultLoggingPolicy,
	Actions:             defaultActionsPolicy,
}

DefaultPolicy is a default policy returned by policy tree in absence of other policies.

View Source
var ErrPolicyNotFound = errors.New("policy not found")

ErrPolicyNotFound is returned when the policy is not found.

View Source
var GlobalPolicySourceInfo = snapshot.SourceInfo{}

GlobalPolicySourceInfo is a source where global policy is attached.

Functions

func ApplyRetentionPolicy

func ApplyRetentionPolicy(ctx context.Context, rep repo.RepositoryWriter, sourceInfo snapshot.SourceInfo, reallyDelete bool) ([]*snapshot.Manifest, error)

ApplyRetentionPolicy applies retention policy to a given source by deleting expired snapshots.

func GetEffectivePolicy

func GetEffectivePolicy(ctx context.Context, rep repo.Repository, si snapshot.SourceInfo) (effective *Policy, sources []*Policy, e error)

GetEffectivePolicy calculates effective snapshot policy for a given source by combining the source-specifc policy (if any) with parent policies. The source must contain a path. Returns the effective policies and all source policies that contributed to that (most specific first).

func IsManualSnapshot added in v0.8.0

func IsManualSnapshot(policyTree *Tree) bool

IsManualSnapshot returns the SchedulingPolicy manual value from the given policy tree.

func RemovePolicy

func RemovePolicy(ctx context.Context, rep repo.RepositoryWriter, si snapshot.SourceInfo) error

RemovePolicy removes the policy for a given source.

func SetManual added in v0.8.0

func SetManual(ctx context.Context, rep repo.RepositoryWriter, sourceInfo snapshot.SourceInfo) error

SetManual sets the manual setting in the SchedulingPolicy on the given source.

func SetPolicy

func SetPolicy(ctx context.Context, rep repo.RepositoryWriter, si snapshot.SourceInfo, pol *Policy) error

SetPolicy sets the policy on a given source.

func ValidatePolicy added in v0.8.0

func ValidatePolicy(pol *Policy) error

ValidatePolicy returns error if the given policy is invalid. Currently, only SchedulingPolicy is validated.

func ValidateSchedulingPolicy added in v0.8.0

func ValidateSchedulingPolicy(p SchedulingPolicy) error

ValidateSchedulingPolicy returns an error if manual field is set along with scheduling fields.

Types

type ActionCommand added in v0.8.0

type ActionCommand struct {
	// command + args to run
	Command   string   `json:"path,omitempty"`
	Arguments []string `json:"args,omitempty"`

	// alternatively inline script to run using either Unix shell or cmd.exe on Windows.
	Script string `json:"script,omitempty"`

	TimeoutSeconds int    `json:"timeout,omitempty"`
	Mode           string `json:"mode,omitempty"` // essential,optional,async
}

ActionCommand configures a action command.

type ActionsPolicy added in v0.8.0

type ActionsPolicy struct {
	// command runs once before and after the folder it's attached to (not inherited).
	BeforeFolder *ActionCommand `json:"beforeFolder,omitempty"`
	AfterFolder  *ActionCommand `json:"afterFolder,omitempty"`

	// commands run once before and after each snapshot root (can be inherited).
	BeforeSnapshotRoot *ActionCommand `json:"beforeSnapshotRoot,omitempty"`
	AfterSnapshotRoot  *ActionCommand `json:"afterSnapshotRoot,omitempty"`
}

ActionsPolicy describes actions to be invoked when taking snapshots.

func (*ActionsPolicy) Merge added in v0.8.0

func (p *ActionsPolicy) Merge(src ActionsPolicy)

Merge applies default values from the provided policy.

func (*ActionsPolicy) MergeNonInheritable added in v0.8.0

func (p *ActionsPolicy) MergeNonInheritable(src ActionsPolicy)

MergeNonInheritable copies non-inheritable properties from the provided actions policy.

type CompressionPolicy added in v0.4.0

type CompressionPolicy struct {
	CompressorName compression.Name `json:"compressorName,omitempty"`
	OnlyCompress   []string         `json:"onlyCompress,omitempty"`
	NeverCompress  []string         `json:"neverCompress,omitempty"`
	MinSize        int64            `json:"minSize,omitempty"`
	MaxSize        int64            `json:"maxSize,omitempty"`
}

CompressionPolicy specifies compression policy.

func (*CompressionPolicy) CompressorForFile added in v0.4.0

func (p *CompressionPolicy) CompressorForFile(e fs.File) compression.Name

CompressorForFile returns compression name to be used for compressing a given file according to policy, using attributes such as name or size.

func (*CompressionPolicy) Merge added in v0.4.0

func (p *CompressionPolicy) Merge(src CompressionPolicy)

Merge applies default values from the provided policy.

type DirLoggingPolicy added in v0.9.5

type DirLoggingPolicy struct {
	Snapshotted *LogDetail `json:"snapshotted,omitempty"`
	Ignored     *LogDetail `json:"ignored,omitempty"`
}

DirLoggingPolicy represents the policy for logging directory information when snapshotting.

func (*DirLoggingPolicy) Merge added in v0.9.5

func (p *DirLoggingPolicy) Merge(src DirLoggingPolicy)

Merge merges the provided directory logging policy.

type EntryLoggingPolicy added in v0.9.5

type EntryLoggingPolicy struct {
	Snapshotted *LogDetail `json:"snapshotted,omitempty"`
	Ignored     *LogDetail `json:"ignored,omitempty"`
	CacheHit    *LogDetail `json:"cacheHit,omitempty"`
	CacheMiss   *LogDetail `json:"cacheMiss,omitempty"`
}

EntryLoggingPolicy represents the policy for logging entry information when snapshotting.

func (*EntryLoggingPolicy) Merge added in v0.9.5

func (p *EntryLoggingPolicy) Merge(src EntryLoggingPolicy)

Merge merges the provided entry logging policy.

type ErrorHandlingPolicy added in v0.5.2

type ErrorHandlingPolicy struct {
	// IgnoreFileErrors controls whether or not snapshot operation should fail when a file throws an error on being read
	IgnoreFileErrors *OptionalBool `json:"ignoreFileErrors,omitempty"`

	// IgnoreDirectoryErrors controls whether or not snapshot operation should fail when a directory throws an error on being read or opened
	IgnoreDirectoryErrors *OptionalBool `json:"ignoreDirectoryErrors,omitempty"`

	// IgnoreUnknownTypes controls whether or not snapshot operation should fail when it encounters a directory entry of an unknown type.
	IgnoreUnknownTypes *OptionalBool `json:"ignoreUnknownTypes,omitempty"`
}

ErrorHandlingPolicy controls error hadnling behavior when taking snapshots.

func (*ErrorHandlingPolicy) Merge added in v0.5.2

Merge applies default values from the provided policy.

type FilesPolicy added in v0.4.0

type FilesPolicy struct {
	IgnoreRules         []string `json:"ignore,omitempty"`
	NoParentIgnoreRules bool     `json:"noParentIgnore,omitempty"`

	DotIgnoreFiles         []string `json:"ignoreDotFiles,omitempty"`
	NoParentDotIgnoreFiles bool     `json:"noParentDotFiles,omitempty"`

	IgnoreCacheDirectories *OptionalBool `json:"ignoreCacheDirs,omitempty"`

	MaxFileSize int64 `json:"maxFileSize,omitempty"`

	OneFileSystem *OptionalBool `json:"oneFileSystem,omitempty"`
}

FilesPolicy describes files to be ignored when taking snapshots.

func (*FilesPolicy) Merge added in v0.4.0

func (p *FilesPolicy) Merge(src FilesPolicy)

Merge applies default values from the provided policy.

type LogDetail added in v0.9.5

type LogDetail int

LogDetail represents the details of log output.

const (
	LogDetailNone   LogDetail = 0
	LogDetailNormal LogDetail = 5
	LogDetailMax    LogDetail = 10
)

Supported log detail levels.

func NewLogDetail added in v0.9.5

func NewLogDetail(l LogDetail) *LogDetail

NewLogDetail returns a pointer to the provided LogDetail.

func (*LogDetail) OrDefault added in v0.9.5

func (l *LogDetail) OrDefault(def LogDetail) LogDetail

OrDefault returns the log detail or the provided default.

type LoggingPolicy added in v0.9.5

type LoggingPolicy struct {
	Directories DirLoggingPolicy   `json:"directories,omitempty"`
	Entries     EntryLoggingPolicy `json:"entries,omitempty"`
}

LoggingPolicy describes policy for emitting logs during snapshots.

func (*LoggingPolicy) Merge added in v0.9.5

func (p *LoggingPolicy) Merge(src LoggingPolicy)

Merge applies default values from the provided policy.

type OptionalBool added in v0.9.5

type OptionalBool bool

OptionalBool provides convenience methods for manipulating optional booleans.

func (*OptionalBool) OrDefault added in v0.9.5

func (b *OptionalBool) OrDefault(def bool) bool

OrDefault returns the value of the boolean or provided default if it's nil.

type Policy

type Policy struct {
	Labels              map[string]string   `json:"-"`
	RetentionPolicy     RetentionPolicy     `json:"retention,omitempty"`
	FilesPolicy         FilesPolicy         `json:"files,omitempty"`
	ErrorHandlingPolicy ErrorHandlingPolicy `json:"errorHandling,omitempty"`
	SchedulingPolicy    SchedulingPolicy    `json:"scheduling,omitempty"`
	CompressionPolicy   CompressionPolicy   `json:"compression,omitempty"`
	Actions             ActionsPolicy       `json:"actions"`
	LoggingPolicy       LoggingPolicy       `json:"logging"`
	NoParent            bool                `json:"noParent,omitempty"`
}

Policy describes snapshot policy for a single source.

func GetDefinedPolicy

func GetDefinedPolicy(ctx context.Context, rep repo.Repository, si snapshot.SourceInfo) (*Policy, error)

GetDefinedPolicy returns the policy defined on the provided snapshot.SourceInfo or ErrPolicyNotFound if not present.

func GetPolicyByID

func GetPolicyByID(ctx context.Context, rep repo.Repository, id manifest.ID) (*Policy, error)

GetPolicyByID gets the policy for a given unique ID or ErrPolicyNotFound if not found.

func ListPolicies

func ListPolicies(ctx context.Context, rep repo.Repository) ([]*Policy, error)

ListPolicies returns a list of all policies.

func MergePolicies

func MergePolicies(policies []*Policy) *Policy

MergePolicies computes the policy by applying the specified list of policies in order.

func (*Policy) ID

func (p *Policy) ID() string

ID returns globally unique identifier of the policy.

func (*Policy) String

func (p *Policy) String() string

func (*Policy) Target

func (p *Policy) Target() snapshot.SourceInfo

Target returns the snapshot.SourceInfo describing username, host and path targeted by the policy.

type RetentionPolicy

type RetentionPolicy struct {
	KeepLatest  *int `json:"keepLatest,omitempty"`
	KeepHourly  *int `json:"keepHourly,omitempty"`
	KeepDaily   *int `json:"keepDaily,omitempty"`
	KeepWeekly  *int `json:"keepWeekly,omitempty"`
	KeepMonthly *int `json:"keepMonthly,omitempty"`
	KeepAnnual  *int `json:"keepAnnual,omitempty"`
}

RetentionPolicy describes snapshot retention policy.

func (*RetentionPolicy) ComputeRetentionReasons

func (r *RetentionPolicy) ComputeRetentionReasons(manifests []*snapshot.Manifest)

ComputeRetentionReasons computes the reasons why each snapshot is retained, based on the settings in retention policy and stores them in RetentionReason field.

func (*RetentionPolicy) Merge

func (r *RetentionPolicy) Merge(src RetentionPolicy)

Merge applies default values from the provided policy.

type SchedulingPolicy

type SchedulingPolicy struct {
	IntervalSeconds int64       `json:"intervalSeconds,omitempty"`
	TimesOfDay      []TimeOfDay `json:"timeOfDay,omitempty"`
	Manual          bool        `json:"manual,omitempty"`
}

SchedulingPolicy describes policy for scheduling snapshots.

func (*SchedulingPolicy) Interval

func (p *SchedulingPolicy) Interval() time.Duration

Interval returns the snapshot interval or zero if not specified.

func (*SchedulingPolicy) Merge

func (p *SchedulingPolicy) Merge(src SchedulingPolicy)

Merge applies default values from the provided policy.

func (*SchedulingPolicy) NextSnapshotTime added in v0.9.4

func (p *SchedulingPolicy) NextSnapshotTime(previousSnapshotTime, now time.Time) (time.Time, bool)

NextSnapshotTime computes next snapshot time given previous snapshot time and current wall clock time.

func (*SchedulingPolicy) SetInterval

func (p *SchedulingPolicy) SetInterval(d time.Duration)

SetInterval sets the snapshot interval (zero disables).

type SubdirectoryPolicyMap added in v0.4.0

type SubdirectoryPolicyMap map[string]*Policy

SubdirectoryPolicyMap implements Getter for a static mapping of relative paths to Policy for subdirectories.

func (SubdirectoryPolicyMap) GetPolicyForPath added in v0.4.0

func (m SubdirectoryPolicyMap) GetPolicyForPath(relativePath string) (*Policy, error)

GetPolicyForPath returns Policy defined in the map or nil.

type TargetWithPolicy added in v0.8.0

type TargetWithPolicy struct {
	ID     string              `json:"id"`
	Target snapshot.SourceInfo `json:"target"`
	*Policy
}

TargetWithPolicy wraps a policy with its target and ID.

type TimeOfDay

type TimeOfDay struct {
	Hour   int `json:"hour"`
	Minute int `json:"min"`
}

TimeOfDay represents the time of day (hh:mm) using 24-hour time format.

func SortAndDedupeTimesOfDay

func SortAndDedupeTimesOfDay(tod []TimeOfDay) []TimeOfDay

SortAndDedupeTimesOfDay sorts the slice of times of day and removes duplicates.

func (*TimeOfDay) Parse

func (t *TimeOfDay) Parse(s string) error

Parse parses the time of day.

func (TimeOfDay) String

func (t TimeOfDay) String() string

String returns string representation of time of day.

type Tree added in v0.4.0

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

Tree represents a node in the policy tree, where a policy can be defined. A nil tree is a valid tree with default policy.

func BuildTree added in v0.4.0

func BuildTree(defined map[string]*Policy, defaultPolicy *Policy) *Tree

BuildTree builds a policy tree from the given map of paths to policies. Each path must be relative and start with "." and be separated by slashes.

func TreeForSource added in v0.4.0

func TreeForSource(ctx context.Context, rep repo.Repository, si snapshot.SourceInfo) (*Tree, error)

TreeForSource returns policy Tree for a given source.

func (*Tree) Child added in v0.4.0

func (t *Tree) Child(name string) *Tree

Child gets a subtree for an entry with a given name.

func (*Tree) DefinedPolicy added in v0.4.0

func (t *Tree) DefinedPolicy() *Policy

DefinedPolicy returns policy that's been explicitly defined for tree node or nil if no policy was defined.

func (*Tree) EffectivePolicy added in v0.4.0

func (t *Tree) EffectivePolicy() *Policy

EffectivePolicy returns policy that's been defined for this tree node or inherited from its parent.

func (*Tree) IsInherited added in v0.4.0

func (t *Tree) IsInherited() bool

IsInherited returns true if the policy inherited to the given tree hode has been inherited from its parent.

Jump to

Keyboard shortcuts

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