我们有一个多样化的开发团队,一个在Windows上,另一个在Ubuntu上,另一个在OSX上.作为windows boy,我设置了流浪者设置脚本的第一个版本,它工作得非常出色;)

然而,在Ubuntu主机上运行它时,当它第一次进入调用bash脚本的配置步骤时,由于权限问题,它会失败.

在windows上,这并不重要,因为samba共享自动拥有足够的权限来运行bash脚本(它位于项目层次 struct 中,因此存在于VM上的/vagrant共享中),但在ubuntu中,我需要在调用该脚本之前在provision脚本中设置该文件的权限.

这不是问题所在,老实说,我怀疑即使有额外的"chmod"步骤,它在windows下仍然可以正常工作,但是,在vagrant文件中是否有方法将某些配置步骤标记为"仅限windows"、"仅限Linux"或"仅限Mac"?

i、 e.在psedoo代码中,类似于.

.
.
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
.
.

提前谢谢.

推荐答案

Find out current OS inside 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

然后你可以随心所欲地使用它.

if OS.windows? [then]
    code...
end

编辑:你是不是错过了比赛?如果条件允许的话.

用于测试的示例:

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

执行:

# Ran provision to call Vagrantfile.
$ vagrant provision
is_windows_host: false
Vagrant launched from mac.

Ruby相关问答推荐

try 安装Chruby时出现问题,终端问题

ruby `encode': "\xC3" 从 ASCII-8BIT 到 UTF-8 (Encoding::UndefinedConversionError)

Kernel#gets try 读取文件而不是标准输入

Ruby 中的 Monad 类似功能

Integer(value) 和 value.to_i 之间的区别

如何拯救 Ruby 中的 eval?

将ruby数组转换为连续对数组

为什么我们要在 Ruby 的类中放置一个模块?

如何在不按 Enter 的情况下获取单个字符?

如何将消息附加到 RSpec 判断?

Rails:Date.today 是 UTC 吗?

是否有等效于 `Array::sample` 的哈希值?

Ruby 中的方法: objects与not?

在 Ruby 中取消定义变量

带有可选参数的方法

区分一个Ruby字符串或数组

如何使用 RVM 更新 Ruby 解释器?

Xcode - 配置:错误:在 $PATH 中找不到可接受的 C 编译器

Ruby 中的自然语言处理

正则表达式中的 `?i` 和 `?-i` 是什么意思?