Trying to modify the file "/root/chef-repo/limits.conf" and include/update the following values:
Modify:
cloudusr hard core 10 ----> cloudusr hard core 20 cloudusr soft core 10 ----> cloudusr soft core 20
Add:
cloudusr soft data unlimited cloudusr hard stack 10240 cloudusr soft stack 10240
The Current content of the file: /root/chef-repo/limits.conf is:
cloudusr hard core 10 cloudusr hard fsize unlimited cloudusr hard nofile 1024 cloudusr hard nproc 4096 cloudusr hard rss unlimited cloudusr hard cpu unlimited cloudusr soft core 10 cloudusr soft fsize unlimited cloudusr soft nofile 5120 cloudusr soft nproc 6144 cloudusr soft rss unlimited cloudusr soft cpu unlimited
Below is the code snippet:
core = "20"
Utype[] = ""
Utype[core] = "both"
data = "unlimited"
Utype[data] = "soft"
stack = "10240"
Utype[stack] = "both"
result = ""
user = "cloudusr"
text = File.read("/root/chef-repo/90-nproc.conf")
new_contents = text.gsub!(%r{(?<!#)^}, "#")
puts new_contents
File.open("/root/chef-repo/90-nproc.conf", "w") {|file| file.puts new_contents }
params = "core#data#stack"
enter code here
params.split('#').each do |param|
puts "Parameter value to change: #{param}"
puts "Limit for #{param} to change #{Utype[#{param}]}"
if "#{Utype["#{param}"]}" == "both"
cmd = Mixlib::ShellOut.new("cat '/root/chef-repo/limits.conf' | grep #{user} | grep #{param} | grep -c 'hard'
")
cmd.run_command
hval = cmd.stdout
cmd = Mixlib::ShellOut.new("cat '/root/chef-repo/limits.conf' | grep #{user} | grep #{param} | grep -c 'soft'
")
cmd.run_command
sval = cmd.stdout
if hval.to_i == 1 || sval.to_i == 1
File.readlines("/root/chef-repo/limits.conf").each do |line|
if line.include?("#{user}")
if line.include?("#{param}")
val = line.split(' ').last
line1 = line.gsub("#{val}", "#{"#{param}"}")
sed = Chef::Util::FileEdit.new("/root/chef-repo/limits.conf")
sed.search_file_replace(line, line1)
sed.write_file
end
end
end
end
if hval.to_i < 1
File.open("/root/chef-repo/limits.conf", "a+") do |file|
file << "#{user} hard #{param} #{"#{param}"}"
end
end
if sval.to_i < 1
File.open("/root/chef-repo/limits.conf", "a+") do |file|
file << "#{user} soft #{param} #{"#{param}"}"
end
end
end
if "#{Utype["#{param}"]}" == "soft"
cmd = Mixlib::ShellOut.new("cat '/root/chef-repo/limits.conf' | grep #{user} | grep #{param} | grep -c 'soft'
")
cmd.run_command
sval = cmd.stdout
if sval.to_i == 1
File.readlines("/root/chef-repo/limits.conf").each do |line|
if line.include?("#{user}")
if line.include?("#{param}")
val = line.split(' ').last
line1 = line.gsub("#{val}", "#{"#{param}"}")
sed = Chef::Util::FileEdit.new("/root/chef-repo/limits.conf")
sed.search_file_replace(line, line1)
sed.write_file
end
end
end
else
File.open("/root/chef-repo/limits.conf", "a+") do |file|
file << "#{user} soft #{param} #{"#{param}"}"
end
end
end
if "#{Utype["#{param}"]}" == "hard"
cmd = Mixlib::ShellOut.new("cat '/root/chef-repo/limits.conf' | grep #{user} | grep #{param} | grep -c 'hard'
")
cmd.run_command
hval = cmd.stdout
if hval.to_i == 1
File.readlines("/root/chef-repo/limits.conf").each do |line|
if line.include?("#{user}")
if line.include?("#{param}")
val = line.split(' ').last
line1 = line.gsub("#{val}", "#{"#{param}"}")
sed = Chef::Util::FileEdit.new("/root/chef-repo/limits.conf")
sed.search_file_replace(line, line1)
sed.write_file
end
end
end
else
File.open("/root/chef-repo/limits.conf", "a+") do |file|
file << "#{user} hard #{param} #{"#{param}"}"
end
end
end
if "#{Utype["#{param}"]}" == "ignore"
next
end
end
