Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Group = endpoint.Define( "command", "Run a command on target hosts", endpoint.Area(area.Command), endpoint.Prefix("node/{hostname}"), endpoint.Uses[dispatch.Deps](), endpoint.Tag("command_operations", "Command", "Run a command on target hosts"), endpoint.CommandNotes("The [command behavior](../../../../../reference/behaviors/command/command/command.md) page tells where the behavior runs, what it is built on and its settings."), endpoint.Doc[types.RowStatus](dispatch.RowStatusDoc), endpoint.Post[ExecRequest, CommandExecResponse]("exec"). Handle(HandleExec). ID("PostNodeCommandExec"). Describe("Run a program with its arguments"). Permission("command:execute"). Returns("Each target host's result, with whether it changed."). Action("exec"). 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("--target web-01 --command echo --args hello", "Print a word"). Example("--target web-01 --command false", "A program that exits non-zero"). Example("--target web-01 --command true --cwd /tmp --timeout 5", "In a directory, for at most 5 seconds"). Result("Print a word, on one host:\n\n```ansi\n$ osapi client node command exec --target web-01 --command echo --args hello\n\x1b[1;38;2;0;213;255mResults:\x1b[0m\n \x1b[38;2;10;143;176mHOSTNAME \x1b[0m\x1b[38;2;10;143;176mSTATUS \x1b[0m\x1b[38;2;10;143;176mSTDOUT \x1b[0m\x1b[38;2;10;143;176mSTDERR \x1b[0m\x1b[38;2;10;143;176mEXIT CODE \x1b[0m\x1b[38;2;10;143;176mDURATION MS\x1b[0m\n \x1b[1mweb-01\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m hello 0 0\n\n 1 host: \x1b[38;2;103;234;147m1 changed\x1b[0m \x1b[38;2;138;138;138mjob 550e8400-e29b-41d4-a716-446655440000\x1b[0m \x1b[38;2;138;138;138mtrace 26e448c9dbe1c4a8d216a98c675079c9\x1b[0m\n```\n\nOn each host, where one cannot run it:\n\n```ansi\n$ osapi client node command exec --target _all --command echo --args hello\n\x1b[1;38;2;0;213;255mResults:\x1b[0m\n \x1b[38;2;10;143;176mHOSTNAME \x1b[0m\x1b[38;2;10;143;176mSTATUS \x1b[0m\x1b[38;2;10;143;176mSTDOUT \x1b[0m\x1b[38;2;10;143;176mSTDERR \x1b[0m\x1b[38;2;10;143;176mEXIT CODE \x1b[0m\x1b[38;2;10;143;176mDURATION MS\x1b[0m\n \x1b[1mweb-01\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m hello 0 0\n \x1b[1mweb-02\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m hello 0 0\n\n 3 hosts: \x1b[38;2;103;234;147m2 changed\x1b[0m, \x1b[38;5;222m1 skipped\x1b[0m \x1b[38;2;138;138;138mjob 550e8400-e29b-41d4-a716-446655440000\x1b[0m \x1b[38;2;138;138;138mtrace 26e448c9dbe1c4a8d216a98c675079c9\x1b[0m\n\n\x1b[1;38;2;255;0;255mDetails:\x1b[0m\n \x1b[1mmac-01\x1b[0m \x1b[38;5;222mskipped\x1b[0m operation not supported on this OS family\n```\n\nWith `--json`, the API's response:\n\n```json\n{\n \"job_id\": \"550e8400-e29b-41d4-a716-446655440000\",\n \"results\": [\n {\n \"changed\": true,\n \"duration_ms\": 0,\n \"exit_code\": 0,\n \"hostname\": \"web-01\",\n \"status\": \"ok\",\n \"stderr\": \"\",\n \"stdout\": \"hello\\n\"\n }\n ]\n}\n```\n\nA program that exits non-zero, on one host:\n\n```ansi\n$ osapi client node command exec --target web-01 --command false\n\x1b[1;38;2;0;213;255mResults:\x1b[0m\n \x1b[38;2;10;143;176mHOSTNAME \x1b[0m\x1b[38;2;10;143;176mSTATUS \x1b[0m\x1b[38;2;10;143;176mSTDOUT \x1b[0m\x1b[38;2;10;143;176mSTDERR \x1b[0m\x1b[38;2;10;143;176mEXIT CODE \x1b[0m\x1b[38;2;10;143;176mDURATION MS\x1b[0m\n \x1b[1mweb-01\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m 1 0\n\n 1 host: \x1b[38;2;103;234;147m1 changed\x1b[0m \x1b[38;2;138;138;138mjob 550e8400-e29b-41d4-a716-446655440000\x1b[0m \x1b[38;2;138;138;138mtrace 26e448c9dbe1c4a8d216a98c675079c9\x1b[0m\n```\n\nOn each host, where one cannot run it:\n\n```ansi\n$ osapi client node command exec --target _all --command false\n\x1b[1;38;2;0;213;255mResults:\x1b[0m\n \x1b[38;2;10;143;176mHOSTNAME \x1b[0m\x1b[38;2;10;143;176mSTATUS \x1b[0m\x1b[38;2;10;143;176mSTDOUT \x1b[0m\x1b[38;2;10;143;176mSTDERR \x1b[0m\x1b[38;2;10;143;176mEXIT CODE \x1b[0m\x1b[38;2;10;143;176mDURATION MS\x1b[0m\n \x1b[1mweb-01\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m 1 0\n \x1b[1mweb-02\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m 1 0\n\n 3 hosts: \x1b[38;2;103;234;147m2 changed\x1b[0m, \x1b[38;5;222m1 skipped\x1b[0m \x1b[38;2;138;138;138mjob 550e8400-e29b-41d4-a716-446655440000\x1b[0m \x1b[38;2;138;138;138mtrace 26e448c9dbe1c4a8d216a98c675079c9\x1b[0m\n\n\x1b[1;38;2;255;0;255mDetails:\x1b[0m\n \x1b[1mmac-01\x1b[0m \x1b[38;5;222mskipped\x1b[0m operation not supported on this OS family\n```\n\nWith `--json`, the API's response:\n\n```json\n{\n \"job_id\": \"550e8400-e29b-41d4-a716-446655440000\",\n \"results\": [\n {\n \"changed\": true,\n \"duration_ms\": 0,\n \"exit_code\": 1,\n \"hostname\": \"web-01\",\n \"status\": \"ok\",\n \"stderr\": \"\",\n \"stdout\": \"\"\n }\n ]\n}\n```\n\nIn a directory, for at most 5 seconds, on one host:\n\n```ansi\n$ osapi client node command exec --target web-01 --command true --cwd /tmp --timeout 5\n\x1b[1;38;2;0;213;255mResults:\x1b[0m\n \x1b[38;2;10;143;176mHOSTNAME \x1b[0m\x1b[38;2;10;143;176mSTATUS \x1b[0m\x1b[38;2;10;143;176mSTDOUT \x1b[0m\x1b[38;2;10;143;176mSTDERR \x1b[0m\x1b[38;2;10;143;176mEXIT CODE \x1b[0m\x1b[38;2;10;143;176mDURATION MS\x1b[0m\n \x1b[1mweb-01\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m 0 0\n\n 1 host: \x1b[38;2;103;234;147m1 changed\x1b[0m \x1b[38;2;138;138;138mjob 550e8400-e29b-41d4-a716-446655440000\x1b[0m \x1b[38;2;138;138;138mtrace 26e448c9dbe1c4a8d216a98c675079c9\x1b[0m\n```\n\nOn each host, where one cannot run it:\n\n```ansi\n$ osapi client node command exec --target _all --command true --cwd /tmp --timeout 5\n\x1b[1;38;2;0;213;255mResults:\x1b[0m\n \x1b[38;2;10;143;176mHOSTNAME \x1b[0m\x1b[38;2;10;143;176mSTATUS \x1b[0m\x1b[38;2;10;143;176mSTDOUT \x1b[0m\x1b[38;2;10;143;176mSTDERR \x1b[0m\x1b[38;2;10;143;176mEXIT CODE \x1b[0m\x1b[38;2;10;143;176mDURATION MS\x1b[0m\n \x1b[1mweb-01\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m 0 0\n \x1b[1mweb-02\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m 0 0\n\n 3 hosts: \x1b[38;2;103;234;147m2 changed\x1b[0m, \x1b[38;5;222m1 skipped\x1b[0m \x1b[38;2;138;138;138mjob 550e8400-e29b-41d4-a716-446655440000\x1b[0m \x1b[38;2;138;138;138mtrace 26e448c9dbe1c4a8d216a98c675079c9\x1b[0m\n\n\x1b[1;38;2;255;0;255mDetails:\x1b[0m\n \x1b[1mmac-01\x1b[0m \x1b[38;5;222mskipped\x1b[0m operation not supported on this OS family\n```\n\nWith `--json`, the API's response:\n\n```json\n{\n \"job_id\": \"550e8400-e29b-41d4-a716-446655440000\",\n \"results\": [\n {\n \"changed\": true,\n \"duration_ms\": 0,\n \"exit_code\": 0,\n \"hostname\": \"web-01\",\n \"status\": \"ok\",\n \"stderr\": \"\",\n \"stdout\": \"\"\n }\n ]\n}\n```\n\nWith the default settings, each example does this on the host:\n\n| Example | On the host |\n| --- | --- |\n| Print a word | runs `echo hello` |\n| A program that exits non-zero | runs `false` |\n| In a directory, for at most 5 seconds | runs `true` |\n\nThe command fails in these cases:\n\n| Call | Flags | Fails with |\n| --- | --- | --- |\n| No program | | required |\n| A program that is an option | `--command -x` | startsnotwith |\n| A timeout above 300 seconds | `--command true --timeout 301` | max |\n| A negative timeout | `--command true --timeout -1` | min |"). Errors(endpoint.NotFound(behavior.ErrNotFound, "The host does not have what the request names, such as the object of a deploy."), endpoint.Conflict(behavior.ErrNotManaged, "osapi did not deploy the item, so it does not change it, or the host does not have a program that the method needs.").Because(types.ReasonNotManaged), endpoint.Conflict(behavior.ErrNotInstalled, "osapi did not deploy the item, so it does not change it, or the host does not have a program that the method needs.").Because(types.ReasonNotInstalled), endpoint.Invalid(dispatch.ErrInvalid, "The request is not valid, or the host refuses the input, such as a path that is a directory.")), endpoint.Post[ShellRequest, CommandShellResponse]("shell"). Handle(HandleShell). ID("PostNodeCommandShell"). Describe("Run a command line through /bin/sh -c"). Permission("command:shell"). Returns("Each target host's result, with whether it changed."). Action("shell"). 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("--target web-01 --command \"echo hello | tr a-z A-Z\"", "A pipe"). Example("--target web-01 --command \"exit 3\"", "A command line that exits non-zero"). Result("A pipe, on one host:\n\n```ansi\n$ osapi client node command shell --target web-01 --command \"echo hello | tr a-z A-Z\"\n\x1b[1;38;2;0;213;255mResults:\x1b[0m\n \x1b[38;2;10;143;176mHOSTNAME \x1b[0m\x1b[38;2;10;143;176mSTATUS \x1b[0m\x1b[38;2;10;143;176mSTDOUT \x1b[0m\x1b[38;2;10;143;176mSTDERR \x1b[0m\x1b[38;2;10;143;176mEXIT CODE \x1b[0m\x1b[38;2;10;143;176mDURATION MS\x1b[0m\n \x1b[1mweb-01\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m HELLO 0 0\n\n 1 host: \x1b[38;2;103;234;147m1 changed\x1b[0m \x1b[38;2;138;138;138mjob 550e8400-e29b-41d4-a716-446655440000\x1b[0m \x1b[38;2;138;138;138mtrace 26e448c9dbe1c4a8d216a98c675079c9\x1b[0m\n```\n\nOn each host, where one cannot run it:\n\n```ansi\n$ osapi client node command shell --target _all --command \"echo hello | tr a-z A-Z\"\n\x1b[1;38;2;0;213;255mResults:\x1b[0m\n \x1b[38;2;10;143;176mHOSTNAME \x1b[0m\x1b[38;2;10;143;176mSTATUS \x1b[0m\x1b[38;2;10;143;176mSTDOUT \x1b[0m\x1b[38;2;10;143;176mSTDERR \x1b[0m\x1b[38;2;10;143;176mEXIT CODE \x1b[0m\x1b[38;2;10;143;176mDURATION MS\x1b[0m\n \x1b[1mweb-01\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m HELLO 0 0\n \x1b[1mweb-02\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m HELLO 0 0\n\n 3 hosts: \x1b[38;2;103;234;147m2 changed\x1b[0m, \x1b[38;5;222m1 skipped\x1b[0m \x1b[38;2;138;138;138mjob 550e8400-e29b-41d4-a716-446655440000\x1b[0m \x1b[38;2;138;138;138mtrace 26e448c9dbe1c4a8d216a98c675079c9\x1b[0m\n\n\x1b[1;38;2;255;0;255mDetails:\x1b[0m\n \x1b[1mmac-01\x1b[0m \x1b[38;5;222mskipped\x1b[0m operation not supported on this OS family\n```\n\nWith `--json`, the API's response:\n\n```json\n{\n \"job_id\": \"550e8400-e29b-41d4-a716-446655440000\",\n \"results\": [\n {\n \"changed\": true,\n \"duration_ms\": 0,\n \"exit_code\": 0,\n \"hostname\": \"web-01\",\n \"status\": \"ok\",\n \"stderr\": \"\",\n \"stdout\": \"HELLO\\n\"\n }\n ]\n}\n```\n\nA command line that exits non-zero, on one host:\n\n```ansi\n$ osapi client node command shell --target web-01 --command \"exit 3\"\n\x1b[1;38;2;0;213;255mResults:\x1b[0m\n \x1b[38;2;10;143;176mHOSTNAME \x1b[0m\x1b[38;2;10;143;176mSTATUS \x1b[0m\x1b[38;2;10;143;176mSTDOUT \x1b[0m\x1b[38;2;10;143;176mSTDERR \x1b[0m\x1b[38;2;10;143;176mEXIT CODE \x1b[0m\x1b[38;2;10;143;176mDURATION MS\x1b[0m\n \x1b[1mweb-01\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m 3 0\n\n 1 host: \x1b[38;2;103;234;147m1 changed\x1b[0m \x1b[38;2;138;138;138mjob 550e8400-e29b-41d4-a716-446655440000\x1b[0m \x1b[38;2;138;138;138mtrace 26e448c9dbe1c4a8d216a98c675079c9\x1b[0m\n```\n\nOn each host, where one cannot run it:\n\n```ansi\n$ osapi client node command shell --target _all --command \"exit 3\"\n\x1b[1;38;2;0;213;255mResults:\x1b[0m\n \x1b[38;2;10;143;176mHOSTNAME \x1b[0m\x1b[38;2;10;143;176mSTATUS \x1b[0m\x1b[38;2;10;143;176mSTDOUT \x1b[0m\x1b[38;2;10;143;176mSTDERR \x1b[0m\x1b[38;2;10;143;176mEXIT CODE \x1b[0m\x1b[38;2;10;143;176mDURATION MS\x1b[0m\n \x1b[1mweb-01\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m 3 0\n \x1b[1mweb-02\x1b[0m \x1b[38;2;103;234;147mchanged\x1b[0m 3 0\n\n 3 hosts: \x1b[38;2;103;234;147m2 changed\x1b[0m, \x1b[38;5;222m1 skipped\x1b[0m \x1b[38;2;138;138;138mjob 550e8400-e29b-41d4-a716-446655440000\x1b[0m \x1b[38;2;138;138;138mtrace 26e448c9dbe1c4a8d216a98c675079c9\x1b[0m\n\n\x1b[1;38;2;255;0;255mDetails:\x1b[0m\n \x1b[1mmac-01\x1b[0m \x1b[38;5;222mskipped\x1b[0m operation not supported on this OS family\n```\n\nWith `--json`, the API's response:\n\n```json\n{\n \"job_id\": \"550e8400-e29b-41d4-a716-446655440000\",\n \"results\": [\n {\n \"changed\": true,\n \"duration_ms\": 0,\n \"exit_code\": 3,\n \"hostname\": \"web-01\",\n \"status\": \"ok\",\n \"stderr\": \"\",\n \"stdout\": \"\"\n }\n ]\n}\n```\n\nWith the default settings, each example does this on the host:\n\n| Example | On the host |\n| --- | --- |\n| A pipe | runs `/bin/sh -c echo hello \\| tr a-z A-Z` |\n| A command line that exits non-zero | runs `/bin/sh -c exit 3` |\n\nThe command fails in these cases:\n\n| Call | Flags | Fails with |\n| --- | --- | --- |\n| No command line | | required |\n| A command line that is an option | `--command -x` | startsnotwith |\n| A timeout above 300 seconds | `--command true --timeout 301` | max |\n| A negative timeout | `--command true --timeout -1` | min |"). Errors(endpoint.NotFound(behavior.ErrNotFound, "The host does not have what the request names, such as the object of a deploy."), endpoint.Conflict(behavior.ErrNotManaged, "osapi did not deploy the item, so it does not change it, or the host does not have a program that the method needs.").Because(types.ReasonNotManaged), endpoint.Conflict(behavior.ErrNotInstalled, "osapi did not deploy the item, so it does not change it, or the host does not have a program that the method needs.").Because(types.ReasonNotInstalled), endpoint.Invalid(dispatch.ErrInvalid, "The request is not valid, or the host refuses the input, such as a path that is a directory.")), )
Group is command as an endpoint group, which its spec comes from.
Functions ¶
Types ¶
type CommandExecRequest ¶
type CommandExecRequest struct {
Command string `doc:"The program, a name on the agent's PATH or a path, such as ls" json:"command" validate:"required,min=1,startsnotwith=-"`
Args []string `doc:"Its arguments, each one word, such as -la /tmp" json:"args,omitempty" validate:"omitempty"`
Cwd string `doc:"The directory to run it in; the agent's own when empty" json:"cwd,omitempty" validate:"omitempty"`
Timeout int `` /* 130-byte string literal not displayed */
}
CommandExecRequest is the body of command exec.
type CommandExecResponse ¶
type CommandExecResponse struct {
JobID string `` /* 133-byte string literal not displayed */
Results []CommandResult `json:"results"`
}
CommandExecResponse is each target host's result.
func HandleExec ¶
func HandleExec( ctx context.Context, d dispatch.Deps, in ExecRequest, ) (CommandExecResponse, error)
HandleExec runs command exec on the target, for the endpoint server.
type CommandResult ¶
type CommandResult struct {
Hostname string `doc:"Hostname of the agent that produced this result." json:"hostname"`
Status types.RowStatus `doc:"The status of the operation for this host." json:"status"`
Stdout *string `doc:"What it wrote to standard output" json:"stdout,omitempty"`
Stderr *string `doc:"What it wrote to standard error" json:"stderr,omitempty"`
ExitCode *int `doc:"Its exit code" json:"exit_code,omitempty"`
DurationMs *int64 `doc:"How long it ran, in milliseconds" json:"duration_ms,omitempty"`
Changed *bool `doc:"Whether the host changed: true, since a command can change anything" json:"changed,omitempty"`
Error *string `doc:"Error message if the agent failed." json:"error,omitempty"`
}
CommandResult is the result of command on one host.
type CommandShellRequest ¶
type CommandShellRequest struct {
Command string `` /* 135-byte string literal not displayed */
Cwd string `doc:"The directory to run it in; the agent's own when empty" json:"cwd,omitempty" validate:"omitempty"`
Timeout int `` /* 130-byte string literal not displayed */
}
CommandShellRequest is the body of command shell.
type CommandShellResponse ¶
type CommandShellResponse struct {
JobID string `` /* 133-byte string literal not displayed */
Results []CommandResult `json:"results"`
}
CommandShellResponse is each target host's result.
func HandleShell ¶
func HandleShell( ctx context.Context, d dispatch.Deps, in ShellRequest, ) (CommandShellResponse, error)
HandleShell runs command shell on the target, for the endpoint server.
type ExecRequest ¶
type ExecRequest struct {
Hostname string `` /* 182-byte string literal not displayed */
Body CommandExecRequest `in:"body" validate:"required"`
}
ExecRequest is the request of command exec: its target and its input.
type ShellRequest ¶
type ShellRequest struct {
Hostname string `` /* 182-byte string literal not displayed */
Body CommandShellRequest `in:"body" validate:"required"`
}
ShellRequest is the request of command shell: its target and its input.