vaguerent/lib/vagrant/action/builtin/provisioner_cleanup.rb
Seth Vargo 64ff69c64b Only run cleanup tasks when they are defined on the provisioner
This helps with some confusion caused in GH-2538, since the output says:

> Running cleanup tasks for 'shell' provisioner...

But that's actually not true. It is running the cleanup tasks iff the 
provisioner defined a cleanup task. This commit changes the 
provisioner_cleanup middleware to only run cleanup tasks if the subclass
defines a cleanup task.

The reason we can't just check if the provisioner `respond_to?` the
`cleanup` method is because the parent provisioner base class (which 
all provisioners inherit from) defines a blank cleanup method. This is
important because it means we never risk calling an unimplemented
cleanup function, and it also helps define the public API for a 
provisioner.
2015-11-26 13:11:51 -05:00

55 lines
1.6 KiB
Ruby

require "log4r"
require_relative "mixin_provisioners"
module Vagrant
module Action
module Builtin
# This action will run the cleanup methods on provisioners and should
# be used as part of any Destroy action.
class ProvisionerCleanup
include MixinProvisioners
def initialize(app, env, place=nil)
@app = app
@logger = Log4r::Logger.new("vagrant::action::builtin::provision_cleanup")
place ||= :after
@place = place.to_sym
end
def call(env)
do_cleanup(env) if @place == :before
# Continue, we need the VM to be booted.
@app.call(env)
do_cleanup(env) if @place == :after
end
def do_cleanup(env)
type_map = provisioner_type_map(env)
# Ask the provisioners to modify the configuration if needed
provisioner_instances(env).each do |p, _|
name = type_map[p].to_s
# Check if the subclass defined a cleanup method. The parent
# provisioning class defines a `cleanup` method, so we cannot use
# `respond_to?` here. Instead, we have to check if _this_ instance
# defines a cleanup task.
if p.public_methods(false).include?(:cleanup)
env[:ui].info(I18n.t("vagrant.provisioner_cleanup",
name: name,
))
p.cleanup
else
@logger.debug("Skipping cleanup tasks for `#{name}' - not defined")
end
end
end
end
end
end
end