backy

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2025 License: Apache-2.0 Imports: 43 Imported by: 0

Documentation

Overview

backy.go Copyright (C) Andrew Woodlee 2023 License: Apache-2.0

notification.go Copyright (C) Andrew Woodlee 2023 License: Apache-2.0

Index

Constants

This section is empty.

Variables

View Source
var PrivateKeyExtraInfoErr = errors.New("Private key may be encrypted. \nIf encrypted, make sure the password is specified correctly in the correct section. This may be done in one of two ways: \n Using external directives - see docs \n privatekeypassword: password (not recommended). \n ")
View Source
var Sprintf = fmt.Sprintf

Functions

func AllowedExternalDirectivesStrings added in v0.10.2

func AllowedExternalDirectivesStrings() []string

AllowedExternalDirectivesStrings returns a slice of all String values of the enum

func CheckConfigValues added in v0.1.0

func CheckConfigValues(config *koanf.Koanf, file string)

func CheckIfHostHasHostName added in v0.7.0

func CheckIfHostHasHostName(host string) (bool, string)

func CommandTypeStrings added in v0.9.0

func CommandTypeStrings() []string

CommandTypeStrings returns a slice of all String values of the enum

func GetPassword added in v0.1.0

func GetPassword(pass string, opts *ConfigOpts) string

GetPassword gets any password

func GetPrivateKeyPassword added in v0.1.0

func GetPrivateKeyPassword(key string, opts *ConfigOpts) string

func GetVaultKey added in v0.3.0

func GetVaultKey(str string, opts *ConfigOpts, log zerolog.Logger) string

func IsCmdStdOutEnabled added in v0.1.0

func IsCmdStdOutEnabled() bool

func IsTerminalActive added in v0.1.0

func IsTerminalActive() bool

func PackageOperationStrings added in v0.9.0

func PackageOperationStrings() []string

PackageOperationStrings returns a slice of all String values of the enum

func VariadicFunctionParameterTest added in v0.10.2

func VariadicFunctionParameterTest(allowedKeys ...string)

Types

type AllowedExternalDirectives added in v0.10.2

type AllowedExternalDirectives int
const (
	DefaultExternalDir                AllowedExternalDirectives = iota
	AllowedExternalDirectiveVault                               // vault
	AllowedExternalDirectiveVaultFile                           // vault-file
	AllowedExternalDirectiveAll                                 // vault-file-env
	AllowedExternalDirectiveFileEnv                             // file-env
	AllowedExternalDirectiveFile                                // file
	AllowedExternalDirectiveEnv                                 // env
)

func AllowedExternalDirectivesString added in v0.10.2

func AllowedExternalDirectivesString(s string) (AllowedExternalDirectives, error)

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

func AllowedExternalDirectivesValues added in v0.10.2

func AllowedExternalDirectivesValues() []AllowedExternalDirectives

AllowedExternalDirectivesValues returns all values of the enum

func (AllowedExternalDirectives) IsAAllowedExternalDirectives added in v0.10.2

func (i AllowedExternalDirectives) IsAAllowedExternalDirectives() bool

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

func (AllowedExternalDirectives) MarshalJSON added in v0.10.2

func (i AllowedExternalDirectives) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for AllowedExternalDirectives

func (AllowedExternalDirectives) MarshalText added in v0.10.2

func (i AllowedExternalDirectives) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for AllowedExternalDirectives

func (AllowedExternalDirectives) MarshalYAML added in v0.10.2

func (i AllowedExternalDirectives) MarshalYAML() (interface{}, error)

MarshalYAML implements a YAML Marshaler for AllowedExternalDirectives

func (AllowedExternalDirectives) String added in v0.10.2

func (i AllowedExternalDirectives) String() string

func (*AllowedExternalDirectives) UnmarshalJSON added in v0.10.2

func (i *AllowedExternalDirectives) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for AllowedExternalDirectives

func (*AllowedExternalDirectives) UnmarshalText added in v0.10.2

func (i *AllowedExternalDirectives) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for AllowedExternalDirectives

func (*AllowedExternalDirectives) UnmarshalYAML added in v0.10.2

