LoginSignup
5
4

More than 5 years have passed since last update.

berks install 先のディレクトリを指定する

Last updated at Posted at 2015-01-24

問題: berkshelf 3 の話。berks vendor には PATH 引数があるのに、berks install にはオプションがない。~/.berkshelf/cookbooks 固定になってしまう。パスを指定したい。

rake install (berks install)

berks install 先のディレクトリを指定するには Rakefile に以下のようなタスクを記述するとできる。rake installvendor/cookbooks にインストールできる。

# berks install to vendor/cookbooks
desc "berks install (download cookbooks)"
task :install do
  require 'berkshelf'
  puts 'berks install to vendor/cookbooks'
  Berkshelf.ui.mute do
    ENV['BERKSHELF_PATH'] = 'vendor'
    Berkshelf::Berksfile.from_file('Berksfile').install
  end
end

rake vendor (berks vendor)

ついでに berks vendor の vendor 先を vendor/cookbooks に変更した rake vendor タスクも書いてみると以下のようになる。

ちなみに berks vendor では、berks install と異なり、Berksfile でローカルパス指定したクックブックや、自分自身でさえも vendor/cookbooks にコピーされる。

desc "berks vendor (sync all cookbooks of even local paths and self)"
task :vendor do
  require 'berkshelf'
  puts 'berks vendor to vendor/cookbooks'
  Berkshelf.ui.mute do
    berksfile = Berkshelf::Berksfile.from_file('Berksfile')
    berksfile.vendor('vendor/cookbooks')
  end
end

まとめ

最終的な Rakefile は次のようになった

Rakefile
# berks install to vendor/cookbooks
desc "berks install (download cookbooks)"
task :install do
  require 'berkshelf'
  puts 'berks install to vendor/cookbooks'
  Berkshelf.ui.mute do
    ENV['BERKSHELF_PATH'] = 'vendor'
    Berkshelf::Berksfile.from_file('Berksfile').install
  end
end

# berks vendor to vendor/cookbooks
desc "berks vendor (sync all cookbooks of even local paths and self)"
task :vendor do
  require 'berkshelf'
  puts 'berks vendor to vendor/cookbooks'
  Berkshelf.ui.mute do
    berksfile = Berkshelf::Berksfile.from_file('Berksfile')
    berksfile.vendor('vendor/cookbooks')
  end
end

task :clean do
  FileUtils.rm_rf('vendor/cookbooks') if File.exist?('vendor/cookbooks')
end

chef から vendor/cookbooks を参照するには、cookbook_path を設定すればよい。ツールによって指定方法が違う(chef-solo なら solo.rb だし、chefspec なら spec_helper.rb) のでここでは省略する。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4