Skip to content

Commit

Permalink
Add support for using kamal inside of the generated devcontainer
Browse files Browse the repository at this point in the history
Kamal requires docker. The docker-outside-of-docker devcontainer feature allow
using the same docker daemon with the container.
  • Loading branch information
JoeDupuis authored and rafaelfranca committed Oct 18, 2024
1 parent aeb0828 commit fa2a809
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Add Kamal support for devcontainers

Previously generated devcontainer could not use docker and therefore Kamal.

*Joé Dupuis*

## Rails 8.0.0.beta1 (September 26, 2024) ##

* Exit `rails g` with code 1 if generator could not be found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def devcontainer_options
redis: !!((defined?(ActionCable) && !defined?(SolidCable)) || (defined?(ActiveJob) && !defined?(SolidQueue))),
system_test: File.exist?("test/application_system_test_case.rb"),
node: File.exist?(".node-version"),
kamal: File.exist?("config/deploy.yml"),
}
end

Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def devcontainer
devcontainer_options = {
database: options[:database],
redis: options[:skip_solid] && !(options[:skip_action_cable] && options[:skip_active_job]),
kamal: !options[:skip_kamal],
system_test: depends_on_system_test?,
active_storage: !options[:skip_active_storage],
dev: options[:dev],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class DevcontainerGenerator < Base # :nodoc:
class_option :dev, type: :boolean, default: false,
desc: "For applications pointing to a local Rails checkout"

class_option :kamal, type: :boolean, default: true,
desc: "Include configuration for Kamal"

source_paths << File.expand_path(File.join(base_name, "app", "templates"), base_root)

def create_devcontainer
Expand Down Expand Up @@ -80,6 +83,7 @@ def container_env
@container_env["CAPYBARA_SERVER_PORT"] = "45678" if options[:system_test]
@container_env["SELENIUM_HOST"] = "selenium" if options[:system_test]
@container_env["REDIS_URL"] = "redis://redis:6379/1" if options[:redis]
@container_env["KAMAL_REGISTRY_PASSWORD"] = "$KAMAL_REGISTRY_PASSWORD" if options[:kamal]
@container_env["DB_HOST"] = database.name if database.service

@container_env
Expand All @@ -105,6 +109,7 @@ def features

@features["ghcr.io/rails/devcontainer/features/activestorage"] = {} if options[:active_storage]
@features["ghcr.io/devcontainers/features/node:1"] = {} if options[:node]
@features["ghcr.io/devcontainers/features/docker-in-docker:2"] = {} if options[:kamal]

@features.merge!(database.feature) if database.feature

Expand Down
1 change: 1 addition & 0 deletions railties/test/commands/devcontainer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Rails::Command::DevcontainerTest < ActiveSupport::TestCase
assert_match "redis: false", output
assert_match "system_test: true", output
assert_match "node: false", output
assert_match "kamal: false", output
end

test "generates dev container for without solid gems" do
Expand Down
14 changes: 13 additions & 1 deletion railties/test/generators/app_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1255,10 +1255,12 @@ def test_devcontainer
assert_devcontainer_json_file do |content|
assert_equal "my_app", content["name"]
assert_equal "45678", content["containerEnv"]["CAPYBARA_SERVER_PORT"]
assert_equal "$KAMAL_REGISTRY_PASSWORD", content["containerEnv"]["KAMAL_REGISTRY_PASSWORD"]
assert_equal "selenium", content["containerEnv"]["SELENIUM_HOST"]
assert_includes content["features"].keys, "ghcr.io/rails/devcontainer/features/activestorage"
assert_includes content["features"].keys, "ghcr.io/devcontainers/features/github-cli:1"
assert_includes content["features"].keys, "ghcr.io/rails/devcontainer/features/sqlite3"
assert_includes content["features"].keys, "ghcr.io/devcontainers/features/docker-in-docker:2"
assert_includes(content["forwardPorts"], 3000)
end
assert_file(".devcontainer/Dockerfile") do |content|
Expand Down Expand Up @@ -1294,6 +1296,15 @@ def test_devcontainer
end
end

def test_devcontainer_skip_kamal
run_generator [destination_root, "--devcontainer", "--name=my-app", "--skip-kamal"]

assert_devcontainer_json_file do |devcontainer_json|
assert_not_includes devcontainer_json["features"].keys, "ghcr.io/devcontainers/features/docker-in-docker:2"
assert_not_includes devcontainer_json["containerEnv"].keys, "KAMAL_REGISTRY_PASSWORD"
end
end

def test_devcontainer_include_redis_skipping_solid
run_generator [destination_root, "--devcontainer", "--name=my-app", "--skip-solid"]

Expand Down Expand Up @@ -1481,7 +1492,8 @@ def test_devcontainer_no_selenium_when_skipping_system_test
assert_not_includes compose_config["services"].keys, "selenium"
end
assert_devcontainer_json_file do |content|
assert_nil content["containerEnv"]
assert_not_includes content["containerEnv"].keys, "SELENIUM_HOST"
assert_not_includes content["containerEnv"].keys, "CAPYBARA_SERVER_PORT"
end
end

Expand Down
18 changes: 18 additions & 0 deletions railties/test/generators/devcontainer_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,24 @@ def test_redis_option_skip
end
end

def test_kamal_option_default
run_generator

assert_devcontainer_json_file do |devcontainer_json|
assert_includes devcontainer_json["features"].keys, "ghcr.io/devcontainers/features/docker-in-docker:2"
assert_equal "$KAMAL_REGISTRY_PASSWORD", devcontainer_json["containerEnv"]["KAMAL_REGISTRY_PASSWORD"]
end
end

def test_kamal_option_skip
run_generator ["--skip-kamal"]

assert_devcontainer_json_file do |devcontainer_json|
assert_not_includes devcontainer_json["features"].keys, "ghcr.io/devcontainers/features/docker-in-docker:2"
assert_not_includes devcontainer_json["containerEnv"].keys, "KAMAL_REGISTRY_PASSWORD"
end
end

def test_system_test_option_default
copy_application_system_test_case

Expand Down

0 comments on commit fa2a809

Please sign in to comment.