Skip to content

Commit 846862c

Browse files
committed
COOK-858, COOK-813
special thanks to jtimberman for mentoring me through this process
1 parent 8885042 commit 846862c

9 files changed

Lines changed: 471 additions & 147 deletions

File tree

README.md

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
Description
22
===========
33

4-
Installs a Java. Uses OpenJDK by default but supports installation of the Sun's Java.
4+
Installs a Java. Uses OpenJDK by default but supports installation of
5+
the Oracle's JDK.
6+
7+
This cookbook also provides the java_ark LWRP which other java
8+
cookbooks can use to install java-related applications from binary
9+
packages.
510

611
---
712
Requirements
@@ -16,13 +21,16 @@ Platform
1621
Cookbooks
1722
---------
1823

19-
* apt
24+
* java
2025

2126
---
2227
Attributes
2328
==========
2429

25-
* `node["java"]["install_flavor"]` - Flavor of JVM you would like installed (`sun` or `openjdk`), default `openjdk`.
30+
* `node["java"]["install_flavor"]` - Flavor of JVM you would like installed (`oracle` or `openjdk`), default `openjdk`.
31+
* `node['java']['java_home']`
32+
* `node['java']['tarball']` - name of the tarball to retrieve from your corporate repository default `jdk1.6.0_29_i386.tar.gz`
33+
* `node['java']['tarball_checksum']` - checksum for the tarball, if you use a different tarball, you also need to create a new sha256 checksum
2634

2735
---
2836
Recipes
@@ -38,54 +46,114 @@ openjdk
3846

3947
This recipe installs the `openjdk` flavor of Java.
4048

41-
sun
49+
oracle
4250
---
4351

44-
This recipe installs the `sun` flavor of Java.
45-
46-
On Debian and Ubuntu systems the recipe will add the correct apt repository (`non-free` on Debian or `partner` on Ubuntu), pre-seed the package and update java alternatives.
47-
48-
On Red Hat flavored Linux (RHEL, CentOS, Fedora), the installation of the Sun flavor of Java is slightly more complicated as the `rpm` package is not readily available in any public Yum repository. The Sun JDK `rpm` package can be downloaded directly from Sun but comes wrapped as a compressed bin file. After the file has been downloaded, decompressed and license accepted the `rpm` package (names something like `jdk-6u25-ea-linux-amd64.rpm`) can be retrieved by this recipe using the `remote_file` or `cookbook_file` resources. The recipe will choose the correct resource based on the existence (or non-existence) of the `node['sun']['rpm_url']` attribute. See below for an example role using this attribute in the proper way. If you would like to deliver the `rpm` package file as part of this cookbook place the `rpm` package file in the `files/default` directory and the cookbook will retrieve the file during installation.
52+
This recipe installs the `oracle` flavor of Java. This recipe does not
53+
use distribution packages as Oracle changed the licensing terms with
54+
JDK 1.6u27 and prohibited the practice for both the debian and EL worlds.
55+
56+
For both debian and centos/rhel, this recipe pulls the binary
57+
distribution from the Oracle website, and installs it in the default
58+
JAVA_HOME for each distribution. For debian/ubuntu, this is
59+
/usr/lib/jvm/default-java. For Centos/RHEL, this is /usr/lib/jvm/java
60+
61+
After putting the binaries in place, the oracle recipe updates
62+
/usr/bin/java to point to the installed JDK using the update-alternatives script
63+
64+
oracle_i386
65+
-----------
66+
67+
This recipe installs the 32-bit Java virtual machine without setting it as the default. This can be useful if you have applications on the same machine that require different versions of the JVM.
68+
69+
Resources/Providers
70+
===================
71+
72+
This LWRP provides an easy way to manage java applications. It uses
73+
the LWRP arkive (deliberately misspelled). It is an arkive and not an
74+
"archive" because the java_ark lwrp is not the same as a java archive
75+
or "jar". Essentially, you provide the java_ark with the URL to a tarball and
76+
the commands within the extracted result that you want symlinked to /usr/bin/
77+
78+
If you have a better name for this lwrp please contact the maintainer.
79+
80+
By default, the extracted directory is extracted to app_root/extracted_dir_name and symlinked to app_root/default
81+
82+
# Actions
83+
84+
- :install: extracts the tarball and makes necessary symlinks
85+
- :remove: removes the tarball and run update-alternatives for all
86+
symlinked bin_cmds
87+
88+
# Attribute Parameters
89+
90+
- url: path to tarball, .tar.gz, .bin (oracle-specific), and .zip
91+
currently supported
92+
- checksum: sha256 checksum, not used for security but avoid
93+
redownloading the archive on each chef-client run
94+
- app_home: the default for installations of this type of
95+
application, for example, /usr/lib/tomcat/default. If your
96+
application is not set to the default, it will be placed at the same
97+
level in the directory hierarchy but the directory name will be
98+
app_root/extracted_directory_name + "_alt"
99+
- app_home_mode: file mode for app_home, is an integer
100+
- bin_cmds: array of binary commands that should be symlinked to
101+
/usr/bin, examples are mvn, java, javac, etc. These cmds must be in
102+
the bin/ subdirectory of the extracted folder. Will be ignored if this
103+
java_ark is not the default
104+
- owner: owner of extracted directory, set to "root" by default
105+
- default: whether this the default installation of this package,
106+
boolean true or false
107+
108+
109+
# Examples
110+
111+
# install jdk6 from Oracle
112+
java_ark "jdk" do
113+
url 'http://download.oracle.com/otn-pub/java/jdk/6u29-b11/jdk-6u29-linux-x64.bin'
114+
checksum 'a8603fa62045ce2164b26f7c04859cd548ffe0e33bfc979d9fa73df42e3b3365'
115+
app_home '/usr/local/java/default'
116+
bin_cmds ["java", "javac"]
117+
action :install
118+
end
119+
120+
# installs maven2
121+
java_ark "maven2" do
122+
url "http://www.apache.org/dist/maven/binaries/apache-maven-2.2.1-bin.tar.gz"
123+
checksum "b9a36559486a862abfc7fb2064fd1429f20333caae95ac51215d06d72c02d376"
124+
app_home "/usr/local/maven/default"
125+
bin_cmds ["mvn"]
126+
action :install
127+
end
128+
129+
49130

