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

@@ -62,7 +62,7 @@ def log_migrate_stage(stage)
yield
# Record the successful stage
File.open(log_file, 'a') { |f| f.puts stage }
hestia_print_error_message_to_cli "Stage #{stage} completed."
hestia_print_info_message_to_cli "Stage #{stage} completed."
rescue => e
hestia_print_error_message_to_cli "Stage #{stage} failed: #{e.message}"
exit 1
@@ -165,10 +165,12 @@ when :migratenginx
# Find all files under the nginx-system directory
nginx_conf_dir = '/usr/local/hestia/nginx-system/etc/nginx'
Dir.glob(File.join(nginx_conf_dir, '**', '*')).each do |path|
hestia_print_info_message_to_cli "stage 4 processing file #{path}"
next if File.directory?(path)
content = File.read(path)
new_content = content.gsub(/(?<!\/usr\/local\/hestia\/nginx-system)\/etc\/nginx/, '/usr/local/hestia/nginx-system/etc/nginx')
if new_content != content
hestia_print_info_message_to_cli "Changed path to config in file #{path}"
File.open(path, 'w') { |f| f.write(new_content) }
end
end
@@ -182,12 +184,13 @@ when :migratenginx
# Find and replace port in all .conf files
Dir.glob(File.join(nginx_conf_dir, '**', '*.conf')).each do |conf_file|
hestia_print_info_message_to_cli "stage 5 processing file #{conf_file}"
content = File.read(conf_file)
modified = false
# Replace HTTP port pattern: listen <IP>:#{$PROXY_PORT} [flags];
# Matches: listen 192.168.3.81:80; or listen 192.168.3.81:80 default_server;
new_content = content.gsub(/(?<![0-9a-fA-F:])\d{1,3}(\.\d{1,3}){3}:\#\{$PROXY_PORT\}\s*(.*)/i) do |match|
new_content = content.gsub(/(?<![0-9a-fA-F:])\d{1,3}(\.\d{1,3}){3}:#{$PROXY_PORT}\s*(.*)/i) do |match|
modified = true
ip_part = $&.split(':')[0] # Get IP address part
remaining = $POSTMATCH.strip # Get any additional flags (default_server, etc.)
@@ -201,7 +204,7 @@ when :migratenginx
# If first substitution didn't match, try simpler pattern for ports without IP
if new_content == content
new_content = content.gsub(/#\{$PROXY_PORT\}\s*(ssl\s*)?;?\s*$/i) do |match|
new_content = content.gsub(/#{$PROXY_PORT}\s*(ssl\s*)?;?\s*$/i) do |match|
modified = true
flags = $1 || ''
";#{proxy_port}#{flags}"
@@ -210,7 +213,7 @@ when :migratenginx
# Replace SSL port pattern: listen <IP>:#{$PROXY_SSL_PORT} ssl;
if new_content == content
new_content = content.gsub(/(?<![0-9a-fA-F:])\d{1,3}(\.\d{1,3}){3}:\#\{$PROXY_SSL_PORT\}\s+ssl\s*;?\s*$/i) do |match|
new_content = content.gsub(/(?<![0-9a-fA-F:])\d{1,3}(\.\d{1,3}){3}:#{$PROXY_SSL_PORT}\s+ssl\s*;?\s*$/i) do |match|
modified = true
ip_part = $&.split(':')[0] # Get IP address part
"#{ip_part}:#{proxy_ssl_port} ssl;"
@@ -219,8 +222,8 @@ when :migratenginx
# Simple fallback: replace the port variable values directly
if new_content == content
new_content = content.gsub("#\{$PROXY_PORT\}", proxy_port)
new_content = new_content.gsub("#\{$PROXY_SSL_PORT\}", proxy_ssl_port)
new_content = content.gsub("#{$PROXY_PORT}", proxy_port)
new_content = new_content.gsub("#{$PROXY_SSL_PORT}", proxy_ssl_port)
modified = true if new_content != content
end
@@ -230,34 +233,6 @@ when :migratenginx
hestia_print_info_message_to_cli " Updated: #{conf_file}"
end
end
# Also update default.conf specifically if it exists
default_conf = File.join(nginx_conf_dir, 'conf.d', 'default.conf')
if File.exist?(default_conf)
content = File.read(default_conf)
modified = false
# Replace HTTP port in default.conf
new_content = content.gsub(/#\{$PROXY_PORT\}\s*(default_server\s*)?;/i) do |match|
modified = true
flags = $1 || ''
"#{proxy_port}#{flags};"
end
# Replace SSL port in default.conf
if new_content == content || new_content.include?("#{$PROXY_SSL_PORT}")
new_content = content.gsub(/#\{$PROXY_SSL_PORT\}\s+default_server\s+ssl\s*;?\s*$/i) do |match|
modified = true
"#{proxy_ssl_port} default_server ssl;"
end
end
# Write changes if modified
if modified
File.open(default_conf, 'w') { |f| f.write(new_content) }
hestia_print_info_message_to_cli " Updated: #{default_conf}"
end
end
end
log_migrate_stage('stage6') do
hestia_change_sys_config_value("PROXY_PORT", "8078")

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,10 +364,10 @@ 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|
lines.each do |line|
line_stripped = line.strip
# Skip comment lines
next if line_stripped.start_with?("#")
@@ -366,21 +375,22 @@ def hestia_change_sys_config_value(key, value)
# Match and replace the key-value pair
if line.match(/^\s*#{Regexp.escape(key)}='[^']*'/)
lines << "#{key}='#{value}'"
new_lines << "#{key}='#{value}'"
else
lines << line
new_lines << line
end
end
File.open(temp_file, "w") do |output_f|
lines.each { |l| output_f.puts(l) }
new_lines.each { |l| output_f.puts(l) }
end
# Atomic file replacement
File.rename(temp_file, config_file)
OK
end
end
else
OK
end
else
check_result error_code: E_NOTEXIST, error_message: "Configuration file #{config_file} does not exist"