diff --git a/README.md b/README.md index 9fcea1ed..093d43bf 100644 --- a/README.md +++ b/README.md @@ -841,7 +841,7 @@ MRSK's default is to boot new containers on all hosts in parallel. But you can c service: myservice boot: - group_limit: 10 + group_limit: 10 # Can also specify as a percentage of total hosts, such as "25%" group_wait: 2 ``` diff --git a/lib/mrsk/configuration.rb b/lib/mrsk/configuration.rb index c3415703..4d2d010a 100644 --- a/lib/mrsk/configuration.rb +++ b/lib/mrsk/configuration.rb @@ -88,7 +88,7 @@ class Mrsk::Configuration end def boot - Mrsk::Configuration::Boot.new(section: raw_config.boot) + Mrsk::Configuration::Boot.new(config: self) end diff --git a/lib/mrsk/configuration/boot.rb b/lib/mrsk/configuration/boot.rb index 150f3d5f..dd86b689 100644 --- a/lib/mrsk/configuration/boot.rb +++ b/lib/mrsk/configuration/boot.rb @@ -1,9 +1,19 @@ class Mrsk::Configuration::Boot - attr_reader :group_wait, :group_limit + def initialize(config:) + @options = config.raw_config.boot || {} + @host_count = config.all_hosts.count + end - def initialize(section:) - section = section || {} - @group_limit = section["group_limit"] - @group_wait = section["group_wait"] + def group_limit + limit = @options["group_limit"] + if limit.to_s.end_with?("%") + @host_count * limit.to_i / 100 + else + limit + end + end + + def group_wait + @options["group_wait"] end end diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index f27c04ea..dc2594b9 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -38,7 +38,7 @@ class CliAppTest < CliTestCase Mrsk::Cli::App.any_instance.stubs(:on).with([ "1.1.1.1" ]) # tag container # Strategy is used when booting the containers - Mrsk::Cli::App.any_instance.expects(:on).with([ "1.1.1.1" ], in: :groups, limit: 3, wait: 30).with_block_given + Mrsk::Cli::App.any_instance.expects(:on).with([ "1.1.1.1" ], in: :groups, limit: 3, wait: 2).with_block_given run_command("boot", config: :with_group_strategy) end diff --git a/test/commander_test.rb b/test/commander_test.rb index 163feaf7..e5fd2961 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -2,9 +2,7 @@ require "test_helper" class CommanderTest < ActiveSupport::TestCase setup do - @mrsk = Mrsk::Commander.new.tap do |mrsk| - mrsk.configure config_file: Pathname.new(File.expand_path("fixtures/deploy_with_roles.yml", __dir__)) - end + configure_with(:deploy_with_roles) end test "lazy configuration" do @@ -55,4 +53,27 @@ class CommanderTest < ActiveSupport::TestCase assert_equal [ "web" ], @mrsk.roles_on("1.1.1.1") assert_equal [ "workers" ], @mrsk.roles_on("1.1.1.3") end + + test "default group strategy" do + assert_empty @mrsk.group_strategy + end + + test "specific limit group strategy" do + configure_with(:deploy_with_group_strategy) + + assert_equal({ in: :groups, limit: 3, wait: 2 }, @mrsk.group_strategy) + end + + test "percentage-based group strategy" do + configure_with(:deploy_with_precentage_group_strategy) + + assert_equal({ in: :groups, limit: 1, wait: 2 }, @mrsk.group_strategy) + end + + private + def configure_with(variant) + @mrsk = Mrsk::Commander.new.tap do |mrsk| + mrsk.configure config_file: Pathname.new(File.expand_path("fixtures/#{variant}.yml", __dir__)) + end + end end diff --git a/test/fixtures/deploy_with_group_strategy.yml b/test/fixtures/deploy_with_group_strategy.yml index a082f539..91ae3cc0 100644 --- a/test/fixtures/deploy_with_group_strategy.yml +++ b/test/fixtures/deploy_with_group_strategy.yml @@ -14,4 +14,4 @@ registry: boot: group_limit: 3 - group_wait: 30 + group_wait: 2 diff --git a/test/fixtures/deploy_with_precentage_group_strategy.yml b/test/fixtures/deploy_with_precentage_group_strategy.yml new file mode 100644 index 00000000..e738d07d --- /dev/null +++ b/test/fixtures/deploy_with_precentage_group_strategy.yml @@ -0,0 +1,17 @@ +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 + +boot: + group_limit: 25% + group_wait: 2