Vagrant - 如何使主机平台特定的配置步骤

Ste*_*lds 41 ruby vagrant

我们有一个多元化的开发团队,一个在Windows上,另一个在Ubuntu上,另一个在OSX上.作为Windows男孩,我设置了流浪汉设置脚本的第一个版本,其工作非常棒;)

但是,在Ubuntu主机上运行它时,第一次进入调用bash脚本的供应步骤时,由于权限而失败.

在Windows中,这没有关系的桑巴份额自动拥有足够的权限来运行bash脚本(驻留在项目层次结构中,因此存在于虚拟机上的/游民的份额),但与Ubuntu的我需要设置在我调用之前,在配置脚本中对此文件的权限.

这不是问题,说实话,我即使有额外的"文件模式"怀疑加强它仍然会在Windows下很好地工作,但是,有没有流浪文件标记某一供应步骤为"仅Windows","办法Linux Only'还是'Mac Only'?

即在pseduo代码中,类似于.

.
.
if (host == windows) then
  config.vm.provision : shell, : inline => "/vagrant/provisioning/only_run_this_on_windows.sh"
else if (host == linux) then
  config.vm.provision : shell, : inline => "/vagrant/provisioning/only_run_this_on_linux.sh"
else if (host == osx) then
  config.vm.provision : shell, : inline => "/vagrant/provisioning/only_run_this_on_osx.sh"
end if
.
.
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Tom*_*ers 56

需要注意的是流浪本身,在对流浪::的Util ::平台类已经实现了平台检查逻辑的更高级版本的答案BernardoSilva.

所以在Vagrantfile中,您可以简单地使用以下内容:

if Vagrant::Util::Platform.windows? then
    myHomeDir = ENV["USERPROFILE"]
else
    myHomeDir = "~"
end
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案. (7认同)
  • 这更像是"如果平台是windows,请在这里应用monkeypatch".爱它. (4认同)
  • 要检查你的机器是否是 Mac,代码实际上是 `Vagrant::Util::Platform::darwin?` (2认同)

Ber*_*lva 45

找出Vagrantfile中的当前操作系统.

将其添加到您的Vagrantfile中:

module OS
    def OS.windows?
        (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
    end

    def OS.mac?
        (/darwin/ =~ RUBY_PLATFORM) != nil
    end

    def OS.unix?
        !OS.windows?
    end

    def OS.linux?
        OS.unix? and not OS.mac?
    end
end
Run Code Online (Sandbox Code Playgroud)

然后你可以随意使用它.

if OS.windows? [then]
    code...
end
Run Code Online (Sandbox Code Playgroud)

编辑:错过了?如果条件.

用于测试的示例:

is_windows_host = "#{OS.windows?}"
puts "is_windows_host: #{OS.windows?}"
if OS.windows?
    puts "Vagrant launched from windows."
elsif OS.mac?
    puts "Vagrant launched from mac."
elsif OS.unix?
    puts "Vagrant launched from unix."
elsif OS.linux?
    puts "Vagrant launched from linux."
else
    puts "Vagrant launched from unknown platform."
end
Run Code Online (Sandbox Code Playgroud)

执行:

# Ran provision to call Vagrantfile.
$ vagrant provision
is_windows_host: false
Vagrant launched from mac.
Run Code Online (Sandbox Code Playgroud)

  • 这是我能找到的最有用的流浪条件/平台/环境教程! (2认同)
  • 将linux检查放在unix one之前,否则将永远不会被视为linux。 (2认同)

Fel*_*Eve 6

这是一个使用 Vagrant 工具检查 mac 和 windows 的版本:

    if Vagrant::Util::Platform.windows?
        # is windows
    elsif Vagrant::Util::Platform.darwin?
        # is mac
    else
        # is linux or some other OS
    end
Run Code Online (Sandbox Code Playgroud)