v0.10.0
This commit is contained in:
@ -144,6 +144,7 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
if command.Shell != "" {
|
||||
cmdCtxLogger.Info().Str("Command", fmt.Sprintf("Running command %s on local machine in %s", command.Name, command.Shell)).Send()
|
||||
|
||||
@ -182,20 +183,7 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
|
||||
|
||||
err = localCMD.Run()
|
||||
|
||||
outScanner := bufio.NewScanner(&cmdOutBuf)
|
||||
|
||||
for outScanner.Scan() {
|
||||
outMap := make(map[string]interface{})
|
||||
outMap["cmd"] = command.Cmd
|
||||
outMap["output"] = outScanner.Text()
|
||||
|
||||
if str, ok := outMap["output"].(string); ok {
|
||||
outputArr = append(outputArr, str)
|
||||
}
|
||||
// if command.GetOutput {
|
||||
cmdCtxLogger.Info().Fields(outMap).Send()
|
||||
// }
|
||||
}
|
||||
outputArr = logCommandOutput(command, cmdOutBuf, cmdCtxLogger, outputArr)
|
||||
if err != nil {
|
||||
cmdCtxLogger.Error().Err(fmt.Errorf("error when running cmd %s: %w", command.Name, err)).Send()
|
||||
return outputArr, err
|
||||
@ -240,7 +228,7 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<-
|
||||
}
|
||||
|
||||
// Collect output if required
|
||||
if list.GetOutput || cmdToRun.GetOutput {
|
||||
if list.GetOutput || cmdToRun.GetOutputInList {
|
||||
outStructArr = append(outStructArr, outStruct{
|
||||
CmdName: currentCmd,
|
||||
CmdExecuted: currentCmd,
|
||||
@ -249,17 +237,14 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<-
|
||||
}
|
||||
}
|
||||
|
||||
// Notify success if no errors occurred
|
||||
if !hasError && list.NotifyConfig != nil && (list.NotifyOnSuccess || list.GetOutput) {
|
||||
notifySuccess(cmdLogger, msgTemps, list, cmdsRan, outStructArr)
|
||||
}
|
||||
|
||||
// Execute success and final hooks for all commands
|
||||
for _, cmd := range list.Order {
|
||||
cmdToRun := opts.Cmds[cmd]
|
||||
|
||||
// Execute success hooks if the command succeeded
|
||||
if !hasError || cmdsRanContains(cmd, cmdsRan) {
|
||||
if !hasError {
|
||||
cmdToRun.ExecuteHooks("success", opts)
|
||||
}
|
||||
|
||||
@ -276,17 +261,6 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<-
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to check if a command is in the list of executed commands
|
||||
func cmdsRanContains(cmd string, cmdsRan []string) bool {
|
||||
for _, c := range cmdsRan {
|
||||
if c == cmd {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Helper to notify errors
|
||||
func notifyError(logger zerolog.Logger, templates *msgTemplates, list *CmdList, cmdsRan []string, outStructArr []outStruct, err error, cmd *Command) {
|
||||
errStruct := map[string]interface{}{
|
||||
"listName": list.Name,
|
||||
@ -355,7 +329,6 @@ func (opts *ConfigOpts) RunListConfig(cron string) {
|
||||
result := <-results
|
||||
opts.Logger.Debug().Msgf("Processing result for list %s, command %s", result.ListName, result.CmdName)
|
||||
|
||||
// Process final hooks for the list (already handled in worker)
|
||||
}
|
||||
opts.closeHostConnections()
|
||||
}
|
||||
@ -367,10 +340,8 @@ func (opts *ConfigOpts) ExecuteCmds() {
|
||||
_, runErr := cmdToRun.RunCmd(cmdLogger, opts)
|
||||
if runErr != nil {
|
||||
opts.Logger.Err(runErr).Send()
|
||||
|
||||
cmdToRun.ExecuteHooks("error", opts)
|
||||
} else {
|
||||
|
||||
cmdToRun.ExecuteHooks("success", opts)
|
||||
}
|
||||
|
||||
@ -378,7 +349,6 @@ func (opts *ConfigOpts) ExecuteCmds() {
|
||||
}
|
||||
|
||||
opts.closeHostConnections()
|
||||
|
||||
}
|
||||
|
||||
func (c *ConfigOpts) closeHostConnections() {
|
||||
@ -425,8 +395,9 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
||||
case "error":
|
||||
for _, v := range cmd.Hooks.Error {
|
||||
errCmd := opts.Cmds[v]
|
||||
opts.Logger.Info().Msgf("Running error hook command %s", v)
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", v).
|
||||
Str("backy-cmd", v).Str("hookType", "error").
|
||||
Logger()
|
||||
errCmd.RunCmd(cmdLogger, opts)
|
||||
}
|
||||
@ -434,16 +405,18 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
||||
case "success":
|
||||
for _, v := range cmd.Hooks.Success {
|
||||
successCmd := opts.Cmds[v]
|
||||
opts.Logger.Info().Msgf("Running success hook command %s", v)
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", v).
|
||||
Str("backy-cmd", v).Str("hookType", "success").
|
||||
Logger()
|
||||
successCmd.RunCmd(cmdLogger, opts)
|
||||
}
|
||||
case "final":
|
||||
for _, v := range cmd.Hooks.Final {
|
||||
finalCmd := opts.Cmds[v]
|
||||
opts.Logger.Info().Msgf("Running final hook command %s", v)
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", v).
|
||||
Str("backy-cmd", v).Str("hookType", "final").
|
||||
Logger()
|
||||
finalCmd.RunCmd(cmdLogger, opts)
|
||||
}
|
||||
@ -481,6 +454,25 @@ func (opts *ConfigOpts) ExecCmdsSSH(cmdList []string, hostsList []string) {
|
||||
}
|
||||
}
|
||||
|
||||
func logCommandOutput(command *Command, cmdOutBuf bytes.Buffer, cmdCtxLogger zerolog.Logger, outputArr []string) []string {
|
||||
|
||||
outScanner := bufio.NewScanner(&cmdOutBuf)
|
||||
|
||||
for outScanner.Scan() {
|
||||
outMap := make(map[string]interface{})
|
||||
outMap["cmd"] = command.Name
|
||||
outMap["output"] = outScanner.Text()
|
||||
|
||||
if str, ok := outMap["output"].(string); ok {
|
||||
outputArr = append(outputArr, str)
|
||||
}
|
||||
if command.OutputToLog {
|
||||
cmdCtxLogger.Info().Fields(outMap).Send()
|
||||
}
|
||||
}
|
||||
return outputArr
|
||||
}
|
||||
|
||||
// func executeUserCommands() []string {
|
||||
|
||||
// }
|
||||
|
@ -22,10 +22,13 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
const macroStart string = "%{"
|
||||
const macroEnd string = "}%"
|
||||
const envMacroStart string = "%{env:"
|
||||
const vaultMacroStart string = "%{vault:"
|
||||
const (
|
||||
externDirectiveStart string = "%{"
|
||||
externDirectiveEnd string = "}%"
|
||||
externFileDirectiveStart string = "%{file:"
|
||||
envExternDirectiveStart string = "%{env:"
|
||||
vaultExternDirectiveStart string = "%{vault:"
|
||||
)
|
||||
|
||||
func (opts *ConfigOpts) InitConfig() {
|
||||
var err error
|
||||
@ -95,41 +98,6 @@ func (opts *ConfigOpts) InitConfig() {
|
||||
opts.koanf = backyKoanf
|
||||
}
|
||||
|
||||
func loadConfigFile(fetcher remotefetcher.RemoteFetcher, filePath string, k *koanf.Koanf, opts *ConfigOpts) {
|
||||
data, err := fetcher.Fetch(filePath)
|
||||
if err != nil {
|
||||
logging.ExitWithMSG(fmt.Sprintf("Could not fetch config file %s: %v", filePath, err), 1, nil)
|
||||
}
|
||||
|
||||
if err := k.Load(rawbytes.Provider(data), yaml.Parser()); err != nil {
|
||||
logging.ExitWithMSG(fmt.Sprintf("error loading config: %v", err), 1, &opts.Logger)
|
||||
}
|
||||
}
|
||||
|
||||
func loadDefaultConfigFiles(fetcher remotefetcher.RemoteFetcher, configFiles []string, k *koanf.Koanf, opts *ConfigOpts) {
|
||||
cFileFailures := 0
|
||||
for _, c := range configFiles {
|
||||
opts.ConfigFilePath = c
|
||||
data, err := fetcher.Fetch(c)
|
||||
if err != nil {
|
||||
cFileFailures++
|
||||
continue
|
||||
}
|
||||
|
||||
if data != nil {
|
||||
if err := k.Load(rawbytes.Provider(data), yaml.Parser()); err == nil {
|
||||
break
|
||||
} else {
|
||||
logging.ExitWithMSG(fmt.Sprintf("error loading config from file %s: %v", c, err), 1, &opts.Logger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cFileFailures == len(configFiles) {
|
||||
logging.ExitWithMSG("Could not find any valid local config file", 1, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func (opts *ConfigOpts) ReadConfig() *ConfigOpts {
|
||||
setTerminalEnv()
|
||||
|
||||
@ -148,7 +116,7 @@ func (opts *ConfigOpts) ReadConfig() *ConfigOpts {
|
||||
|
||||
CheckConfigValues(backyKoanf, opts.ConfigFilePath)
|
||||
|
||||
validateCommands(backyKoanf, opts)
|
||||
validateExecCommandsFromCLI(backyKoanf, opts)
|
||||
|
||||
setLoggingOptions(backyKoanf, opts)
|
||||
|
||||
@ -192,6 +160,41 @@ func (opts *ConfigOpts) ReadConfig() *ConfigOpts {
|
||||
return opts
|
||||
}
|
||||
|
||||
func loadConfigFile(fetcher remotefetcher.RemoteFetcher, filePath string, k *koanf.Koanf, opts *ConfigOpts) {
|
||||
data, err := fetcher.Fetch(filePath)
|
||||
if err != nil {
|
||||
logging.ExitWithMSG(generateFileFetchErrorString(filePath, "config", err), 1, nil)
|
||||
}
|
||||
|
||||
if err := k.Load(rawbytes.Provider(data), yaml.Parser()); err != nil {
|
||||
logging.ExitWithMSG(fmt.Sprintf("error loading config: %v", err), 1, &opts.Logger)
|
||||
}
|
||||
}
|
||||
|
||||
func loadDefaultConfigFiles(fetcher remotefetcher.RemoteFetcher, configFiles []string, k *koanf.Koanf, opts *ConfigOpts) {
|
||||
cFileFailures := 0
|
||||
for _, c := range configFiles {
|
||||
opts.ConfigFilePath = c
|
||||
data, err := fetcher.Fetch(c)
|
||||
if err != nil {
|
||||
cFileFailures++
|
||||
continue
|
||||
}
|
||||
|
||||
if data != nil {
|
||||
if err := k.Load(rawbytes.Provider(data), yaml.Parser()); err == nil {
|
||||
break
|
||||
} else {
|
||||
logging.ExitWithMSG(fmt.Sprintf("error loading config from file %s: %v", c, err), 1, &opts.Logger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cFileFailures == len(configFiles) {
|
||||
logging.ExitWithMSG("Could not find any valid local config file", 1, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func setTerminalEnv() {
|
||||
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
|
||||
os.Setenv("BACKY_TERM", "enabled")
|
||||
@ -200,7 +203,7 @@ func setTerminalEnv() {
|
||||
}
|
||||
}
|
||||
|
||||
func validateCommands(k *koanf.Koanf, opts *ConfigOpts) {
|
||||
func validateExecCommandsFromCLI(k *koanf.Koanf, opts *ConfigOpts) {
|
||||
for _, c := range opts.executeCmds {
|
||||
if !k.Exists(getCmdFromConfig(c)) {
|
||||
logging.ExitWithMSG(fmt.Sprintf("command %s is not in config file %s", c, opts.ConfigFilePath), 1, nil)
|
||||
@ -238,7 +241,7 @@ func setupLogger(opts *ConfigOpts) zerolog.Logger {
|
||||
|
||||
func unmarshalConfig(k *koanf.Koanf, key string, target interface{}, log zerolog.Logger) {
|
||||
if err := k.UnmarshalWithConf(key, target, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
|
||||
logging.ExitWithMSG(fmt.Sprintf("error unmarshalling key %s into struct: %v", key, err), 1, &log)
|
||||
logging.ExitWithMSG(fmt.Sprintf("error unmarshaling key %s into struct: %v", key, err), 1, &log)
|
||||
}
|
||||
}
|
||||
|
||||
@ -304,9 +307,10 @@ func loadCommandLists(opts *ConfigOpts, backyKoanf *koanf.Koanf) {
|
||||
}
|
||||
|
||||
if backyKoanf.Exists("cmdLists") {
|
||||
unmarshalConfig(backyKoanf, "cmdLists", &opts.CmdConfigLists, opts.Logger)
|
||||
if backyKoanf.Exists("cmdLists.file") {
|
||||
loadCmdListsFile(backyKoanf, listsConfig, opts)
|
||||
} else {
|
||||
unmarshalConfig(backyKoanf, "cmdLists", &opts.CmdConfigLists, opts.Logger)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -366,7 +370,7 @@ func loadCmdListsFile(backyKoanf *koanf.Koanf, listsConfig *koanf.Koanf, opts *C
|
||||
|
||||
data, err := fetcher.Fetch(opts.CmdListFile)
|
||||
if err != nil {
|
||||
logging.ExitWithMSG(fmt.Sprintf("Could not fetch config file %s: %v", opts.CmdListFile, err), 1, nil)
|
||||
logging.ExitWithMSG(generateFileFetchErrorString(opts.CmdListFile, "list config", err), 1, nil)
|
||||
}
|
||||
|
||||
if err := listsConfig.Load(rawbytes.Provider(data), yaml.Parser()); err != nil {
|
||||
@ -378,6 +382,10 @@ func loadCmdListsFile(backyKoanf *koanf.Koanf, listsConfig *koanf.Koanf, opts *C
|
||||
opts.Logger.Info().Str("using lists config file", opts.CmdListFile).Send()
|
||||
}
|
||||
|
||||
func generateFileFetchErrorString(file, fileType string, err error) string {
|
||||
return fmt.Sprintf("Could not fetch %s file %s: %v", file, fileType, err)
|
||||
}
|
||||
|
||||
func validateCommandLists(opts *ConfigOpts) {
|
||||
var cmdNotFoundSliceErr []error
|
||||
for cmdListName, cmdList := range opts.CmdConfigLists {
|
||||
@ -455,7 +463,7 @@ func (opts *ConfigOpts) setupVault() error {
|
||||
|
||||
unmarshalErr := opts.koanf.UnmarshalWithConf("vault.keys", &opts.VaultKeys, koanf.UnmarshalConf{Tag: "yaml"})
|
||||
if unmarshalErr != nil {
|
||||
logging.ExitWithMSG(fmt.Sprintf("error unmarshalling vault.keys into struct: %v", unmarshalErr), 1, &opts.Logger)
|
||||
logging.ExitWithMSG(fmt.Sprintf("error unmarshaling vault.keys into struct: %v", unmarshalErr), 1, &opts.Logger)
|
||||
}
|
||||
|
||||
opts.vaultClient = client
|
||||
@ -565,6 +573,10 @@ func processCmds(opts *ConfigOpts) error {
|
||||
cmd.RemoteHost.HostName = host.HostName
|
||||
}
|
||||
} else {
|
||||
opts.Logger.Info().Msgf("adding host %s to host list", *cmd.Host)
|
||||
if opts.Hosts == nil {
|
||||
opts.Hosts = make(map[string]*Host)
|
||||
}
|
||||
opts.Hosts[*cmd.Host] = &Host{Host: *cmd.Host}
|
||||
cmd.RemoteHost = &Host{Host: *cmd.Host}
|
||||
}
|
||||
@ -577,10 +589,11 @@ func processCmds(opts *ConfigOpts) error {
|
||||
return err
|
||||
}
|
||||
cmd.Dir = &cmdDir
|
||||
} else {
|
||||
cmd.Dir = &opts.ConfigDir
|
||||
}
|
||||
}
|
||||
|
||||
// Parse package commands
|
||||
if cmd.Type == PackageCT {
|
||||
if cmd.PackageManager == "" {
|
||||
return fmt.Errorf("package manager is required for package command %s", cmd.PackageName)
|
||||
@ -612,19 +625,29 @@ func processCmds(opts *ConfigOpts) error {
|
||||
return fmt.Errorf("username is required for user command %s", cmd.Name)
|
||||
}
|
||||
|
||||
detectOSType(cmd, opts)
|
||||
var err error
|
||||
err := detectOSType(cmd, opts)
|
||||
if err != nil {
|
||||
opts.Logger.Info().Err(err).Str("command", cmdName).Send()
|
||||
}
|
||||
|
||||
// Validate the operation
|
||||
switch cmd.UserOperation {
|
||||
case "add", "remove", "modify", "checkIfExists", "delete", "password":
|
||||
cmd.userMan, err = usermanager.NewUserManager(cmd.OS)
|
||||
if cmd.UserOperation == "password" {
|
||||
cmd.UserPassword = expandExternalConfigDirectives(cmd.UserPassword, opts)
|
||||
}
|
||||
if cmd.Host != nil {
|
||||
host, ok := opts.Hosts[*cmd.Host]
|
||||
if ok {
|
||||
cmd.userMan, err = usermanager.NewUserManager(host.OS)
|
||||
}
|
||||
}
|
||||
for indx, key := range cmd.UserSshPubKeys {
|
||||
opts.Logger.Debug().Msg("adding SSH Keys")
|
||||
key = expandExternalConfigDirectives(key, opts)
|
||||
cmd.UserSshPubKeys[indx] = key
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -656,14 +679,8 @@ func processCmds(opts *ConfigOpts) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// processHooks evaluates if hooks are valid Commands
|
||||
//
|
||||
// The cmd.hookRefs[hookType] is created with any hooks found.
|
||||
//
|
||||
// Returns an error, if any, if the hook command is not found
|
||||
func processHooks(cmd *Command, hooks []string, opts *ConfigOpts, hookType string) error {
|
||||
|
||||
// initialize hook type
|
||||
var hookCmdFound bool
|
||||
cmd.hookRefs = map[string]map[string]*Command{}
|
||||
cmd.hookRefs[hookType] = map[string]*Command{}
|
||||
@ -691,11 +708,14 @@ func processHooks(cmd *Command, hooks []string, opts *ConfigOpts, hookType strin
|
||||
|
||||
func detectOSType(cmd *Command, opts *ConfigOpts) error {
|
||||
if cmd.Host == nil {
|
||||
if runtime.GOOS == "linux" { // also can be specified to FreeBSD
|
||||
if runtime.GOOS == "linux" {
|
||||
cmd.OS = "linux"
|
||||
opts.Logger.Info().Msg("Unix/Linux type OS detected")
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("using an os that is not yet supported for user commands")
|
||||
}
|
||||
|
||||
host, ok := opts.Hosts[*cmd.Host]
|
||||
if ok {
|
||||
if host.OS != "" {
|
||||
|
@ -71,9 +71,8 @@ func (opts *ConfigOpts) SetupNotify() {
|
||||
opts.Logger.Info().Str("list", confName).Err(fmt.Errorf("error: configuring matrix id %s failed during setup: %w", id, mtrxErr))
|
||||
continue
|
||||
}
|
||||
// append the services
|
||||
services = append(services, mtrxConf)
|
||||
// service is not recognized
|
||||
|
||||
default:
|
||||
opts.Logger.Info().Err(fmt.Errorf("id %s not found", id)).Str("list", confName).Send()
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
|
||||
"github.com/kevinburke/ssh_config"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pkg/sftp"
|
||||
"github.com/rs/zerolog"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
@ -569,6 +570,57 @@ func (command *Command) RunCmdSSH(cmdCtxLogger zerolog.Logger, opts *ConfigOpts)
|
||||
if err := commandSession.Run(ArgsStr); err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error running command: %w", err)
|
||||
}
|
||||
|
||||
if command.Type == UserCT && command.UserOperation == "add" {
|
||||
if command.UserSshPubKeys != nil {
|
||||
var (
|
||||
f *sftp.File
|
||||
err error
|
||||
userHome []byte
|
||||
client *sftp.Client
|
||||
)
|
||||
|
||||
cmdCtxLogger.Info().Msg("adding SSH Keys")
|
||||
|
||||
commandSession, _ = command.RemoteHost.createSSHSession(opts)
|
||||
userHome, err = commandSession.CombinedOutput(fmt.Sprintf("grep \"%s\" /etc/passwd | cut -d: -f6", command.Username))
|
||||
if err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error finding user home from /etc/passwd: %v", err)
|
||||
}
|
||||
|
||||
command.UserHome = strings.TrimSpace(string(userHome))
|
||||
userSshDir := fmt.Sprintf("%s/.ssh", command.UserHome)
|
||||
client, err = sftp.NewClient(command.RemoteHost.SshClient)
|
||||
if err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating sftp client: %v", err)
|
||||
}
|
||||
|
||||
client.MkdirAll(userSshDir)
|
||||
_, err = client.Create(fmt.Sprintf("%s/authorized_keys", userSshDir))
|
||||
if err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error opening file %s/authorized_keys: %v", userSshDir, err)
|
||||
}
|
||||
f, err = client.OpenFile(fmt.Sprintf("%s/authorized_keys", userSshDir), os.O_APPEND|os.O_CREATE|os.O_WRONLY)
|
||||
if err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error opening file %s/authorized_keys: %v", userSshDir, err)
|
||||
}
|
||||
defer f.Close()
|
||||
for _, k := range command.UserSshPubKeys {
|
||||
buf := bytes.NewBufferString(k)
|
||||
cmdCtxLogger.Info().Str("key", k).Msg("adding SSH key")
|
||||
if _, err := f.ReadFrom(buf); err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error adding to authorized keys: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
commandSession, _ = command.RemoteHost.createSSHSession(opts)
|
||||
_, err = commandSession.CombinedOutput(fmt.Sprintf("chown -R %s:%s %s", command.Username, command.Username, userHome))
|
||||
if err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), err
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), nil
|
||||
@ -622,7 +674,7 @@ func (command *Command) runScript(session *ssh.Session, cmdCtxLogger zerolog.Log
|
||||
return collectOutput(outputBuf, command.Name, cmdCtxLogger, true), fmt.Errorf("error waiting for shell: %w", err)
|
||||
}
|
||||
|
||||
return collectOutput(outputBuf, command.Name, cmdCtxLogger, command.GetOutput), nil
|
||||
return collectOutput(outputBuf, command.Name, cmdCtxLogger, command.OutputToLog), nil
|
||||
}
|
||||
|
||||
// runScriptFile handles the execution of script files.
|
||||
|
@ -51,44 +51,30 @@ type (
|
||||
Command struct {
|
||||
Name string `yaml:"name,omitempty"`
|
||||
|
||||
// command to run
|
||||
Cmd string `yaml:"cmd"`
|
||||
|
||||
// See CommandType enum further down the page for acceptable values
|
||||
Type CommandType `yaml:"type,omitempty"`
|
||||
|
||||
// host on which to run cmd
|
||||
Host *string `yaml:"host,omitempty"`
|
||||
|
||||
// Hooks are for running commands on certain events
|
||||
Hooks *Hooks `yaml:"hooks,omitempty"`
|
||||
|
||||
// hook refs are internal references of commands for each hook type
|
||||
hookRefs map[string]map[string]*Command
|
||||
|
||||
// Shell specifies which shell to run the command in, if any.
|
||||
Shell string `yaml:"shell,omitempty"`
|
||||
|
||||
RemoteHost *Host `yaml:"-"`
|
||||
|
||||
// Args is an array that holds the arguments to cmd
|
||||
Args []string `yaml:"args,omitempty"`
|
||||
|
||||
/*
|
||||
Dir specifies a directory in which to run the command.
|
||||
*/
|
||||
Dir *string `yaml:"dir,omitempty"`
|
||||
|
||||
// Env points to a file containing env variables to be used with the command
|
||||
Env string `yaml:"env,omitempty"`
|
||||
|
||||
// Environment holds env variables to be used with the command
|
||||
Environment []string `yaml:"environment,omitempty"`
|
||||
|
||||
// Output determines if output is requested.
|
||||
//
|
||||
// Only for when command is in a list.
|
||||
GetOutput bool `yaml:"getOutput,omitempty"`
|
||||
GetOutputInList bool `yaml:"getOutputInList,omitempty"`
|
||||
|
||||
ScriptEnvFile string `yaml:"scriptEnvFile"`
|
||||
|
||||
@ -102,10 +88,8 @@ type (
|
||||
|
||||
PackageName string `yaml:"packageName,omitempty"`
|
||||
|
||||
// Version specifies the desired version for package execution
|
||||
PackageVersion string `yaml:"packageVersion,omitempty"`
|
||||
|
||||
// PackageOperation specifies the action for package-related commands (e.g., "install" or "remove")
|
||||
PackageOperation PackageOperation `yaml:"packageOperation,omitempty"`
|
||||
|
||||
pkgMan pkgman.PackageManager
|
||||
@ -113,42 +97,35 @@ type (
|
||||
packageCmdSet bool
|
||||
// END PACKAGE COMMAND FIELDS
|
||||
|
||||
// RemoteSource specifies a URL to fetch the command or configuration remotely
|
||||
RemoteSource string `yaml:"remoteSource,omitempty"`
|
||||
|
||||
// FetchBeforeExecution determines if the remoteSource should be fetched before running
|
||||
FetchBeforeExecution bool `yaml:"fetchBeforeExecution,omitempty"`
|
||||
|
||||
Fetcher remotefetcher.RemoteFetcher
|
||||
|
||||
// BEGIN USER COMMAND FIELDS
|
||||
|
||||
// Username specifies the username for user creation or related operations
|
||||
Username string `yaml:"userName,omitempty"`
|
||||
|
||||
UserID string `yaml:"userID,omitempty"`
|
||||
|
||||
// UserGroups specifies the groups to add the user to
|
||||
UserGroups []string `yaml:"userGroups,omitempty"`
|
||||
|
||||
// UserHome specifies the home directory for the user
|
||||
UserHome string `yaml:"userHome,omitempty"`
|
||||
|
||||
// UserShell specifies the shell for the user
|
||||
UserShell string `yaml:"userShell,omitempty"`
|
||||
|
||||
// SystemUser specifies whether the user is a system account
|
||||
SystemUser bool `yaml:"systemUser,omitempty"`
|
||||
|
||||
// UserPassword specifies the password for the user (can be file: or plain text)
|
||||
UserPassword string `yaml:"userPassword,omitempty"`
|
||||
|
||||
UserSshPubKeys []string `yaml:"userSshPubKeys,omitempty"`
|
||||
|
||||
userMan usermanager.UserManager
|
||||
|
||||
// OS for the command, only used when type is user
|
||||
OS string `yaml:"OS,omitempty"`
|
||||
|
||||
// UserOperation specifies the action for user-related commands (e.g., "create" or "remove")
|
||||
UserOperation string `yaml:"userOperation,omitempty"`
|
||||
|
||||
userCmdSet bool
|
||||
|
@ -247,7 +247,6 @@ func (opts *ConfigOpts) loadEnv() {
|
||||
opts.backyEnv = backyEnv
|
||||
}
|
||||
|
||||
// expandEnvVars expands environment variables with the env used in the config
|
||||
func expandEnvVars(backyEnv map[string]string, envVars []string) {
|
||||
|
||||
env := func(name string) string {
|
||||
@ -261,10 +260,10 @@ func expandEnvVars(backyEnv map[string]string, envVars []string) {
|
||||
|
||||
// parse env variables using new macros
|
||||
for indx, v := range envVars {
|
||||
if strings.HasPrefix(v, macroStart) && strings.HasSuffix(v, macroEnd) {
|
||||
if strings.HasPrefix(v, envMacroStart) {
|
||||
v = strings.TrimPrefix(v, envMacroStart)
|
||||
v = strings.TrimRight(v, macroEnd)
|
||||
if strings.HasPrefix(v, externDirectiveStart) && strings.HasSuffix(v, externDirectiveEnd) {
|
||||
if strings.HasPrefix(v, envExternDirectiveStart) {
|
||||
v = strings.TrimPrefix(v, envExternDirectiveStart)
|
||||
v = strings.TrimRight(v, externDirectiveEnd)
|
||||
out, _ := shell.Expand(v, env)
|
||||
envVars[indx] = out
|
||||
}
|
||||
@ -324,7 +323,7 @@ func parsePackageVersion(output string, cmdCtxLogger zerolog.Logger, command *Co
|
||||
// println(output)
|
||||
if err != nil {
|
||||
cmdCtxLogger.Error().Err(err).Str("package", command.PackageName).Msg("Error parsing package version output")
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.GetOutput), err
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), err
|
||||
}
|
||||
|
||||
cmdCtxLogger.Info().
|
||||
@ -349,3 +348,39 @@ func parsePackageVersion(output string, cmdCtxLogger zerolog.Logger, command *Co
|
||||
}
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, false), err
|
||||
}
|
||||
|
||||
func expandExternalConfigDirectives(key string, opts *ConfigOpts) string {
|
||||
if !(strings.HasPrefix(key, externDirectiveStart) && strings.HasSuffix(key, externDirectiveEnd)) {
|
||||
return key
|
||||
}
|
||||
opts.Logger.Info().Str("expanding external key", key).Send()
|
||||
if strings.HasPrefix(key, envExternDirectiveStart) {
|
||||
key = strings.TrimPrefix(key, envExternDirectiveStart)
|
||||
key = strings.TrimSuffix(key, externDirectiveEnd)
|
||||
return os.Getenv(key)
|
||||
}
|
||||
if strings.HasPrefix(key, externFileDirectiveStart) {
|
||||
var err error
|
||||
var keyValue []byte
|
||||
key = strings.TrimPrefix(key, externFileDirectiveStart)
|
||||
key = strings.TrimSuffix(key, externDirectiveEnd)
|
||||
key, err = getFullPathWithHomeDir(key)
|
||||
if err != nil {
|
||||
opts.Logger.Err(err).Send()
|
||||
return ""
|
||||
}
|
||||
keyValue, err = os.ReadFile(key)
|
||||
if err != nil {
|
||||
opts.Logger.Err(err).Send()
|
||||
return ""
|
||||
}
|
||||
key = string(keyValue)
|
||||
}
|
||||
if strings.HasPrefix(key, vaultExternDirectiveStart) {
|
||||
key = strings.TrimPrefix(key, vaultExternDirectiveStart)
|
||||
key = strings.TrimSuffix(key, externDirectiveEnd)
|
||||
key = GetVaultKey(key, opts, opts.Logger)
|
||||
}
|
||||
|
||||
return key
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ func NewRemoteFetcher(source string, cache *Cache, options ...FetcherOption) (Re
|
||||
option(&config)
|
||||
}
|
||||
|
||||
// If FileType is empty (i.e. WithFileType was not called), yaml is the default file type
|
||||
// WithFileType was not called. yaml is the default file type
|
||||
if strings.TrimSpace(config.FileType) == "" {
|
||||
config.FileType = "yaml"
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ func (l *LocalFetcher) Fetch(source string) ([]byte, error) {
|
||||
if l.config.IgnoreFileNotFound {
|
||||
return nil, ErrIgnoreFileNotFound
|
||||
}
|
||||
return nil, nil
|
||||
return nil, err
|
||||
}
|
||||
file, err := os.Open(source)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user