diff --git a/README.md b/README.md index c41be086..37b8b99c 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ See `attributes/default.rb` for default values. - `node['java']['download_path']` - Location to download and extract the tarball - `node['java']['install_flavor']` - Flavor of JVM you would like installed (`oracle`, `oracle_rpm`, `openjdk`, `adoptopenjdk`, `ibm`, `windows`), default `openjdk` on Linux/Unix platforms, `windows` on Windows platforms. - `node['java']['install_type']` - Type of Java installation, defauls to jdk, needed for JCE to find the install path of jar's for JDK/JRE installation. -- `node['java']['jdk_version']` - JDK version to install, defaults to `'6'`. +- `node['java']['jdk_version']` - JDK version to install, defaults to `'8'`. - `node['java']['java_home']` - Default location of the "`$JAVA_HOME`". To configure this attribute for `ibm`, `ibm_tar`, and `oracle_rpm` install flavors, you must use an attribute precedence of `force_default` or higher in your attribute file. - `node['java']['set_etc_environment']` - Optionally sets JAVA_HOME in `/etc/environment` for Default `false`. - `node['java']['openjdk_packages']` - Array of OpenJDK package names to install in the `java::openjdk` recipe. This is set based on the platform. diff --git a/libraries/helpers.rb b/libraries/helpers.rb index cd6b00d9..a843cbc8 100644 --- a/libraries/helpers.rb +++ b/libraries/helpers.rb @@ -16,6 +16,7 @@ # require 'chef/version_constraint' +require 'chef/util/path_helper' require 'uri' require 'pathname' @@ -84,5 +85,40 @@ def valid_ibm_jdk_uri?(url) def platform_requires_license_acceptance? %w(smartos).include?(node['platform']) end + + def find_java(java_home=nil, version=nil) + # java_home - given JAVA_HOME to check version + # version - major JAVA version, e.g. '1.8.0_232' -> 8 + if node['platform'] == 'windows' + existence_check = :exists? + which = 'where' + java_in_path = java_home ? "#{java_home}\\bin:java.exe" : "java.exe" + else + existence_check = :executable? + which = 'which' + java_in_path = java_home ? "#{java_home}/bin/java" : "java" + end + + Chef::Log.debug "Using '#{which}' in #{java_in_path} to check the Java binary" + + # check all executables for version + shell_out("#{which} #{java_in_path}").stdout.chomp.split(/\n+/).each do |p| + p = "\"#{p}\"" if ['windows'].include?(node['platform']) + if version + if shell_out("#{p} -version").stderr.chomp =~ /^([^ ]+) version "(\d+)\.(\d+)\.(.*)"/m + jdk = $1 + ver = $2 + rel = $3 + subver = $4 + Chef::Log.debug "Found JDK: #{jdk} version #{ver}.#{rel}.#{subver} for node['jdk_version'] = #{version}" + return p if version && version == rel + end + else + return p + end + end + false + end + end end diff --git a/recipes/windows.rb b/recipes/windows.rb index f472967b..59fda7f9 100644 --- a/recipes/windows.rb +++ b/recipes/windows.rb @@ -35,36 +35,6 @@ uri = ::URI.parse(node['java']['windows']['url']) cache_file_path = File.join(node['java']['download_path'], File.basename(::URI.unescape(uri.path))) -if s3_bucket && s3_remote_path - aws_s3_file cache_file_path do - aws_access_key_id aws_access_key_id - aws_secret_access_key aws_secret_access_key - aws_session_token aws_session_token - checksum pkg_checksum if pkg_checksum - bucket s3_bucket - remote_path s3_remote_path - backup false - action :create - end -else - ruby_block 'Enable Accessing cookies' do - block do - cookie_jar = Chef::HTTP::CookieJar - - cookie_jar.instance["#{uri.host}:#{uri.port}"] = 'oraclelicense=accept-securebackup-cookie' - end - - only_if { node['java']['oracle']['accept_oracle_download_terms'] } - end - - remote_file cache_file_path do - checksum pkg_checksum if pkg_checksum - source node['java']['windows']['url'] - backup false - action :create - end -end - if node['java'].attribute?('java_home') && !node['java']['java_home'].nil? java_home_win = win_friendly_path(node['java']['java_home']) additional_options = if node['java']['jdk_version'].to_s == '8' @@ -96,14 +66,48 @@ additional_options = "#{additional_options} REMOVEOUTOFDATEJRES=1" end -windows_package node['java']['windows']['package_name'] do - source cache_file_path - checksum node['java']['windows']['checksum'] - action :install - returns node['java']['windows']['returns'] - installer_type :custom - options "/s #{additional_options}" - notifies :write, 'log[jdk-version-changed]', :immediately +# Check if java executable exists +unless find_java(java_home_win ? java_home_win : java_publicjre_home_win, node['java']['jdk_version']) + if s3_bucket && s3_remote_path + aws_s3_file cache_file_path do + aws_access_key_id aws_access_key_id + aws_secret_access_key aws_secret_access_key + aws_session_token aws_session_token + checksum pkg_checksum if pkg_checksum + bucket s3_bucket + remote_path s3_remote_path + backup false + action :create + end + else + ruby_block 'Enable Accessing cookies' do + block do + cookie_jar = Chef::HTTP::CookieJar + + cookie_jar.instance["#{uri.host}:#{uri.port}"] = 'oraclelicense=accept-securebackup-cookie' + end + + only_if { node['java']['oracle']['accept_oracle_download_terms'] } + end + + remote_file cache_file_path do + checksum pkg_checksum if pkg_checksum + source node['java']['windows']['url'] + backup false + action :create + end + end + + + windows_package node['java']['windows']['package_name'] do + source cache_file_path + checksum node['java']['windows']['checksum'] + action :install + returns node['java']['windows']['returns'] + installer_type :custom + options "/s #{additional_options}" + notifies :write, 'log[jdk-version-changed]', :immediately + end end include_recipe 'java::oracle_jce' if node['java']['oracle']['jce']['enabled']