50131
---
51132
Usage
52133
=====
53134

54135
Simply include the `java` recipe where ever you would like Java installed.
55136

56-
To install Sun flavored Java on Debian or Ubuntu override the `node['java']['install_flavor']` attribute with in role:
137+
To install Oracle flavored Java on Debian or Ubuntu override the `node['java']['install_flavor']` attribute with in role:
57138

58139
name "java"
59-
description "Install Sun Java on Ubuntu"
140+
description "Install Oracle Java on Ubuntu"
60141
override_attributes(
61142
"java" => {
62-
"install_flavor" => "sun"
143+
"install_flavor" => "oracle"
63144
}
64145
)
65146
run_list(
66147
"recipe[java]"
67148
)
68149

69-
On RedHat flavored Linux be sure to set the `rpm_url` and `rpm_checksum` attributes if you placed the `rpm` file on a remote server:
70150

71-
name "java"
72-
description "Install Sun Java on CentOS"
73-
override_attributes(
74-
"java" => {
75-
"install_flavor" => "sun",
76-
"version" => "6u25",
77-
"rpm_url" => "https://mycompany.s3.amazonaws.com/sun_jdk",
78-
"rpm_checksum" => "c473e3026f991e617710bad98f926435959303fe084a5a31140ad5ad75d7bf13"
79-
}
80-
)
81-
run_list(
82-
"recipe[java]"
83-
)
84151

85152
License and Author
86153
==================
87154

88155
Author:: Seth Chisamore (<[email protected]>)
156+
Author:: Bryan W. Berry (<[email protected]>)
89157

90158
Copyright:: 2008-2011, Opscode, Inc
91159

attributes/default.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#
22
# Author:: Seth Chisamore (<[email protected]>)
3+
# Author:: Bryan W. Berry (<[email protected]>)
34
# Cookbook Name:: java
45
# Attributes:: default
56
#
@@ -17,13 +18,32 @@
1718
# See the License for the specific language governing permissions and
1819
# limitations under the License.
1920

21+
# default jdk attributes
2022
default['java']['install_flavor'] = "openjdk"
23+
default['java']['jdk_version'] = '6'
24+
default['java']['arch'] = kernel['machine'] =~ /x86_64/ ? "x86_64" : "i586"
2125

