Skip to content

Commit

Permalink
Merge branch 'basecamp:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
akumajoe authored Jan 18, 2025
2 parents cf650fb + a388937 commit 160a521
Show file tree
Hide file tree
Showing 54 changed files with 1,154 additions and 196 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- "3.1"
- "3.2"
- "3.3"
- "3.4.0-preview2"
- "3.4"
gemfile:
- Gemfile
- gemfiles/rails_edge.gemfile
Expand Down
1 change: 1 addition & 0 deletions lib/kamal/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Kamal::Cli
class BootError < StandardError; end
class HookError < StandardError; end
class LockError < StandardError; end
class DependencyError < StandardError; end
end

# SSHKit uses instance eval, so we need a global const for ergonomics
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/cli/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def remove_accessory(name)
def prepare(name)
with_accessory(name) do |accessory, hosts|
on(hosts) do
execute *KAMAL.registry.login
execute *KAMAL.registry.login(registry_config: accessory.registry)
execute *KAMAL.docker.create_network
rescue SSHKit::Command::Failed => e
raise unless e.message.include?("already exists")
Expand Down
19 changes: 17 additions & 2 deletions lib/kamal/cli/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Kamal::Cli
class Base < Thor
include SSHKit::DSL

def self.exit_on_failure?() false end
def self.exit_on_failure?() true end
def self.dynamic_command_class() Kamal::Cli::Alias::Command end

class_option :verbose, type: :boolean, aliases: "-v", desc: "Detailed logging"
Expand All @@ -30,7 +30,8 @@ def initialize(args = [], local_options = {}, config = {})
else
super
end
initialize_commander unless KAMAL.configured?

initialize_commander unless config[:invoked_via_subcommand]
end

private
Expand Down Expand Up @@ -194,5 +195,19 @@ def with_env(env)
ENV.clear
ENV.update(current_env)
end

def ensure_docker_installed
run_locally do
begin
execute *KAMAL.builder.ensure_docker_installed
rescue SSHKit::Command::Failed => e
error = e.message =~ /command not found/ ?
"Docker is not installed locally" :
"Docker buildx plugin is not installed locally"

raise DependencyError, error
end
end
end
end
end
16 changes: 1 addition & 15 deletions lib/kamal/cli/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def deliver
def push
cli = self

verify_local_dependencies
ensure_docker_installed
run_hook "pre-build"

uncommitted_changes = Kamal::Git.uncommitted_changes
Expand Down Expand Up @@ -109,20 +109,6 @@ def details
end

private
def verify_local_dependencies
run_locally do
begin
execute *KAMAL.builder.ensure_local_dependencies_installed
rescue SSHKit::Command::Failed => e
build_error = e.message =~ /command not found/ ?
"Docker is not installed locally" :
"Docker buildx plugin is not installed locally"

raise BuildError, build_error
end
end
end

def connect_to_remote_host(remote_host)
remote_uri = URI.parse(remote_host)
if remote_uri.scheme == "ssh"
Expand Down
7 changes: 4 additions & 3 deletions lib/kamal/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ def setup
say "Ensure Docker is installed...", :magenta
invoke "kamal:cli:server:bootstrap", [], invoke_options

invoke "kamal:cli:accessory:boot", [ "all" ], invoke_options
deploy
deploy(boot_accessories: true)
end
end
end

desc "deploy", "Deploy app to servers"
option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip image build and push"
def deploy
def deploy(boot_accessories: false)
runtime = print_runtime do
invoke_options = deploy_options

Expand All @@ -38,6 +37,8 @@ def deploy
say "Ensure kamal-proxy is running...", :magenta
invoke "kamal:cli:proxy:boot", [], invoke_options

invoke "kamal:cli:accessory:boot", [ "all" ], invoke_options if boot_accessories

say "Detect stale containers...", :magenta
invoke "kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true)

Expand Down
3 changes: 2 additions & 1 deletion lib/kamal/cli/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def boot

desc "boot_config <set|get|reset>", "Manage kamal-proxy boot configuration"
option :publish, type: :boolean, default: true, desc: "Publish the proxy ports on the host"
option :publish_host_ip, type: :string, repeatable: true, default: nil, desc: "Host IP address to bind HTTP/HTTPS traffic to. Defaults to all interfaces"
option :http_port, type: :numeric, default: Kamal::Configuration::PROXY_HTTP_PORT, desc: "HTTP port to publish on the host"
option :https_port, type: :numeric, default: Kamal::Configuration::PROXY_HTTPS_PORT, desc: "HTTPS port to publish on the host"
option :log_max_size, type: :string, default: Kamal::Configuration::PROXY_LOG_MAX_SIZE, desc: "Max size of proxy logs"
Expand All @@ -31,7 +32,7 @@ def boot_config(subcommand)
case subcommand
when "set"
boot_options = [
*(KAMAL.config.proxy_publish_args(options[:http_port], options[:https_port]) if options[:publish]),
*(KAMAL.config.proxy_publish_args(options[:http_port], options[:https_port], options[:publish_host_ip]) if options[:publish]),
*(KAMAL.config.proxy_logging_args(options[:log_max_size])),
*options[:docker_options].map { |option| "--#{option}" }
]
Expand Down
2 changes: 2 additions & 0 deletions lib/kamal/cli/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Kamal::Cli::Registry < Kamal::Cli::Base
option :skip_local, aliases: "-L", type: :boolean, default: false, desc: "Skip local login"
option :skip_remote, aliases: "-R", type: :boolean, default: false, desc: "Skip remote login"
def login
ensure_docker_installed

