2

I've took a look on BerkShelf documentation. I've been able to figure out it stands for managing cookbook dependencies. So, guess I'd like to build a machine with java. I've first generated my custom cookbook:

chef generate cookbook custom-java

My Berksfile is:

source 'https://supermarket.chef.io'
metadata

cookbook 'java'

and my metadata.rb is:

name 'custom-java'
...
version '0.1.0'

After that, I've simply run berks install, so all dependencies have been resolved and located under ~\.berkshelf\cookbooks.

Nevertheless, I don't quite figure out how to use my custom-java into my vagrant configuration. What do I need to do in order for vagrant to provistion this cookbook into my machines?

My vagrant structure is:

VagrantFile
├───chef
│   ├───cookbooks
│   │   ├───(((1)))  <<<<<<<<<<<<<<<<<<<<<<
│   ├───roles
│   │   ├───java-dev-workstation.rb

Vagrantfile content is:

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provision "chef_solo" do |chef|
    chef.roles_path = "chef/roles"
    chef.cookbooks_path = "chef/cookbooks"
    chef.add_role "java-dev-workstation"
  end
end

And java-dev-workstation.rb:

name "java-dev-workstation"

default_attributes( 
  # Because it's Oracle, we need to agree to the terms and conditions.
  :custom-java => {  >>>>>>>>>   (((2)))
    :install_flavor => 'oracle',
    :jdk_version => '8',
    :accept_license_agreement => true,
    :oracle => { "accept_oracle_download_terms" => true } 
  }

)

run_list(
  "recipe[java]"
)

I'm using Chef 12.18.31.

  1. On (((1))): Do I need to "import" my custom-java cookbook, how? Where is it located?
  2. On (((2))): How should I configure java?

EDIT I've set chef.cookbooks_path:

config.vm.provision "chef_solo" do |chef|
    chef.roles_path = "chef/roles"
    chef.cookbooks_path = 'D:\projects\living\vagrant\production\berk\custom-java'
    chef.add_role "java-dev-workstation"
end

EDIT2

Nevertheless, custom-java dependencies are not resolved now:

================================================================================
Error Resolving Cookbooks for Run List:
================================================================================

Missing Cookbooks:
------------------
No such cookbook: yum

Expanded Run List:
------------------
* custom-java

Platform:
---------
x86_64-linux

My metadata.rb content is:

name 'berk'
...
version '0.1.0'

supports 'centos'

depends 'yum'

Currently, all dependencies are located in ~/.berkshelf/cookbooks. It seems shef-solo is not looking for in it...

Jordi
  • 20,868
  • 39
  • 149
  • 333
  • You can add `custom-java` to the run_list of `java-dev-workstation.rb` role. – Draco Ater Feb 21 '17 at 08:43
  • How is vagrant going to resolve `custom-java` cookbook if I've not upload it in anywhere? – Jordi Feb 21 '17 at 09:27
  • you are provisioning with `chef_solo` and provide `cookbooks_path` - it's enough. – Draco Ater Feb 21 '17 at 09:33
  • I'm getting a not resolved cookbook. I've edited the post. – Jordi Feb 21 '17 at 09:46
  • You don't need `\custom-java` part in the `chef.cookbooks_path`. Should be like `D:\projects\living\vagrant\production\berk` – Draco Ater Feb 21 '17 at 09:49
  • I'm getting a cookbook dependency is not resolved now. It seems like that even though I've set that it look at on `D:\projects\living\vagrant\production\berk`, it's not able to look for dependencies located on `~/berkshelf/cookbooks`. I've edited the post. – Jordi Feb 21 '17 at 10:04
  • `cookbooks_path` can be an array. Add there all the folders where your cookbooks are. – Draco Ater Feb 21 '17 at 11:14
  • It keeps failing. I've set `chef.cookbooks_path = ['D:\projects\living\vagrant\production\berk', 'C:\Users\Beep\.berkshelf\cookbooks']`. – Jordi Feb 21 '17 at 11:29

1 Answers1

0

You did take it backward for a wrapper cookbook pattern, your custom_java should depends on java and change its default behavior (overriding node attributes).

Your custom-java metadata.rb should contain a line like this, what is in the berksfile will never be interpreted by the chef-client run, it's an outside dependency resolver to create coherent bundles.

depends 'java', '~> 1.47'

And the default.rb a line

include_recipe 'java'

Then your Berksfile can omit the cookbook line (unless you're pulling a version elsewhere than the source at top)

Next your role should use custom-java as recipe and not java, then the chef-client run (the vagrant part before edit sounds ok and should pull both cookbooks) will compute attributes from the java coobooks, overwrite those defined in custom-java and you should end up with the desired behavior.

Alternatively, you can just set the runlist to custom-java and avoid the role altogether, that will works.

I highly recommend you to go through all the tutorials on https://learn.chef.io to get a better overview.

Tensibai
  • 15,557
  • 1
  • 37
  • 57