func (i *AllowedExternalDirectives) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements a YAML Unmarshaler for AllowedExternalDirectives

type BackyOptionFunc

type BackyOptionFunc func(*ConfigOpts)

func AddCommandLists added in v0.1.0

func AddCommandLists(lists []string) BackyOptionFunc

AddCommandLists adds lists to ConfigOpts

func AddCommands added in v0.1.0

func AddCommands(commands []string) BackyOptionFunc

AddCommands adds commands to ConfigOpts

func EnableCron added in v0.7.0

func EnableCron() BackyOptionFunc

EnableCron enables the execution of command lists at specified times

func SetCmdStdOut added in v0.9.0

func SetCmdStdOut(setStdOut bool) BackyOptionFunc

SetCmdStdOut forces the command output to stdout

func SetCmdsToSearch added in v0.4.0

func SetCmdsToSearch(cmds []string) BackyOptionFunc

AddPrintLists adds lists to print out

func SetListsToSearch added in v0.4.0

func SetListsToSearch(lists []string) BackyOptionFunc

SetListsToSearch adds lists to search

func SetLogFile added in v0.7.0

func SetLogFile(logFile string) BackyOptionFunc

SetLogFile sets the path to the log file

type CmdList added in v0.1.0

type CmdList struct {
	Name            string   `yaml:"name,omitempty"`
	Cron            string   `yaml:"cron,omitempty"`
	RunCmdOnFailure string   `yaml:"runCmdOnFailure,omitempty"`
	Order           []string `yaml:"order,omitempty"`
	Notifications   []string `yaml:"notifications,omitempty"`
	GetOutput       bool     `yaml:"getOutput,omitempty"`
	NotifyOnSuccess bool     `yaml:"notifyOnSuccess,omitempty"`

	NotifyConfig *notify.Notify
	Source       string `yaml:"source"` // URL to fetch remote commands
	Type         string `yaml:"type"`
}

type CmdOutput

type CmdOutput struct {
	Err    error
	Output bytes.Buffer
}

type CmdResult added in v0.6.1

type CmdResult struct {
	CmdName  string // Name of the command executed
	ListName string // Name of the command list
	Error    error  // Error encountered, if any
}

type Command

type Command struct {
	Name string `yaml:"name,omitempty"`

	Cmd string `yaml:"cmd"`

	// See CommandType enum further down the page for acceptable values
	Type CommandType `yaml:"type,omitempty"`

	Host *string `yaml:"host,omitempty"`

	Hooks *Hooks `yaml:"hooks,omitempty"`

	Shell string `yaml:"shell,omitempty"`

	RemoteHost *Host `yaml:"-"`

	Args []string `yaml:"args,omitempty"`

	Dir *string `yaml:"dir,omitempty"`

	Env string `yaml:"env,omitempty"`

	Environment []string `yaml:"environment,omitempty"`

	GetOutputInList bool `yaml:"getOutputInList,omitempty"`

	ScriptEnvFile string `yaml:"scriptEnvFile"`

	OutputToLog bool `yaml:"outputToLog,omitempty"`

	OutputFile string `yaml:"outputFile,omitempty"`

	PackageManager string `yaml:"packageManager,omitempty"`

	PackageName string `yaml:"packageName,omitempty"`

	PackageVersion string `yaml:"packageVersion,omitempty"`

	PackageOperation PackageOperation `yaml:"packageOperation,omitempty"`

	RemoteSource string `yaml:"remoteSource,omitempty"`

	FetchBeforeExecution bool `yaml:"fetchBeforeExecution,omitempty"`

	Fetcher remotefetcher.RemoteFetcher

	Username string `yaml:"userName,omitempty"`

	UserID string `yaml:"userID,omitempty"`

	UserGroups []string `yaml:"userGroups,omitempty"`

	UserHome string `yaml:"userHome,omitempty"`

	UserShell string `yaml:"userShell,omitempty"`

	UserCreateHome bool `yaml:"userCreateHome,omitempty"`

	UserIsSystem bool `yaml:"userIsSystem,omitempty"`

	UserPassword string `yaml:"userPassword,omitempty"`

	UserSshPubKeys []string `yaml:"userSshPubKeys,omitempty"`

	// OS for the command, only used when type is user
	OS string `yaml:"OS,omitempty"`

	UserOperation string `yaml:"userOperation,omitempty"`
	// contains filtered or unexported fields
}

