kamal icon indicating copy to clipboard operation
kamal copied to clipboard

Add in a server exec command for running ad-hoc commands directly on the server

Open nickhammond opened this issue 1 year ago • 6 comments

This PR adds a kamal server exec command to the CLI which was mentioned in this discussion thread: https://github.com/basecamp/kamal/discussions/371

Running an interactive command against the server, same option that you pass for an accessory or app container:

$ kamal server exec --interactive "/bin/bash"
Running '/bin/bash' on 867.53.0.9 interactively...
root@server-1:~#

Running a command across all matching servers/roles/etc., output is displayed for each host:

$ kamal server exec "date"     
Running 'date' on 867.53.0.9...
  INFO [8c0d0725] Running /usr/bin/env date on 867.53.0.9
  INFO [8c0d0725] Finished in 0.192 seconds with exit status 0 (successful).
App Host: 867.53.0.9
Mon May 13 20:45:58 UTC 2024

You can pass all of the usual filters such as role or host to target those specific servers:

$ kamal server exec "date" -r web
Running 'date' on 867.53.0.9...
  INFO [62769fed] Running /usr/bin/env date on 867.53.0.9
  INFO [62769fed] Finished in 0.182 seconds with exit status 0 (successful).
App Host: 867.53.0.9
Mon May 13 20:55:41 UTC 2024

nickhammond avatar Jan 12 '24 06:01 nickhammond

Looks great @nickhammond 🙏

I think we should go ahead with the interactive version as well though - would you be able to add that?

djmb avatar Mar 04 '24 11:03 djmb

@djmb Yah, I'll explore that this week when I get some time.

nickhammond avatar Mar 04 '24 17:03 nickhammond

can't wait for this to be released. It's a small thing, but very crucial for us.

Thank you @nickhammond

abuaboud avatar Mar 05 '24 01:03 abuaboud

@djmb Perhaps the interactive version could be merged as a followup in order to proceed with this PR?

Sija avatar Mar 05 '24 02:03 Sija

Hi @nickhammond and @djmb,

Is there anything remaining in this PR or blocking? we would love to use it as soon as possible.

Thank you so much!

abuaboud avatar May 13 '24 12:05 abuaboud

@djmb Just added in the interactive version with run_locally. I'm not super familiar with SSHKit so if there's a better way to do this let me know. It mirrors the same options and flow that you'd normally use with an accessory or an app container to run exec.

nickhammond avatar May 13 '24 20:05 nickhammond

Thanks @nickhammond!

djmb avatar May 21 '24 10:05 djmb