2226
case platform
2327
when "centos","redhat","fedora"
24-
default['java']['version'] = "6u25"
25-
default['java']['arch'] = kernel['machine'] =~ /x86_64/ ? "amd64" : "i586"
26-
set['java']['java_home'] = "/usr/lib/jvm/java"
28+
default['java']['java_home'] = "/usr/lib/jvm/java"
2729
else
28-
set['java']['java_home'] = "/usr/lib/jvm/default-java"
30+
default['java']['java_home'] = "/usr/lib/jvm/default-java"
2931
end
32+
33+
# jdk6 attributes
34+
# x86_64
35+
default['java']['jdk']['6']['x86_64']['url'] = 'http://download.oracle.com/otn-pub/java/jdk/6u29-b11/jdk-6u29-linux-x64.bin'
36+
default['java']['jdk']['6']['x86_64']['checksum'] = 'a8603fa62045ce2164b26f7c04859cd548ffe0e33bfc979d9fa73df42e3b3365'
37+
38+
# i586
39+
default['java']['jdk']['6']['i586']['url'] = 'http://download.oracle.com/otn-pub/java/jdk/6u29-b11/jdk-6u29-linux-i586.bin'
40+
default['java']['jdk']['6']['i586']['checksum'] = '1117f4dfc45632b68ec0f4d5e61e5cafc1d85dc655ee3df5fa6f50128b8c3faf'
41+
42+
# jdk7 attributes
43+
# x86_64
44+
default['java']['jdk']['7']['x86_64']['url'] = 'http://download.oracle.com/otn-pub/java/jdk/7u1-b08/jdk-7u1-linux-x64.tar.gz'
45+
default['java']['jdk']['7']['x86_64']['checksum'] = 'f88070cfe7fe5ed60dfda7729cf9dd110b77840e2ca8cd14a86d5d2274b09c9c'
46+
47+
# i586
48+
default['java']['jdk']['7']['i586']['url'] = 'http://download.oracle.com/otn-pub/java/jdk/7u1-b08/jdk-7u1-linux-i586.tar.gz'
49+
default['java']['jdk']['7']['i586']['checksum'] = 'acbfb8912a287facbee02ff138d94457aabab409b2f1d15855714ec9608a6cd4'

metadata.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
license "Apache 2.0"
44
description "Installs Java runtime."
55
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
6-
version "1.1"
7-
8-
depends "apt"
6+
version "1.3.1"
97

108
recipe "java", "Installs Java runtime"
119
recipe "java::openjdk", "Installs the OpenJDK flavor of Java"
12-
recipe "java::sun", "Installs the Sun flavor of Java"
10+
recipe "java::oracle", "Installs the Oracle flavor of Java"
11+
recipe "java::oracle_i386", "Installs the 32-bit jvm without setting it as the default"
12+
1313

1414
%w{ debian ubuntu centos redhat fedora }.each do |os|
1515
supports os

