Is it possible to run a middleware action at a specific point in the provisioning process?

24 views
Skip to first unread message

Rob Ots

unread,
Jan 18, 2019, 3:29:48 PM1/18/19
to Vagrant
I'm new to Vagrant and don't fully understand the middleware pattern, but I have a middleware action - a class MyAction with an initialize(app) and a call(env) method - that I'd like to run at a specific point in the provisioning process.

The following runs the action, but it runs it right at the beginning of the vagrant provision command, not when I want.

def self.action_provision
  Vagrant::Action::Builder.new.tap do |b|
    b.use Provision
    b.use MyAction
  end
end

Is there a way to run that action at a specific point in the provisioning process?

Brian Cain

unread,
Jan 18, 2019, 4:11:16 PM1/18/19
to vagra...@googlegroups.com
Hey there,

Your understanding of the middleware class is correct! Your action is likely technically running
after the builtin Provision action (I assume Provision in this case is the action coming from core Vagrant).
The trick is that the Provision action does not totally function in an obvious way :) This might be more
obvious if you run vagrant with the --debug flag.

If you look closely at the builtin Provision action, when it runs it initially only sets up the provisioner prior to the vm
booting. So if you were running Vagrant without the debug flag, the UI output might make it seem like your custom action
is running before the Provision action step. But what's actually happening is the provisioner action sets up the provisioner
to be run later, and then the middleware goes on and executes your custom action class and anything else on the stack.

I assume you are writing this for a Vagrant plugin? If you're writing your own plugin, you can make your custom
action class run after the provision step (in the way you intended to) with Action Hooks: https://www.vagrantup.com/docs/plugins/action-hooks.html

Hope this clears things up, thanks!

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/247ff4af-b0f1-4bc6-9add-b6399d4e2ff9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Brian Cain

Rob Ots

unread,
Jan 18, 2019, 6:09:05 PM1/18/19
to Vagrant
Hi Brian,

Thanks for your reply!


> The trick is that the Provision action does not totally function in an obvious way :)

I can attest to that :)


> This might be more obvious if you run vagrant with the --debug flag.

Ok, thanks, I'll try that out to try to understand.


> I assume you are writing this for a Vagrant plugin?

Yes. Thanks for the pointer to Action Hooks - machine_action_provision, "called after all provisioners have executed", was one I considered. For right now I've resorted to adding a Provisioner to the plugin as I seem to be able to call that where I want within the provisioning steps.

The "machine" and "config" parameters provided to the provisioner aren't quite equivalent to the "env" hash given to the Action, so I ended up duplicating some of the code from the Action and passing needed info pulled from the "machine" and "config" inputs ...
Reply all
Reply to author
Forward
0 new messages