func (*Command) ExecuteHooks added in v0.5.0

func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts)

func (*Command) GenerateLogger added in v0.6.0

func (cmd *Command) GenerateLogger(opts *ConfigOpts) zerolog.Logger

func (*Command) GetVariablesFromConf added in v0.10.2

func (c *Command) GetVariablesFromConf(opts *ConfigOpts)

func (*Command) RunCmd

func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([]string, error)

RunCmd runs a Command. The environment of local commands will be the machine's environment plus any extra variables specified in the Env file or Environment. Dir can also be specified for local commands.

Returns the output as a slice and an error, if any

func (*Command) RunCmdSSH added in v0.6.0

func (command *Command) RunCmdSSH(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([]string, error)

type CommandType added in v0.9.0

type CommandType int

use ints so we can use enums

const (
	DefaultCT      CommandType = iota //
	ScriptCT                          // script
	ScriptFileCT                      // scriptFile
	RemoteScriptCT                    // remoteScript
	PackageCT                         // package
	UserCT                            // user
)

func CommandTypeString added in v0.9.0

func CommandTypeString(s string) (CommandType, error)

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

func CommandTypeValues added in v0.9.0

func CommandTypeValues() []CommandType

CommandTypeValues returns all values of the enum

func (CommandType) IsACommandType added in v0.9.0

func (i CommandType) IsACommandType() bool

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

func (CommandType) MarshalJSON added in v0.9.0

func (i CommandType) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for CommandType

func (CommandType) MarshalText added in v0.9.0

func (i CommandType) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for CommandType

func (CommandType) MarshalYAML added in v0.9.0

func (i CommandType) MarshalYAML() (interface{}, error)

MarshalYAML implements a YAML Marshaler for CommandType

func (CommandType) String added in v0.9.0

func (i CommandType) String() string

func (*CommandType) UnmarshalJSON added in v0.9.0

func (i *CommandType) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for CommandType

func (*CommandType) UnmarshalText added in v0.9.0

func (i *CommandType) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for CommandType

func (*CommandType) UnmarshalYAML added in v0.9.0

func (i *CommandType) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements a YAML Unmarshaler for CommandType

type ConfigOpts added in v0.3.0

type ConfigOpts struct {
	// Cmds holds the commands for a list.
	// Key is the name of the command,
	Cmds map[string]*Command `yaml:"commands"`

	// CmdConfigLists holds the lists of commands to be run in order.
	// Key is the command list name.
	CmdConfigLists map[string]*CmdList `yaml:"cmdLists"`

	// Hosts holds the Host config.
	// key is the host.
	Hosts map[string]*Host `yaml:"hosts"`

	Logger zerolog.Logger

	// Global log level
	BackyLogLvl *string

	CmdStdOut bool

	ConfigFilePath string

	ConfigDir string

	LogFilePath string

	CmdListFile string

	List ListConfig

	Vars map[string]string `yaml:"variables"`

	VaultKeys []*VaultKey `yaml:"keys"`

	NotificationConf *Notifications `yaml:"notifications"`

	Cache      *remotefetcher.Cache
	CachedData []*remotefetcher.CacheData
	// contains filtered or unexported fields
}

func NewOpts

func NewOpts(configFilePath string, opts ...BackyOptionFunc) *ConfigOpts

func (*ConfigOpts) Cron added in v0.3.0

func (opts *ConfigOpts) Cron()

func (*ConfigOpts) ExecCmdsSSH added in v0.6.0

func (opts *ConfigOpts) ExecCmdsSSH(cmdList []string, hostsList []string)

func (*ConfigOpts) ExecuteCmds added in v0.4.0

func (opts *ConfigOpts) ExecuteCmds()

func (*ConfigOpts) InitConfig added in v0.3.0

func (opts *ConfigOpts) InitConfig()

func (*ConfigOpts) ListCommand added in v0.5.0

func (opts *ConfigOpts) ListCommand(cmd string)

ListCommand searches the commands in the file to find one

func (*ConfigOpts) ListCommandList added in v0.9.0

func (opts *ConfigOpts) ListCommandList(list string)

func (*ConfigOpts) LogLvl added in v0.3.0

func (c *ConfigOpts) LogLvl(level string) BackyOptionFunc

func (*ConfigOpts) ReadConfig added in v0.7.0

func (opts *ConfigOpts) ReadConfig() *ConfigOpts

func (*ConfigOpts) RunListConfig added in v0.4.0

func (opts *ConfigOpts) RunListConfig(cron string)

RunListConfig runs a command list from the ConfigFile.

func (*ConfigOpts) SetupNotify added in v0.4.0

func (opts *ConfigOpts) SetupNotify()

SetupNotify sets up notify instances for each command list.

type Hooks added in v0.5.0

type Hooks struct {
	Error   []string `yaml:"error,omitempty"`
	Success []string `yaml:"success,omitempty"`
	Final   []string `yaml:"final,omitempty"`
}

type Host

type Host struct {
	OS                 string `yaml:"OS,omitempty"`
	ConfigFilePath     string `yaml:"config,omitempty"`
	Host               string `yaml:"host,omitempty"`
	HostName           string `yaml:"hostname,omitempty"`
	KnownHostsFile     string `yaml:"knownHostsFile,omitempty"`
	ClientConfig       *ssh.ClientConfig
	SSHConfigFile      *sshConfigFile
	SshClient          *ssh.Client
	Port               uint16 `yaml:"port,omitempty"`
	ProxyJump          string `yaml:"proxyjump,omitempty"`
	Password           string `yaml:"password,omitempty"`
	PrivateKeyPath     string `yaml:"privateKeyPath,omitempty"`
	PrivateKeyPassword string `yaml:"privateKeyPassword,omitempty"`

	User string `yaml:"user,omitempty"`

	// ProxyHost holds the configuration for a ProxyJump host
	ProxyHost []*Host
	// contains filtered or unexported fields
}

Host defines a host to which to connect. If not provided, the values will be looked up in the default ssh config files

func (*Host) CombineHostNameWithPort added in v0.2.0

func (remoteHost *Host) CombineHostNameWithPort()

func (*Host) ConnectThroughBastion added in v0.1.0

func (remoteHost *Host) ConnectThroughBastion(log zerolog.Logger) (*ssh.Client, error)

func (*Host) ConnectToHost added in v0.6.0

func (remoteConfig *Host) ConnectToHost(opts *ConfigOpts) error

ConnectToHost connects to a host by looking up the config values in the file ~/.ssh/config It uses any set values and looks up an unset values in the config files remoteConfig is modified directly. The *ssh.Client is returned as part of remoteConfig, If configFile is empty, any required configuration is looked up in the default config files If any value is not found, defaults are used

func (*Host) DetectOS added in v0.7.0

func (h *Host) DetectOS(opts *ConfigOpts) (string, error)

func (*Host) GetAuthMethods added in v0.1.0

func (remoteHost *Host) GetAuthMethods(opts *ConfigOpts) error

func (*Host) GetHostName added in v0.2.0

func (remoteHost *Host) GetHostName()

func (*Host) GetKnownHosts added in v0.5.0

func (remoteHost *Host) GetKnownHosts() error

GetKnownHosts resolves the host's KnownHosts file if it is defined if not defined, the default location for this file is used

func (*Host) GetPort added in v0.2.0

func (remoteHost *Host) GetPort()

GetPort checks if the port from the config file is 0 If it is the port is searched in the SSH config file(s)

func (*Host) GetPrivateKeyFileFromConfig added in v0.2.0

func (remoteHost *Host) GetPrivateKeyFileFromConfig()

GetPrivateKeyFromConfig checks to see if the privateKeyPath is empty. If not, it keeps the value. If empty, the key is looked for in the specified config file. If that path is empty, the default config file is searched. If not found in the default file, the privateKeyPath is set to ~/.ssh/id_rsa

func (*Host) GetProxyJumpConfig added in v0.2.0

func (remoteConfig *Host) GetProxyJumpConfig(hosts map[string]*Host, opts *ConfigOpts) error

func (*Host) GetProxyJumpFromConfig added in v0.2.0

func (remoteConfig *Host) GetProxyJumpFromConfig(hosts map[string]*Host) error

func (*Host) GetSshUserFromConfig added in v0.1.0

func (remoteHost *Host) GetSshUserFromConfig()

type HttpConfig added in v0.10.2

type HttpConfig struct {
	URL     string              `yaml:"url"`
	Method  string              `yaml:"method"`
	Headers map[string][]string `yaml:"headers"`
}

type ListConfig added in v0.4.0

type ListConfig struct {
	Lists    []string
	Commands []string
	Hosts    []string
}

type MailConfig added in v0.4.0

type MailConfig struct {
	Host          string   `yaml:"host"`
	Port          string   `yaml:"port"`
	Username      string   `yaml:"username"`
	SenderAddress string   `yaml:"senderaddress"`
	To            []string `yaml:"to"`
	Password      string   `yaml:"password"`
}

type MatrixStruct added in v0.4.0

type MatrixStruct struct {
	Homeserver  string    `yaml:"homeserver"`
	Roomid      id.RoomID `yaml:"room-id"`
	AccessToken string    `yaml:"access-token"`
	UserId      id.UserID `yaml:"user-id"`
}

type Notifications added in v0.4.0

type Notifications struct {
	MailConfig   map[string]MailConfig   `yaml:"mail,omitempty"`
	MatrixConfig map[string]MatrixStruct `yaml:"matrix,omitempty"`
	HttpConfig   map[string]HttpConfig   `yaml:"http,omitempty"`
}

type PackageOperation added in v0.9.0

type PackageOperation int
const (
	DefaultPO          PackageOperation = iota //
	PackOpInstall                              // install
	PackOpUpgrade                              // upgrade
	PackOpPurge                                // purge
	PackOpRemove                               // remove
	PackOpCheckVersion                         // checkVersion
	PackOpIsInstalled                          // isInstalled
)

func PackageOperationString added in v0.9.0

func PackageOperationString(s string) (PackageOperation, error)

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

func PackageOperationValues added in v0.9.0

func PackageOperationValues() []PackageOperation

PackageOperationValues returns all values of the enum

func (PackageOperation) IsAPackageOperation added in v0.9.0

func (i PackageOperation) IsAPackageOperation() bool

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

func (PackageOperation) MarshalJSON added in v0.9.0

func (i PackageOperation) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for PackageOperation

func (PackageOperation) MarshalText added in v0.9.0

func (i PackageOperation) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for PackageOperation

func (PackageOperation) MarshalYAML added in v0.9.0

func (i PackageOperation) MarshalYAML() (interface{}, error)

MarshalYAML implements a YAML Marshaler for PackageOperation

func (PackageOperation) String added in v0.9.0

func (i PackageOperation) String() string

func (*PackageOperation) UnmarshalJSON added in v0.9.0

func (i *PackageOperation) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for PackageOperation

func (*PackageOperation) UnmarshalText added in v0.9.0

func (i *PackageOperation) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for PackageOperation

func (*PackageOperation) UnmarshalYAML added in v0.9.0

func (i *PackageOperation) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements a YAML Unmarshaler for PackageOperation

type RemoteSource added in v0.6.0

type RemoteSource struct {
	URL  string `yaml:"url"`
	Type string `yaml:"type"` // e.g., s3, http
	Auth struct {
		AccessKey string `yaml:"accessKey"`
		SecretKey string `yaml:"secretKey"`
	} `yaml:"auth"`
}

type VaultConfig added in v0.3.0

type VaultConfig struct {
	Token   string      `yaml:"token"`
	Address string      `yaml:"address"`
	Enabled string      `yaml:"enabled"`
	Keys    []*VaultKey `yaml:"keys"`
}

type VaultKey added in v0.3.0

type VaultKey struct {
	Name      string `yaml:"name"`
	Key       string `yaml:"key"`
	Path      string `yaml:"path"`
	ValueType string `yaml:"type"`
	MountPath string `yaml:"mountpath"`
}

Jump to

Keyboard shortcuts

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