providers/ark.rb

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#
2+
# Author:: Bryan W. Berry (<[email protected]>)
3+
# Cookbook Name:: java
4+
# Provider:: ark
5+
#
6+
# Copyright 2011, Bryan w. Berry
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
20+
def parse_app_dir_name url
21+
file_name = url.split('/')[-1]
22+
# funky logic to parse oracle's non-standard naming convention
23+
# for jdk1.6
24+
if file_name =~ /^(jre|jdk).*$/
25+
major_num = file_name.scan(/\d/)[0]
26+
update_num = file_name.scan(/\d+/)[1]
27+
# pad a single digit number with a zero
28+
if update_num.length < 2
29+
update_num = "0" + update_num
30+
end
31+
package_name = file_name.scan(/[a-z]+/)[0]
32+
app_dir_name = "#{package_name}1.#{major_num}.0_#{update_num}"
33+
else
34+
app_dir_name = file_name.split(/(.tar.gz|.zip)/)[0]
35+
app_dir_name = app_dir_name.split("-bin")[0]
36+
end
37+
[app_dir_name, file_name]
38+
end
39+
40+
action :install do
41+
app_dir_name, tarball_name = parse_app_dir_name(new_resource.url)
42+
app_root = new_resource.app_home.split('/')[0..-2].join('/')
43+
app_dir = app_root + '/' + app_dir_name
44+
45+
unless new_resource.default
46+
Chef::Log.debug("processing alternate jdk")
47+
app_dir = app_dir + "_alt"
48+
app_home = app_dir
49+
else
50+
app_home = new_resource.app_home
51+
end
52+
53+
unless ::File.exists?(app_dir)
54+
Chef::Log.info "Adding #{new_resource.name} to #{app_dir}"
55+
require 'fileutils'
56+
57+
unless ::File.exists?(app_root)
58+
FileUtils.mkdir app_root, :mode => new_resource.app_home_mode
59+
FileUtils.chown new_resource.owner, new_resource.owner, app_root
60+
end
61+
62+
r = remote_file "#{Chef::Config[:file_cache_path]}/#{tarball_name}" do
63+
source new_resource.url
64+
checksum new_resource.checksum
65+
mode 0755
66+
action :nothing
67+
end
68+
r.run_action(:create_if_missing)
69+
70+
require 'tmpdir'
71+
72+
tmpdir = Dir.mktmpdir
73+
case tarball_name
74+
when /^.*\.bin/
75+
cmd = Chef::ShellOut.new(
76+
%Q[ cd "#{tmpdir}";
77+
cp "#{Chef::Config[:file_cache_path]}/#{tarball_name}" . ;
78+
bash ./#{tarball_name} -noregister
79+
] ).run_command
80+
unless cmd.exitstatus == 0
81+
Chef::Application.fatal!("Failed to extract file #{tarball_name}!")
82+
end
83+
when /^.*\.zip/
84+
cmd = Chef::ShellOut.new(
85+
%Q[ unzip "#{Chef::Config[:file_cache_path]}/#{tarball_name}" -d "#{tmpdir}" ]
86+
).run_command
87+
unless cmd.exitstatus == 0
88+
Chef::Application.fatal!("Failed to extract file #{tarball_name}!")
89+
end
90+
when /^.*\.tar.gz/
91+
cmd = Chef::ShellOut.new(
92+
%Q[ tar xvzf "#{Chef::Config[:file_cache_path]}/#{tarball_name}" -C "#{tmpdir}" ]
93+
).run_command
94+
unless cmd.exitstatus == 0
95+
Chef::Application.fatal!("Failed to extract file #{tarball_name}!")
96+
end
97+
end
98+
99+
cmd = Chef::ShellOut.new(
100+
%Q[ mv "#{tmpdir}/#{app_dir_name}" "#{app_dir}" ]
101+
).run_command
102+
unless cmd.exitstatus == 0
103+
Chef::Application.fatal!(%Q[ Command \' mv "#{tmpdir}/#{app_dir_name}" "#{app_dir}" \' failed ])
104+
end
105+
FileUtils.rm_r tmpdir
106+
new_resource.updated_by_last_action(true)
107+
end
108+
109+
#update-alternatives
110+
if new_resource.default
111+
Chef::Log.debug "app_home is #{app_home} and app_dir is #{app_dir}"
112+
current_link = ::File.symlink?(app_home) ? ::File.readlink(app_home) : nil
113+
if current_link != app_dir
114+
Chef::Log.debug "symlinking #{app_dir} to #{app_home}"
115+
FileUtils.rm_f app_home
116+
FileUtils.ln_sf app_dir, app_home
117+
end
118+
if new_resource.bin_cmds
119+
new_resource.bin_cmds.each do |cmd|
120+
if ::File.exists? "/usr/bin/#{cmd}"
121+
current_bin_link = ::File.readlink("/usr/bin/#{cmd}")
122+
else
123+
current_bin_link = false
124+
end
125+
should_be_link = "#{app_home}/bin/#{cmd}"
126+
if current_bin_link != should_be_link
127+
cmd = Chef::ShellOut.new(
128+
%Q[ update-alternatives --install /usr/bin/#{cmd} #{cmd} #{app_home}/bin/#{cmd} 1;
129+
update-alternatives --set #{cmd} #{app_home}/bin/#{cmd} ]
130+
).run_command
131+
unless cmd.exitstatus == 0
132+
Chef::Application.fatal!(%Q[ update alternatives failed ])
133+
end
134+
end
135+
end
136+
end
137+
end
138+
end
139+
140+
action :remove do
141+
app_dir_name, tarball_name = parse_app_dir_name(new_resource.url)
142+
app_root = new_resource.app_home.split('/')[0..-2].join('/')
143+
app_dir = app_root + '/' + app_dir_name
144+
145+
if ::File.exists?(app_dir)
146+
new_resource.bin_cmds.each do |cmd|
147+
cmd = execute "update_alternatives" do
148+
command "update-alternatives --remove #{cmd} #{app_dir} "
149+
returns [0,2]
150+
action :nothing
151+
end
152+
cmd.run_action(:run)
153+
end
154+
Chef::Log.info "Removing #{new_resource.name} at #{app_dir}"
155+
FileUtils.rm_rf app_dir
156+
new_resource.updated_by_last_action(true)
157+
end
158+
end

0 commit comments

Comments
 (0)