2023-01-17 06:55:28 +00:00
|
|
|
// backy.go
|
|
|
|
// Copyright (C) Andrew Woodlee 2023
|
|
|
|
// License: Apache-2.0
|
2022-10-13 17:03:36 +00:00
|
|
|
package backy
|
|
|
|
|
|
|
|
import (
|
2023-01-17 06:55:28 +00:00
|
|
|
"bufio"
|
2022-12-27 05:20:11 +00:00
|
|
|
"bytes"
|
2023-02-02 05:45:23 +00:00
|
|
|
"context"
|
2022-12-27 05:20:11 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2022-10-13 17:03:36 +00:00
|
|
|
"os/exec"
|
2023-05-12 05:42:14 +00:00
|
|
|
"strings"
|
2023-02-12 05:50:19 +00:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"embed"
|
2022-10-13 17:03:36 +00:00
|
|
|
|
2023-01-10 04:18:56 +00:00
|
|
|
"github.com/rs/zerolog"
|
2022-10-13 17:03:36 +00:00
|
|
|
)
|
|
|
|
|
2023-02-12 05:50:19 +00:00
|
|
|
//go:embed templates/*.txt
|
|
|
|
var templates embed.FS
|
|
|
|
|
|
|
|
var requiredKeys = []string{"commands", "cmd-configs"}
|
2023-01-04 02:09:02 +00:00
|
|
|
|
2023-01-17 06:55:28 +00:00
|
|
|
var Sprintf = fmt.Sprintf
|
2023-01-04 02:09:02 +00:00
|
|
|
|
2023-01-20 08:42:52 +00:00
|
|
|
// 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.
|
2023-02-02 05:45:23 +00:00
|
|
|
// Dir can also be specified for local commands.
|
2023-07-02 02:46:54 +00:00
|
|
|
func (command *Command) RunCmd(log zerolog.Logger, backyConf *ConfigFile, opts *ConfigOpts) ([]string, error) {
|
2023-01-17 06:55:28 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
var (
|
2023-02-19 04:42:15 +00:00
|
|
|
outputArr []string
|
2023-02-02 05:45:23 +00:00
|
|
|
ArgsStr string
|
|
|
|
cmdOutBuf bytes.Buffer
|
|
|
|
cmdOutWriters io.Writer
|
|
|
|
|
|
|
|
envVars = environmentVars{
|
|
|
|
file: command.Env,
|
|
|
|
env: command.Environment,
|
|
|
|
}
|
|
|
|
)
|
2022-10-13 17:03:36 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
for _, v := range command.Args {
|
|
|
|
ArgsStr += fmt.Sprintf(" %s", v)
|
2023-01-17 06:55:28 +00:00
|
|
|
}
|
2022-10-13 17:03:36 +00:00
|
|
|
|
2023-01-17 06:55:28 +00:00
|
|
|
if command.Host != nil {
|
2023-05-12 05:42:14 +00:00
|
|
|
command.Type = strings.TrimSpace(command.Type)
|
|
|
|
|
|
|
|
if command.Type != "" {
|
|
|
|
log.Info().Str("Command", fmt.Sprintf("Running script %s on host %s", command.Cmd, *command.Host)).Send()
|
|
|
|
} else {
|
|
|
|
log.Info().Str("Command", fmt.Sprintf("Running command %s %s on host %s", command.Cmd, ArgsStr, *command.Host)).Send()
|
|
|
|
}
|
2023-02-02 05:45:23 +00:00
|
|
|
|
2023-03-10 22:01:02 +00:00
|
|
|
if command.RemoteHost.SshClient == nil {
|
2023-07-02 02:46:54 +00:00
|
|
|
err := command.RemoteHost.ConnectToSSHHost(opts, backyConf)
|
2023-03-10 22:01:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-27 05:20:11 +00:00
|
|
|
}
|
2023-02-12 05:50:19 +00:00
|
|
|
commandSession, err := command.RemoteHost.SshClient.NewSession()
|
2022-12-27 05:20:11 +00:00
|
|
|
if err != nil {
|
2023-02-19 04:42:15 +00:00
|
|
|
return nil, err
|
2022-12-27 05:20:11 +00:00
|
|
|
}
|
2023-01-17 06:55:28 +00:00
|
|
|
defer commandSession.Close()
|
2022-12-27 05:20:11 +00:00
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
injectEnvIntoSSH(envVars, commandSession, opts, log)
|
2022-12-27 05:20:11 +00:00
|
|
|
cmd := command.Cmd
|
2023-02-02 05:45:23 +00:00
|
|
|
for _, a := range command.Args {
|
2022-12-27 05:20:11 +00:00
|
|
|
cmd += " " + a
|
|
|
|
}
|
2023-02-02 05:45:23 +00:00
|
|
|
cmdOutWriters = io.MultiWriter(&cmdOutBuf)
|
2022-10-13 17:03:36 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
if IsCmdStdOutEnabled() {
|
|
|
|
cmdOutWriters = io.MultiWriter(os.Stdout, &cmdOutBuf)
|
|
|
|
}
|
|
|
|
|
|
|
|
commandSession.Stdout = cmdOutWriters
|
|
|
|
commandSession.Stderr = cmdOutWriters
|
2023-05-12 05:42:14 +00:00
|
|
|
if command.Type != "" {
|
|
|
|
|
|
|
|
if command.Type == "script" {
|
|
|
|
script := bytes.NewBufferString(cmd + "\n")
|
|
|
|
|
|
|
|
commandSession.Stdin = script
|
|
|
|
if err := commandSession.Shell(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := commandSession.Wait(); err != nil {
|
|
|
|
outScanner := bufio.NewScanner(&cmdOutBuf)
|
|
|
|
for outScanner.Scan() {
|
|
|
|
outMap := make(map[string]interface{})
|
|
|
|
outMap["cmd"] = cmd
|
|
|
|
outMap["output"] = outScanner.Text()
|
|
|
|
if str, ok := outMap["output"].(string); ok {
|
|
|
|
outputArr = append(outputArr, str)
|
|
|
|
}
|
|
|
|
log.Info().Fields(outMap).Send()
|
|
|
|
}
|
|
|
|
return outputArr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
outScanner := bufio.NewScanner(&cmdOutBuf)
|
|
|
|
for outScanner.Scan() {
|
|
|
|
outMap := make(map[string]interface{})
|
|
|
|
outMap["cmd"] = cmd
|
|
|
|
outMap["output"] = outScanner.Text()
|
|
|
|
if str, ok := outMap["output"].(string); ok {
|
|
|
|
outputArr = append(outputArr, str)
|
|
|
|
}
|
|
|
|
log.Info().Fields(outMap).Send()
|
|
|
|
}
|
|
|
|
return outputArr, nil
|
|
|
|
}
|
|
|
|
if command.Type == "scriptFile" {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
var dirErr error
|
|
|
|
command.Cmd, dirErr = resolveDir(command.Cmd)
|
|
|
|
if dirErr != nil {
|
|
|
|
return nil, dirErr
|
|
|
|
}
|
|
|
|
file, err := os.Open(command.Cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
_, err = io.Copy(&buffer, file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
script := &buffer
|
|
|
|
|
|
|
|
commandSession.Stdin = script
|
|
|
|
if err := commandSession.Shell(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := commandSession.Wait(); err != nil {
|
|
|
|
outScanner := bufio.NewScanner(&cmdOutBuf)
|
|
|
|
for outScanner.Scan() {
|
|
|
|
outMap := make(map[string]interface{})
|
|
|
|
outMap["cmd"] = cmd
|
|
|
|
outMap["output"] = outScanner.Text()
|
|
|
|
if str, ok := outMap["output"].(string); ok {
|
|
|
|
outputArr = append(outputArr, str)
|
|
|
|
}
|
|
|
|
log.Info().Fields(outMap).Send()
|
|
|
|
}
|
|
|
|
return outputArr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
outScanner := bufio.NewScanner(&cmdOutBuf)
|
|
|
|
for outScanner.Scan() {
|
|
|
|
outMap := make(map[string]interface{})
|
|
|
|
outMap["cmd"] = cmd
|
|
|
|
outMap["output"] = outScanner.Text()
|
|
|
|
if str, ok := outMap["output"].(string); ok {
|
|
|
|
outputArr = append(outputArr, str)
|
|
|
|
}
|
|
|
|
log.Info().Fields(outMap).Send()
|
|
|
|
}
|
|
|
|
return outputArr, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("command type not recognized")
|
|
|
|
}
|
|
|
|
|
2023-01-17 06:55:28 +00:00
|
|
|
err = commandSession.Run(cmd)
|
2023-02-02 05:45:23 +00:00
|
|
|
outScanner := bufio.NewScanner(&cmdOutBuf)
|
|
|
|
for outScanner.Scan() {
|
|
|
|
outMap := make(map[string]interface{})
|
|
|
|
outMap["cmd"] = cmd
|
|
|
|
outMap["output"] = outScanner.Text()
|
2023-02-19 04:42:15 +00:00
|
|
|
if str, ok := outMap["output"].(string); ok {
|
|
|
|
outputArr = append(outputArr, str)
|
|
|
|
}
|
2023-02-02 05:45:23 +00:00
|
|
|
log.Info().Fields(outMap).Send()
|
|
|
|
}
|
2023-01-04 05:57:19 +00:00
|
|
|
|
2022-12-27 05:20:11 +00:00
|
|
|
if err != nil {
|
2023-01-17 06:55:28 +00:00
|
|
|
log.Error().Err(fmt.Errorf("error when running cmd: %s: %w", command.Cmd, err)).Send()
|
2023-02-19 04:42:15 +00:00
|
|
|
return outputArr, err
|
2022-10-13 17:03:36 +00:00
|
|
|
}
|
2022-12-27 05:20:11 +00:00
|
|
|
} else {
|
2023-02-02 05:45:23 +00:00
|
|
|
|
2023-01-04 05:57:19 +00:00
|
|
|
var err error
|
|
|
|
if command.Shell != "" {
|
2023-02-19 04:42:15 +00:00
|
|
|
log.Info().Str("Command", fmt.Sprintf("Running command %s %s on local machine in %s", command.Cmd, ArgsStr, command.Shell)).Send()
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
ArgsStr = fmt.Sprintf("%s %s", command.Cmd, ArgsStr)
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
localCMD := exec.Command(command.Shell, "-c", ArgsStr)
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-01-10 04:18:56 +00:00
|
|
|
if command.Dir != nil {
|
|
|
|
localCMD.Dir = *command.Dir
|
|
|
|
}
|
2023-01-17 06:55:28 +00:00
|
|
|
injectEnvIntoLocalCMD(envVars, localCMD, log)
|
2023-02-02 05:45:23 +00:00
|
|
|
|
|
|
|
cmdOutWriters = io.MultiWriter(&cmdOutBuf)
|
|
|
|
|
|
|
|
if IsCmdStdOutEnabled() {
|
|
|
|
cmdOutWriters = io.MultiWriter(os.Stdout, &cmdOutBuf)
|
|
|
|
}
|
|
|
|
|
|
|
|
localCMD.Stdout = cmdOutWriters
|
|
|
|
localCMD.Stderr = cmdOutWriters
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-01-04 05:57:19 +00:00
|
|
|
err = localCMD.Run()
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
outScanner := bufio.NewScanner(&cmdOutBuf)
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
for outScanner.Scan() {
|
|
|
|
outMap := make(map[string]interface{})
|
|
|
|
outMap["cmd"] = command.Cmd
|
|
|
|
outMap["output"] = outScanner.Text()
|
2023-02-19 04:42:15 +00:00
|
|
|
if str, ok := outMap["output"].(string); ok {
|
|
|
|
outputArr = append(outputArr, str)
|
|
|
|
}
|
2023-02-02 05:45:23 +00:00
|
|
|
log.Info().Fields(outMap).Send()
|
|
|
|
}
|
2023-01-04 05:57:19 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-02-19 04:42:15 +00:00
|
|
|
log.Error().Err(fmt.Errorf("error when running cmd %s: %w", command.Cmd, err)).Send()
|
|
|
|
return outputArr, err
|
2023-01-04 05:57:19 +00:00
|
|
|
}
|
2023-02-19 04:42:15 +00:00
|
|
|
return outputArr, nil
|
2023-01-04 05:57:19 +00:00
|
|
|
}
|
2023-02-19 04:42:15 +00:00
|
|
|
log.Info().Str("Command", fmt.Sprintf("Running command %s %s on local machine", command.Cmd, ArgsStr)).Send()
|
2023-02-02 05:45:23 +00:00
|
|
|
|
|
|
|
localCMD := exec.Command(command.Cmd, command.Args...)
|
2023-01-10 04:18:56 +00:00
|
|
|
if command.Dir != nil {
|
|
|
|
localCMD.Dir = *command.Dir
|
|
|
|
}
|
2023-02-19 04:42:15 +00:00
|
|
|
// fmt.Printf("%v\n", envVars.env)
|
|
|
|
|
2023-01-17 06:55:28 +00:00
|
|
|
injectEnvIntoLocalCMD(envVars, localCMD, log)
|
2023-02-02 05:45:23 +00:00
|
|
|
cmdOutWriters = io.MultiWriter(&cmdOutBuf)
|
2023-02-19 04:42:15 +00:00
|
|
|
// fmt.Printf("%v\n", localCMD.Environ())
|
2023-02-02 05:45:23 +00:00
|
|
|
|
|
|
|
if IsCmdStdOutEnabled() {
|
|
|
|
cmdOutWriters = io.MultiWriter(os.Stdout, &cmdOutBuf)
|
|
|
|
}
|
|
|
|
localCMD.Stdout = cmdOutWriters
|
|
|
|
localCMD.Stderr = cmdOutWriters
|
2022-12-27 05:20:11 +00:00
|
|
|
err = localCMD.Run()
|
2023-02-02 05:45:23 +00:00
|
|
|
outScanner := bufio.NewScanner(&cmdOutBuf)
|
|
|
|
for outScanner.Scan() {
|
|
|
|
outMap := make(map[string]interface{})
|
|
|
|
outMap["cmd"] = command.Cmd
|
|
|
|
outMap["output"] = outScanner.Text()
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-19 04:42:15 +00:00
|
|
|
if str, ok := outMap["output"].(string); ok {
|
|
|
|
outputArr = append(outputArr, str)
|
|
|
|
}
|
2023-02-02 05:45:23 +00:00
|
|
|
log.Info().Fields(outMap).Send()
|
|
|
|
}
|
2022-10-13 17:03:36 +00:00
|
|
|
if err != nil {
|
2023-02-19 04:42:15 +00:00
|
|
|
log.Error().Err(fmt.Errorf("error when running cmd %s: %w", command.Cmd, err)).Send()
|
|
|
|
return outputArr, err
|
2022-10-13 17:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-19 04:42:15 +00:00
|
|
|
return outputArr, nil
|
2023-01-04 05:57:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, config *ConfigFile, results chan<- string, opts *ConfigOpts) {
|
2023-02-19 04:42:15 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
for list := range jobs {
|
|
|
|
fieldsMap := make(map[string]interface{})
|
|
|
|
fieldsMap["list"] = list.Name
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
cmdLog := config.Logger.Info()
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
var count int
|
2023-02-12 05:50:19 +00:00
|
|
|
var cmdsRan []string
|
2023-05-12 05:42:14 +00:00
|
|
|
var outStructArr []outStruct
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
for _, cmd := range list.Order {
|
2023-05-12 05:42:14 +00:00
|
|
|
currentCmd := config.Cmds[cmd].Cmd
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
fieldsMap["cmd"] = config.Cmds[cmd].Cmd
|
2023-01-04 05:57:19 +00:00
|
|
|
cmdToRun := config.Cmds[cmd]
|
2023-05-12 05:42:14 +00:00
|
|
|
cmdLog.Fields(fieldsMap).Send()
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
cmdLogger := config.Logger.With().
|
2023-03-10 22:01:02 +00:00
|
|
|
Str("backy-cmd", cmd).Str("Host", "local machine").
|
2023-02-02 05:45:23 +00:00
|
|
|
Logger()
|
2023-03-10 22:01:02 +00:00
|
|
|
|
|
|
|
if cmdToRun.Host != nil {
|
|
|
|
cmdLogger = config.Logger.With().
|
|
|
|
Str("backy-cmd", cmd).Str("Host", *cmdToRun.Host).
|
|
|
|
Logger()
|
|
|
|
}
|
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
outputArr, runOutErr := cmdToRun.RunCmd(cmdLogger, config, opts)
|
|
|
|
if list.NotifyConfig != nil {
|
|
|
|
|
|
|
|
if cmdToRun.GetOutput || list.GetOutput {
|
|
|
|
outputStruct := outStruct{
|
|
|
|
CmdName: cmd,
|
|
|
|
CmdExecuted: currentCmd,
|
|
|
|
Output: outputArr,
|
|
|
|
}
|
2023-05-12 05:42:14 +00:00
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
outStructArr = append(outStructArr, outputStruct)
|
2023-05-12 05:42:14 +00:00
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
}
|
2023-05-12 05:42:14 +00:00
|
|
|
}
|
2023-02-02 05:45:23 +00:00
|
|
|
count++
|
|
|
|
if runOutErr != nil {
|
2023-02-12 05:50:19 +00:00
|
|
|
var errMsg bytes.Buffer
|
2023-02-02 05:45:23 +00:00
|
|
|
if list.NotifyConfig != nil {
|
2023-02-12 05:50:19 +00:00
|
|
|
errStruct := make(map[string]interface{})
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-12 05:50:19 +00:00
|
|
|
errStruct["listName"] = list.Name
|
|
|
|
errStruct["Command"] = currentCmd
|
2023-05-12 05:42:14 +00:00
|
|
|
errStruct["Cmd"] = cmd
|
|
|
|
errStruct["Args"] = config.Cmds[cmd].Args
|
2023-02-12 05:50:19 +00:00
|
|
|
errStruct["Err"] = runOutErr
|
|
|
|
errStruct["CmdsRan"] = cmdsRan
|
2023-02-19 04:42:15 +00:00
|
|
|
errStruct["Output"] = outputArr
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-05-12 05:42:14 +00:00
|
|
|
errStruct["CmdOutput"] = outStructArr
|
|
|
|
|
2023-02-19 04:42:15 +00:00
|
|
|
tmpErr := msgTemps.err.Execute(&errMsg, errStruct)
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-12 05:50:19 +00:00
|
|
|
if tmpErr != nil {
|
|
|
|
config.Logger.Err(tmpErr).Send()
|
|
|
|
}
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-12 05:50:19 +00:00
|
|
|
notifySendErr := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s failed on command %s ", list.Name, cmd), errMsg.String())
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
if notifySendErr != nil {
|
|
|
|
config.Logger.Err(notifySendErr).Send()
|
|
|
|
}
|
|
|
|
}
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
config.Logger.Err(runOutErr).Send()
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
break
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if count == len(list.Order) {
|
2023-02-12 05:50:19 +00:00
|
|
|
cmdsRan = append(cmdsRan, cmd)
|
|
|
|
var successMsg bytes.Buffer
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
if list.NotifyConfig != nil {
|
2023-02-12 05:50:19 +00:00
|
|
|
successStruct := make(map[string]interface{})
|
2023-05-12 05:42:14 +00:00
|
|
|
|
2023-02-12 05:50:19 +00:00
|
|
|
successStruct["listName"] = list.Name
|
|
|
|
successStruct["CmdsRan"] = cmdsRan
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-05-12 05:42:14 +00:00
|
|
|
successStruct["CmdOutput"] = outStructArr
|
|
|
|
|
2023-02-19 04:42:15 +00:00
|
|
|
tmpErr := msgTemps.success.Execute(&successMsg, successStruct)
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-12 05:50:19 +00:00
|
|
|
if tmpErr != nil {
|
|
|
|
config.Logger.Err(tmpErr).Send()
|
|
|
|
break
|
|
|
|
}
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-12 05:50:19 +00:00
|
|
|
err := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s succeded", list.Name), successMsg.String())
|
2023-03-10 22:01:02 +00:00
|
|
|
|
2023-02-02 05:45:23 +00:00
|
|
|
if err != nil {
|
|
|
|
config.Logger.Err(err).Send()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-12 05:50:19 +00:00
|
|
|
cmdsRan = append(cmdsRan, cmd)
|
2023-02-02 05:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-04 05:57:19 +00:00
|
|
|
}
|
2023-02-02 05:45:23 +00:00
|
|
|
|
2023-01-17 06:55:28 +00:00
|
|
|
results <- "done"
|
2022-10-13 17:03:36 +00:00
|
|
|
}
|
2023-02-12 05:50:19 +00:00
|
|
|
|
2022-10-13 17:03:36 +00:00
|
|
|
}
|
2022-12-27 05:20:11 +00:00
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
// RunListConfig runs a command list from the ConfigFile.
|
|
|
|
func (config *ConfigFile) RunListConfig(cron string, opts *ConfigOpts) {
|
2023-02-19 04:42:15 +00:00
|
|
|
mTemps := &msgTemplates{
|
|
|
|
err: template.Must(template.New("error.txt").ParseFS(templates, "templates/error.txt")),
|
|
|
|
success: template.Must(template.New("success.txt").ParseFS(templates, "templates/success.txt")),
|
|
|
|
}
|
2023-01-17 06:55:28 +00:00
|
|
|
configListsLen := len(config.CmdConfigLists)
|
2023-02-02 05:45:23 +00:00
|
|
|
listChan := make(chan *CmdList, configListsLen)
|
2023-01-17 06:55:28 +00:00
|
|
|
results := make(chan string)
|
2023-01-10 04:18:56 +00:00
|
|
|
|
2023-01-17 06:55:28 +00:00
|
|
|
// This starts up 3 workers, initially blocked
|
|
|
|
// because there are no jobs yet.
|
2023-02-12 05:50:19 +00:00
|
|
|
for w := 1; w <= configListsLen; w++ {
|
2023-05-12 05:42:14 +00:00
|
|
|
go cmdListWorker(mTemps, listChan, config, results, opts)
|
2023-01-10 04:18:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 06:55:28 +00:00
|
|
|
// Here we send 5 `jobs` and then `close` that
|
|
|
|
// channel to indicate that's all the work we have.
|
|
|
|
// configChan <- config.Cmds
|
2023-02-12 05:50:19 +00:00
|
|
|
for listName, cmdConfig := range config.CmdConfigLists {
|
|
|
|
if cmdConfig.Name == "" {
|
|
|
|
cmdConfig.Name = listName
|
|
|
|
}
|
2023-02-02 05:45:23 +00:00
|
|
|
if cron != "" {
|
|
|
|
if cron == cmdConfig.Cron {
|
|
|
|
listChan <- cmdConfig
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
listChan <- cmdConfig
|
|
|
|
}
|
2023-01-10 04:18:56 +00:00
|
|
|
}
|
2023-02-02 05:45:23 +00:00
|
|
|
close(listChan)
|
2023-01-10 04:18:56 +00:00
|
|
|
|
2023-01-17 06:55:28 +00:00
|
|
|
for a := 1; a <= configListsLen; a++ {
|
|
|
|
<-results
|
2023-01-04 02:09:02 +00:00
|
|
|
}
|
2023-01-17 06:55:28 +00:00
|
|
|
|
2023-03-10 22:01:02 +00:00
|
|
|
config.closeHostConnections()
|
2023-01-04 02:09:02 +00:00
|
|
|
}
|
2023-01-04 05:57:19 +00:00
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
func (config *ConfigFile) ExecuteCmds(opts *ConfigOpts) {
|
2023-02-19 04:42:15 +00:00
|
|
|
for _, cmd := range opts.executeCmds {
|
|
|
|
cmdToRun := config.Cmds[cmd]
|
|
|
|
cmdLogger := config.Logger.With().
|
|
|
|
Str("backy-cmd", cmd).
|
|
|
|
Logger()
|
2023-07-02 02:46:54 +00:00
|
|
|
_, runErr := cmdToRun.RunCmd(cmdLogger, config, opts)
|
2023-02-12 05:50:19 +00:00
|
|
|
if runErr != nil {
|
|
|
|
config.Logger.Err(runErr).Send()
|
|
|
|
}
|
2023-01-20 08:42:52 +00:00
|
|
|
}
|
2023-02-19 04:42:15 +00:00
|
|
|
|
2023-03-10 22:01:02 +00:00
|
|
|
config.closeHostConnections()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
func (c *ConfigFile) closeHostConnections() {
|
2023-03-10 22:01:02 +00:00
|
|
|
for _, host := range c.Hosts {
|
2023-03-14 01:25:27 +00:00
|
|
|
if host.isProxyHost {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-10 22:01:02 +00:00
|
|
|
if host.SshClient != nil {
|
|
|
|
if _, err := host.SshClient.NewSession(); err == nil {
|
|
|
|
c.Logger.Info().Msgf("Closing host connection %s", host.HostName)
|
|
|
|
host.SshClient.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, proxyHost := range host.ProxyHost {
|
2023-03-14 01:25:27 +00:00
|
|
|
if proxyHost.isProxyHost {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-10 22:01:02 +00:00
|
|
|
if proxyHost.SshClient != nil {
|
|
|
|
if _, err := host.SshClient.NewSession(); err == nil {
|
|
|
|
c.Logger.Info().Msgf("Closing connection to proxy host %s", host.HostName)
|
|
|
|
host.SshClient.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 01:25:27 +00:00
|
|
|
for _, host := range c.Hosts {
|
|
|
|
if host.SshClient != nil {
|
|
|
|
if _, err := host.SshClient.NewSession(); err == nil {
|
|
|
|
c.Logger.Info().Msgf("Closing proxy host connection %s", host.HostName)
|
|
|
|
host.SshClient.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-20 08:42:52 +00:00
|
|
|
}
|