Compare commits
No commits in common. "bf8d261cf3e27124f0959a23e4b520911498e625" and "b8a63f39f50ddf1a402acdb1eb698ae5c2e7b5f6" have entirely different histories.
bf8d261cf3
...
b8a63f39f5
@ -1,3 +0,0 @@
|
|||||||
kind: Added
|
|
||||||
body: Hooks for Commands.[name]. Error, success, and final. [#12]
|
|
||||||
time: 2024-11-14T21:01:48.823426401-06:00
|
|
@ -1,3 +0,0 @@
|
|||||||
kind: Fixed
|
|
||||||
body: make command logger be used for errors, not just when running the command
|
|
||||||
time: 2024-11-14T21:13:06.404134926-06:00
|
|
@ -7,4 +7,4 @@ steps:
|
|||||||
release:
|
release:
|
||||||
image: golangci/golangci-lint:v1.53.3
|
image: golangci/golangci-lint:v1.53.3
|
||||||
commands:
|
commands:
|
||||||
- golangci-lint run -v --timeout 5m
|
- golangci-lint run -v
|
@ -391,11 +391,12 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
|
|||||||
// cmdListWorker
|
// cmdListWorker
|
||||||
func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<- string, opts *ConfigOpts) {
|
func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<- string, opts *ConfigOpts) {
|
||||||
// iterate over list to run
|
// iterate over list to run
|
||||||
res := CmdListResults{}
|
|
||||||
for list := range jobs {
|
for list := range jobs {
|
||||||
|
res := CmdListResults{}
|
||||||
fieldsMap := make(map[string]interface{})
|
fieldsMap := make(map[string]interface{})
|
||||||
fieldsMap["list"] = list.Name
|
fieldsMap["list"] = list.Name
|
||||||
var cmdLogger zerolog.Logger
|
|
||||||
|
cmdLog := opts.Logger.Info()
|
||||||
|
|
||||||
var count int // count of how many commands have been executed
|
var count int // count of how many commands have been executed
|
||||||
var cmdsRan []string // store the commands that have been executed
|
var cmdsRan []string // store the commands that have been executed
|
||||||
@ -407,9 +408,17 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<-
|
|||||||
|
|
||||||
fieldsMap["cmd"] = opts.Cmds[cmd].Name
|
fieldsMap["cmd"] = opts.Cmds[cmd].Name
|
||||||
cmdToRun := opts.Cmds[cmd]
|
cmdToRun := opts.Cmds[cmd]
|
||||||
|
cmdLog.Fields(fieldsMap).Send()
|
||||||
|
|
||||||
cmdLogger = cmdToRun.generateLogger(opts)
|
cmdLogger := opts.Logger.With().
|
||||||
cmdLogger.Info().Fields(fieldsMap).Send()
|
Str("backy-cmd", cmd).Str("Host", "local machine").
|
||||||
|
Logger()
|
||||||
|
|
||||||
|
if cmdToRun.Host != nil {
|
||||||
|
cmdLogger = opts.Logger.With().
|
||||||
|
Str("backy-cmd", cmd).Str("Host", *cmdToRun.Host).
|
||||||
|
Logger()
|
||||||
|
}
|
||||||
|
|
||||||
outputArr, runOutErr := cmdToRun.RunCmd(cmdLogger, opts)
|
outputArr, runOutErr := cmdToRun.RunCmd(cmdLogger, opts)
|
||||||
|
|
||||||
@ -447,17 +456,17 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<-
|
|||||||
tmpErr := msgTemps.err.Execute(&errMsg, errStruct)
|
tmpErr := msgTemps.err.Execute(&errMsg, errStruct)
|
||||||
|
|
||||||
if tmpErr != nil {
|
if tmpErr != nil {
|
||||||
cmdLogger.Err(tmpErr).Send()
|
opts.Logger.Err(tmpErr).Send()
|
||||||
}
|
}
|
||||||
|
|
||||||
notifySendErr := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s failed", list.Name), errMsg.String())
|
notifySendErr := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s failed", list.Name), errMsg.String())
|
||||||
|
|
||||||
if notifySendErr != nil {
|
if notifySendErr != nil {
|
||||||
cmdLogger.Err(notifySendErr).Send()
|
opts.Logger.Err(notifySendErr).Send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdLogger.Err(runOutErr).Send()
|
opts.Logger.Err(runOutErr).Send()
|
||||||
|
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
@ -480,14 +489,14 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<-
|
|||||||
tmpErr := msgTemps.success.Execute(&successMsg, successStruct)
|
tmpErr := msgTemps.success.Execute(&successMsg, successStruct)
|
||||||
|
|
||||||
if tmpErr != nil {
|
if tmpErr != nil {
|
||||||
cmdLogger.Err(tmpErr).Send()
|
opts.Logger.Err(tmpErr).Send()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
err := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s succeeded", list.Name), successMsg.String())
|
err := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s succeeded", list.Name), successMsg.String())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmdLogger.Err(err).Send()
|
opts.Logger.Err(err).Send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -529,7 +538,7 @@ func (opts *ConfigOpts) RunListConfig(cron string) {
|
|||||||
}
|
}
|
||||||
close(listChan)
|
close(listChan)
|
||||||
|
|
||||||
for a := 1; a <= configListsLen; a++ {
|
for a := 0; a <= configListsLen; a++ {
|
||||||
l := <-results
|
l := <-results
|
||||||
|
|
||||||
opts.Logger.Debug().Msg(l)
|
opts.Logger.Debug().Msg(l)
|
||||||
@ -537,15 +546,11 @@ func (opts *ConfigOpts) RunListConfig(cron string) {
|
|||||||
if l != "" {
|
if l != "" {
|
||||||
// execute error hooks
|
// execute error hooks
|
||||||
opts.Logger.Debug().Msg("hooks are working")
|
opts.Logger.Debug().Msg("hooks are working")
|
||||||
opts.Cmds[l].ExecuteHooks("error", opts)
|
|
||||||
} else {
|
} else {
|
||||||
// execute success hooks
|
// execute success hooks
|
||||||
opts.Cmds[l].ExecuteHooks("success", opts)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute final hooks
|
// execute final hooks
|
||||||
opts.Cmds[l].ExecuteHooks("final", opts)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -555,18 +560,20 @@ func (opts *ConfigOpts) RunListConfig(cron string) {
|
|||||||
func (config *ConfigOpts) ExecuteCmds(opts *ConfigOpts) {
|
func (config *ConfigOpts) ExecuteCmds(opts *ConfigOpts) {
|
||||||
for _, cmd := range opts.executeCmds {
|
for _, cmd := range opts.executeCmds {
|
||||||
cmdToRun := opts.Cmds[cmd]
|
cmdToRun := opts.Cmds[cmd]
|
||||||
cmdLogger := cmdToRun.generateLogger(opts)
|
cmdLogger := opts.Logger.With().
|
||||||
|
Str("backy-cmd", cmd).
|
||||||
|
Logger()
|
||||||
_, runErr := cmdToRun.RunCmd(cmdLogger, opts)
|
_, runErr := cmdToRun.RunCmd(cmdLogger, opts)
|
||||||
if runErr != nil {
|
if runErr != nil {
|
||||||
opts.Logger.Err(runErr).Send()
|
opts.Logger.Err(runErr).Send()
|
||||||
|
|
||||||
cmdToRun.ExecuteHooks("error", opts)
|
ExecuteHooks(*cmdToRun, "error", opts)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
cmdToRun.ExecuteHooks("success", opts)
|
ExecuteHooks(*cmdToRun, "success", opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdToRun.ExecuteHooks("final", opts)
|
ExecuteHooks(*cmdToRun, "final", opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.closeHostConnections()
|
opts.closeHostConnections()
|
||||||
@ -610,7 +617,7 @@ func (c *ConfigOpts) closeHostConnections() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
func ExecuteHooks(cmd Command, hookType string, opts *ConfigOpts) {
|
||||||
switch hookType {
|
switch hookType {
|
||||||
case "error":
|
case "error":
|
||||||
for _, v := range cmd.Hooks.Error {
|
for _, v := range cmd.Hooks.Error {
|
||||||
@ -621,35 +628,5 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
|||||||
errCmd.RunCmd(cmdLogger, opts)
|
errCmd.RunCmd(cmdLogger, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "success":
|
|
||||||
for _, v := range cmd.Hooks.Success {
|
|
||||||
successCmd := opts.Cmds[v]
|
|
||||||
cmdLogger := opts.Logger.With().
|
|
||||||
Str("backy-cmd", v).
|
|
||||||
Logger()
|
|
||||||
successCmd.RunCmd(cmdLogger, opts)
|
|
||||||
}
|
|
||||||
case "final":
|
|
||||||
for _, v := range cmd.Hooks.Final {
|
|
||||||
finalCmd := opts.Cmds[v]
|
|
||||||
cmdLogger := opts.Logger.With().
|
|
||||||
Str("backy-cmd", v).
|
|
||||||
Logger()
|
|
||||||
finalCmd.RunCmd(cmdLogger, opts)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (cmd *Command) generateLogger(opts *ConfigOpts) zerolog.Logger {
|
|
||||||
cmdLogger := opts.Logger.With().
|
|
||||||
Str("backy-cmd", cmd.Name).Str("Host", "local machine").
|
|
||||||
Logger()
|
|
||||||
|
|
||||||
if cmd.Host != nil {
|
|
||||||
cmdLogger = opts.Logger.With().
|
|
||||||
Str("backy-cmd", cmd.Name).Str("Host", *cmd.Host).
|
|
||||||
Logger()
|
|
||||||
|
|
||||||
}
|
|
||||||
return cmdLogger
|
|
||||||
}
|
|
||||||
|
@ -298,16 +298,14 @@ func ReadConfig(opts *ConfigOpts) *ConfigOpts {
|
|||||||
|
|
||||||
// process commands
|
// process commands
|
||||||
if err := processCmds(opts); err != nil {
|
if err := processCmds(opts); err != nil {
|
||||||
logging.ExitWithMSG(err.Error(), 1, &opts.Logger)
|
log.Panic().Err(err).Send()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(opts.executeLists) > 0 {
|
|
||||||
for l := range opts.CmdConfigLists {
|
for l := range opts.CmdConfigLists {
|
||||||
if !contains(opts.executeLists, l) {
|
if !contains(opts.executeLists, l) {
|
||||||
delete(opts.CmdConfigLists, l)
|
delete(opts.CmdConfigLists, l)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if backyKoanf.Exists("notifications") {
|
if backyKoanf.Exists("notifications") {
|
||||||
|
|
||||||
@ -449,6 +447,9 @@ func GetVaultKey(str string, opts *ConfigOpts, log zerolog.Logger) string {
|
|||||||
func processCmds(opts *ConfigOpts) error {
|
func processCmds(opts *ConfigOpts) error {
|
||||||
// process commands
|
// process commands
|
||||||
for cmdName, cmd := range opts.Cmds {
|
for cmdName, cmd := range opts.Cmds {
|
||||||
|
cmd.hookRefs = map[string]map[string]*Command{}
|
||||||
|
cmd.hookRefs["error"] = map[string]*Command{}
|
||||||
|
cmd.hookRefs["success"] = map[string]*Command{}
|
||||||
|
|
||||||
if cmd.Name == "" {
|
if cmd.Name == "" {
|
||||||
cmd.Name = cmdName
|
cmd.Name = cmdName
|
||||||
@ -457,19 +458,19 @@ func processCmds(opts *ConfigOpts) error {
|
|||||||
hooks := cmd.Hooks
|
hooks := cmd.Hooks
|
||||||
// resolve hooks
|
// resolve hooks
|
||||||
if hooks != nil {
|
if hooks != nil {
|
||||||
|
opts.Logger.Debug().Msg("Hooks found")
|
||||||
|
|
||||||
processHookSuccess := processHooks(cmd, hooks.Error, opts, "error")
|
errHook, hookRefs, processHookErr := processHooks(hooks.Error, opts.Cmds, "error")
|
||||||
if processHookSuccess != nil {
|
if !processHookErr {
|
||||||
return processHookSuccess
|
return fmt.Errorf("error in command %s hook list: hook command %s not found", cmd.Name, errHook)
|
||||||
}
|
}
|
||||||
processHookSuccess = processHooks(cmd, hooks.Success, opts, "success")
|
cmd.hookRefs["error"] = hookRefs
|
||||||
if processHookSuccess != nil {
|
|
||||||
return processHookSuccess
|
successHook, SuccessHookRefs, processHookSuccess := processHooks(hooks.Error, opts.Cmds, "error")
|
||||||
}
|
if !processHookSuccess {
|
||||||
processHookSuccess = processHooks(cmd, hooks.Final, opts, "final")
|
return fmt.Errorf("error in command %s hook list: hook command %s not found", cmd.Name, successHook)
|
||||||
if processHookSuccess != nil {
|
|
||||||
return processHookSuccess
|
|
||||||
}
|
}
|
||||||
|
cmd.hookRefs["success"] = SuccessHookRefs
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolve hosts
|
// resolve hosts
|
||||||
@ -502,31 +503,34 @@ func processCmds(opts *ConfigOpts) error {
|
|||||||
//
|
//
|
||||||
// Returns the following:
|
// Returns the following:
|
||||||
//
|
//
|
||||||
// An error, if any, if the command is not found
|
// 1. command string
|
||||||
func processHooks(cmd *Command, hooks []string, opts *ConfigOpts, hookType string) error {
|
// 2. each hook type's command map
|
||||||
|
// 2. a bool which determines if the command is valid
|
||||||
|
func processHooks(hooks []string, cmds map[string]*Command, hookType string) (hook string, hookRefs map[string]*Command, hookCmdFound bool) {
|
||||||
|
// fmt.Printf("%v\n", hooks)
|
||||||
|
// for _, v := range cmds {
|
||||||
|
|
||||||
|
// fmt.Printf("CmdName=%v\n", v.Name)
|
||||||
|
// fmt.Printf("Cmd=%v\n", v.Cmd)
|
||||||
|
// }
|
||||||
// initialize hook type
|
// initialize hook type
|
||||||
var hookCmdFound bool
|
hookRefs = make(map[string]*Command)
|
||||||
cmd.hookRefs = map[string]map[string]*Command{}
|
// hookRefs[hookType] = map[string]*Command{}
|
||||||
cmd.hookRefs[hookType] = map[string]*Command{}
|
for _, hook = range hooks {
|
||||||
|
|
||||||
for _, hook := range hooks {
|
|
||||||
|
|
||||||
var hookCmd *Command
|
var hookCmd *Command
|
||||||
// TODO: match by Command.Name
|
// TODO: match by Command.Name
|
||||||
|
|
||||||
hookCmd, hookCmdFound = opts.Cmds[hook]
|
hookCmd, hookCmdFound = cmds[hook]
|
||||||
|
|
||||||
if !hookCmdFound {
|
if !hookCmdFound {
|
||||||
return fmt.Errorf("error in command %s hook %s list: command %s not found", cmd.Name, hookType, hook)
|
return
|
||||||
}
|
}
|
||||||
|
hookRefs[hook] = hookCmd
|
||||||
cmd.hookRefs[hookType][hook] = hookCmd
|
|
||||||
|
|
||||||
// Recursive, decide if this is good
|
// Recursive, decide if this is good
|
||||||
// if hookCmd.hookRefs == nil {
|
// if hookCmd.hookRefs == nil {
|
||||||
// }
|
// }
|
||||||
// hookRef[hookType][h] = hookCmd
|
// hookRef[hookType][h] = hookCmd
|
||||||
}
|
}
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
@ -198,8 +198,8 @@ type (
|
|||||||
|
|
||||||
Hooks struct {
|
Hooks struct {
|
||||||
Error []string `yaml:"error,omitempty"`
|
Error []string `yaml:"error,omitempty"`
|
||||||
Success []string `yaml:"success,omitempty"`
|
SuccessHooks []string `yaml:"success,omitempty"`
|
||||||
Final []string `yaml:"final,omitempty"`
|
FinalHooks []string `yaml:"final,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
CmdListResults struct {
|
CmdListResults struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user