run_locally { execute *KAMAL.registry.login } unless options[:skip_local]
on(KAMAL.hosts) { execute *KAMAL.registry.login } unless options[:skip_remote]
end
Expand Down
6 changes: 0 additions & 6 deletions lib/kamal/commander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ def accessory_names
config.accessories&.collect(&:name) || []
end

def accessories_on(host)
config.accessories.select { |accessory| accessory.hosts.include?(host.to_s) }.map(&:name)
end


def app(role: nil, host: nil)
Kamal::Commands::App.new(config, role: role, host: host)
end
Expand Down Expand Up @@ -129,7 +124,6 @@ def alias(name)
config.aliases[name]
end


def with_verbosity(level)
old_level = self.verbosity

Expand Down
6 changes: 1 addition & 5 deletions lib/kamal/commands/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ class Kamal::Commands::Accessory < Kamal::Commands::Base
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?,
: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)
@accessory_config = config.accessory(name)
Expand Down Expand Up @@ -42,7 +41,6 @@ def info
docker :ps, *service_filter
end


def logs(timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil)
pipe \
docker(:logs, service_name, (" --since #{since}" if since), (" --tail #{lines}" if lines), ("--timestamps" if timestamps), "2>&1"),
Expand All @@ -56,7 +54,6 @@ def follow_logs(timestamps: true, grep: nil, grep_options: nil)
(%(grep "#{grep}"#{" #{grep_options}" if grep_options}) if grep)
end


def execute_in_existing_container(*command, interactive: false)
docker :exec,
("-it" if interactive),
Expand Down Expand Up @@ -87,7 +84,6 @@ def run_over_ssh(command)
super command, host: hosts.first
end


def ensure_local_file_present(local_file)
if !local_file.is_a?(StringIO) && !Pathname.new(local_file).exist?
raise "Missing file: #{local_file}"
Expand Down
8 changes: 4 additions & 4 deletions lib/kamal/commands/app/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ def extract_assets

combine \
make_directory(role.asset_extracted_directory),
[ *docker(:stop, "-t 1", asset_container, "2> /dev/null"), "|| true" ],
docker(:run, "--name", asset_container, "--detach", "--rm", "--entrypoint", "sleep", config.absolute_image, "1000000"),
docker(:cp, "-L", "#{asset_container}:#{role.asset_path}/.", role.asset_extracted_directory),
docker(:stop, "-t 1", asset_container),
[ *docker(:container, :rm, asset_container, "2> /dev/null"), "|| true" ],
docker(:container, :create, "--name", asset_container, config.absolute_image),
docker(:container, :cp, "-L", "#{asset_container}:#{role.asset_path}/.", role.asset_extracted_directory),
docker(:container, :rm, asset_container),
by: "&&"
end

Expand Down
14 changes: 14 additions & 0 deletions lib/kamal/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def remove_file(path)
[ :rm, path ]
end

def ensure_docker_installed
combine \
ensure_local_docker_installed,
ensure_local_buildx_installed
end

private
def combine(*commands, by: "&&")
commands
Expand Down Expand Up @@ -104,5 +110,13 @@ def ssh_keys
" -i #{key}"
end
end

def ensure_local_docker_installed
docker "--version"
end

def ensure_local_buildx_installed
docker :buildx, "version"
end
end
end
20 changes: 0 additions & 20 deletions lib/kamal/commands/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,4 @@ def local
def hybrid
@hybrid ||= Kamal::Commands::Builder::Hybrid.new(config)
end


def ensure_local_dependencies_installed
if name.native?
ensure_local_docker_installed
else
combine \
ensure_local_docker_installed,
ensure_local_buildx_installed
end
end

private
def ensure_local_docker_installed
docker "--version"
end

def ensure_local_buildx_installed
docker :buildx, "version"
end
end
16 changes: 9 additions & 7 deletions lib/kamal/commands/registry.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
class Kamal::Commands::Registry < Kamal::Commands::Base
delegate :registry, to: :config
def login(registry_config: nil)
registry_config ||= config.registry

def login
docker :login,
registry.server,
"-u", sensitive(Kamal::Utils.escape_shell_value(registry.username)),
"-p", sensitive(Kamal::Utils.escape_shell_value(registry.password))
registry_config.server,
"-u", sensitive(Kamal::Utils.escape_shell_value(registry_config.username)),
"-p", sensitive(Kamal::Utils.escape_shell_value(registry_config.password))
end

def logout
docker :logout, registry.server
def logout(registry_config: nil)
registry_config ||= config.registry

docker :logout, registry_config.server
end
end
Loading

0 comments on commit 160a521

Please sign in to comment.