我写了一个 rake 任务来在子目录中的 Berksfile 上执行 berks package
命令:
task :cook do
`berks package ./cookbooks.tar.gz -b ./cookbook/Berksfile`
end
我也尝试了这些变体:
task :cook do
`.\scripts\berks_package.bat` # containing stuff
end
task :cook do
`C:\opscode\chefdk\embedded\bin\ruby.exe "C:\opscode\chefdk\bin\berks" package ..\cookbooks.tar.gz -b ..\cookbook\Berksfile`
end
无论我以何种方式执行 berks,任务都会失败并显示以下错误:
C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/resolver.rb:434:in `version_conflict': Bundler could not find compatible versions for gem "nokogiri": (Bundler::VersionConflict)
In snapshot (Gemfile.lock):
nokogiri (1.6.5)
In Gemfile:
albacore (~> 2.0.0) x86-mingw32 depends on
nokogiri (~> 1.5) x86-mingw32
Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/resolver.rb:232:in `resolve_for_conflict'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/resolver.rb:250:in `resolve_conflict'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/resolver.rb:373:in `resolve'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/resolver.rb:166:in `start'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/resolver.rb:129:in `resolve'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/definition.rb:193:in `resolve'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/definition.rb:132:in `specs'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/definition.rb:177:in `specs_for'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/definition.rb:166:in `requested_specs'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/environment.rb:18:in `requested_specs'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/runtime.rb:13:in `setup'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler.rb:122:in `setup'
from C:/tools/ruby213/lib/ruby/gems/2.1.0/gems/bundler-1.7.9/lib/bundler/setup.rb:17:in `<top (required)>'
from C:/opscode/chefdk/embedded/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/opscode/chefdk/embedded/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
rake aborted!
Albacore::CommandFailedError: Command failed with status (1):
.scriptsberks_package.bat
从命令提示符执行berks package ./cookbooks.tar.gz -b ./cookbook/Berksfile
的工作方式与在任务中执行其他正常系统命令相同。
宝石文件:
source 'https://rubygems.org'
gem 'albacore', '~> 2.0.0'
gem 'semver2', '~> 3.4.0'
Gemfile.lock
GEM
remote: https://rubygems.org/
specs:
albacore (2.0.16)
map (~> 6.5)
nokogiri (~> 1.5)
rake (~> 10)
semver2 (~> 3.4)
map (6.5.5)
mini_portile (0.6.1)
nokogiri (1.6.5-x64-mingw32)
mini_portile (~> 0.6.0)
rake (10.4.2)
semver2 (3.4.0)
PLATFORMS
x64-mingw32
DEPENDENCIES
albacore (~> 2.0.0)
semver2 (~> 3.4.0)
我在写问题时发现了问题,并偶然发现了新的想法。
根本问题是与 ChefDK 一起安装的 Ruby 二进制文件和 gem 与 Ruby 的系统安装冲突。具体来说,耙子任务是在系统Ruby安装下执行的,但是当berks被执行时,它在嵌入式的ChefDK Ruby下运行。我不清楚下一部分,但似乎 ChefDK Ruby 知道 Gemfile 并试图解决依赖项本身,但由于 nokogiri 无法由 Windows 上的捆绑器解析(即构建)而不会感到痛苦,它失败了。
这是我为解决问题所做的工作:
- 卸载了系统红宝石
- 向系统 PATH 变量添加了
C:opscodechefdkbin
,以便我可以运行红宝石、耙子、宝石、捆绑包等。 - Ran
gem install nokogiri
(现在在ChefDK的Ruby下执行) - 通过运行
bundle install
重新创建了 Gemfile.lock 文件 - 用
Bundler.with_clean_env
块包围了 berks 系统调用(这是关键!- 多亏了这个问题
该任务现在如下所示:
task :cook do
Bundler.with_clean_env do
`berks package cookbooks.tar.gz -b .\cookbook\Berksfile`
end
end