Chef execute fails for the first time

424 views Asked by At

I have a weird problem with a Chef Recipe. Let me tell you that I'm new to Chef so pardon me if something looks awfully wrong.

I have my war file, which is built by Spring Boot. I just need to run java -jar <file>.war -config=config/ to run my app.

I recently started experimenting with Chef, and getting to write recipes that do this job.

The code from my recipe is as follows:

#Some code has been omitted intentionally.

directory "#{home}" do
  owner 'root'
  group 'root'
  mode '0755'
  recursive true
end

directory "#{home}/config" do
  owner 'root'
  group 'root'
  mode '0755'
end
cookbook_file "#{home}/config/ehcache.xml" do
  source "ehcache.xml"
  mode "0644"
end
# Get the war file from the repo
remote_file "#{home}/app.war" do
   source "#{node['baseos']['files_repo_url']}/wars/app.war"
   owner 'root'
   group 'root'
   mode '0644'
end

execute 'Run the war file' do
  command "java -jar '#{home}/app.war' -config='#{home}/config/'"
  action :run
end

The war file, and the related config folder along with its contents are successfully being copied to their respective destinations before the execute command gets fired. The problem is when the machine gets freshly created with kitchen create, the first kitchen verify would fail, saying 'Errno::ENOENT: No such file or directory - java -jar /opt/com/app.war -config=config/'. This only happens for the first time. Surprisingly, after saying kitchen verify again, the app starts up, and successfully runs.

This is weird because of the fact that the required file app.war and the config/ are [or should be] already there in the machine with appropriate privileges.

I know Chef processes these things sequentially, so given that the execute command is the very last line in my recipe, it should already have what is required to run the war file. I'm going nuts, can anyone provide some insight into this one?

Thank you!

1

There are 1 answers

0
coderanger On BEST ANSWER

While it is somewhat counter-intuitive, this is probably because you don't have java available on your $PATH. This might mean java is not installed, or that it is installed but not in a way that Chef can find it. Remember that environment variables are only inherited when a process starts, so if you installed Java in such a way that the installer set up some global change to $PATH it wouldn't be visible to Chef. A good fix would be to use the full path to the JVM binary (/opt/whatever/bin/java or something).