How can I programmatically query Vagrant for its provisioning status?
Asked Answered
S

1

8

The Goal

I am attempting to conditionally run the vagrant-berkshelf plugin. By default, enabling the plugin causes Berkshelf to resolve and vendor cookbooks on every single vagrant up (which is a relatively expensive operation) even if the current vagrant operation isn't a provisioning run. For example, I expect Berkshelf to run when I run:

  • vagrant up the first time, or
  • when I execute vagrant reload --provision.

The source implies there ought to be a way to query Vagrant itself to determine if it's a provisioning run. Specifically, there ought to be a way to hook into @env[:provision_enabled] or vagrant.actions.vm.provision, but I'm unable to figure out how to do this from within the Vagrantfile itself.

Is this method actually bound to the Vagrant object? If not there, then where? And how can I introspect it?

Software Versions

  • Vagrant 1.8.1
  • vagrant-berkshelf 4.1.0

What I've Tried

As a workaround, I have tried moving the Berkshelf plugin inside the Chef block, intending that it only run when the Chef provisioner does. For example:

Vagrant.configure(2) do |config|
  config.berkshelf.enabled = false

  config.vm.provision :chef_solo do |chef|
    config.berkshelf.enabled = true
  end
end

However, Berkshelf still runs every time I vagrant up or vagrant reload, which is not the desired behavior. The cookbooks are still resolved and vendored on each run. Consider the following elided output:

==> default: Updating Vagrant's Berkshelf...
==> default: Resolving cookbook dependencies...
==> default: Using karaf (0.2.1)
==> default: Vendoring karaf (0.2.1) to /Users/foo/.berkshelf/vagrant-berkshelf/shelves/berkshelf20160215-19428-unzcx1-default/karaf

Questions That Aren't Duplicates of This One

There is a vaguely related question where the accepted answer is an ugly hack that looks for the presence of .vagrant/machines/default/virtualbox/action_provision or similar, but it is not an exact duplicate of this question as it doesn't address how to programmatically query Vagrant's internal state through the runtime objects or API Vagrant exposes. It may hold a pragmatic (if kludgey) answer based on filesystem semaphores, but it does not answer the question I'm actually asking.

Spahi answered 15/2, 2016 at 19:45 Comment(2)
Assuming the linked answer ever worked, as of Vagrant 1.8.1 this file simply tells you whether or not the box has ever been provisioned. It isn't removed after the provisioning run, so you have no way of telling whether the --provision flag has been passed or not, so it doesn't help determine the current state.Spahi
honestly out of a plugin where you could access the state to @machine property I doubt this can be doneThetes
J
1

If it's possible for you to provision using vagrant provision instead of the --provision flag you can check with the following code in Vagrantfile:

if ARGV[0] == 'provision'
  # Run berkshelf plugin
end

The code inside the conditional will only run on vagrant provision. This won't work when using vagrant reload with the --provision flag as you were saying, but you could simply run vagrant provision after running vagrant reload. You can also use if ARGV[0] == 'up' or if ARGV[0] == 'reload' to run when using other commands.

See: Getting command line arguments inside the Vagrantfile

Jumbala answered 7/6, 2016 at 16:38 Comment(2)
Interesting. config.berkshelf.enabled = true if ARGV.include? 'provision' would work on an explicit provisioning run, but wouldn't really handle the case of the first vagrant up without some other way of inspecting the Vagrant instance or the filesystem.Spahi
The first run can be detected with def provisioned?(vm_name='default', provider='virtualbox') File.exists?(File.join(File.dirname(__FILE__),".vagrant/machines/#{vm_name}/#{provider}/action_provision")) end (see this SO-answer)Diakinesis

© 2022 - 2024 — McMap. All rights reserved.