【问题标题】:RVM problems `bundle install` in a Vagrant/Chef-solo vmVagrant/Chef-solo vm 中的 RVM 问题“捆绑安装”
【发布时间】:2014-12-18 15:47:39
【问题描述】:

作为vagrant up 配置我的VM 的一部分,我在处理我的rails 应用程序依赖项的bundle install 时遇到了一些麻烦。

我遇到的问题是让捆绑安装以具有正确 rvm 的正确用户 (vagrant) 身份运行。

所以我的 rvm 是通过 Berkshelf 设置的,配置如下:

  rvm: {
    rubies: ['2.0.0'],
    default_ruby: '2.0.0',
    global_gems: [{ name: 'bundler' },
                    { name: 'rake' }],
    vagrant: { system_chef_solo: '/usr/bin/chef-solo' }
  }

我正在使用以下 RVM 配方:

chef.run_list = [
  'recipe[apt]',
  'recipe[rvm]',
  'recipe[rvm::vagrant]',
  'recipe[rvm::system]',
  'recipe[rvm::gem_package]',
  'recipe[mongodb::10gen_repo]',
  'recipe[mongodb]',
  'recipe[nodejs]',
  'recipe[phantomjs]',
  'recipe[lasso-plan]'
]

(注意套索计划是我自己的本地计划)

现在如果我尝试:

execute 'bundle install' do
  cwd node['lasso_plan']['path']
  user 'vagrant'
end

rvm_shell "bundle the gems" do 
  cwd node['lasso_plan']['path']
  code 'bundle install'
  user 'vagrant'
end

我明白了

STDERR: /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/dependency.rb:296:in `to_specs': Could not find 'bundler' (>= 0) among 74 total gem(s) (Gem::LoadError)`

虽然没有用户声明,它似乎可以很好地安装 gem,但它们不能作为 vagrant 用户访问。

我假设这是因为 rvm 并且它对待用户的方式不同,如果有人能阐明如何推进这一点,那就太好了。

谢谢

更新

所以经过一些帮助(谢谢),我在这方面取得了一些进展,我发现之前报告过类似的问题:http://lists.opscode.com/sympa/arc/chef/2012-05/msg00153.html

目前我有一些工作可以通过跑步来解决:

rvm_shell 'bundle the gems' do 
  ruby_string ruby_version
  cwd node['lasso_plan']['path']
  code 'bundle install --path .bundle'
  user 'vagrant'
end 

这允许将捆绑包安装在我的应用程序的目录中,它的缺点是 bin 不在路径中,但这可以通过 bundle exec 解决

如果我跑:

rvm_shell 'bundle the gems' do 
  ruby_string ruby_version
  cwd node['lasso_plan']['path']
  code 'bundle install'
  user 'vagrant'
end

我收到以下错误:

STDERR: /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:245:in `mkdir': Permission denied - /root/.bundler (Errno::EACCES)
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:245:in `fu_mkdir'
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:219:in `block (2 levels) in mkdir_p'
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:217:in `reverse_each'
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:217:in `block in mkdir_p'
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:203:in `each'
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:203:in `mkdir_p'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/source/git/git_proxy.rb:46:in `checkout'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/source/git.rb:144:in `specs'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/lazy_specification.rb:52:in `__materialize__'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/spec_set.rb:88:in `block in materialize'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/spec_set.rb:85:in `map!'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/spec_set.rb:85:in `materialize'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/definition.rb:114:in `specs'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/definition.rb:109:in `resolve_remotely!'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/installer.rb:83:in `run'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/installer.rb:14:in `install'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/cli.rb:247:in `install'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/vendor/thor/task.rb:27:in `run'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/vendor/thor/invocation.rb:120:in `invoke_task'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/vendor/thor.rb:344:in `dispatch'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/vendor/thor/base.rb:434:in `start'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/bin/bundle:20:in `block in <top (required)>'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/friendly_errors.rb:3:in `with_friendly_errors'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/bin/bundle:20:in `<top (required)>'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/bin/bundle:23:in `load'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/bin/bundle:23:in `<main>'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/bin/ruby_noexec_wrapper:14:in `eval'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/bin/ruby_noexec_wrapper:14:in `<main>'
---- End output of "bash"  "/tmp/chef-script20130711-23336-uqohfl" ----

我认为这可能与此处概述的 sudo 问题有关:https://github.com/bundler/bundler/blob/master/man/bundle-install.ronn 因为我假设厨师使用 sudo 作为流浪用户运行,它可能认为主目录是 /root/

然后如果我不指定用户并以root身份运行它:

rvm_shell 'bundle the gems' do 
  ruby_string ruby_version
  cwd node['lasso_plan']['path']
  code 'bundle install'
end

它会安装其中的大多数,但不会安装我引用 git 存储库的那些,以及关于缺失的不存在的错误,如下所示:

Gems included by the bundle:
git://github.com/bkeepers/dotenv.git (at master) is not checked out. Please run
`bundle install`

