From 2023c377ab8788d394c72c8aa947795b4b7bcc94 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 13:52:31 +0100 Subject: [PATCH] Reboot if running --- lib/mrsk/cli/app.rb | 12 +++++++++--- test/cli/app_test.rb | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index d3f94a9f..36816178 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -3,15 +3,21 @@ require "mrsk/cli/base" class Mrsk::Cli::App < Mrsk::Cli::Base desc "boot", "Boot app on servers (or start them if they've already been booted)" def boot - using_version(options[:version] || most_recent_version_available) do + cli = self + + using_version(options[:version] || most_recent_version_available) do |version| MRSK.config.roles.each do |role| on(role.hosts) do |host| begin execute *MRSK.app.run(role: role.name) rescue SSHKit::Command::Failed => e if e.message =~ /already in use/ - error "Container with same version already deployed on #{host}, starting that instead" - execute *MRSK.app.start, host: host + error "Rebooting container with same version already deployed on #{host}" + + cli.stop + cli.remove_container version + + execute *MRSK.app.run(role: role.name) else raise end diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index d3856c98..2b77f447 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -5,6 +5,24 @@ class CliAppTest < CliTestCase assert_match /Running docker run -d --restart unless-stopped/, run_command("boot") end + test "boot will reboot if same version is already running" do + run_command("details") # Preheat MRSK const + + # Prevent expected failures from outputting to terminal + Thread.report_on_exception = false + + MRSK.app.stubs(:run).raises(SSHKit::Command::Failed.new("already in use")).then.returns([ :docker, :run ]) + + run_command("boot").tap do |output| + assert_match /Rebooting container with same version already deployed/, output # Can't start what's already running + assert_match /docker ps -q --filter label=service=app | xargs docker stop/, output # Stop what's running + assert_match /docker container ls -a -f name=app-999 -q | docker container rm/, output # Remove old container + assert_match /docker run/, output # Start new container + end + ensure + Thread.report_on_exception = true + end + test "reboot to default version" do run_command("reboot").tap do |output| assert_match /docker ps --filter label=service=app/, output # Find current container