Skip to content

Commit

Permalink
Added commands to deploy accessory to kamal-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-alexandrov committed Sep 26, 2024
1 parent 4a13803 commit 3030405
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/kamal/cli/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def boot(name, prepare: true)
execute *accessory.ensure_env_directory
upload! accessory.secrets_io, accessory.secrets_path, mode: "0600"
execute *accessory.run
execute *accessory.deploy if accessory.running_proxy?
end
end
end
Expand Down Expand Up @@ -73,6 +74,7 @@ def start(name)
on(hosts) do
execute *KAMAL.auditor.record("Started #{name} accessory"), verbosity: :debug
execute *accessory.start
execute *accessory.deploy if accessory.running_proxy?
end
end
end
Expand All @@ -85,6 +87,7 @@ def stop(name)
on(hosts) do
execute *KAMAL.auditor.record("Stopped #{name} accessory"), verbosity: :debug
execute *accessory.stop, raise_on_non_zero_exit: false
# execute *accessory.remove if accessory.running_proxy?
end
end
end
Expand Down
18 changes: 17 additions & 1 deletion lib/kamal/commands/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ 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,
:secrets_io, :secrets_path, :env_directory,
:secrets_io, :secrets_path, :env_directory, :running_proxy?,
to: :accessory_config
delegate :proxy_container_name, to: :config


def initialize(config, name:)
super(config)
Expand Down Expand Up @@ -38,6 +40,16 @@ def info
docker :ps, *service_filter
end

def deploy
target = container_id_for(container_name: service_name, only_running: true)
proxy_exec :deploy, service_name, *accessory_config.proxy.deploy_command_args(target: target) if target
end

def remove
target = container_id_for(container_name: service_name, only_running: true)
proxy_exec :remove, service_name, *accessory_config.proxy.remove_command_args(target: target) if target
end


def logs(timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil)
pipe \
Expand Down Expand Up @@ -110,4 +122,8 @@ def ensure_env_directory
def service_filter
[ "--filter", "label=service=#{service_name}" ]
end

def proxy_exec(*command)
docker :exec, proxy_container_name, "kamal-proxy", *command
end
end
15 changes: 14 additions & 1 deletion lib/kamal/configuration/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Kamal::Configuration::Accessory

delegate :argumentize, :optionize, to: Kamal::Utils

attr_reader :name, :accessory_config, :env
attr_reader :name, :accessory_config, :env, :proxy

def initialize(name, config:)
@name, @config, @accessory_config = name.inquiry, config, config.raw_config["accessories"][name]
Expand All @@ -18,6 +18,8 @@ def initialize(name, config:)
config: accessory_config.fetch("env", {}),
secrets: config.secrets,
context: "accessories/#{name}/env"

# initialize_proxy if running_proxy?
end

def service_name
Expand Down Expand Up @@ -100,6 +102,17 @@ def cmd
accessory_config["cmd"]
end

def running_proxy?
@accessory_config["proxy"].present?
end

def initialize_proxy
@proxy = Kamal::Configuration::Proxy.new \
config: config,
proxy_config: accessory_config["proxy"],
context: "accessories/#{name}/proxy"
end

private
attr_accessor :config

Expand Down

0 comments on commit 3030405

Please sign in to comment.