Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

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

I am having syntax errors and I am unsure how to resolve them. In the roles file I have defined the fallowing:

default_attributes(
  'jdk' => {
    'version' => '1.8.0_31'
  },
  'java' => {
    'home' => '/usr/lib/jvm/jdk1.8.0_31'
  },
)

I try to use the java home in the following, but it fails to run

execute "update_alt_java" do
  command "update-alternatives --install "/usr/bin/java" "java" "#{node['java']['home']}/bin/java" 1"
  action :creates
end

I get the following error

SyntaxError
-----------
/etc/chef/src/cookbooks/jdk/recipes/default.rb:50: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
...tives --install "/usr/bin/java" "java" "#{node['java']['home...
...                               ^
/etc/chef/src/cookbooks/jdk/recipes/default.rb:50: syntax error, unexpected tIDENTIFIER, expecting keyword_end
...--install "/usr/bin/java" "java" "#{node['java']['home']}/bi...
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Are you using the community java cookbook?

It includes an LWRP for this purpose:

# 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

Update: "my_java" wrapper cookbook example

The following is a sample cookbook called "my_java" designed to install the oracle JDK on Ubuntu:

├── attributes
│?? └── java.rb   <-- Used for java cookbook attribute overrides
├── Berksfile
├── Berksfile.lock
├── metadata.rb
├── README.md
└── recipes
    └── default.rb

After running chef the oracle JDK is intalled

$ java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

Notes:

  • This cookbook relies on overriding attributes. This could just as easily be done from a chef environment or role.

metadata.rb

name             'my_java'
maintainer       'Mark O''Connor'
maintainer_email 'XXXXXXXXXXXXXXX'
license          'All rights reserved'
description      'Installs/Configures my_java'
long_description 'Installs/Configures my_java'
version          '0.1.0'

depends "apt"
depends "java"

attribute/java.rb

normal['java']['jdk_version'] = '8'
normal['java']['install_flavor'] = 'oracle'
normal['java']['oracle']['accept_oracle_download_terms'] = true

Notes:

  • By default java will install openJDK. These overrides specify v8 of OracleJDK

recipes/default.rb

include_recipe "apt"
include_recipe "java"

Notes:

  • Obviously the "apt" cookbook only applies to Debian based linux. This recipe would require minor tweaking to work on redhat as well.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...