How to Use Chef to Update-Alternatives for Java Using Execute

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

Attribute Resolution in Chef

See https://coderanger.net/derived-attributes/ for an overview of this problem. There is no good solution that doesn't involve modifying the upstream cookbook. Easiest solution is to duplicate the derived attribute in your wrapper as well.

How to check if Java is installed on Windows using Chef

there are 2 approaches which you can take...

the recommended one, is having your recipe to always install java. due to chef nature (idempotence), chef will skip java installation if java is already installed.

an alternative, is having a guard (as you already used) that checks whether java is installed. the most straightforward to check whether java is installed, is to test whether java command works.

how to run a java program at background from chef recipe

You are best advised to configure your code to run as a service. Several wrappers available, for example:

  • http://wrapper.tanukisoftware.com/doc/english/app-hello-world-server.html

Once this is done you can configure chef to manage the new service:

service "myapp_service" do
supports :status => true, :restart => true
start_command "/usr/lib/myapp/bin/myapp start"
restart_command "/usr/lib/myapp/bin/myapp restart"
status_command "/usr/lib/myapp/bin/myapp status"
action [ :enable, :start ]
end

Best approach to change or override an action in a chef recipe

Chef is not a remote execution framework, what you are doing is the correct way to handle this (though maybe with a better SSH client like Fabric, Invoke, or Capistrano).

Is there a way to bootstrap a Chef node from pure Java implementation?

As Everett pointed out, that is exactly what jclouds-chef does. Check out the guide to see how it works.

You don't have to install Ruby/Chef or anything neither locally nor in the machines you are going to bootstrap. The only requirement is that the machine you are going to bootstrap has SSH access enabled and can connect to the Internet. jclouds-chef will connect to it and take care of installing Ruby, Chef, and all the requirements and bootstrap the node, just as knife would do.



Related Topics



Leave a reply



Submit