Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kamal app open command to open app in a web browser #1070

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jeromedalbert
Copy link
Contributor

@jeromedalbert jeromedalbert commented Oct 7, 2024

Problem

Kamal is a no-PaaS tool with PaaS-like ergonomics, where you can easily open a production Rails (or other) console, a shell session, tail logs, etc. A small feature that could be implemented to further align with this vision is a command to open the app in a web browser, similar to Heroku's heroku open or Fly.io's fly apps open.

This would allow for quick access to a deployed web application, from the CLI, without having to manually copy/paste the URL from one of your browser tabs or your config/deploy.yml or some other file. Examples where this can be useful:

  • right after deploying your app
  • when switching between different Kamal-enabled repos
  • in copy/paste-able Readme instructions
  • any time you're in the command line and want to easily open the app in your browser

Solution

Add a kamal app open command. It opens the primary role's (usually web) first domain defined in proxy host(s), or falls back to the primary role's first IP address.

The implementation is short, so adding a dependency for finding the OS opening command (with a gem like launchy) felt like too much. But I can change the PR if needed.

To open the app in a web browser
@djmb
Copy link
Collaborator

djmb commented Nov 21, 2024

I like this, but I'm not sure.

The command will not work properly in lots of setups - where there's no proxy host, where the host if a wildcard etc.
Adding a command that's often broken seems a bit iffy.

@jeromedalbert
Copy link
Contributor Author

jeromedalbert commented Nov 23, 2024

Normally kamal app open wouldn't break because it would fall back gracefully.

For example when there is no proxy host, this PR already falls back to the primary IP, and a valid Kamal config always has at least one IP. Falling back to IPs is pretty handy as I often like to create short-lived apps where I don't bother to configure domains, so I access them by IP.

Normal non-wildcard proxy hosts (e.g. mydomain.com or alpha.mydomain.com) also work.

That leaves us with wildcards proxy hosts (e.g. *.mydomain.com) which can't be opened by a browser. Is this an issue though? I don't think Kamal currently supports wildcards in the proxy>hosts key unless I am missing something. But if it does kamal app open could maybe find the first non-wildcard host, or do its usual fallback to the primary IP. Or it could emit a warning that it cannot open wildcard hosts.

I think this would cover the majority of cases, and most apps have a pretty standard setup with normal domains or IPs. Do you have other breakage concerns or examples in mind?

@AxelTheGerman
Copy link
Contributor

You would have to duplicate the host name, but this would also be possible using Aliases on a per project basis.

@jeromedalbert
Copy link
Contributor Author

jeromedalbert commented Nov 26, 2024

@AxelTheGerman I am not sure how this would be possible. Can you provide an example? AFAIK aliases are for things like app exec which execute on the server whereas this command executes on your local.

@AxelTheGerman
Copy link
Contributor

AxelTheGerman commented Nov 26, 2024

@AxelTheGerman I am not sure how this would be possible. Can you provide an example? AFAIK aliases are for things like app exec which execute on the server whereas this command executes on your local.

Duh you're right... but in that case I guess it would be possible to A) write a custom rake task or B) creating a kamal-open gem that could read and parse the deploy yaml file as well.

It does seem much easier to add it into kamal but I'm agreeing with @djmb regarding

The command will not work properly in lots of setups

One option though for implementing it in kamal would just be to print a message that the app cannot be opened if we can't find the host/IP for the app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants