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

[Improvement] Extract proxy commands for deploy and remove service. #1364

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/kamal/commands/accessory.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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}" ]
Expand Down
16 changes: 0 additions & 16 deletions lib/kamal/commands/accessory/proxy.rb

This file was deleted.

11 changes: 9 additions & 2 deletions lib/kamal/commands/app.rb
Original file line number Diff line number Diff line change
@@ -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 ]

Expand Down Expand Up @@ -50,7 +50,6 @@ def info
docker :ps, *container_filter_args
end


def current_running_container_id
current_running_container(format: "--quiet")
end
Expand All @@ -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}}'"
Expand Down
16 changes: 0 additions & 16 deletions lib/kamal/commands/app/proxy.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/kamal/commands/builder/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down
28 changes: 18 additions & 10 deletions lib/kamal/commands/proxy.rb
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
10 changes: 1 addition & 9 deletions lib/kamal/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/kamal/configuration/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions lib/kamal/secrets/adapters/one_password.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading