Documentation
¶
Overview ¶
Package command is the command behavior: run a program, or a command line through /bin/sh -c, on the host. Both are admin only, and the agent runs them only where agent.command.enabled is true.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Behavior = behavior.Register(behavior.Define( "command", "Run a command on target hosts", behavior.Node, behavior.API, behavior.Area(area.Command), behavior.CLI, behavior.Platform(behavior.PlatformLinux, newRunner), behavior.Platform(behavior.PlatformDarwin, newRunner), behavior.Runs(behavior.AnyProgram), behavior.Action("exec", Exec).AdminOnly().Always(). ReadsNothing("the output of the command is its result, which the call answers; no read of the host shows a command that ran"). Describe("Run a program with its arguments"). Explain("Runs the program with its arguments, each one word, with no shell "+ "between them: a ; or a | in an argument is that character. It answers "+ "with standard output and standard error apart, the exit code and the "+ "duration. A program that exits non-zero is a result, not an error. "+ "Only the admin role may run it, and the agent runs it only where "+ "agent.command.enabled is true."). Example("Print a word", ExecInput{Command: "echo", Args: []string{"hello"}}, Result{Stdout: "hello\n", Changed: true}, behavior.Prints("echo hello", "hello\n"), behavior.Ran("echo", "hello"), ). Example("A program that exits non-zero", ExecInput{Command: "false"}, Result{ExitCode: 1, Changed: true}, behavior.Exits("false", 1), behavior.Ran("false"), ). Example("In a directory, for at most 5 seconds", ExecInput{Command: "true", Cwd: "/tmp", Timeout: 5}, Result{Changed: true}, behavior.Ran("true"), ). Fails("No program", ExecInput{}, "required"). Fails("A program that is an option", ExecInput{Command: "-x"}, "startsnotwith"). Fails("A timeout above 300 seconds", ExecInput{Command: "true", Timeout: 301}, "max"). Fails("A negative timeout", ExecInput{Command: "true", Timeout: -1}, "min"), behavior.Action("shell", Shell).Shell().Always(). ReadsNothing("the output of the command line is its result, which the call answers; no read of the host shows a command that ran"). Describe("Run a command line through /bin/sh -c"). Explain("Runs the command line through /bin/sh -c, so pipes, redirects and "+ "variables work. It answers as exec does. Its permission is "+ "command:shell, apart from exec's command:execute: a token that may run "+ "a program may not open a shell. Only the admin role gets it."). Example("A pipe", ShellInput{Command: "echo hello | tr a-z A-Z"}, Result{Stdout: "HELLO\n", Changed: true}, behavior.Prints("/bin/sh -c echo hello | tr a-z A-Z", "HELLO\n"), behavior.Ran("/bin/sh", "-c", "echo hello | tr a-z A-Z"), ). Example("A command line that exits non-zero", ShellInput{Command: "exit 3"}, Result{ExitCode: 3, Changed: true}, behavior.Exits("/bin/sh -c exit 3", 3), behavior.Ran("/bin/sh", "-c", "exit 3"), ). Fails("No command line", ShellInput{}, "required"). Fails("A command line that is an option", ShellInput{Command: "-x"}, "startsnotwith"). Fails("A timeout above 300 seconds", ShellInput{Command: "true", Timeout: 301}, "max"). Fails("A negative timeout", ShellInput{Command: "true", Timeout: -1}, "min"), ))
Behavior is the command definition.
Functions ¶
This section is empty.
Types ¶
type ExecInput ¶
type ExecInput struct {
Command string `` /* 154-byte string literal not displayed */
Args []string `json:"args,omitempty" validate:"omitempty" doc:"Its arguments, each one word, such as -la /tmp"`
Cwd string `` /* 127-byte string literal not displayed */
Timeout int `` /* 137-byte string literal not displayed */
}
ExecInput is a program to run, with no shell between it and its arguments.
type Provider ¶
type Provider interface {
// Exec runs a program with its arguments, with no shell.
Exec(ctx context.Context, in ExecInput) (Result, error)
// Shell runs a command line through /bin/sh -c.
Shell(ctx context.Context, in ShellInput) (Result, error)
}
Provider runs a command on the host.
type Result ¶
type Result struct {
Stdout string `json:"stdout" doc:"What it wrote to standard output"`
Stderr string `json:"stderr" doc:"What it wrote to standard error"`
ExitCode int `json:"exit_code" doc:"Its exit code"`
DurationMs int64 `json:"duration_ms" doc:"How long it ran, in milliseconds"`
Changed bool `json:"changed" doc:"Whether the host changed: true, since a command can change anything"`
}
Result is what the command did: its output, apart by stream, its exit code and how long it ran. A command that exits non-zero is a result, not a failure.
type ShellInput ¶
type ShellInput struct {
Command string `` /* 162-byte string literal not displayed */
Cwd string `` /* 127-byte string literal not displayed */
Timeout int `` /* 137-byte string literal not displayed */
}
ShellInput is a command line to run through /bin/sh -c.