Reputation: 37
I'm trying to update our cookbook to confirm whether or not Java is available on a Windows server. I don't care if it is Oracle Java, OpenJDK or whatever. Whichever Java is present is irrelevant, I need to know Java is there before the service block is run as the app my cookbook installs is dependent and the user base can decide on the Java installed. Java choices available right now to the user base are Oracle Java and Azul Zulu Java, and that could change in the future.
I've thought of not_if, only_if, etc, but still the cookbook fails because it attempts to start the service and I know that my test instance does not have Java (deliberately as a test).
Not sure it will help in answering, but so far my code to start the service is:
service 'MyApp' do
action %i[enable start]
not_if { ::File.exist?("#{install_dir}\\scripts\\instance-stopped") }
end
This works well if Java is there on the server, but the cookbook fails once the Java element is removed. Executing the "install" step works, Java is not needed for the actual install of the software but it is needed to run the app.
Any help you can give would be great.
Upvotes: 0
Views: 400
Reputation: 37
Unfortunately, I can't choose to deploy Java because the various project teams I deal with could choose to deploy any of four separate packages, each one defined as valid by our internal compliance team, for their personal project needs.
I've gone down the route of using shell_out to run "java -version". This was found with a little more digging into the options available : How to check which java version is installed
I haven't created an ohai function, I used what I saw there and created something that sets a var named java_version, initialised to "none" at the start of the recipe. Then run a modified snippet of the ohai function to overwrite the value of the java_version var with just the version value, if there is one, from the shell_out call.
Then I have a guard, using unless, to check if java_version is set to "none". If it is, skip the processing, otherwise start my application.
Upvotes: 0
Reputation: 10102
there are 2 approaches which you can take...
the recommended one, is having your recipe to always install java. due to chef nature (idempotence), chef will skip java installation if java is already installed.
an alternative, is having a guard (as you already used) that checks whether java is installed. the most straightforward to check whether java is installed, is to test whether java
command works.
Upvotes: 1