在流浪者文件中要求一个流浪者插件?

假设执行 Vagrantfile需要安装一个特定的流浪者插件。所以,基本上你需要做的就是

$ vagrant plugin install foobar-plugin
$ vagrant up

如果跳过第一步,vagrant up将失败。

在流浪汉有一个选项,使它自动安装插件?换句话说: 是否可以在 Vagrantfile中指定在创建和启动机器之前自动安装哪些插件?

24930 次浏览

作为 I 指出了我对你另一个问题的回答,您可以使用 Bindler使用单个命令安装一组特定于项目的插件。

如果 bindler 已经安装,而所需的插件没有安装,bindler 将出错并中止进程。也有 一个悬而未决的问题相关的自动安装插件的 vagrant up,但迄今为止还没有人注册。

如果你不想使用 bindler,你可以在 Vagrantfile的顶部使用 Vagrant.has_plugin?(在1.3.0 + 上可用) ,如果没有安装所需的插件,就会出错。

比如:

unless Vagrant.has_plugin?("vagrant-some-plugin")
raise 'some-plugin is not installed!'
end


Vagrant.configure("2") do |config|
config.vm.box = "box-name"
end

更新 : 截至2015年5月11日,Vagrant core 不再支持 Bindler,也没有提供相应的功能

我刚注意到这里有一条指令

Vagrant.require_plugin "vagrant-aws"

它的功能和 fgrehm 描述的完全一样: 如果没有安装插件,就会迅速引发一个错误。

据我所知,目前还没有自动安装插件的方法

因为我是一个 Ruby 开发人员,而且 Bindler 不再被维护,所以我发现最自然的做法就是在我的 Vagrantfile 顶部编写一些代码来安装必要的插件(例如在 Vagrant.configure行之前)

下面这些对我有用:

required_plugins = %w( vagrant-hostmanager vagrant-someotherplugin )
required_plugins.each do |plugin|
system "vagrant plugin install #{plugin}" unless Vagrant.has_plugin? plugin
end

与使用反勾不同,system将把命令回送到 stdout,就像您自己运行命令一样。这样,我就不需要另一个名字奇怪的插件或系统来跟踪需要的插件,无论如何,流浪汉都可以更新这些插件。

请注意,从流浪者1.5开始,您可以在 Gemfile中指定您的依赖项:

现在,流浪者1.5将自动加载“插件”中的任何 gems 作为一个例子,这里是一个双子文件 “流浪者酒吧”插件:

source "https://rubygems.org"


group :development do
gem "vagrant",
git: "https://github.com/mitchellh/vagrant.git"
end


group :plugins do
gem "vagrant-foo",
gem "vagrant-bar", path: "."
end

2018年8月31日更新: 有关 Vagrant (1.8及以上版本)的更新,请参见 @ 斯塔克斯在下面

下面是基于 Louis St. Amour 的解决方案和 Rob Kinyon 关于如果安装了一个新插件就重新执行的评论,我在自己的设置中成功地使用了它:

required_plugins = %w(vagrant-share vagrant-vbguest...)


plugins_to_install = required_plugins.select { |plugin| not Vagrant.has_plugin? plugin }
if not plugins_to_install.empty?
puts "Installing plugins: #{plugins_to_install.join(' ')}"
if system "vagrant plugin install #{plugins_to_install.join(' ')}"
exec "vagrant #{ARGV.join(' ')}"
else
abort "Installation of one or more plugins has failed. Aborting."
end
end

我不能对路易斯 · 圣 · 阿莫尔的回答做任何评论,但是我想发表这篇文章,以防有人需要帮助来扩展他的解决方案。

# Check for missing plugins
required_plugins = %w(vagrant-list)
plugin_installed = false
required_plugins.each do |plugin|
unless Vagrant.has_plugin?(plugin)
system "vagrant plugin install #{plugin}"
plugin_installed = true
end
end


# If new plugins installed, restart Vagrant process
if plugin_installed === true
exec "vagrant #{ARGV.join' '}"
end

我的答案非常接近于 Louis St-Amour 的回答,但是它不是自动安装插件,而只是引发一个错误消息,因此用户必须手动安装插件。

我更希望用户知道安装了哪些插件,因为它们适用于全球范围内的所有流浪者实例,而不仅仅是当前的 Vagrantfile。

在每个插件的 Vagrantfile顶部放一行这样的代码,在这个例子中,vagrant-vbguest:

 raise "vagrant-vbguest plugin must be installed" unless Vagrant.has_plugin? "vagrant-vbguest"

