4

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...
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
pitchblack408
  • 2,913
  • 4
  • 36
  • 54

2 Answers2

6

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.
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • I couldn't get java from oracle to install using that cookbook because oracle doesn't allow you download the file using the cookie. So, I was trying to do it manually by creating a new cookbook just for this instance. If you have an idea how to use the java cookbook without having to create a repository, I would like to know. – pitchblack408 Feb 27 '15 at 23:30
  • @pitchblack408 Example given on how to install Oracle using the "Java" cookbook. The documentation states that the cookbook is fully aware of the cookie problem (see: https://supermarket.chef.io/cookbooks/java) – Mark O'Connor Feb 28 '15 at 13:19
  • Hi. I use cookbook from https://supermarket.chef.io/cookbooks/java linux ok. but on window i can't install i have error bad URI. plz help me – Luna Aug 25 '15 at 07:28
  • @Luna Looks like on windows you must provide a URL for the java package binary. See: https://github.com/agileorbit-cookbooks/java/blob/master/attributes/default.rb#L41 – Mark O'Connor Aug 25 '15 at 22:51
  • .thank so much but it dont run. have new error windown_package[tar] T_T – Luna Aug 26 '15 at 12:21
  • "java_alternatives "set java alternatives" do" where will this come in cookbook ? after running "chef-client --local-mode --runlist 'recipe[chef]' ", it gives me error of missing cookbook : windows – goCode Mar 14 '17 at 06:15
  • @goCode Best posed as a new question. At first glance your problem appears to be missing cookbook in the chef server – Mark O'Connor Mar 14 '17 at 20:49
  • Ok. Thank you so much for all the help Mark. – goCode Mar 15 '17 at 04:12
2

You should escape all double quotes in your command string, because it starts and ends with double quotes.

There is no action :creates for execute resource. The action should be :run.

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

But you don't want to run this resource on every chef run, so you should come up with some kind of condition, when it should be run (using only_if, not_if statements).

Draco Ater
  • 20,820
  • 8
  • 62
  • 86