1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-03-31 01:24:31 +00:00

added useDetectedShell to message, so it can be decided on the message level to use it or not for cliCommand's

This commit is contained in:
postmannen 2024-12-25 17:51:27 +01:00
parent 77ca78195d
commit e0f4671534
3 changed files with 54 additions and 9 deletions

View file

@ -1,6 +1,28 @@
# cliCommand
In JSON.
With **cliCommand** you specify the command to run in **methodArgs** prefixed with the interpreter to use, for example **bash**
On Linux and Darwin, the shell interpreter can also be auto detected by setting the value of **useDetectedShell** in the message to **true**. If set to true the methodArgs only need a single string value with command to run. Example below.
```yaml
---
- toNodes:
- node2
UseDetectedShell: true
method: cliCommand
methodArgs:
- |
rm -rf ./data & systemctl restart ctrl
replyMethod: fileAppend
ACKTimeout: 30
retries: 1
ACKTimeout: 30
directory: system
fileName: system.log
```
## Example in JSON
```json
[
@ -18,6 +40,8 @@ In JSON.
]
```
## Example in YAML
In YAML.
```yaml

View file

@ -87,6 +87,8 @@ type Message struct {
PreviousMessage *Message
// Schedule
Schedule []int `json:"schedule" yaml:"schedule"`
// Use auto detection of shell for cliCommands
UseDetectedShell bool `json:"useDetectedShell" yaml:"useDetectedShell"`
}
// --- Subject

View file

@ -83,6 +83,7 @@ func methodCliCommand(proc process, message Message, node string) ([]byte, error
err := cmd.Run()
if err != nil {
er := fmt.Errorf("error: methodCliCommand: cmd.Run failed : %v, methodArgs: %v, error_output: %v", err, message.MethodArgs, stderr.String())
proc.errorKernel.errSend(proc, message, er, logWarning)
newReplyMessage(proc, msgForErrors, []byte(er.Error()))
}
@ -245,27 +246,45 @@ func methodCliCommandCont(proc process, message Message, node string) ([]byte, e
return ackMsg, nil
}
// getCmdAndArgs will get the command and arguments from the message, and return
// a command to execute with the arguments.
// getCmdAndArgs will return a *exec.Cmd.
func getCmdAndArgs(ctx context.Context, proc process, message Message) *exec.Cmd {
var cmd *exec.Cmd
// For the Linux and Darwin operating system we allow to automatically detect
// shell interpreter, so the user don't have to type "bash", "-c" as the first
// two arguments of the methodArgs.
// We use the shell defined in the ShellOnNode variable as interpreter. Since
// it expects a "-c" directly after in the command we prefix it to the args.
// UseDetectedShell defaults to false , or it was not set in message.
// Return a cmd based only on what was defined in the message.
if !message.UseDetectedShell {
fmt.Printf("\n *** UseDetectedShell was set to false: %v\n", message.UseDetectedShell)
fmt.Printf("args in q : %q\n", message.MethodArgs)
fmt.Printf("args in v : %v\n\n", message.MethodArgs)
cmd = exec.CommandContext(ctx, message.MethodArgs[0], message.MethodArgs[1:]...)
return cmd
}
// UseDetectedShell have been set to true in message.
//
// Check if a shell have been detected on the node.
if proc.configuration.ShellOnNode != "" {
switch runtime.GOOS {
// If we have a supported os, return the *exec.Cmd based on the
// detected shell on the node, and the args defined in the message.
case "linux", "darwin":
args := []string{"-c"}
args = append(args, message.MethodArgs...)
cmd = exec.CommandContext(ctx, proc.configuration.ShellOnNode, args...)
return cmd
// Not supported OS, use cmd and args defined in message only.
default:
cmd = exec.CommandContext(ctx, message.MethodArgs[0], message.MethodArgs...)
cmd = exec.CommandContext(ctx, message.MethodArgs[0], message.MethodArgs[1:]...)
return cmd
}
}
//args := []string{"-c"}
//args = append(args, message.MethodArgs...)
//cmd = exec.CommandContext(ctx, proc.configuration.ShellOnNode, args...)
return cmd
}