Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

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

I have been trying to learn chef recently because I was planning on using it for deploying server config and server application software. I am having issues understanding how to use other people's cookbooks. For example, I want to deploy JDK8u31. I can't figure it out how to implement this cookbook. https://supermarket.chef.io/cookbooks/java

I read the instructions and I see the following

Simply include the java recipe wherever you would like Java installed, such as a run list (recipe[java]) or a cookbook (include_recipe 'java')

I tried to

include_recipe 'java' 

inside my cookbook called common_java_server

then

directory '/usr/lib/jvm/' do
  owner 'root'
  group 'root'
  mode '0644'
end




java_ark "jdk" do
    url 'http://download.oracle.com/otn-pub/java/jdk/8u31/jdk-8u31-linux-x64.bin'
    checksum  'a8603fa62045ce2164b26f7c04859cd548ffe0e33bfc979d9fa73df42e3b3365'
    app_home '/usr/lib/jvm/'
    bin_cmds ["java", "javac"]
    action :install
end

# set alternatives for java and javac commands
java_alternatives "set java alternatives" do
    java_location '/usr/local/java'
    bin_cmds ["java", "javac"]
    action :set
end

This is the error I get

Recipe Compile Error in /etc/chef/src/cookbooks/common/recipes/java_dev_server.rb
====
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...