This commit is contained in:
Alexey Berezhok
2026-05-19 23:04:15 +03:00
parent 6ff9d67909
commit 5e32af0148
2 changed files with 42 additions and 57 deletions

View File

@@ -327,11 +327,20 @@ def hestia_change_sys_config_value(key, value)
config_file = "/usr/local/hestia/conf/hestia.conf"
if File.exist?(config_file)
File.open(config_file, File::RDWR) do |f|
# First pass: read entire file to check if key exists and get all lines
content = nil
File.open(config_file, "r") do |f|
content = f.read
end
if content
lines = content.split("\n")
# Check if key exists in the configuration file
existing_line_index = -1
f.each_with_index do |line, idx|
lines.each_with_index do |line, idx|
line_stripped = line.strip
# Skip comment lines
next if line_stripped.start_with?("#")
@@ -355,32 +364,33 @@ def hestia_change_sys_config_value(key, value)
# Use temporary file for safety and atomic replacement
temp_file = "#{config_file}.tmp"
File.open(config_file, "r") do |input_f|
lines = []
# Second pass: rebuild the content with updated value
new_lines = []
input_f.each do |line|
line_stripped = line.strip
# Skip comment lines
next if line_stripped.start_with?("#")
next if line_stripped.empty?
lines.each do |line|
line_stripped = line.strip
# Skip comment lines
next if line_stripped.start_with?("#")
next if line_stripped.empty?
# Match and replace the key-value pair
if line.match(/^\s*#{Regexp.escape(key)}='[^']*'/)
lines << "#{key}='#{value}'"
else
lines << line
end
# Match and replace the key-value pair
if line.match(/^\s*#{Regexp.escape(key)}='[^']*'/)
new_lines << "#{key}='#{value}'"
else
new_lines << line
end
File.open(temp_file, "w") do |output_f|
lines.each { |l| output_f.puts(l) }
end
# Atomic file replacement
File.rename(temp_file, config_file)
OK
end
File.open(temp_file, "w") do |output_f|
new_lines.each { |l| output_f.puts(l) }
end
# Atomic file replacement
File.rename(temp_file, config_file)
OK
end
else
OK
end
else
check_result error_code: E_NOTEXIST, error_message: "Configuration file #{config_file} does not exist"