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.

4

1 回答 1

1

如果您可以使用vagrant provision而不是--provision标志进行配置,则可以使用 Vagrantfile 中的以下代码进行检查:

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

条件内的代码只会在vagrant provision. 正如您所说,vagrant reload与标志一起使用时这将不起作用,但您可以在 running 之后简单地运行。也可以在使用其他命令时使用or来运行。--provisionvagrant provisionvagrant reloadif ARGV[0] == 'up'if ARGV[0] == 'reload'

请参阅:在 Vagrantfile 中获取命令行参数

于 2016-06-07T16:38:00.163 回答