如果我想再次运行 bundle install,我不确定由 root 而不是 vagrant 用户安装捆绑包是否会产生其他影响

因此,理想情况下,我想以用户身份安装到 gem 路径而不是应用程序中的 .bundle(或者如果我至少不明白为什么)。感谢您的帮助

【问题讨论】:

  • 快速提问:您是否为每次测试销毁并重新创建您的虚拟机?我这样问是因为您在一项测试中所做的可能会以某种方式污染以下测试的环境。
  • 是的,很多破坏正在进行

标签: ruby-on-rails ruby rvm chef-infra vagrant


【解决方案1】:

我上次尝试使用这本食谱已经有一段时间了,但这是我记得的……

global_gems 数组将这些 gem 放入一个名为 global 的 gemset 中。在您的情况下,它们将被放置在 ruby​​ 2.0.0@global

如果您将其设置为您的 default_ruby,您可能会以最少的努力解决您的问题(免责声明 - 我没有测试它,但它应该可以工作)。

另一种选择是利用gems 属性,将其设置为:

node['rvm']['gems'] = {
  '2.0.0' => [
    { name: 'bundler' },
    { name: 'rake' }
  ]
}

后一个选项应该在没有 gemset 的情况下安装 gem,从而让它们可以在您当前的 default_ruby 中找到。

更新:

您的问题一定是bundle install 命令在 RVM 之外运行,因此使用的是 Chef 的嵌入式 Ruby。 在运行 bundle 之前尝试“采购”rvm 配置文件脚本:

execute 'bundle install' do
  cwd node['lasso_plan']['path']
  command <-EOC
    . /etc/profile.d/rvm.sh;
    bundle install
  EOC
  user 'vagrant'
end

您可能需要将 /etc/profile.d/rvm.sh 更改为 RVM 安装了其配置文件脚本的位置。

【讨论】:

  • @KevinDew 很明显,您没有使用 bundlerrake 可用的 ruby​​ 环境。我原以为只要您正确使用 rvm,您就可以使用全局 gemset 中的 gem。有什么方法可以从rvm_shell 块中检查rvm 的状态吗?
  • @cbl 感谢您的建议,我已经尝试过了,但这次我仍然遇到致命错误,它是mkdir': Permission denied - /root/.bundler (Errno::EACCES) when setting the user to vagrant。我猜有一种方法可以通过更改权限来完成这项工作,但感觉就像是错误的路径,因为如果我只是在 vagrant ssh 中使用 rvm 就可以使用:-/
  • @MattDressel 我把它放到我的食谱中:pastebin.com/UDkhwbKX 输出到:pastebin.com/XiuJ7eXR 看起来正确,并且与我运行 rvm info 登录到 vagrant ssh 时相同
  • @KevinDew 你介意澄清一下“没有用户声明,它似乎可以很好地安装 gem,但作为 vagrant 用户无法访问它们”的意思吗?这可能会给我们提供解决您问题的缺失线索。
  • @cbl 当然,如果我的食谱中有这个 (pastebin.com/MH6HChc1),那么安装包 - 我可以在 /tmp/bundle-install 中看到输出,显示它是成功的。我似乎无法确定找到它们,但认为它们可能在 /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/ - 这解释了一些事情。
【解决方案2】:

我遇到了完全相同的问题。当使用系统安装到 /usr/local/rvm 正常安装时,它会跳过自定义 GitHub gems(出于某种原因??)。但是当尝试以 vagrant 用户身份安装时,我会收到 Permission denied - /root/.bundler 错误。这是我想出的:

rvm_shell "bundle" do
  ruby_string node[:rvm][:default_ruby]
  user        "vagrant"
  group       "vagrant"
  cwd         "/home/vagrant/my_app"
  code        <<-EOF
    bundle install --path .bundle
  EOF
end

使用 --path 指令将 gem 安装到当前工作目录中。它似乎工作。必须使用此方法为每个应用程序独立安装 Gems,并且必须始终从该目录使用 bundle exec 运行所有内容,但它似乎可以工作。

【讨论】:

    【解决方案3】:

    我使用了 OpsWorks 并遇到了同样的问题。根据 cbl 的建议,我成功解决了它,但我使用了rvmsudo bundle install 而不仅仅是bundle install

    execute 'bundle install' do
      cwd node['lasso_plan']['path']
      command <<-EOF
        source /etc/profile.d/rvm.sh
        rvmsudo bundle install
      EOF
      user 'vagrant'
    end
    

    使用rvmsudo的原因是,我认为有些gem试图在不允许普通用户使用的地方创建目录。

    【讨论】:

      【解决方案4】:
      execute "bundle install" do                                                                                                                                                                      
        cwd "/vagrant"                                                                                                                                                                                 
        command "/usr/local/rvm/bin/rvm ruby-2.1.2 do /usr/bin/env bundle install"                                                                                                                     
        action :nothing                                                                                                                                                                                
      end
      

      【讨论】:

        猜你喜欢
        • 2014-07-11
        • 2015-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多