我有一个问题,新安装的流浪者,在哪里。D 目录尚未创建。通过捕捉异常,我可以让来自 Luis St. Amour 的代码片段工作起来。

required_plugins = %w(vagrant-share vagrant-vbguest)


begin
plugins_to_install = required_plugins.select { |plugin| not Vagrant.has_plugin? plugin }
if not plugins_to_install.empty?
puts "Installing plugins: #{plugins_to_install.join(' ')}"
if system "vagrant plugin install #{plugins_to_install.join(' ')}"
exec "vagrant #{ARGV.join(' ')}"
else
abort "Installation of one or more plugins has failed. Aborting."
end
end
rescue
exec "vagrant #{ARGV.join(' ')}"
end

下面是我在流浪者1.8上使用的程序,它运行良好。把这个放在 Vagrantfile 中的 configure 块之前的某个地方。

# install required plugins if necessary
if ARGV[0] == 'up'
# add required plugins here
required_plugins = %w( plugin1 plugin2 plugin3 )
missing_plugins = []
required_plugins.each do |plugin|
missing_plugins.push(plugin) unless Vagrant.has_plugin? plugin
end


if ! missing_plugins.empty?
install_these = missing_plugins.join(' ')
puts "Found missing plugins: #{install_these}.  Installing using sudo..."
exec "sudo vagrant plugin install #{install_these}; vagrant up"
end
end

您可以使用这个项目(我是作者) : https://github.com/DevNIX/Vagrant-dependency-manager

它是基于类似的答案,但试图更完整和稳定。这个想法的最大优点是,您可以分发您的流浪汉文件,它将在每台计算机上运行,而不管在该环境中安装了哪些插件。

它很容易使用:

  1. 在 Vagrantfile 旁边复制 Depency_ manager. rb
  2. 包含它并调用 check_plugins,将依赖项作为数组传递

    例如:

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    
    require File.dirname(__FILE__)+"./dependency_manager"
    
    
    check_plugins ["vagrant-exec", "vagrant-hostsupdater", "vagrant-cachier", "vagrant-triggers"]
    
    
    Vagrant.configure(2) do |config|
    
    
    config.vm.box = "base"
    
    
    end
    
  3. ???

  4. Profit!

I would love to merge pull requests, fix any issue you could have, and to get ideas of new features. Currently I'm thinking about updating the dependency manager itself, and requiring specific plugin versions :D

Regards!

在新版《流浪者》中,@Amos Shapira 的回答陷入了一个无限循环。原因是每次对 vagrant plugin install的调用也处理 Vagrantfile,当处理时执行与安装插件有关的代码,一次又一次,如此循环。

这是我的解决方案,它避免了循环。

# Plugins
#
# Check if the first argument to the vagrant
# command is plugin or not to avoid the loop
if ARGV[0] != 'plugin'


# Define the plugins in an array format
required_plugins = [
'vagrant-vbguest', 'vagrant-hostmanager',
'vagrant-disksize'
]
plugins_to_install = required_plugins.select { |plugin| not Vagrant.has_plugin? plugin }
if not plugins_to_install.empty?


puts "Installing plugins: #{plugins_to_install.join(' ')}"
if system "vagrant plugin install #{plugins_to_install.join(' ')}"
exec "vagrant #{ARGV.join(' ')}"
else
abort "Installation of one or more plugins has failed. Aborting."
end


end
end

2019年更新: 流浪者现在有功能,通过 Vagrantfile需要插件,通过:

Vagrant.configure("2") do |config|
config.vagrant.plugins = "vagrant-some-plugin"


# or as array:
config.vagrant.plugins = ["vagrant-some-plugin", "vagrant-some-other-plugin"]


# or as hash
config.vagrant.plugins = {"vagrant-some-plugin" => {"version" => "1.0.0"}}
end

如果流浪汉发现有插件没有安装,它会提示用户自己安装它们:

$ vagrant up
Vagrant has detected project local plugins configured for this
project which are not installed.


vagrant-some-plugin
Install local plugins (Y/N) [N]: y
Installing the 'vagrant-some-plugin' plugin. This can take a few minutes...
Fetching vagrant-some-plugin-1.0.0.gem
Installed the plugin 'vagrant-some-plugin (1.0.0)'!




Vagrant has completed installing local plugins for the current Vagrant
project directory. Please run the requested command again.

参见 https://www.vagrantup.com/docs/vagrantfile/vagrant_settings.html