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 24, 2025
2 parents 160a521 + 5ddaa38 commit 7c86e94
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
run: bundle exec rubocop --parallel
tests:
strategy:
fail-fast: false
matrix:
ruby-version:
- "3.1"
Expand All @@ -37,7 +38,6 @@ jobs:
gemfile: gemfiles/rails_edge.gemfile
name: ${{ format('Tests (Ruby {0})', matrix.ruby-version) }}
runs-on: ubuntu-latest
continue-on-error: true
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}
steps:
Expand Down
3 changes: 0 additions & 3 deletions lib/kamal/cli/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ def reboot
execute *KAMAL.auditor.record("Rebooted proxy"), verbosity: :debug
execute *KAMAL.registry.login

"Stopping and removing Traefik on #{host}, if running..."
execute *KAMAL.proxy.cleanup_traefik

"Stopping and removing kamal-proxy on #{host}, if running..."
execute *KAMAL.proxy.stop, raise_on_non_zero_exit: false
execute *KAMAL.proxy.remove_container
Expand Down
4 changes: 2 additions & 2 deletions lib/kamal/cli/templates/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ builder:
arch: amd64
# Pass in additional build args needed for your Dockerfile.
# args:
# RUBY_VERSION: <%= File.read('.ruby-version').strip %>
# RUBY_VERSION: <%= ENV["RBENV_VERSION"] || ENV["rvm_ruby_string"] || "#{RUBY_ENGINE}-#{RUBY_ENGINE_VERSION}" %>

# Inject ENV variables into containers (secrets come from .kamal/secrets).
#
Expand All @@ -49,7 +49,7 @@ builder:
# - RAILS_MASTER_KEY

# Aliases are triggered with "bin/kamal <alias>". You can overwrite arguments on invocation:
# "bin/kamal logs -r job" will tail logs from the first server in the job section.
# "bin/kamal app logs -r job" will tail logs from the first server in the job section.
#
# aliases:
# shell: app exec --interactive --reuse "bash"
Expand Down
8 changes: 7 additions & 1 deletion lib/kamal/commands/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Kamal::Commands::Builder < Kamal::Commands::Base
delegate :create, :remove, :push, :clean, :pull, :info, :inspect_builder, :validate_image, :first_mirror, to: :target
delegate :local?, :remote?, to: "config.builder"
delegate :local?, :remote?, :cloud?, to: "config.builder"

include Clone

Expand All @@ -17,6 +17,8 @@ def target
else
remote
end
elsif cloud?
cloud
else
local
end
Expand All @@ -33,4 +35,8 @@ def local
def hybrid
@hybrid ||= Kamal::Commands::Builder::Hybrid.new(config)
end

def cloud
@cloud ||= Kamal::Commands::Builder::Cloud.new(config)
end
end
22 changes: 22 additions & 0 deletions lib/kamal/commands/builder/cloud.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Kamal::Commands::Builder::Cloud < Kamal::Commands::Builder::Base
# Expects `driver` to be of format "cloud docker-org-name/builder-name"

def create
docker :buildx, :create, "--driver", driver
end

def remove
docker :buildx, :rm, builder_name
end

private
def builder_name
driver.gsub(/[ \/]/, "-")
end

def inspect_buildx
pipe \
docker(:buildx, :inspect, builder_name),
grep("-q", "Endpoint:.*cloud://.*")
end
end
4 changes: 4 additions & 0 deletions lib/kamal/configuration/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def local?
!local_disabled? && (arches.empty? || local_arches.any?)
end

def cloud?
driver.start_with? "cloud"
end

def cached?
!!builder_config["cache"]
end
Expand Down
3 changes: 3 additions & 0 deletions lib/kamal/configuration/docs/builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ builder:
#
# The build driver to use, defaults to `docker-container`:
driver: docker
#
# If you want to use Docker Build Cloud (https://www.docker.com/products/build-cloud/), you can set the driver to:
driver: cloud org-name/builder-name

# Provenance
#
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/secrets/adapters/last_pass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def fetch_secrets(secrets, from:, account:, session:)
end

if (missing_items = secrets - results.keys).any?
raise RuntimeError, "Could not find #{missing_items.join(", ")} in LassPass"
raise RuntimeError, "Could not find #{missing_items.join(", ")} in LastPass"
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/cli/build_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ class CliBuildTest < CliTestCase
end
end

test "create cloud" do
run_command("create", fixture: :with_cloud_builder).tap do |output|
assert_match /docker buildx create --driver cloud example_org\/cloud_builder/, output
end
end

test "create with error" do
stub_setup
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
Expand All @@ -252,6 +258,12 @@ class CliBuildTest < CliTestCase
end
end

test "remove cloud" do
run_command("remove", fixture: :with_cloud_builder).tap do |output|
assert_match /docker buildx rm cloud-example_org-cloud_builder/, output
end
end

test "details" do
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
.with(:docker, :context, :ls, "&&", :docker, :buildx, :ls)
Expand Down
2 changes: 0 additions & 2 deletions test/cli/proxy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ class CliProxyTest < CliTestCase

run_command("reboot", "-y").tap do |output|
assert_match "docker container stop kamal-proxy on 1.1.1.1", output
assert_match "Running docker container stop traefik ; docker container prune --force --filter label=org.opencontainers.image.title=Traefik && docker image prune --all --force --filter label=org.opencontainers.image.title=Traefik on 1.1.1.1", output
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy on 1.1.1.1", output
assert_match "docker run --name kamal-proxy --network kamal --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 --log-opt max-size=10m\") #{KAMAL.config.proxy_image} on 1.1.1.1", output
assert_match "docker exec kamal-proxy kamal-proxy deploy app-web --target=\"abcdefabcdef:80\" --deploy-timeout=\"6s\" --drain-timeout=\"30s\" --buffer-requests --buffer-responses --log-request-header=\"Cache-Control\" --log-request-header=\"Last-Modified\" --log-request-header=\"User-Agent\" on 1.1.1.1", output

assert_match "docker container stop kamal-proxy on 1.1.1.2", output
assert_match "Running docker container stop traefik ; docker container prune --force --filter label=org.opencontainers.image.title=Traefik && docker image prune --all --force --filter label=org.opencontainers.image.title=Traefik on 1.1.1.2", output
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy on 1.1.1.2", output
assert_match "docker run --name kamal-proxy --network kamal --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 --log-opt max-size=10m\") #{KAMAL.config.proxy_image} on 1.1.1.2", output
assert_match "docker exec kamal-proxy kamal-proxy deploy app-web --target=\"abcdefabcdef:80\" --deploy-timeout=\"6s\" --drain-timeout=\"30s\" --buffer-requests --buffer-responses --log-request-header=\"Cache-Control\" --log-request-header=\"Last-Modified\" --log-request-header=\"User-Agent\" on 1.1.1.2", output
Expand Down
8 changes: 8 additions & 0 deletions test/commands/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class CommandsBuilderTest < ActiveSupport::TestCase
builder.push.join(" ")
end

test "cloud builder" do
builder = new_builder_command(builder: { "arch" => [ "#{local_arch}" ], "driver" => "cloud docker-org-name/builder-name" })
assert_equal "cloud", builder.name
assert_equal \
"docker buildx build --push --platform linux/#{local_arch} --builder cloud-docker-org-name-builder-name -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile .",
builder.push.join(" ")
end

test "build args" do
builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } })
assert_equal \
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/deploy_with_cloud_builder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
service: app
image: dhh/app
servers:
web:
- "1.1.1.1"
- "1.1.1.2"
workers:
- "1.1.1.3"
- "1.1.1.4"
registry:
username: user
password: pw

accessories:
mysql:
image: mysql:5.7
host: 1.1.1.3
port: 3306
env:
clear:
MYSQL_ROOT_HOST: '%'
secret:
- MYSQL_ROOT_PASSWORD
files:
- test/fixtures/files/my.cnf:/etc/mysql/my.cnf
directories:
- data:/var/lib/mysql
redis:
image: redis:latest
roles:
- web
port: 6379
directories:
- data:/data

readiness_delay: 0

builder:
arch: <%= Kamal::Utils.docker_arch == "arm64" ? "amd64" : "arm64" %>
driver: cloud example_org/cloud_builder

0 comments on commit 7c86e94

Please sign in to comment.