From f04cae529abfdf5b4a1930b804eced4a84db34ed Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Wed, 9 Oct 2024 09:53:17 +0400 Subject: [PATCH 1/6] Added network configuration option to application, proxy and accessory sections --- lib/kamal/commands/accessory.rb | 6 +++--- lib/kamal/commands/app.rb | 2 +- lib/kamal/commands/app/execution.rb | 2 +- lib/kamal/configuration.rb | 9 +++++++++ lib/kamal/configuration/accessory.rb | 10 ++++++++++ 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/kamal/commands/accessory.rb b/lib/kamal/commands/accessory.rb index 0c1b9009c..9abb6dfc0 100644 --- a/lib/kamal/commands/accessory.rb +++ b/lib/kamal/commands/accessory.rb @@ -1,7 +1,7 @@ class Kamal::Commands::Accessory < Kamal::Commands::Base attr_reader :accessory_config delegate :service_name, :image, :hosts, :port, :files, :directories, :cmd, - :publish_args, :env_args, :volume_args, :label_args, :option_args, + :network_args, :publish_args, :env_args, :volume_args, :label_args, :option_args, :secrets_io, :secrets_path, :env_directory, to: :accessory_config @@ -15,7 +15,7 @@ def run "--name", service_name, "--detach", "--restart", "unless-stopped", - "--network", "kamal", + *network_args, *config.logging_args, *publish_args, *env_args, @@ -64,7 +64,7 @@ def execute_in_new_container(*command, interactive: false) docker :run, ("-it" if interactive), "--rm", - "--network", "kamal", + *network_args, *env_args, *volume_args, image, diff --git a/lib/kamal/commands/app.rb b/lib/kamal/commands/app.rb index 6d8f44c60..56a5656b9 100644 --- a/lib/kamal/commands/app.rb +++ b/lib/kamal/commands/app.rb @@ -18,7 +18,7 @@ def run(hostname: nil) "--detach", "--restart unless-stopped", "--name", container_name, - "--network", "kamal", + *config.network_args, *([ "--hostname", hostname ] if hostname), "-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"", "-e", "KAMAL_VERSION=\"#{config.version}\"", diff --git a/lib/kamal/commands/app/execution.rb b/lib/kamal/commands/app/execution.rb index 4434c26aa..5770f7aa2 100644 --- a/lib/kamal/commands/app/execution.rb +++ b/lib/kamal/commands/app/execution.rb @@ -11,7 +11,7 @@ def execute_in_new_container(*command, interactive: false, env:) docker :run, ("-it" if interactive), "--rm", - "--network", "kamal", + *config.network_args, *role&.env_args(host), *argumentize("--env", env), *config.volume_args, diff --git a/lib/kamal/configuration.rb b/lib/kamal/configuration.rb index 09f1ed895..b26c30c9c 100644 --- a/lib/kamal/configuration.rb +++ b/lib/kamal/configuration.rb @@ -18,6 +18,7 @@ class Kamal::Configuration PROXY_HTTP_PORT = 80 PROXY_HTTPS_PORT = 443 PROXY_LOG_MAX_SIZE = "10m" + NETWORK = "kamal" class << self def create_from(config_file:, destination: nil, version: nil) @@ -193,6 +194,10 @@ def logging_args logging.args end + def network_args + argumentize "--network", network + end + def readiness_delay raw_config.readiness_delay || 7 @@ -297,6 +302,10 @@ def to_h end private + def network + raw_config["network"] || NETWORK + end + # Will raise ArgumentError if any required config keys are missing def ensure_destination_if_required if require_destination? && destination.nil? diff --git a/lib/kamal/configuration/accessory.rb b/lib/kamal/configuration/accessory.rb index 804a15029..7aed80eaf 100644 --- a/lib/kamal/configuration/accessory.rb +++ b/lib/kamal/configuration/accessory.rb @@ -1,6 +1,8 @@ class Kamal::Configuration::Accessory include Kamal::Configuration::Validation + NETWORK = "kamal" + delegate :argumentize, :optionize, to: Kamal::Utils attr_reader :name, :accessory_config, :env @@ -38,6 +40,10 @@ def port end end + def network_args + argumentize "--network", network + end + def publish_args argumentize "--publish", port if port end @@ -173,4 +179,8 @@ def hosts_from_roles accessory_config["roles"].flat_map { |role| config.role(role).hosts } end end + + def network + accessory_config["network"] || NETWORK + end end From c917dd82cf93c123c2e6afe78651bfe5df9bfd08 Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Tue, 1 Oct 2024 14:40:34 +0400 Subject: [PATCH 2/6] Added network_args to proxy configuration --- lib/kamal/commands/proxy.rb | 2 +- lib/kamal/configuration/proxy.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/kamal/commands/proxy.rb b/lib/kamal/commands/proxy.rb index acff3dbd6..3399531fb 100644 --- a/lib/kamal/commands/proxy.rb +++ b/lib/kamal/commands/proxy.rb @@ -4,7 +4,7 @@ class Kamal::Commands::Proxy < Kamal::Commands::Base def run docker :run, "--name", container_name, - "--network", "kamal", + *config.network_args, "--detach", "--restart", "unless-stopped", "--volume", "kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy", diff --git a/lib/kamal/configuration/proxy.rb b/lib/kamal/configuration/proxy.rb index 6232c3e03..99c7aa1fa 100644 --- a/lib/kamal/configuration/proxy.rb +++ b/lib/kamal/configuration/proxy.rb @@ -3,6 +3,7 @@ class Kamal::Configuration::Proxy DEFAULT_LOG_REQUEST_HEADERS = [ "Cache-Control", "Last-Modified", "User-Agent" ] CONTAINER_NAME = "kamal-proxy" + NETWORK = "kamal" delegate :argumentize, :optionize, to: Kamal::Utils @@ -51,6 +52,10 @@ def deploy_command_args(target:) optionize ({ target: "#{target}:#{app_port}" }).merge(deploy_options), with: "=" end + def network_args + argumentize "--network", network + end + def merge(other) self.class.new config: config, proxy_config: proxy_config.deep_merge(other.proxy_config) end @@ -59,4 +64,8 @@ def merge(other) def seconds_duration(value) value ? "#{value}s" : nil end + + def network + proxy_config["network"] || NETWORK + end end From b6a10df56aaff503edf5acb9389138ea39f60efe Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Tue, 1 Oct 2024 20:24:28 +0400 Subject: [PATCH 3/6] Added tests for network configuration option --- lib/kamal/configuration/docs/accessory.yml | 8 ++++++++ lib/kamal/configuration/docs/configuration.yml | 7 +++++++ lib/kamal/configuration/docs/proxy.yml | 7 +++++++ test/configuration/accessory_test.rb | 9 +++++++++ test/configuration/proxy_test.rb | 9 +++++++++ test/configuration_test.rb | 9 +++++++++ 6 files changed, 49 insertions(+) diff --git a/lib/kamal/configuration/docs/accessory.yml b/lib/kamal/configuration/docs/accessory.yml index 86d49b77a..e77bf754f 100644 --- a/lib/kamal/configuration/docs/accessory.yml +++ b/lib/kamal/configuration/docs/accessory.yml @@ -90,3 +90,11 @@ accessories: # They are not created or copied before mounting: volumes: - /path/to/mysql-logs:/var/log/mysql + + # Network + # + # The network the accessory will be attached to. + # + # Defaults to kamal: + network: custom + diff --git a/lib/kamal/configuration/docs/configuration.yml b/lib/kamal/configuration/docs/configuration.yml index 6b0593468..b59394a3d 100644 --- a/lib/kamal/configuration/docs/configuration.yml +++ b/lib/kamal/configuration/docs/configuration.yml @@ -176,3 +176,10 @@ logging: # Alias configuration, see kamal docs alias: aliases: ... + +# Network +# +# The network the application will be attached to. +# +# Defaults to kamal: +network: custom diff --git a/lib/kamal/configuration/docs/proxy.yml b/lib/kamal/configuration/docs/proxy.yml index 76ec3e41d..0af148261 100644 --- a/lib/kamal/configuration/docs/proxy.yml +++ b/lib/kamal/configuration/docs/proxy.yml @@ -103,3 +103,10 @@ proxy: # By default, kamal-proxy will not forward the headers if the `ssl` option is set to `true`, and # will forward them if it is set to `false`. forward_headers: true + + # Network + # + # The network the proxy container will be attached to. + # + # Defaults to kamal: + network: custom diff --git a/test/configuration/accessory_test.rb b/test/configuration/accessory_test.rb index 2615dab60..f52209026 100644 --- a/test/configuration/accessory_test.rb +++ b/test/configuration/accessory_test.rb @@ -152,4 +152,13 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase test "options" do assert_equal [ "--cpus", "\"4\"", "--memory", "\"2GB\"" ], @config.accessory(:redis).option_args end + + test "network_args default" do + assert_equal [ "--network", "kamal" ], @config.accessory(:mysql).network_args + end + + test "network_args with configured options" do + @deploy[:accessories]["mysql"]["network"] = "database" + assert_equal [ "--network", "database" ], @config.accessory(:mysql).network_args + end end diff --git a/test/configuration/proxy_test.rb b/test/configuration/proxy_test.rb index 588e5a350..562c229f6 100644 --- a/test/configuration/proxy_test.rb +++ b/test/configuration/proxy_test.rb @@ -38,6 +38,15 @@ class ConfigurationProxyTest < ActiveSupport::TestCase assert_not config.proxy.ssl? end + test "network_args defaults" do + assert_equal [ "--network", "kamal" ], config.proxy.network_args + end + + test "network_args with configured options" do + @deploy[:proxy] = { "network" => "example" } + assert_equal [ "--network", "example" ], config.proxy.network_args + end + private def config Kamal::Configuration.new(@deploy) diff --git a/test/configuration_test.rb b/test/configuration_test.rb index c1aaa6971..da53ddb61 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -217,6 +217,15 @@ class ConfigurationTest < ActiveSupport::TestCase assert_equal [ "--log-driver", "\"local\"", "--log-opt", "max-size=\"100m\"", "--log-opt", "max-file=\"5\"" ], config.logging_args end + test "network_args default" do + assert_equal [ "--network", "kamal" ], @config.network_args + end + + test "network_args with configured options" do + config = Kamal::Configuration.new(@deploy.tap { |c| c.merge!(network: "custom") }) + assert_equal [ "--network", "custom" ], config.network_args + end + test "erb evaluation of yml config" do config = Kamal::Configuration.create_from config_file: Pathname.new(File.expand_path("fixtures/deploy.erb.yml", __dir__)) assert_equal "my-user", config.registry.username From 08dacd27458368c7228a5ba99945690d4c37233b Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Tue, 1 Oct 2024 20:41:36 +0400 Subject: [PATCH 4/6] Added command tests --- test/commands/accessory_test.rb | 8 ++++++++ test/commands/app_test.rb | 8 ++++++++ test/commands/proxy_test.rb | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/test/commands/accessory_test.rb b/test/commands/accessory_test.rb index f3d71ffd3..1befd9e64 100644 --- a/test/commands/accessory_test.rb +++ b/test/commands/accessory_test.rb @@ -71,6 +71,14 @@ class CommandsAccessoryTest < ActiveSupport::TestCase new_command(:busybox).run.join(" ") end + test "run in custom network" do + @config[:accessories]["mysql"]["network"] = "custom" + + assert_equal \ + "docker run --name app-mysql --detach --restart unless-stopped --network custom --log-opt max-size=\"10m\" --publish 3306:3306 --env MYSQL_ROOT_HOST=\"%\" --env-file .kamal/apps/app/env/accessories/mysql.env --label service=\"app-mysql\" private.registry/mysql:8.0", + new_command(:mysql).run.join(" ") + end + test "start" do assert_equal \ "docker container start app-mysql", diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index 0e5cad796..403e90d4e 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -64,6 +64,14 @@ class CommandsAppTest < ActiveSupport::TestCase new_command.run.join(" ") end + test "run in custom network" do + @config[:network] = "custom" + + assert_equal \ + "docker run --detach --restart unless-stopped --name app-web-999 --network custom -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination dhh/app:999", + new_command.run.join(" ") + end + test "start" do assert_equal \ "docker start app-web-999", diff --git a/test/commands/proxy_test.rb b/test/commands/proxy_test.rb index b7cc9f3dc..80f420d2d 100644 --- a/test/commands/proxy_test.rb +++ b/test/commands/proxy_test.rb @@ -27,6 +27,14 @@ class CommandsProxyTest < ActiveSupport::TestCase new_command.run.join(" ") end + test "run in custom network" do + @config[:network] = "custom" + + assert_equal \ + "docker run --name kamal-proxy --network custom --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy $(cat .kamal/proxy/options || echo \"--publish 80:80 --publish 443:443\") basecamp/kamal-proxy:#{Kamal::Configuration::PROXY_MINIMUM_VERSION}", + new_command.run.join(" ") + end + test "proxy start" do assert_equal \ "docker container start kamal-proxy", From da2a543cbcfa995265cb5583ee9e5802a58c5109 Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Wed, 9 Oct 2024 10:00:49 +0400 Subject: [PATCH 5/6] Reverted network arguments everywhere except to accessory config --- lib/kamal/commands/app.rb | 2 +- lib/kamal/commands/app/execution.rb | 2 +- lib/kamal/commands/proxy.rb | 2 +- lib/kamal/configuration.rb | 9 --------- lib/kamal/configuration/docs/configuration.yml | 7 ------- lib/kamal/configuration/docs/proxy.yml | 7 ------- lib/kamal/configuration/proxy.rb | 9 --------- test/commands/app_test.rb | 8 -------- test/commands/proxy_test.rb | 8 -------- test/configuration/proxy_test.rb | 9 --------- test/configuration_test.rb | 9 --------- 11 files changed, 3 insertions(+), 69 deletions(-) diff --git a/lib/kamal/commands/app.rb b/lib/kamal/commands/app.rb index 56a5656b9..6d8f44c60 100644 --- a/lib/kamal/commands/app.rb +++ b/lib/kamal/commands/app.rb @@ -18,7 +18,7 @@ def run(hostname: nil) "--detach", "--restart unless-stopped", "--name", container_name, - *config.network_args, + "--network", "kamal", *([ "--hostname", hostname ] if hostname), "-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"", "-e", "KAMAL_VERSION=\"#{config.version}\"", diff --git a/lib/kamal/commands/app/execution.rb b/lib/kamal/commands/app/execution.rb index 5770f7aa2..4434c26aa 100644 --- a/lib/kamal/commands/app/execution.rb +++ b/lib/kamal/commands/app/execution.rb @@ -11,7 +11,7 @@ def execute_in_new_container(*command, interactive: false, env:) docker :run, ("-it" if interactive), "--rm", - *config.network_args, + "--network", "kamal", *role&.env_args(host), *argumentize("--env", env), *config.volume_args, diff --git a/lib/kamal/commands/proxy.rb b/lib/kamal/commands/proxy.rb index 3399531fb..acff3dbd6 100644 --- a/lib/kamal/commands/proxy.rb +++ b/lib/kamal/commands/proxy.rb @@ -4,7 +4,7 @@ class Kamal::Commands::Proxy < Kamal::Commands::Base def run docker :run, "--name", container_name, - *config.network_args, + "--network", "kamal", "--detach", "--restart", "unless-stopped", "--volume", "kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy", diff --git a/lib/kamal/configuration.rb b/lib/kamal/configuration.rb index b26c30c9c..09f1ed895 100644 --- a/lib/kamal/configuration.rb +++ b/lib/kamal/configuration.rb @@ -18,7 +18,6 @@ class Kamal::Configuration PROXY_HTTP_PORT = 80 PROXY_HTTPS_PORT = 443 PROXY_LOG_MAX_SIZE = "10m" - NETWORK = "kamal" class << self def create_from(config_file:, destination: nil, version: nil) @@ -194,10 +193,6 @@ def logging_args logging.args end - def network_args - argumentize "--network", network - end - def readiness_delay raw_config.readiness_delay || 7 @@ -302,10 +297,6 @@ def to_h end private - def network - raw_config["network"] || NETWORK - end - # Will raise ArgumentError if any required config keys are missing def ensure_destination_if_required if require_destination? && destination.nil? diff --git a/lib/kamal/configuration/docs/configuration.yml b/lib/kamal/configuration/docs/configuration.yml index b59394a3d..6b0593468 100644 --- a/lib/kamal/configuration/docs/configuration.yml +++ b/lib/kamal/configuration/docs/configuration.yml @@ -176,10 +176,3 @@ logging: # Alias configuration, see kamal docs alias: aliases: ... - -# Network -# -# The network the application will be attached to. -# -# Defaults to kamal: -network: custom diff --git a/lib/kamal/configuration/docs/proxy.yml b/lib/kamal/configuration/docs/proxy.yml index 0af148261..76ec3e41d 100644 --- a/lib/kamal/configuration/docs/proxy.yml +++ b/lib/kamal/configuration/docs/proxy.yml @@ -103,10 +103,3 @@ proxy: # By default, kamal-proxy will not forward the headers if the `ssl` option is set to `true`, and # will forward them if it is set to `false`. forward_headers: true - - # Network - # - # The network the proxy container will be attached to. - # - # Defaults to kamal: - network: custom diff --git a/lib/kamal/configuration/proxy.rb b/lib/kamal/configuration/proxy.rb index 99c7aa1fa..6232c3e03 100644 --- a/lib/kamal/configuration/proxy.rb +++ b/lib/kamal/configuration/proxy.rb @@ -3,7 +3,6 @@ class Kamal::Configuration::Proxy DEFAULT_LOG_REQUEST_HEADERS = [ "Cache-Control", "Last-Modified", "User-Agent" ] CONTAINER_NAME = "kamal-proxy" - NETWORK = "kamal" delegate :argumentize, :optionize, to: Kamal::Utils @@ -52,10 +51,6 @@ def deploy_command_args(target:) optionize ({ target: "#{target}:#{app_port}" }).merge(deploy_options), with: "=" end - def network_args - argumentize "--network", network - end - def merge(other) self.class.new config: config, proxy_config: proxy_config.deep_merge(other.proxy_config) end @@ -64,8 +59,4 @@ def merge(other) def seconds_duration(value) value ? "#{value}s" : nil end - - def network - proxy_config["network"] || NETWORK - end end diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index 403e90d4e..0e5cad796 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -64,14 +64,6 @@ class CommandsAppTest < ActiveSupport::TestCase new_command.run.join(" ") end - test "run in custom network" do - @config[:network] = "custom" - - assert_equal \ - "docker run --detach --restart unless-stopped --name app-web-999 --network custom -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination dhh/app:999", - new_command.run.join(" ") - end - test "start" do assert_equal \ "docker start app-web-999", diff --git a/test/commands/proxy_test.rb b/test/commands/proxy_test.rb index 80f420d2d..b7cc9f3dc 100644 --- a/test/commands/proxy_test.rb +++ b/test/commands/proxy_test.rb @@ -27,14 +27,6 @@ class CommandsProxyTest < ActiveSupport::TestCase new_command.run.join(" ") end - test "run in custom network" do - @config[:network] = "custom" - - assert_equal \ - "docker run --name kamal-proxy --network custom --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy $(cat .kamal/proxy/options || echo \"--publish 80:80 --publish 443:443\") basecamp/kamal-proxy:#{Kamal::Configuration::PROXY_MINIMUM_VERSION}", - new_command.run.join(" ") - end - test "proxy start" do assert_equal \ "docker container start kamal-proxy", diff --git a/test/configuration/proxy_test.rb b/test/configuration/proxy_test.rb index 562c229f6..588e5a350 100644 --- a/test/configuration/proxy_test.rb +++ b/test/configuration/proxy_test.rb @@ -38,15 +38,6 @@ class ConfigurationProxyTest < ActiveSupport::TestCase assert_not config.proxy.ssl? end - test "network_args defaults" do - assert_equal [ "--network", "kamal" ], config.proxy.network_args - end - - test "network_args with configured options" do - @deploy[:proxy] = { "network" => "example" } - assert_equal [ "--network", "example" ], config.proxy.network_args - end - private def config Kamal::Configuration.new(@deploy) diff --git a/test/configuration_test.rb b/test/configuration_test.rb index da53ddb61..c1aaa6971 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -217,15 +217,6 @@ class ConfigurationTest < ActiveSupport::TestCase assert_equal [ "--log-driver", "\"local\"", "--log-opt", "max-size=\"100m\"", "--log-opt", "max-file=\"5\"" ], config.logging_args end - test "network_args default" do - assert_equal [ "--network", "kamal" ], @config.network_args - end - - test "network_args with configured options" do - config = Kamal::Configuration.new(@deploy.tap { |c| c.merge!(network: "custom") }) - assert_equal [ "--network", "custom" ], config.network_args - end - test "erb evaluation of yml config" do config = Kamal::Configuration.create_from config_file: Pathname.new(File.expand_path("fixtures/deploy.erb.yml", __dir__)) assert_equal "my-user", config.registry.username From 69b13ebc6a63ac00aaf126eedefa92344ff6b2a5 Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Wed, 2 Oct 2024 19:27:08 +0400 Subject: [PATCH 6/6] Renamed NETWORK to DEFAULT_NETWORK --- lib/kamal/configuration/accessory.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kamal/configuration/accessory.rb b/lib/kamal/configuration/accessory.rb index 7aed80eaf..3258c9d09 100644 --- a/lib/kamal/configuration/accessory.rb +++ b/lib/kamal/configuration/accessory.rb @@ -1,7 +1,7 @@ class Kamal::Configuration::Accessory include Kamal::Configuration::Validation - NETWORK = "kamal" + DEFAULT_NETWORK = "kamal" delegate :argumentize, :optionize, to: Kamal::Utils @@ -181,6 +181,6 @@ def hosts_from_roles end def network - accessory_config["network"] || NETWORK + accessory_config["network"] || DEFAULT_NETWORK end end