diff --git a/lib/kamal/commands/accessory.rb b/lib/kamal/commands/accessory.rb index 77ceb607b..30af20475 100644 --- a/lib/kamal/commands/accessory.rb +++ b/lib/kamal/commands/accessory.rb @@ -1,12 +1,10 @@ class Kamal::Commands::Accessory < Kamal::Commands::Base - include Proxy - attr_reader :accessory_config + delegate :service_name, :image, :hosts, :port, :files, :directories, :cmd, :network_args, :publish_args, :env_args, :volume_args, :label_args, :option_args, :secrets_io, :secrets_path, :env_directory, :proxy, :running_proxy?, :registry, to: :accessory_config - delegate :proxy_container_name, to: :config def initialize(config, name:) super(config) @@ -106,6 +104,14 @@ def ensure_env_directory make_directory env_directory end + def deploy(target:) + KAMAL.proxy.deploy_service(service_name, proxy_config: proxy, target: target) + end + + def remove + KAMAL.proxy.remove_service(service_name) + end + private def service_filter [ "--filter", "label=service=#{service_name}" ] diff --git a/lib/kamal/commands/accessory/proxy.rb b/lib/kamal/commands/accessory/proxy.rb deleted file mode 100644 index 195a321bf..000000000 --- a/lib/kamal/commands/accessory/proxy.rb +++ /dev/null @@ -1,16 +0,0 @@ -module Kamal::Commands::Accessory::Proxy - delegate :proxy_container_name, to: :config - - def deploy(target:) - proxy_exec :deploy, service_name, *proxy.deploy_command_args(target: target) - end - - def remove - proxy_exec :remove, service_name - end - - private - def proxy_exec(*command) - docker :exec, proxy_container_name, "kamal-proxy", *command - end -end diff --git a/lib/kamal/commands/app.rb b/lib/kamal/commands/app.rb index 6c4df0e4a..e2b4126cc 100644 --- a/lib/kamal/commands/app.rb +++ b/lib/kamal/commands/app.rb @@ -1,5 +1,5 @@ class Kamal::Commands::App < Kamal::Commands::Base - include Assets, Containers, Execution, Images, Logging, Proxy + include Assets, Containers, Execution, Images, Logging ACTIVE_DOCKER_STATUSES = [ :running, :restarting ] @@ -50,7 +50,6 @@ def info docker :ps, *container_filter_args end - def current_running_container_id current_running_container(format: "--quiet") end @@ -75,6 +74,14 @@ def ensure_env_directory make_directory role.env_directory end + def deploy(target:) + KAMAL.proxy.deploy_service(role.container_prefix, proxy_config: role.proxy, target: target) + end + + def remove + KAMAL.proxy.remove_service(role.container_prefix) + end + private def latest_image_id docker :image, :ls, *argumentize("--filter", "reference=#{config.latest_image}"), "--format", "'{{.ID}}'" diff --git a/lib/kamal/commands/app/proxy.rb b/lib/kamal/commands/app/proxy.rb deleted file mode 100644 index 777a4aaf5..000000000 --- a/lib/kamal/commands/app/proxy.rb +++ /dev/null @@ -1,16 +0,0 @@ -module Kamal::Commands::App::Proxy - delegate :proxy_container_name, to: :config - - def deploy(target:) - proxy_exec :deploy, role.container_prefix, *role.proxy.deploy_command_args(target: target) - end - - def remove - proxy_exec :remove, role.container_prefix - end - - private - def proxy_exec(*command) - docker :exec, proxy_container_name, "kamal-proxy", *command - end -end diff --git a/lib/kamal/commands/builder/base.rb b/lib/kamal/commands/builder/base.rb index dea04a3a3..b0d1dd0ea 100644 --- a/lib/kamal/commands/builder/base.rb +++ b/lib/kamal/commands/builder/base.rb @@ -3,7 +3,6 @@ class BuilderError < StandardError; end ENDPOINT_DOCKER_HOST_INSPECT = "'{{.Endpoints.docker.Host}}'" - delegate :argumentize, to: Kamal::Utils delegate \ :args, :secrets, :dockerfile, :target, :arches, :local_arches, :remote_arches, :remote, :cache_from, :cache_to, :ssh, :provenance, :sbom, :driver, :docker_driver?, diff --git a/lib/kamal/commands/proxy.rb b/lib/kamal/commands/proxy.rb index acff3dbd6..2d1d79311 100644 --- a/lib/kamal/commands/proxy.rb +++ b/lib/kamal/commands/proxy.rb @@ -1,9 +1,9 @@ class Kamal::Commands::Proxy < Kamal::Commands::Base - delegate :argumentize, :optionize, to: Kamal::Utils + CONTAINER_NAME = "kamal-proxy" def run docker :run, - "--name", container_name, + "--name", CONTAINER_NAME, "--network", "kamal", "--detach", "--restart", "unless-stopped", @@ -13,10 +13,10 @@ def run end def start - docker :container, :start, container_name + docker :container, :start, CONTAINER_NAME end - def stop(name: container_name) + def stop(name: CONTAINER_NAME) docker :container, :stop, name end @@ -25,24 +25,24 @@ def start_or_run end def info - docker :ps, "--filter", "name=^#{container_name}$" + docker :ps, "--filter", "name=^#{CONTAINER_NAME}$" end def version pipe \ - docker(:inspect, container_name, "--format '{{.Config.Image}}'"), + docker(:inspect, CONTAINER_NAME, "--format '{{.Config.Image}}'"), [ :cut, "-d:", "-f2" ] end def logs(timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil) pipe \ - docker(:logs, container_name, ("--since #{since}" if since), ("--tail #{lines}" if lines), ("--timestamps" if timestamps), "2>&1"), + docker(:logs, CONTAINER_NAME, ("--since #{since}" if since), ("--tail #{lines}" if lines), ("--timestamps" if timestamps), "2>&1"), ("grep '#{grep}'#{" #{grep_options}" if grep_options}" if grep) end def follow_logs(host:, timestamps: true, grep: nil, grep_options: nil) run_over_ssh pipe( - docker(:logs, container_name, ("--timestamps" if timestamps), "--tail", "10", "--follow", "2>&1"), + docker(:logs, CONTAINER_NAME, ("--timestamps" if timestamps), "--tail", "10", "--follow", "2>&1"), (%(grep "#{grep}"#{" #{grep_options}" if grep_options}) if grep) ).join(" "), host: host end @@ -80,8 +80,16 @@ def reset_boot_options remove_file config.proxy_options_file end + def deploy_service(name, proxy_config:, target:) + exec :deploy, name, *proxy_config.deploy_command_args(target: target) + end + + def remove_service(name) + exec :remove, name + end + private - def container_name - config.proxy_container_name + def exec(*command) + docker :exec, CONTAINER_NAME, "kamal-proxy", *command end end diff --git a/lib/kamal/configuration.rb b/lib/kamal/configuration.rb index 023a2d7c3..48db72211 100644 --- a/lib/kamal/configuration.rb +++ b/lib/kamal/configuration.rb @@ -7,7 +7,7 @@ class Kamal::Configuration delegate :service, :image, :labels, :hooks_path, to: :raw_config, allow_nil: true - delegate :argumentize, :optionize, to: Kamal::Utils + delegate :argumentize, to: Kamal::Utils attr_reader :destination, :raw_config, :secrets attr_reader :accessories, :aliases, :boot, :builder, :env, :logging, :proxy, :servers, :ssh, :sshkit, :registry @@ -141,10 +141,6 @@ def proxy_roles roles.select(&:running_proxy?) end - def proxy_role_names - proxy_roles.flat_map(&:name) - end - def proxy_hosts proxy_roles.flat_map(&:hosts).uniq end @@ -265,10 +261,6 @@ def proxy_image "basecamp/kamal-proxy:#{PROXY_MINIMUM_VERSION}" end - def proxy_container_name - "kamal-proxy" - end - def proxy_directory File.join run_directory, "proxy" end diff --git a/lib/kamal/configuration/proxy.rb b/lib/kamal/configuration/proxy.rb index 6232c3e03..4ed2ca850 100644 --- a/lib/kamal/configuration/proxy.rb +++ b/lib/kamal/configuration/proxy.rb @@ -2,9 +2,8 @@ class Kamal::Configuration::Proxy include Kamal::Configuration::Validation DEFAULT_LOG_REQUEST_HEADERS = [ "Cache-Control", "Last-Modified", "User-Agent" ] - CONTAINER_NAME = "kamal-proxy" - delegate :argumentize, :optionize, to: Kamal::Utils + delegate :optionize, to: Kamal::Utils attr_reader :config, :proxy_config diff --git a/lib/kamal/secrets/adapters/one_password.rb b/lib/kamal/secrets/adapters/one_password.rb index fe4543422..3c706e090 100644 --- a/lib/kamal/secrets/adapters/one_password.rb +++ b/lib/kamal/secrets/adapters/one_password.rb @@ -1,6 +1,4 @@ class Kamal::Secrets::Adapters::OnePassword < Kamal::Secrets::Adapters::Base - delegate :optionize, to: Kamal::Utils - private def login(account) unless loggedin?(account)