This commit is contained in:
Alexey Berezhok
2026-05-14 00:32:06 +03:00
parent 3cc428df43
commit e0419fea4b
2 changed files with 196 additions and 0 deletions

View File

@@ -315,3 +315,72 @@ def hestia_save_file_key_pair(file, key, value)
end
end
end
def hestia_change_sys_config_value(key, value)
# Privileged access check
hestia_check_privileged_user unless Process.uid == 0
config_file = "/usr/local/hestia/conf/hestia.conf"
if File.exist?(config_file)
File.open(config_file, File::RDWR | File::LOCK_SH) do |f|
# Check if key exists in the configuration file
existing_line_index = -1
f.each_with_index do |line, idx|
line_stripped = line.strip
# Skip comment lines
next if line_stripped.start_with?("#")
next if line_stripped.empty?
key_match = line_stripped.match(/^\s*#{Regexp.escape(key)}='\s*(.*?)\s*$/)
if key_match
existing_line_index = idx + 1
break
end
end
if existing_line_index.nil? || existing_line_index == -1
# Key doesn't exist - append new line to file
File.open(config_file, "a") do |append_f|
append_f.flock(File::LOCK_EX)
append_f.puts("#{key}='#{value}'")
end
OK
else
# Key exists - update value using Ruby operators (in-place edit)
# Use temporary file for safety and atomic replacement
temp_file = "#{config_file}.tmp"
File.open(config_file, "r") do |input_f|
lines = []
input_f.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
end
File.open(temp_file, "w") do |output_f|
output_f.flock(File::LOCK_EX)
lines.each { |l| output_f.puts(l) }
end
# Atomic file replacement
File.rename(temp_file, config_file)
OK
end
end
end
else
check_result error_code: E_NOTEXIST, error_message: "Configuration file #{config_file} does not exist"
end
end