hubops

package
v1.6.5 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package hubops is responsible for managing the local hub (items and data files) for CrowdSec.

The index file itself (.index.json) is still managed by pkg/cwhub, which also provides the Hub and Item structs.

The hubops package is mostly used by cscli for the "cscli <hubtype> install/remove/upgrade ..." commands.

It adopts a command-based pattern: a Plan contains a sequence of Commands. Both Plan and Command have separate preparation and execution methods.

  • Command Interface: The Command interface defines the contract for all operations that can be performed on hub items. Each operation implements the Prepare and Run methods, allowing for pre-execution setup and actual execution logic.

  • ActionPlan: ActionPlan serves as a container for a sequence of Commands. It manages the addition of commands, handles dependencies between them, and orchestrates their execution. ActionPlan also provides a mechanism for interactive confirmation and dry-run.

To perform operations on hub items, create an ActionPlan and add the desired Commands to it. Once all commands are added, execute the ActionPlan to perform the operations in the correct order, handling dependencies and user confirmations.

Example:

hub := cwhub.NewHub(...)
plan := hubops.NewActionPlan(hub)

downloadCmd := hubops.NewDownloadCommand(item, force)
if err := plan.AddCommand(downloadCmd); err != nil {
	logrus.Fatalf("Failed to add download command: %v", err)
}

enableCmd := hubops.NewEnableCommand(item, force)
if err := plan.AddCommand(enableCmd); err != nil {
	logrus.Fatalf("Failed to add enable command: %v", err)
}

