diff --git a/lib/mrsk/commander.rb b/lib/mrsk/commander.rb index be2fd2e3..ad61e895 100644 --- a/lib/mrsk/commander.rb +++ b/lib/mrsk/commander.rb @@ -14,22 +14,38 @@ class Mrsk::Commander .tap { |config| configure_sshkit_with(config) } end - attr_accessor :specific_hosts + attr_reader :specific_roles, :specific_hosts def specific_primary! self.specific_hosts = [ config.primary_web_host ] end def specific_roles=(role_names) - self.specific_hosts = config.roles.select { |r| role_names.include?(r.name) }.flat_map(&:hosts) if role_names.present? + @specific_roles = config.roles.select { |r| role_names.include?(r.name) } if role_names.present? + end + + def specific_hosts=(hosts) + @specific_hosts = config.all_hosts & hosts if hosts.present? end def primary_host specific_hosts&.first || config.primary_web_host end + def roles + (specific_roles || config.roles).select do |role| + ((specific_hosts || config.all_hosts) & role.hosts).any? + end + end + def hosts - specific_hosts || config.all_hosts + (specific_hosts || config.all_hosts).select do |host| + (specific_roles || config.roles).flat_map(&:hosts).include?(host) + end + end + + def roles_on(host) + roles.select { |role| role.hosts.include?(host) } end def traefik_hosts diff --git a/test/commander_test.rb b/test/commander_test.rb index b4ce24dc..918f69f8 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -19,25 +19,36 @@ class CommanderTest < ActiveSupport::TestCase assert_match /no git repository found/, error.message end - test "overwriting hosts" do - assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @mrsk.hosts + test "filtering hosts" do + assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], @mrsk.hosts - @mrsk.specific_hosts = [ "1.2.3.4", "1.2.3.5" ] - assert_equal [ "1.2.3.4", "1.2.3.5" ], @mrsk.hosts + @mrsk.specific_hosts = [ "1.1.1.1", "1.1.1.2" ] + assert_equal [ "1.1.1.1", "1.1.1.2" ], @mrsk.hosts end - test "overwriting hosts with roles" do - assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @mrsk.hosts + test "filtering hosts by filtering roles" do + assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], @mrsk.hosts - @mrsk.specific_roles = [ "workers", "web" ] - assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @mrsk.hosts + @mrsk.specific_roles = [ "web" ] + assert_equal [ "1.1.1.1", "1.1.1.2" ], @mrsk.hosts + end + + test "filtering roles" do + assert_equal [ "web", "workers" ], @mrsk.roles.map(&:name) @mrsk.specific_roles = [ "workers" ] - assert_equal [ "1.1.1.3", "1.1.1.4" ], @mrsk.hosts + assert_equal [ "workers" ], @mrsk.roles.map(&:name) + end + + test "filtering roles by filtering hosts" do + assert_equal [ "web", "workers" ], @mrsk.roles.map(&:name) + + @mrsk.specific_hosts = [ "1.1.1.3" ] + assert_equal [ "workers" ], @mrsk.roles.map(&:name) end test "overwriting hosts with primary" do - assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @mrsk.hosts + assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], @mrsk.hosts @mrsk.specific_primary! assert_equal [ "1.1.1.1" ], @mrsk.hosts @@ -47,4 +58,10 @@ class CommanderTest < ActiveSupport::TestCase @mrsk.specific_roles = "web" assert_equal "1.1.1.1", @mrsk.primary_host end + + test "roles_on" do + assert_equal [ "web", "workers" ], @mrsk.roles_on("1.1.1.1").map(&:name) + assert_equal [ "web" ], @mrsk.roles_on("1.1.1.2").map(&:name) + assert_equal [ "workers" ], @mrsk.roles_on("1.1.1.3").map(&:name) + end end diff --git a/test/fixtures/deploy_with_roles.yml b/test/fixtures/deploy_with_roles.yml index 345c6301..bac32e44 100644 --- a/test/fixtures/deploy_with_roles.yml +++ b/test/fixtures/deploy_with_roles.yml @@ -5,8 +5,8 @@ servers: - 1.1.1.1 - 1.1.1.2 workers: + - 1.1.1.1 - 1.1.1.3 - - 1.1.1.4 env: REDIS_URL: redis://x/y registry: