-
Notifications
You must be signed in to change notification settings - Fork 470
Support for Kamal deployment #518
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Secrets defined here are available for reference under registry/password, env/secret, builder/secrets, | ||
| # and accessories/*/env/secret in config/deploy.yml. All secrets should be pulled from either | ||
| # password manager, ENV, or a file. DO NOT ENTER RAW CREDENTIALS HERE! This file needs to be safe for git. | ||
|
|
||
| # Grab the registry password from ENV | ||
| KAMAL_REGISTRY_PASSWORD=$KAMAL_REGISTRY_PASSWORD | ||
|
|
||
| # Improve security by using a password manager. Never check config/master.key or config/credentials/*.key into git! | ||
| RAILS_MASTER_KEY=$(cat config/credentials/production.key) | ||
|
|
||
| # Either use .env or rails credentials to store database password. | ||
| # HOSTEDGPT_DATABASE_PASSWORD=$HOSTEDGPT_DATABASE_PASSWORD | ||
| credentials=$(bin/rails credentials:show --environment production) | ||
| HOSTEDGPT_DATABASE_PASSWORD=$(echo "$credentials" | yq '.database.password // "password"') | ||
|
|
||
| # Used by postgres:16 image to set password | ||
| POSTGRES_PASSWORD=$HOSTEDGPT_DATABASE_PASSWORD |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,16 +27,17 @@ This project is led by an experienced rails developer, but I'm actively looking | |
| - [Troubleshooting Render](#troubleshooting-render) | ||
| - [Deploy the app on Fly.io](#deploy-the-app-on-flyio) | ||
| - [Deploy the app on Heroku](#deploy-the-app-on-heroku) | ||
| - [Deploy to own servers with Kamal2](#deploy-to-own-servers-with-kamal2) | ||
| - [Deploy on your own server](#deploy-on-your-own-server) | ||
| - [Configure optional features](#configure-optional-features) | ||
| - [Give assistant access to your Google apps](#configuring-google-tools) | ||
| - [Configuring Google Tools](#configuring-google-tools) | ||
| - [Authentication](#authentication) | ||
| - [Password authentication](#password-authentication) | ||
| - [Google OAuth authentication](#google-oauth-authentication) | ||
| - [HTTP header authentication](#http-header-authentication) | ||
| - [Contribute as a developer](#contribute-as-a-developer) | ||
| - [Running locally](#Running-locally) | ||
| - [Alternatively, you can skip Docker:](#alternatively-you-can-set-skip-docker) | ||
| - [Running locally](#running-locally) | ||
| - [Alternatively, you can skip Docker](#alternatively-you-can-skip-docker) | ||
| - [Running tests](#running-tests) | ||
| - [Understanding the Docker configuration](#understanding-the-docker-configuration) | ||
| - [Changelog](#changelog) | ||
|
|
@@ -111,6 +112,38 @@ Eligible students can apply for Heroku platform credits through [Heroku for GitH | |
|
|
||
| You may want to read about [configuring optional features](#configure-optional-features). | ||
|
|
||
| ## Deploy to own servers with Kamal | ||
|
|
||
| [Kamal](https://kamal-deploy.org/) offers zero-downtime deploys, rolling restarts, asset bridging, remote builds, accessory service management, and everything else you need to deploy and manage your web app in production with Docker. Originally built for Rails apps, Kamal will work with any type of web app that can be containerized. | ||
|
|
||
| First, create your production credentials file. | ||
|
|
||
| ```plain | ||
| bin/rails credentials:edit --environment production | ||
| ``` | ||
|
|
||
| Second, create a Docker Hub access token and store it as local env var `KAMAL_REGISTRY_PASSWORD`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know github lets you host containers. I was using that for a different github project. If I were to enable github container registery for HostedGPT would that save people the trouble of needing to create a Docker Hub account? It must be that Kamal needs to pull from a docker container registery? (I need to watch the Kamal tutorial video)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think Kamal likes to build a new image and deploy it; whereas docker compose supports running pre-built containers or re-building them. Specifically:
So each person who wants to use Kamal will be building their own image to their own docker registry (docker hub, or wherever). |
||
|
|
||
| Next, edit `config/deploy.yml`: | ||
|
|
||
| 1. Change `my-docker-user` to your Docker Hub username | ||
| 2. Change `168.192.0.1` to the IP or hostname of your target Linux server | ||
| 3. If you need to `ssh` into that server as anything other than `root` user, then uncomment `ssh:` section and edit your ssh username. | ||
| 4. Change `hostedgpt.example.com` to the public CNAME or A record that points to your server IP address. | ||
|
|
||
| Next, commit all the changes to git so Kamal picks them up. | ||
|
|
||
| ```plain | ||
| git add . | ||
| git commit -m "Add production credentials and Kamal config" | ||
| ``` | ||
|
|
||
| Now, run the command to setup the Postgres database, build HostedGPT using docker buildx, and deploy it to your server: | ||
|
|
||
| ```plain | ||
| kamal setup | ||
| ``` | ||
|
|
||
| ## Deploy on your own server | ||
|
|
||
| There are only two services that need to be running for this app to work: the Puma web server and a Postgres database. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,27 @@ | ||
| default: &default | ||
| adapter: postgresql | ||
| encoding: unicode | ||
| host: localhost | ||
| pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> | ||
| port: <%= ENV['HOSTEDGPT_DATABASE_PORT'] || 5432 %> | ||
| <% if RUBY_PLATFORM =~ /darwin/ %> | ||
| <% if ENV["HOSTEDGPT_DATABASE_HOST"] %> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rails will already, by default, read DATABASE_URL from the environment and use that as the database configuration instead of what's in database.yml. Does that make it so we don't need to declare all these new ENV variables? I'm pretty sure other users of HostedGPT have asked for these config so maybe the DATABASE_URL behavior is just too unknown. It probably doesn't hurt to add these regardless.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if I thought it'd made sure all the existing behaviour/defaults existed if the new env vars weren't provided. It's definitely difficult to read YAML + ERB :) |
||
| host: <%= ENV["HOSTEDGPT_DATABASE_HOST"] %> | ||
| <% end %> | ||
| <% if ENV["HOSTEDGPT_DATABASE_PORT"] %> | ||
| port: <%= ENV["HOSTEDGPT_DATABASE_PORT"] %> | ||
| <% end %> | ||
| <% if RUBY_PLATFORM =~ /darwin/ %> | ||
| gssencmode: disable | ||
| <% end %> | ||
| <% end %> | ||
|
|
||
| development: | ||
| <<: *default | ||
| database: <%= ENV['HOSTEDGPT_DEV_DB'] || "hostedgpt_development" %> | ||
| database: <%= ENV.fetch("HOSTEDGPT_DEV_DB", "hostedgpt_development") %> | ||
|
|
||
| test: | ||
| <<: *default | ||
| database: <%= ENV['HOSTEDGPT_TEST_DB'] || "hostedgpt_test" %> | ||
| database: <%= ENV.fetch("HOSTEDGPT_TEST_DB", "hostedgpt_test") %> | ||
|
|
||
| production: | ||
| <<: *default | ||
| database: hostedgpt_production | ||
| username: hostedgpt | ||
| database: <%= ENV.fetch("HOSTEDGPT_PRODUCTION_DB", "hostedgpt_production") %> | ||
| username: <%= ENV.fetch("HOSTEDGPT_DATABASE_USERNAME", "hostedgpt") %> | ||
| password: <%= ENV["HOSTEDGPT_DATABASE_PASSWORD"] %> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Name of your application. Used to uniquely configure containers. | ||
| service: hostedgpt | ||
|
|
||
| # Name of the container image. | ||
| image: my-docker-user/hostedgpt | ||
|
|
||
| # Deploy to these servers. | ||
| servers: | ||
| web: | ||
| - 168.192.0.1 | ||
| # job: | ||
| # hosts: | ||
| # - 168.192.0.1 | ||
| # cmd: bin/rake solid_queue:start | ||
|
|
||
| # Enable SSL auto certification via Let's Encrypt (and allow for multiple apps on one server). | ||
| # Set ssl: false if using something like Cloudflare to terminate SSL (but keep host!). | ||
| proxy: | ||
| ssl: true | ||
| host: hostedgpt.example.com | ||
| app_port: 8080 | ||
|
|
||
| # Credentials for your image host. | ||
| registry: | ||
| # Specify the registry server, if you're not using Docker Hub | ||
| # server: registry.digitalocean.com / ghcr.io / ... | ||
| username: my-docker-user | ||
|
|
||
| # Always use an access token rather than real password when possible. | ||
| password: | ||
| - KAMAL_REGISTRY_PASSWORD | ||
|
|
||
| # Inject ENV variables into containers (secrets come from .kamal/secrets). | ||
| env: | ||
| secret: | ||
| - RAILS_MASTER_KEY | ||
| - HOSTEDGPT_DATABASE_PASSWORD | ||
| clear: | ||
| # Run the Solid Queue Supervisor inside the web server's Puma process to do jobs. | ||
| # When you start using multiple servers, you should split out job processing to a dedicated machine. | ||
| RUN_SOLID_QUEUE_IN_PUMA: true | ||
|
|
||
| # Set number of processes dedicated to Solid Queue (default: 1) | ||
| # JOB_CONCURRENCY: 3 | ||
|
|
||
| # Set number of cores available to the application on each server (default: 1). | ||
| # WEB_CONCURRENCY: 2 | ||
|
|
||
| HOSTEDGPT_FORCE_SSL: "false" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why disable SSL, was there some complication with Kamal? I thought I remembered hearing that Kamal would auto-configure SSL for you using Let's Authenticate.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this either and perhaps it wasn't necessary. I just found I needed to turn off force_ssl for the app running behind kamal proxy. Hopefully we learn more over time about good Rails/Kamal practices.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have this deployed with kamal and the FORCE_SSL issue was related to the health check. I had to uncomment the config.ssl_options in production.rb: # Skip http-to-https redirect for the default health check endpoint.
config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } } |
||
|
|
||
| # Match this to any external database server to configure Active Record correctly | ||
| HOSTEDGPT_DATABASE_HOST: hostedgpt-db | ||
| HOSTEDGPT_DATABASE_USERNAME: postgres | ||
| HOSTEDGPT_PRODUCTION_DB: hostedgpt_production | ||
|
|
||
| # Log everything from Rails | ||
| RAILS_LOG_LEVEL: debug | ||
|
|
||
| # Aliases are triggered with "bin/kamal <alias>". You can overwrite arguments on invocation: | ||
| # "bin/kamal logs -r job" will tail logs from the first server in the job section. | ||
| aliases: | ||
| console: app exec --interactive --reuse "bin/rails console" | ||
| shell: app exec --interactive --reuse "bash" | ||
| logs: app logs -f | ||
| dbc: app exec --interactive --reuse "bin/rails dbconsole" | ||
|
|
||
| # Use a persistent storage volume for sqlite database files and local Active Storage files. | ||
| # Recommended to change this to a mounted volume path that is backed up off server. | ||
| volumes: | ||
| - "hostedgpt_storage:/rails/storage" | ||
|
|
||
| # Bridge fingerprinted assets, like JS and CSS, between versions to avoid | ||
| # hitting 404 on in-flight requests. Combines all files from new and old | ||
| # version inside the asset_path. | ||
| asset_path: /rails/public/assets | ||
|
|
||
| # Configure the image builder. | ||
| builder: | ||
| arch: amd64 | ||
|
|
||
| # # Build image via remote server (useful for faster amd64 builds on arm64 computers) | ||
| # remote: ssh://docker@docker-builder-server | ||
| # | ||
| # # Pass arguments and secrets to the Docker build process | ||
| # args: | ||
| # RUBY_VERSION: ruby-3.3.5 | ||
| # secrets: | ||
| # - GITHUB_TOKEN | ||
| # - RAILS_MASTER_KEY | ||
|
|
||
| # Use a different ssh user than root | ||
| # ssh: | ||
| # user: deploy | ||
|
|
||
| # Use accessory services (secrets come from .kamal/secrets). | ||
| accessories: | ||
| db: | ||
| image: postgres:16 | ||
| host: 168.192.0.1 | ||
| # port: 5432 | ||
| env: | ||
| clear: | ||
| POSTGRES_DB: hostedgpt_production | ||
| secret: | ||
| - POSTGRES_PASSWORD | ||
| directories: | ||
| - data:/var/lib/postgresql/data | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies. | ||
| secret_key_base: <%= secret_key_base %> | ||
|
|
||
| active_record_encryption: | ||
| primary_key: <%= SecureRandom.alphanumeric(32) %> | ||
| deterministic_key: <%= SecureRandom.alphanumeric(32) %> | ||
| key_derivation_salt: <%= SecureRandom.alphanumeric(32) %> | ||
|
|
||
| database: | ||
| password: <%= SecureRandom.alphanumeric(32) %> |
Uh oh!
There was an error while loading. Please reload this page.