if err := plan.Execute(ctx, confirm, dryRun, verbose); err != nil {
	logrus.Fatalf("Failed to execute action plan: %v", err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateInstallLink(i *cwhub.Item) error

CreateInstallLink creates a symlink between the actual config file at hub.HubDir and hub.ConfigDir.

func DownloadDataIfNeeded

func DownloadDataIfNeeded(ctx context.Context, hub *cwhub.Hub, item *cwhub.Item, force bool) (bool, error)

XXX: TODO: temporary for hubtests, but will have to go. DownloadDataIfNeeded downloads the data set for the item.

func RemoveInstallLink(i *cwhub.Item) error

RemoveInstallLink removes the item's symlink between the installation directory and the local hub.

func UniqueKey

func UniqueKey(c Command) string

UniqueKey generates a unique string key for a Command based on its operation type, item type, and detail. Is is used to avoid adding duplicate commands to the action plan.

Types

type ActionPlan

type ActionPlan struct {

	// Indicates whether a reload of the CrowdSec service is required after executing the action plan.
	ReloadNeeded bool
	// contains filtered or unexported fields
}

ActionPlan orchestrates the sequence of operations (Commands) to manage CrowdSec hub items.

func NewActionPlan

func NewActionPlan(hub *cwhub.Hub) *ActionPlan

func (*ActionPlan) AddCommand

func (p *ActionPlan) AddCommand(c Command) error

func (*ActionPlan) Confirm

func (p *ActionPlan) Confirm(verbose bool) (bool, error)

func (*ActionPlan) Description

func (p *ActionPlan) Description(verbose bool) string

Description returns a string representation of the action plan. If verbose is false, the operations are grouped by item type and operation type. If verbose is true, they are listed as they appear in the command slice.

func (*ActionPlan) Execute

func (p *ActionPlan) Execute(ctx context.Context, interactive bool, dryRun bool, verbose bool) error

func (*ActionPlan) Info

func (p *ActionPlan) Info(msg string)

func (*ActionPlan) Warning

func (p *ActionPlan) Warning(msg string)

type Command

type Command interface {
	// Prepare sets up the command for execution within the given
	// ActionPlan. It may add additional commands to the ActionPlan based
	// on dependencies or prerequisites. Returns a boolean indicating
	// whether the command execution should be skipped (it can be
	// redundant, like installing something that is already installed) and
	// an error if the preparation failed.
	// NOTE: Returning an error will bubble up from the plan.AddCommand() method,
	// but Prepare() might already have modified the plan's command slice.
	Prepare(*ActionPlan) (bool, error)

	// Run executes the command within the provided context and ActionPlan.
	// It performs the actual operation and returns an error if execution fails.
	// NOTE: Returning an error will currently stop the execution of the action plan.
	Run(ctx context.Context, plan *ActionPlan) error

	// OperationType returns a unique string representing the type of operation to perform
	// (e.g., "download", "enable").
	OperationType() string

	// ItemType returns the type of item the operation is performed on
	// (e.g., "collections"). Used in confirmation prompt and dry-run.
	ItemType() string

	// Detail provides further details on the operation,
	// such as the item's name and version.
	Detail() string
}

Command represents an operation that can be performed on a CrowdSec hub item.

Each concrete implementation defines a Prepare() method to check for errors and preconditions, decide which sub-commands are required (like installing dependencies) and add them to the action plan.

type DataRefreshCommand

type DataRefreshCommand struct {
	Force bool
}

DataRefreshCommand updates the data files associated with the installed hub items.

func NewDataRefreshCommand

func NewDataRefreshCommand(force bool) *DataRefreshCommand

func (*DataRefreshCommand) Detail

func (c *DataRefreshCommand) Detail() string

func (*DataRefreshCommand) ItemType

func (c *DataRefreshCommand) ItemType() string

func (*DataRefreshCommand) OperationType

func (c *DataRefreshCommand) OperationType() string

func (*DataRefreshCommand) Prepare

func (c *DataRefreshCommand) Prepare(plan *ActionPlan) (bool, error)

func (*DataRefreshCommand) Run

func (c *DataRefreshCommand) Run(ctx context.Context, plan *ActionPlan) error

type DataSet

type DataSet struct {
	Data []types.DataSource `yaml:"data,omitempty"`
}

The DataSet is a list of data sources required by an item (built from the data: section in the yaml).

type DisableCommand

type DisableCommand struct {
	Item  *cwhub.Item
	Force bool
}

DisableCommand uninstalls an item and its dependencies, ensuring that no sub-item is left in an inconsistent state.

func NewDisableCommand

func NewDisableCommand(item *cwhub.Item, force bool) *DisableCommand

func (*DisableCommand) Detail

func (c *DisableCommand) Detail() string

func (*DisableCommand) ItemType

func (c *DisableCommand) ItemType() string

func (*DisableCommand) OperationType

func (c *DisableCommand) OperationType() string

func (*DisableCommand) Prepare

func (c *DisableCommand) Prepare(plan *ActionPlan) (bool, error)

func (*DisableCommand) Run

func (c *DisableCommand) Run(ctx context.Context, plan *ActionPlan) error

type DownloadCommand

type DownloadCommand struct {
	Item  *cwhub.Item
	Force bool
	// contains filtered or unexported fields
}

DownloadCommand handles the downloading of hub items. It ensures that items are fetched from the hub (or from the index file if it also has content) managing dependencies and verifying the integrity of downloaded content. This is used by "cscli install" and "cscli upgrade". Tainted items require the force parameter, local items are skipped.

func NewDownloadCommand

func NewDownloadCommand(item *cwhub.Item, contentProvider cwhub.ContentProvider, force bool) *DownloadCommand

func (*DownloadCommand) Detail

func (c *DownloadCommand) Detail() string

func (*DownloadCommand) ItemType

func (c *DownloadCommand) ItemType() string

func (*DownloadCommand) OperationType

func (c *DownloadCommand) OperationType() string

func (*DownloadCommand) Prepare

func (c *DownloadCommand) Prepare(plan *ActionPlan) (bool, error)

func (*DownloadCommand) Run

func (c *DownloadCommand) Run(ctx context.Context, plan *ActionPlan) error

type EnableCommand

type EnableCommand struct {
	Item       *cwhub.Item
	Force      bool
	FromLatest bool
}

EnableCommand installs a hub item and its dependencies. In case this command is called during an upgrade, the sub-items list it taken from the latest version in the index, otherwise from the version that is currently installed.

func NewEnableCommand

func NewEnableCommand(item *cwhub.Item, force bool) *EnableCommand

func (*EnableCommand) Detail

func (c *EnableCommand) Detail() string

func (*EnableCommand) ItemType

func (c *EnableCommand) ItemType() string

func (*EnableCommand) OperationType

func (c *EnableCommand) OperationType() string

func (*EnableCommand) Prepare

func (c *EnableCommand) Prepare(plan *ActionPlan) (bool, error)

func (*EnableCommand) Run

func (c *EnableCommand) Run(ctx context.Context, plan *ActionPlan) error

type PurgeCommand

type PurgeCommand struct {
	Item  *cwhub.Item
	Force bool
}

PurgeCommand removes the downloaded content of a hub item, effectively removing it from the local system. This command also removes the sub-items but not the associated data files.

func NewPurgeCommand

func NewPurgeCommand(item *cwhub.Item, force bool) *PurgeCommand

func (*PurgeCommand) Detail

func (c *PurgeCommand) Detail() string

func (*PurgeCommand) ItemType

func (c *PurgeCommand) ItemType() string

func (*PurgeCommand) OperationType

func (c *PurgeCommand) OperationType() string

func (*PurgeCommand) Prepare

func (c *PurgeCommand) Prepare(plan *ActionPlan) (bool, error)

func (*PurgeCommand) Run

func (c *PurgeCommand) Run(ctx context.Context, plan *ActionPlan) error

Jump to

Keyboard shortcuts

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