I have some struggle about the converge/compile phase of a chef recipe.
I want to update/create a java keystore, only_if the cert provided in a vault is different that the one in the keystore.
So I write temporary file from the vault on the system, use a ruby_block to compute the md5, and use this md5 on the only_if condition on the execute block that manage the keystore.
Here is the last code I have tried :
ruby_block "get private cert md5" do
block do
vault_md5 = Mixlib::ShellOut.new("openssl x509 -in /tmp/mycerrt.crt -fingerprint -md5 | head -1 | sed -e 's/MD5 Fingerprint=//'")
vault_md5.run_command
# get dynamically the only_if statement to update it.
exec_r = run_context.resource_collection.find(:execute => "create p12 store")
exec_r.only_if "[ \"#{cur_md5.stdout}\" -ne \"#{vault_md5.stdout}\" ]"
end
end
execute "create p12 store" do
command "openssl pkcs12 -export -in /tmp/mycerrt.crt -inkey /tmp/myKey -certfile /tmp/mycerrt.crt -name priv -out /tmp/keystore.p12 -password pass:#{key['PrivateKeystorePassword']}"
only_if "[ \"#{cur_md5.stdout}\" -eq '']"
notifies :run, 'execute[convert keystore]', :immediately
action :run
end
With this code the execute block if always skipped due to only_if. Thanks for your help