From 544c061e65c6ec146acdfce95dba6b943522df4a Mon Sep 17 00:00:00 2001 From: Robert Heinzmann Date: Tue, 16 Feb 2016 12:13:53 +0100 Subject: [PATCH] Fix handling of non-fatal lxc-stop return code Fixes #405 --- lib/vagrant-lxc/driver/cli.rb | 13 ++++++++++++- lib/vagrant-lxc/errors.rb | 3 ++- lib/vagrant-lxc/sudo_wrapper.rb | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/vagrant-lxc/driver/cli.rb b/lib/vagrant-lxc/driver/cli.rb index ea47afe..b055a09 100644 --- a/lib/vagrant-lxc/driver/cli.rb +++ b/lib/vagrant-lxc/driver/cli.rb @@ -85,9 +85,20 @@ module Vagrant run :start, '-d', '--name', @name, *Array(options) end + ## lxc-stop will exit 2 if machine was already stopped + # Man Page: + # 2 The specified container exists but was not running. def stop attach '/sbin/halt' if supports_attach? - run :stop, '--name', @name + begin + run :stop, '--name', @name + rescue LXC::Errors::ExecuteError => e + if e.exitcode == 2 + @logger.debug "Machine already stopped, lxc-stop returned 2" + else + raise e + end + end end def attach(*cmd) diff --git a/lib/vagrant-lxc/errors.rb b/lib/vagrant-lxc/errors.rb index e2254ff..1651d5d 100644 --- a/lib/vagrant-lxc/errors.rb +++ b/lib/vagrant-lxc/errors.rb @@ -5,12 +5,13 @@ module Vagrant module Errors class ExecuteError < Vagrant::Errors::VagrantError error_key(:lxc_execute_error) - attr_reader :stderr, :stdout + attr_reader :stderr, :stdout, :exitcode def initialize(message, *args) super if message.is_a?(Hash) @stderr = message[:stderr] @stdout = message[:stdout] + @exitcode = message[:exitcode] end end end diff --git a/lib/vagrant-lxc/sudo_wrapper.rb b/lib/vagrant-lxc/sudo_wrapper.rb index 798435e..aa580a8 100644 --- a/lib/vagrant-lxc/sudo_wrapper.rb +++ b/lib/vagrant-lxc/sudo_wrapper.rb @@ -49,7 +49,7 @@ module Vagrant @logger.info("Exit code != 0, but interrupted. Ignoring.") else raise LXC::Errors::ExecuteError, - command: command.inspect, stderr: r.stderr, stdout: r.stdout + command: command.inspect, stderr: r.stderr, stdout: r.stdout, exitcode: r.exit_code end end end