From 713785035415e44bc018127c0ad39ab65d9c9f1e Mon Sep 17 00:00:00 2001 From: Matthew Kent Date: Sun, 12 Nov 2023 23:22:08 -0800 Subject: [PATCH] Add support for wildcard matches with '*' on roles and hosts. eg: --roles=*_chicago,*_tokyo --hosts=app-* Useful for targeted deploys. --- lib/kamal/commander.rb | 4 ++-- lib/kamal/utils.rb | 16 ++++++++++++++++ test/commander_test.rb | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/kamal/commander.rb b/lib/kamal/commander.rb index 9ba3be12..d97f1991 100644 --- a/lib/kamal/commander.rb +++ b/lib/kamal/commander.rb @@ -28,11 +28,11 @@ class Kamal::Commander end def specific_roles=(role_names) - @specific_roles = config.roles.select { |r| role_names.include?(r.name) } if role_names.present? + @specific_roles = Kamal::Utils.filter_specific_items(role_names, config.roles) if role_names.present? end def specific_hosts=(hosts) - @specific_hosts = config.all_hosts & hosts if hosts.present? + @specific_hosts = Kamal::Utils.filter_specific_items(hosts, config.all_hosts) if hosts.present? end def primary_host diff --git a/lib/kamal/utils.rb b/lib/kamal/utils.rb index da16633e..54185cf4 100644 --- a/lib/kamal/utils.rb +++ b/lib/kamal/utils.rb @@ -58,4 +58,20 @@ module Kamal::Utils .gsub(/`/, '\\\\`') .gsub(DOLLAR_SIGN_WITHOUT_SHELL_EXPANSION_REGEX, '\$') end + + # Apply a list of host or role filters, including wildcard matches + def filter_specific_items(filters, items) + matches = [] + + Array(filters).select do |filter| + matches += Array(items).select do |item| + # Only allow * for a wildcard + pattern = Regexp.escape(filter).gsub('\*', '.*') + # items are roles or hosts + (item.respond_to?(:name) ? item.name : item).match(/^#{pattern}$/) + end + end + + matches + end end diff --git a/test/commander_test.rb b/test/commander_test.rb index 2290e0f0..06cc5bbb 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -14,6 +14,18 @@ class CommanderTest < ActiveSupport::TestCase @kamal.specific_hosts = [ "1.1.1.1", "1.1.1.2" ] assert_equal [ "1.1.1.1", "1.1.1.2" ], @kamal.hosts + + @kamal.specific_hosts = [ "1.1.1.1*" ] + assert_equal [ "1.1.1.1" ], @kamal.hosts + + @kamal.specific_hosts = [ "1.1.1.*", "*.1.2.*" ] + assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @kamal.hosts + + @kamal.specific_hosts = [ "*" ] + assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @kamal.hosts + + @kamal.specific_hosts = [ "*miss" ] + assert_equal [], @kamal.hosts end test "filtering hosts by filtering roles" do @@ -28,6 +40,18 @@ class CommanderTest < ActiveSupport::TestCase @kamal.specific_roles = [ "workers" ] assert_equal [ "workers" ], @kamal.roles.map(&:name) + + @kamal.specific_roles = [ "w*" ] + assert_equal [ "web", "workers" ], @kamal.roles.map(&:name) + + @kamal.specific_roles = [ "we*", "*orkers" ] + assert_equal [ "web", "workers" ], @kamal.roles.map(&:name) + + @kamal.specific_roles = [ "*" ] + assert_equal [ "web", "workers" ], @kamal.roles.map(&:name) + + @kamal.specific_roles = [ "*miss" ] + assert_equal [], @kamal.roles.map(&:name) end test "filtering roles by filtering hosts" do