Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ExecCmd = &cobra.Command{ Use: "exec [flags] [USER@]SERVER [--] COMMAND...", Short: "Execute a command on a remote server", Long: `Execute a command on a remote server. This command executes a specified command on a remote server and returns the output. It supports SSH-like syntax for specifying the user and server. Use -- to separate alpacon flags from the remote command, ensuring that flags intended for the remote command (e.g., -U, -d) are not interpreted as alpacon flags. All flags must be placed before the server name. Shell metacharacters (;, |, &, $) pass through unquoted to the remote shell. To send a literal metacharacter, wrap the argument in quotes: alpacon exec server 'echo hello;world' Flags: -u, --username [USER_NAME] Specify the username for command execution. -g, --groupname [GROUP_NAME] Specify the group name for command execution. --work-session [UUID] Attach this command to a work-session. Overrides the workspace's active session set via 'alpacon work-session use'. --detach Submit the command and return immediately without waiting for completion. Prints the job ID to stdout. Use 'alpacon exec logs JOB_ID' to retrieve the result. Exit code 3 indicates a WorkSession gate denial; run with --output json to parse a machine-readable diagnostic on stderr. Requires an active WorkSession when using Browser login (Auth0); Token auth (API token or Service token) bypasses this requirement.`, Example: ` # Simple command execution alpacon exec prod-docker docker ps alpacon exec root@prod-docker docker ps # Use -- to pass flags to the remote command alpacon exec root@db-server -- docker exec postgres psql -U myproject -d myproject alpacon exec my-server -- grep -r "pattern" /var/log # Specify user and group with flags alpacon exec -u root prod-docker systemctl status nginx alpacon exec -g docker user@server docker images # Submit a command asynchronously and retrieve the result later alpacon exec --detach web-server -- apt-get update alpacon exec logs <JOB_ID>`, DisableFlagParsing: true, Run: func(cmd *cobra.Command, args []string) { parsed := ParseRemoteExecArgs(args) if parsed.ShowHelp { _ = cmd.Help() return } if parsed.Err != "" { utils.CliErrorWithExit("%s", parsed.Err) return } if parsed.Server == "" { _ = cmd.Help() utils.CliErrorWithExit("server name is required.") return } if parsed.Command == "" { utils.CliErrorWithExit("You must specify a command to execute.") return } if parsed.OutputFormat != "" { if parsed.OutputFormat != utils.OutputFormatTable && parsed.OutputFormat != utils.OutputFormatJSON { utils.CliErrorWithExit("invalid --output value %q: must be 'table' or 'json'", parsed.OutputFormat) } utils.OutputFormat = parsed.OutputFormat } workSessionID := worksession.ResolveOrExit(parsed.WorkSessionID) authMethod := config.ResolveAuthMethod() alpaconClient, err := client.NewAlpaconAPIClient() if err != nil { utils.CliErrorWithExit("Connection to Alpacon API failed: %s. Consider re-logging.", err) return } env := make(map[string]string) if parsed.Detach { resp, err := event.SubmitCommand(alpaconClient, parsed.Server, parsed.Command, parsed.Username, parsed.Groupname, env, workSessionID) if err != nil { err = utils.HandleCommonErrors(err, parsed.Server, utils.ErrorHandlerCallbacks{ OnMFARequired: func(srv string) error { return mfa.HandleMFAError(alpaconClient, srv) }, OnUsernameRequired: func() error { _, err := iam.HandleUsernameRequired() return err }, CheckMFACompleted: func() (bool, error) { return mfa.CheckMFACompletion(alpaconClient) }, RefreshToken: alpaconClient.RefreshToken, RetryOperation: func() error { resp, err = event.SubmitCommand(alpaconClient, parsed.Server, parsed.Command, parsed.Username, parsed.Groupname, env, workSessionID) return err }, }) } if err != nil { utils.HandleWorkSessionError(err, "command", parsed.Server, authMethod, workSessionID) utils.CliErrorWithExit("failed to submit command on '%s': %s", parsed.Server, err) return } if utils.OutputFormat == utils.OutputFormatJSON { data, err := json.Marshal(map[string]string{"job_id": resp.ID}) if err != nil { utils.CliErrorWithExit("failed to marshal JSON: %s", err) return } utils.PrintJson(data) } else { line1, line2 := detachResultLines(resp.ID) fmt.Println(line1) fmt.Fprintln(os.Stderr, line2) } return } result, err := RunCommandWithRetry(alpaconClient, parsed.Server, parsed.Command, parsed.Username, parsed.Groupname, env, workSessionID) utils.HandleWorkSessionError(err, "command", parsed.Server, authMethod, workSessionID) HandleCommandResult(result, err) }, }
Functions ¶
func HandleCommandResult ¶ added in v1.4.8
HandleCommandResult prints result on success, or exits appropriately on error.
func RunCommandWithRetry ¶ added in v1.3.2
func RunCommandWithRetry(ac *client.AlpaconClient, serverName, command, username, groupname string, env map[string]string, workSessionID string) (string, error)
RunCommandWithRetry executes a remote command with MFA and username-required error handling and retry logic. Used by both exec and websh commands. workSessionID is forwarded to the server as the work_session field; pass "" to omit it.
func ShellJoin ¶ added in v1.4.8
ShellJoin reassembles tokenized command parts into a single string. Parts containing whitespace or single quotes are re-quoted to preserve argument boundaries; metacharacters pass through for the remote shell. A single-element slice is returned as-is (already a formed command line).
Types ¶
type RemoteExecArgs ¶ added in v1.3.2
type RemoteExecArgs struct {
Username string
Groupname string
WorkSessionID string
OutputFormat string
Server string
Command string
Detach bool
ShowHelp bool
Err string
}
RemoteExecArgs holds parsed arguments for remote command execution.
func ParseRemoteExecArgs ¶ added in v1.3.2
func ParseRemoteExecArgs(args []string) RemoteExecArgs
ParseRemoteExecArgs parses raw CLI arguments with manual flag handling. It recognizes -u/--username, -g/--groupname, and -h/--help flags before the server name.
The -- separator has two roles depending on position:
- After the server name: acts as a command separator. Everything after it is the remote command, with no further flag parsing.
- Before the server name (e.g., after flags): acts as an end-of-flags marker. The first arg after -- becomes the server, the rest is the command.
Without --, everything after the server name is the remote command.
Layout: [flags] [USER@]SERVER [--] COMMAND...