Can the Java Cookbook Be Used to Install a Local Copy of Oracle Java

Can the java cookbook be used to install a local copy of oracle java?

The java cookbook is designed to support the installation of different Java variants. It's behaviour is controlled by node attributes. The defaults are in the cookbook and will install the OpenJDK.

So, to install the oracle JDK you need to specify alternative overrides and these are discussed in the README

How do you do this? In chef you have at least two options:

  1. Wrapper cookbook
  2. Role

For an example of a wrapper cookbook I refer you to my other answer.

  • How to use chef to update-alternatives for java using execute?

For an example role try this:

{
"name": "java",
"description": "Oracle java role",
"override_attributes": {
"java": {
"jdk_version": 8,
"install_flavor": "oracle",
"oracle": {
"accept_oracle_download_terms": true
}
}
},
"run_list": [
"recipe[apt]",
"recipe[java]"
]
}

Add this role to the run-list of your node and the OracleJDK will be installed.


Test Kitchen project that tests the install of OracleJDK

The following is a test kitchen example that will install and test a "java" role against both ubuntu and centos

├── Berksfile
├── .kitchen.yml
├── roles
│   └── java.json
└── test
└── integration
└── default
└── serverspec
└── java_spec.rb

Install chefDK, vagrant and run the following command

kitchen test

Notes:

  • The simplest way to get test kitchen running is to install both vagrant and chefdk

Berksfile

source "https://supermarket.chef.io"

cookbook "apt"
cookbook "java"

.kitchen.yml

---
driver:
name: vagrant

provisioner:
name: chef_zero
require_chef_omnibus: 12.0.3
client_rb:
"Ohai::Config[:disabled_plugins] = [:GCE] #":

platforms:
- name: ubuntu-12.04
- name: centos-6.4

suites:
- name: default
run_list:
- role[java]

Notes:

  • The special role "java" is added to the node run-list.
  • This example disables the "gce" plugin. See issue 624.

roles/java.json

See above

test/integration/default/serverspec/java_spec.rb

require 'serverspec'

# Required by serverspec
set :backend, :exec

describe file('/usr/lib/jvm/java-8-oracle-amd64/release'), :if => os[:family] == "ubuntu" do
it { should contain 'JAVA_VERSION="1.8.0_31"' }
end

describe file('/usr/lib/jvm/java/release'), :if => os[:family] == "redhat" do
it { should contain 'JAVA_VERSION="1.8.0_31"' }
end

Why is the java cookbook depending on so many other cookbooks?

In short: Chef does not (yet) support conditional dependencies. That's why all cookbooks that provide resources or recipes that might be used need to be declared as dependency.

The Java cookbook uses resources from many other cookbooks to install Java on different systems, e.g., Windows, Linux, MacOS etc. Therefore, it makes use of other cookbooks, that provide resources for e.g. installing a package under Windows, adding an APT repository etc.

In order to allow the cookbook to either include a recipe or use a resource (e.g. apt_repository) from another cookbook, this one has to be specified as dependency so that it is loaded prior to executing the cookbook (e.g. Java). Otherwise, this resource/recipe would not be known to Chef.

So all of these cookbooks will be loaded during the Chef run, but their code will not be executed. While this feels a bit annoying, esp. in your case when you obviously manually download the cookbooks, this isn't so disturbing when you use Berkshelf for dependency resolution. This is highly recommended.

How to use chef to install JDK8 on RHEL

I'm really unsure of where you're stuck, quoting the README for the java cookbook (the link you gave):

You have to make a wrapper cookbook with:

  • metadata.rb

    [...] # Usual data in a cookbook

    depends "java"
  • attribute/default.rb

    default["java"]["install_flavor"] = "oracle"
    default["java"]["jdk_version"] = "7"
    default['java']['jdk']['7']['x86_64']['url'] = "http://you_local_host/package.tgz" # Oracle does not allow to directly download the jdk
  • recipes/default.rb

    include_recipe "java"

And then add this cookbook to your target runlist.

Best idea would be to start with the learning material here about what is a runlist and about wrapper cookbooks.

Chef-Client tries to install Java on Windows but it is already installed

So as we already figured it out, java was installed on the first run and now we get the error, when we try to install it again.

Possibility 1 :

Windows package resource comes from opscode windows cookbook and in README there is written:

PLEASE NOTE - For proper idempotence the resource's package_name
should be the same as the 'DisplayName' registry value in the
uninstallation data that is created during package installation. The
easiest way to definitively find the proper 'DisplayName' value is to
install the package on a machine and search for the uninstall
information under the following registry keys:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall
HKEY_LOCAL_MACHINE\Software\Wow6464Node\Microsoft\Windows\CurrentVersion\Uninstall

I have java 7u72 installed and I have found a Registry key, where Windows stores information about the installation:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{64A3A4F4-B792-11D6-A78A-00B0D0170720}

The Windows cookbook tries to match the DisplayName value in Registry to the package_name of the resource. In my case it is "Java SE Development Kit 7 Update 72 (64-bit)". I guess in your case it should be "Java SE Development Kit 7 Update 75 (64-bit)" and not "OracleJDK7u75". So you may try to change your

normal['java']['windows']['package_name'] = 'Java SE Development Kit 7 Update 75 (64-bit)'

Possibility 2:

I have my own java cookbook to install java on windows and I do not follow the windows_package naming conventions. My java gets reinstalled every chef run. The difference with your windows_package declaration is that I have the following options:

/qn

Which means: "quiet, no UI". And "/s" seems like means nothing. You can see all the available parameters by running in command line:

jdk-7u75-windows-x64.exe /?

Installing java using chef

This is a long out-of-date question, but I'll include an answer in case someone stumbles in on this one.

Opscode has a Java cookbook that they maintain that can install either OpenJDK or Oracle JDK. It's available at the opscode community site here.

You can use it by adding a dependency to your cookbook's metadata.rb:

depends "java",     "~> 1.10.2"

and including the recipe in your cookbook's default.rb:

include_recipe "java"

Vagrant, Chef-solo and Java cookbook: Error executing action `install` on resource 'java_ark[jdk]'

I think that the problem is a mismatch between the version of Chef you are using (10.14.2) and the java cookbook.

The implementation of the java_ark resource provider on the cookbook is attempting to use the header method of the Chef remote_file resource when it downloads the Oracle installer. The problem is that the header method was only implemented on the remote_file resource in Chef 11.6.0 (by my reading of the chef source code on Github).

The solution is to either upgrade to the latest version of Chef, or use an older version of the java cookbook that works with Chef 10.x. (The latter is probably a bad idea ... unless you are prepared to manually set the latest Oracle download URL and checksum in the relevant attributes ...)



Related Topics



Leave a reply



Submit