ruby-on-rails - Vagrant/Chef-solo 虚拟机中的 RVM 问题 `bundle install`

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

作为 vagrant up 配置我的 VM 的一部分,我在 bundle install 我的 Rails 应用程序依赖项时遇到了一些问题。

我遇到的问题是让 bundle 安装以正确的用户 (vagrant) 和正确的 rvm 运行。

所以我的 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 

它允许将 bundle 安装在我的应用程序的目录中,它的缺点是垃圾箱不在路径中,但这可以通过 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因为我假设 chef 使用 sudo 作为 vagrant 用户运行,它可能认为主目录是 /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 用户安装 bundle 是否会有其他影响

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

最佳答案

自从我上次尝试使用这本 Recipe 以来已经有一段时间了,但这是我记得的......

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 安装其配置文件脚本的位置。

关于ruby-on-rails - Vagrant/Chef-solo 虚拟机中的 RVM 问题 `bundle install`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17583435/

相关文章:

css - 应用程序不读取样式表

ruby-on-rails - Rails 安装 Ubuntu 12.04 期间出错

installation - Rvm 安装中的问题,以二进制模式运行

ruby - 从数组中删除空格、制表符和新行

ruby-on-rails - 获取嵌套哈希的键/值对

ruby - Bundler 通过 RVM 提供什么?

ruby-on-rails - 工厂女孩多重嵌套属性

ruby-on-rails - compass-rails gem 没有在 heroku 中运行?

ruby-on-rails - 在 Ruby 中,如何从 CSV 文件中按列读取数据?

ruby-on-rails - 如何将参数传递给通过 CSS 显示的表单部分?