Filter roles and hosts by their respective counterpart

This commit is contained in:
Tobias Bühlmann
2023-03-09 18:21:39 +01:00
parent e178907a21
commit 901484d75d
3 changed files with 47 additions and 14 deletions

View File

@@ -14,22 +14,38 @@ class Mrsk::Commander
.tap { |config| configure_sshkit_with(config) } .tap { |config| configure_sshkit_with(config) }
end end
attr_accessor :specific_hosts attr_reader :specific_roles, :specific_hosts
def specific_primary! def specific_primary!
self.specific_hosts = [ config.primary_web_host ] self.specific_hosts = [ config.primary_web_host ]
end end
def specific_roles=(role_names) 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 end
def primary_host def primary_host
specific_hosts&.first || config.primary_web_host specific_hosts&.first || config.primary_web_host
end end
def roles
(specific_roles || config.roles).select do |role|
((specific_hosts || config.all_hosts) & role.hosts).any?
end
end
def hosts 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 end
def traefik_hosts def traefik_hosts

View File

@@ -19,25 +19,36 @@ class CommanderTest < ActiveSupport::TestCase
assert_match /no git repository found/, error.message assert_match /no git repository found/, error.message
end end
test "overwriting hosts" do test "filtering hosts" 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_hosts = [ "1.2.3.4", "1.2.3.5" ] @mrsk.specific_hosts = [ "1.1.1.1", "1.1.1.2" ]
assert_equal [ "1.2.3.4", "1.2.3.5" ], @mrsk.hosts assert_equal [ "1.1.1.1", "1.1.1.2" ], @mrsk.hosts
end end
test "overwriting hosts with roles" do test "filtering hosts by filtering roles" 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_roles = [ "workers", "web" ] @mrsk.specific_roles = [ "web" ]
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" ], @mrsk.hosts
end
test "filtering roles" do
assert_equal [ "web", "workers" ], @mrsk.roles.map(&:name)
@mrsk.specific_roles = [ "workers" ] @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 end
test "overwriting hosts with primary" do 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! @mrsk.specific_primary!
assert_equal [ "1.1.1.1" ], @mrsk.hosts assert_equal [ "1.1.1.1" ], @mrsk.hosts
@@ -47,4 +58,10 @@ class CommanderTest < ActiveSupport::TestCase
@mrsk.specific_roles = "web" @mrsk.specific_roles = "web"
assert_equal "1.1.1.1", @mrsk.primary_host assert_equal "1.1.1.1", @mrsk.primary_host
end 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 end

View File

@@ -5,8 +5,8 @@ servers:
- 1.1.1.1 - 1.1.1.1
- 1.1.1.2 - 1.1.1.2
workers: workers:
- 1.1.1.1
- 1.1.1.3 - 1.1.1.3
- 1.1.1.4
env: env:
REDIS_URL: redis://x/y REDIS_URL: redis://x/y
registry: registry: