From f69c45b7ead7b3786df9702504608e144cf647cc Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Tue, 7 Nov 2023 15:43:30 +0000 Subject: [PATCH] Add docker-setup hook This allows the user to make any necessary configuration changes to Docker before setting up any containers, allowing those configuration changes to take effect from the outset. --- lib/kamal/cli/server.rb | 2 ++ lib/kamal/cli/templates/sample_hooks/docker-setup.sample | 7 +++++++ test/cli/server_test.rb | 3 +++ .../docker/deployer/app/.kamal/hooks/docker-setup | 3 +++ 4 files changed, 15 insertions(+) create mode 100644 lib/kamal/cli/templates/sample_hooks/docker-setup.sample create mode 100755 test/integration/docker/deployer/app/.kamal/hooks/docker-setup diff --git a/lib/kamal/cli/server.rb b/lib/kamal/cli/server.rb index 6dcc6b02..e4d270f3 100644 --- a/lib/kamal/cli/server.rb +++ b/lib/kamal/cli/server.rb @@ -19,5 +19,7 @@ class Kamal::Cli::Server < Kamal::Cli::Base if missing.any? raise "Docker is not installed on #{missing.join(", ")} and can't be automatically installed without having root access and the `curl` command available. Install Docker manually: https://docs.docker.com/engine/install/" end + + run_hook "docker-setup" end end diff --git a/lib/kamal/cli/templates/sample_hooks/docker-setup.sample b/lib/kamal/cli/templates/sample_hooks/docker-setup.sample new file mode 100644 index 00000000..fe68b937 --- /dev/null +++ b/lib/kamal/cli/templates/sample_hooks/docker-setup.sample @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby + +# A sample docker-setup hook +# +# Sets up a Docker network which can then be used by the application’s containers + +ssh user@example.com docker network create kamal diff --git a/test/cli/server_test.rb b/test/cli/server_test.rb index 1c8a2607..04cdc312 100644 --- a/test/cli/server_test.rb +++ b/test/cli/server_test.rb @@ -23,10 +23,13 @@ class CliServerTest < CliTestCase SSHKit::Backend::Abstract.any_instance.expects(:execute).with('[ "${EUID:-$(id -u)}" -eq 0 ] || command -v sudo >/dev/null || command -v su >/dev/null', raise_on_non_zero_exit: false).returns(true).at_least_once SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:curl, "-fsSL", "https://get.docker.com", "|", :sh).at_least_once SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:mkdir, "-p", ".kamal").returns("").at_least_once + Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true) + SSHKit::Backend::Abstract.any_instance.expects(:execute).with(".kamal/hooks/docker-setup", anything).at_least_once run_command("bootstrap").tap do |output| ("1.1.1.1".."1.1.1.4").map do |host| assert_match "Missing Docker on #{host}. Installing…", output + assert_match "Running the docker-setup hook", output end end end diff --git a/test/integration/docker/deployer/app/.kamal/hooks/docker-setup b/test/integration/docker/deployer/app/.kamal/hooks/docker-setup new file mode 100755 index 00000000..2c218dc5 --- /dev/null +++ b/test/integration/docker/deployer/app/.kamal/hooks/docker-setup @@ -0,0 +1,3 @@ +#!/bin/sh +echo "Docker set up!" +mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/docker-setup