I have recipe , where I am basically trying to update multi_string windows registry using registry_key
chef resource . I have to make some conditional checks on existing value before updating it & hence I am using ruby_block
for doing so.
However I am facing an issue where in node attributes are not getting evaluated lazily , I am using this lazy node attr for setting registry value.
For more detailed info , I am pasting a full recipe code here for analysis
# Cookbook:: myCookbook
# Recipe:: myrecipe-rectify-java-param-tomcat-service
# In brief , this recipe does simple things
### 1 Fetch existing value of registry(HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\ABC\Parameters\Java)
### 2 Check if it has '-Dsun.net.maxDatagramSockets' in it , if yes then update its value , if no then add -Dsun.net.maxDatagramSockets=1024 entry in it
### 3 Update registry(HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\ABC\Parameters\Java)
node.default['myCookbook']['options_value'] = nil
node.default['myCookbook']['existing_java_key_value'] = nil
#1
ruby_block "fetching JAVA options of ABC tomcat service" do
block do
node.override['myCookbook']['existing_java_key_value'] = registry_get_values('HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\ABC\Parameters\Java', :x86_64)
node['myCookbook']['existing_java_key_value'].each{|e|
if e[:name] == "Options"
node.override['myCookbook']['options_value'] = e[:data]
Chef::Log.info("1--- herrreeee options value is ")
Chef::Log.info(node['myCookbook']['options_value'])
end
}
end
action :run
end
#2
ruby_block "modifying JAVA reg key value through ruby block" do
block do
Chef::Log.info("2--- here in ruby lblockck ")
Chef::Log.info("2.1 --- options_value is ")
doesItemExist = false
indexis = 0
node['myCookbook']['options_value'].each_with_index { |item,index|
if (item.include? "-Dsun.net.maxDatagramSockets")
Chef::Log.info("item is "+item)
doesItemExist = true
indexis = index
break
end
}
if (doesItemExist)
Chef::Log.info("hey registry already has maxDatagramSockets thing at index #{indexis} , so need to replace it")
node.override['myCookbook']['options_value'][indexis]="-Dsun.net.maxDatagramSockets=#{node['myCookbook']['abcservice']['max_datagram_socket']}"
else
Chef::Log.info("Seems it is first time we are adding maxDatagramsocket")
node.override['myCookbook']['options_value'].insert(-1, "-Dsun.net.maxDatagramSockets=#{node['myCookbook']['abcservice']['max_datagram_socket']}")
end
Chef::Log.info("2.2 ##### #{node['myCookbook']['options_value']} #####")
Chef::Log.info(node['myCookbook']['options_value'])
end
action :run
end
#3
registry_key 'HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\ABC\Parameters\Java' do
values [{
name: 'Options',
type: :multi_string,
#data: options_value
data: lazy { node[myCookbook][:options_value] }
}]
action :create
end
Error , which I receive when running this recipe through chef solo , I face an issue at Step#3 (i.e setting registry with updated value) , it seems that lazy
node attribute node[myCookbook][:options_value]
is still having nil
value & doesn't persist the value , which I am setting in ruby_block
[2021-07-08T15:48:23+05:30] DEBUG: Converging node AMD-MYVM-0
Recipe: myCookbook::myrecipe-rectify-java-param-tomcat-service[0m
* ruby_block[fetching JAVA options of ABC tomcat service] action run[2021-07-08T15:48:23+05:30] INFO: Processing ruby_block[fetching JAVA options of ABC tomcat service] action run (myCookbook::myrecipe-rectify-java-param-tomcat-service line 29)
[2021-07-08T15:48:23+05:30] INFO: 1--- herrreeee options value is
[2021-07-08T15:48:23+05:30] INFO: ["-XX:+PrintGCDetails,", "-XX:+CMSClassUnloadingEnabled,", "-XX:ReservedCodeCacheSize=256m,", "-Dabc.home=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat\\\\..,", "-Dabc.host=AMD-MYVM-0,", "-Dcatalina.base=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat,", "-Dcatalina.home=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat,", "-Djava.endorsed.dirs=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat\\\\endorsed,", "-Djava.io.tmpdir=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat\\\\temp,", "-Darchaius.configurationSource.additionalUrls=file:///D:/TESTCOMPANY_HOME/TESTCOMPANY/Properties/application.properties,file:///D:/TESTCOMPANY_HOME/TESTCOMPANY/ABC/conf/overrides.properties,", "-DFileCacheSharedRootDir=\\\\\\\\127.0.0.1\\\\Repositories\\\\data\\\\cache,", "-Dlog4j.configurationFile=D:\\\\TESTCOMPANY_CONFIGS\\\\ABC\\\\conf\\\\log4j2.xml,", "-Xdebug,", "-Xrunjdwp:transport=dt_socket,address=8969,server=y,suspend=n,", "-Dcom.sun.management.jmxremote,", "-Dcom.sun.management.jmxremote.port=7969,", "-Dcom.sun.management.jmxremote.ssl=false,", "-Dcom.sun.management.jmxremote.authenticate=false,", "-Dsun.net.maxDatagramSockets=1028,", "-XX:-CreateMinidumpOnCrash"]
[2021-07-08T15:48:23+05:30] INFO: ruby_block[fetching JAVA options of ABC tomcat service] called
[32m- execute the ruby block fetching JAVA options of ABC tomcat service[0m
[0m * ruby_block[modifying JAVA reg key value through ruby block] action run[2021-07-08T15:48:23+05:30] INFO: Processing ruby_block[modifying JAVA reg key value through ruby block] action run (myCookbook::myrecipe-rectify-java-param-tomcat-service line 43)
[2021-07-08T15:48:23+05:30] INFO: 2--- here in ruby lblockck
[2021-07-08T15:48:23+05:30] INFO: 2.1 --- options_value is
[2021-07-08T15:48:23+05:30] INFO: item is -Dsun.net.maxDatagramSockets=1028,
[2021-07-08T15:48:23+05:30] INFO: now item is -Dsun.net.maxDatagramSockets=1028,
[2021-07-08T15:48:23+05:30] INFO: hey registry already has maxDatagramSockets thing at index 18 , so need to replace it
[2021-07-08T15:48:23+05:30] INFO: 2.2 ##### ["-XX:+PrintGCDetails,", "-XX:+CMSClassUnloadingEnabled,", "-XX:ReservedCodeCacheSize=256m,", "-Dabc.home=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat\\\\..,", "-Dabc.host=AMD-MYVM-0,", "-Dcatalina.base=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat,", "-Dcatalina.home=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat,", "-Djava.endorsed.dirs=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat\\\\endorsed,", "-Djava.io.tmpdir=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat\\\\temp,", "-Darchaius.configurationSource.additionalUrls=file:///D:/TESTCOMPANY_HOME/TESTCOMPANY/Properties/application.properties,file:///D:/TESTCOMPANY_HOME/TESTCOMPANY/ABC/conf/overrides.properties,", "-DFileCacheSharedRootDir=\\\\\\\\127.0.0.1\\\\Repositories\\\\data\\\\cache,", "-Dlog4j.configurationFile=D:\\\\TESTCOMPANY_CONFIGS\\\\ABC\\\\conf\\\\log4j2.xml,", "-Xdebug,", "-Xrunjdwp:transport=dt_socket,address=8969,server=y,suspend=n,", "-Dcom.sun.management.jmxremote,", "-Dcom.sun.management.jmxremote.port=7969,", "-Dcom.sun.management.jmxremote.ssl=false,", "-Dcom.sun.management.jmxremote.authenticate=false,", "-Dsun.net.maxDatagramSockets=1024", "-XX:-CreateMinidumpOnCrash"] #####
[2021-07-08T15:48:23+05:30] INFO: ["-XX:+PrintGCDetails,", "-XX:+CMSClassUnloadingEnabled,", "-XX:ReservedCodeCacheSize=256m,", "-Dabc.home=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat\\\\..,", "-Dabc.host=AMD-MYVM-0,", "-Dcatalina.base=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat,", "-Dcatalina.home=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat,", "-Djava.endorsed.dirs=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat\\\\endorsed,", "-Djava.io.tmpdir=D:\\\\TESTCOMPANY_HOME\\\\TESTCOMPANY\\\\ABC\\\\tomcat\\\\temp,", "-Darchaius.configurationSource.additionalUrls=file:///D:/TESTCOMPANY_HOME/TESTCOMPANY/Properties/application.properties,file:///D:/TESTCOMPANY_HOME/TESTCOMPANY/ABC/conf/overrides.properties,", "-DFileCacheSharedRootDir=\\\\\\\\127.0.0.1\\\\Repositories\\\\data\\\\cache,", "-Dlog4j.configurationFile=D:\\\\TESTCOMPANY_CONFIGS\\\\ABC\\\\conf\\\\log4j2.xml,", "-Xdebug,", "-Xrunjdwp:transport=dt_socket,address=8969,server=y,suspend=n,", "-Dcom.sun.management.jmxremote,", "-Dcom.sun.management.jmxremote.port=7969,", "-Dcom.sun.management.jmxremote.ssl=false,", "-Dcom.sun.management.jmxremote.authenticate=false,", "-Dsun.net.maxDatagramSockets=1024", "-XX:-CreateMinidumpOnCrash"]
[2021-07-08T15:48:23+05:30] INFO: ruby_block[modifying JAVA reg key value through ruby block] called
[32m- execute the ruby block modifying JAVA reg key value through ruby block[0m
[0m * registry_key[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\ABC\Parameters\Java] action create[2021-07-08T15:48:23+05:30] INFO: Processing registry_key[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\ABC\Parameters\Java] action create (myCookbook::myrecipe-rectify-java-param-tomcat-service line 75)
[0m
================================================================================[0m
[31mError executing action `create` on resource 'registry_key[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\ABC\Parameters\Java]'[0m
================================================================================[0m
[0m NoMethodError[0m
-------------[0m
undefined method `to_a' for #<Chef::DelayedEvaluator:0x0000000009f91f10>
[0m Did you mean? to_s[0m
[0m Resource Declaration:[0m
---------------------[0m
# In D:/chef_repos_copied_from_testcompany_git/cache/cookbooks/myCookbook/recipes/myrecipe-rectify-java-param-tomcat-service.rb
[0m
[0m 75: registry_key 'HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\ABC\Parameters\Java' do
[0m 76: values [{
[0m 77: name: 'Options',
[0m 78: type: :multi_string,
[0m 79: #data: options_value
[0m 80: data: lazy { node[myCookbook][:options_value] }
[0m 81: }]
[0m 82: action :create
[0m 83: end
[0m
[0m Compiled Resource:[0m
------------------[0m
# Declared in D:/chef_repos_copied_from_testcompany_git/cache/cookbooks/myCookbook/recipes/myrecipe-rectify-java-param-tomcat-service.rb:75:in `from_file'
[0m
[0m registry_key("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\ABC\Parameters\Java") do
[0m action [:create]
[0m default_guard_interpreter :default
[0m values [{:name=>"Options", :type=>:multi_string, :data=>#<Chef::DelayedEvaluator:0x0000000009f91f10@D:/chef_repos_copied_from_testcompany_git/cache/cookbooks/myCookbook/recipes/myrecipe-rectify-java-param-tomcat-service.rb:80>}]
[0m unscrubbed_values [{:name=>"Options", :type=>:multi_string, :data=>#<Chef::DelayedEvaluator:0x0000000009f91f10@D:/chef_repos_copied_from_testcompany_git/cache/cookbooks/myCookbook/recipes/myrecipe-rectify-java-param-tomcat-service.rb:80>}]
[0m declared_type :registry_key
[0m cookbook_name "myCookbook"
[0m recipe_name "myrecipe-rectify-java-param-tomcat-service"
[0m key "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Apache Software Foundation\\Procrun 2.0\\ABC\\Parameters\\Java"
[0m end
[0m
[0m System Info:[0m
------------[0m
chef_version=15.7.32
[0m platform=windows
[0m platform_version=6.3.9600
[0m ruby=ruby 2.6.5p114 (2019-10-01 revision 67812) [x64-mingw32]
[0m program_name=C:/opscode/chefdk/bin/chef-solo
[0m executable=C:/opscode/chefdk/bin/chef-solo[0m
[0m[2021-07-08T15:48:23+05:30] INFO: Running queued delayed notifications before re-raising exception
[0m
Running handlers:[0m
[2021-07-08T15:48:23+05:30] ERROR: Running exception handlers
Running handlers complete
[0m[2021-07-08T15:48:23+05:30] ERROR: Exception handlers complete
Chef Infra Client failed. 2 resources updated in 04 seconds[0m
[2021-07-08T15:48:23+05:30] FATAL: Stacktrace dumped to D:/chef_repos_copied_from_testcompany_git/cache/chef-stacktrace.out
[2021-07-08T15:48:23+05:30] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2021-07-08T15:48:23+05:30] DEBUG: NoMethodError: registry_key[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\ABC\Parameters\Java] (myCookbook::myrecipe-rectify-java-param-tomcat-service line 75) had an error: NoMethodError: undefined method `to_a' for #<Chef::DelayedEvaluator:0x0000000009f91f10>
Did you mean? to_s
C:/opscode/chefdk/embedded/lib/ruby/2.6.0/win32/registry.rb:749:in `write'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.6.0/gems/chef-15.7.32-universal-ming