From 38d1f0c0aad8f21cdeb73314a5b6e33a90eaf54e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 5 May 2014 22:02:13 -0700 Subject: [PATCH] providers/docker: smarter destroy, does not require host machine --- plugins/providers/docker/action.rb | 46 +++++++++++-------- .../docker/action/is_host_machine_created.rb | 29 ++++++++++++ 2 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 plugins/providers/docker/action/is_host_machine_created.rb diff --git a/plugins/providers/docker/action.rb b/plugins/providers/docker/action.rb index 3c84e9951..e21621b26 100644 --- a/plugins/providers/docker/action.rb +++ b/plugins/providers/docker/action.rb @@ -134,29 +134,38 @@ module VagrantPlugins # freeing the resources of the underlying virtual machine. def self.action_destroy Vagrant::Action::Builder.new.tap do |b| - b.use Call, IsState, :host_state_unknown do |env, b2| - if env[:result] - b2.use HostMachine - end - end - - b.use Call, IsState, :not_created do |env, b2| - if env[:result] + b.use Call, IsHostMachineCreated do |env, b2| + if !env[:result] b2.use Message, I18n.t("docker_provider.messages.not_created") next end - b2.use Call, DestroyConfirm do |env2, b3| + b2.use Call, IsState, :host_state_unknown do |env2, b3| if env2[:result] - b3.use ConfigValidate - b3.use EnvSet, :force_halt => true - b3.use action_halt - b3.use HostMachineSyncFoldersDisable - b3.use Destroy - b3.use DestroyBuildImage - b3.use ProvisionerCleanup - else - b3.use Message, I18n.t("docker_provider.messages.will_not_destroy") + b3.use HostMachine + end + end + + b2.use Call, IsState, :not_created do |env2, b3| + if env2[:result] + b3.use Message, + I18n.t("docker_provider.messages.not_created") + next + end + + b3.use Call, DestroyConfirm do |env3, b4| + if env3[:result] + b4.use ConfigValidate + b4.use EnvSet, :force_halt => true + b4.use action_halt + b4.use HostMachineSyncFoldersDisable + b4.use Destroy + b4.use DestroyBuildImage + b4.use ProvisionerCleanup + else + b4.use Message, + I18n.t("docker_provider.messages.will_not_destroy") + end end end end @@ -285,6 +294,7 @@ module VagrantPlugins autoload :HostMachineSyncFolders, action_root.join("host_machine_sync_folders") autoload :HostMachineSyncFoldersDisable, action_root.join("host_machine_sync_folders_disable") autoload :IsBuild, action_root.join("is_build") + autoload :IsHostMachineCreated, action_root.join("is_host_machine_created") autoload :PrepareSSH, action_root.join("prepare_ssh") autoload :Stop, action_root.join("stop") autoload :PrepareNFSValidIds, action_root.join("prepare_nfs_valid_ids") diff --git a/plugins/providers/docker/action/is_host_machine_created.rb b/plugins/providers/docker/action/is_host_machine_created.rb new file mode 100644 index 000000000..07037b0ff --- /dev/null +++ b/plugins/providers/docker/action/is_host_machine_created.rb @@ -0,0 +1,29 @@ +module VagrantPlugins + module DockerProvider + module Action + class IsHostMachineCreated + def initialize(app, env) + @app = app + end + + def call(env) + if !env[:machine].provider.host_vm? + env[:result] = true + return @app.call(env) + end + + host_machine = env[:machine].provider.host_vm + env[:result] = + host_machine.state.id != Vagrant::MachineState::NOT_CREATED_ID + + # If the host machine isn't created, neither are we. It is + # important we set this to nil here so that global-status + # sees the right thing. + env[:machine].id = nil if !env[:result] + + @app.call(env) + end + end + end + end +end