Brett Brett - 2 years ago 282
Java Question

I including a recipe multiple times with different attributes

I thought I could install multiple JDKs by doing something like this:

node[:mycookbook][:jdk_versions].each do |jdk_version|
node.set[:java][:jdk_version] = jdk_version
include_recipe "java::default"

However, this only installs 1 JDK version: the 1st version in the attribute. I'm guessing because attributes are determined during compile phase and then the installation of java from the include_recipe just re-runs with the same attribute (and considers itself already up to date).

Any suggestions for re-using a recipe with different attributes set?

** edit **

I just noticed this over here

If the include_recipe method is used more than once to include a recipe, only the first inclusion is processed and any subsequent inclusions are ignored.

* edit 2 *
based on @tensibai suggestion, I am now able to get multiple JDKs installed, using the resources from the java cookbook manually in my own cookbook.

arch = node[:java][:arch]

node[:mycookbook][:jdk_versions].each do |jdk_version|
node.set[:java][:java_home] = "/usr/lib/jvm/jdk#{jdk_version}"
is_default = jdk_version == node[:mycookbook][:default_jdk_version]

java_ark "jdk#{jdk_version}" do
url node[:java][:jdk][jdk_version][arch][:url]
default is_default
checksum node[:java][:jdk][jdk_version][arch][:checksum]
app_home node[:java][:java_home]
bin_cmds node[:java][:jdk][jdk_version][arch][:bin_cmds]
alternatives_priority node[:java][:alternatives_priority]
retries node[:java][:ark_retries]
retry_delay node[:java][:ark_retry_delay]
action :install

After that, I get the following (release version redacted) after the initial chef run:

- (dir) jdk1.6.0_X
- (dir) jdk1.7.0_Y
- (dir) jdk1.8.0_Z
- (link) jdk6 -> /usr/lib/jvm/jdk1.6.0_X
- (link) jdk7 -> /usr/lib/jvm/jdk1.7.0_Y
- (link) jdk8 -> /usr/lib/jvm/jdk1.6.0_Z

For some reason, JDK8 (which for this run was set to default, so
resolves to true) gets linked to the jdk 6 path. I also get clone resources warnings relating to set-alternatives during the run. However, if I re-run chef (with same attributes set), it fixes the symlink:

* java_ark[jdk6] action install (up to date)
* java_ark[jdk7] action install[2016-05-04T07:50:37+00:00] WARN: Cloning resource attributes for java_alternatives[set-java-alternatives] from prior resource (CHEF-3694)
* java_ark[jdk8] action install
- Symlink /usr/lib/jvm/jdk1.8.0_Y to /usr/lib/jvm/jdk8[2016-05-04T07:50:37+00:00] WARN: Cloning resource attributes for java_alternatives[set-java-alternatives] from prior resource (CHEF-3694)

Any idea why this would be happening?

Answer Source

From the java cookbook README:

This cookbook contains the java_ark LWRP. Generally speaking this LWRP is deprecated in favor of ark from the ark cookbook, but it is still used in this cookbook for handling the Oracle JDK installation.

And from the [ark] README examples:

ark 'jdk' do
   url ''
   version '7.2'
   path "/usr/local/jvm/"
   home_dir "/usr/local/jvm/default"
   checksum  '89ba5fde0c596db388c3bbd265b63007a9cc3df3a8e6d79a46780c1a39408cb5'
   append_env_path true
   owner 'foobar'

You can just make an hask of jdk flavors you wish to install and call ark multiples times with something like:

Attributes (dummy values):

default['jdk']['open-1.7'] = {
  "url" => "",
  "version" => "7.2",
  "checksum" => "89ba5fde0c596db388c3bbd265b63007a9cc3df3a8e6d79a46780c1a39408cb5"

default['jdk']['open-1.6'] = {
  "url" => "",
  "version" => "6.42",
  "checksum" => "89xxxxxxxxxx08cb5"


node['jdk'].each do |jdk,properties|
  ark jdk do
    url properties['url']
       version properties['version']
       path "/usr/local/jvm/"
       home_dir "/usr/local/jvm/default"
       checksum  propertie['checksum']
       append_env_path true
       owner 'foobar'

I let you extend the values in hash and within the recipe according to your needs (setting the default jdk, etc).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download