This commit is contained in:
Alexey Berezhok
2026-05-20 23:24:30 +03:00
parent 5e32af0148
commit 4f999094f3

View File

@@ -69,6 +69,74 @@ def log_migrate_stage(stage)
end
end
# Function to parse and migrate nginx.conf from /usr/local/hestia/nginx-system/etc/nginx/nginx.conf
def migrate_nginx_config_from_file(source_path)
return false unless File.exist?(source_path)
hestia_print_info_message_to_cli "Processing nginx config from: #{source_path}"
content = File.read(source_path)
original_content = content.dup
modified = false
# Replace all paths starting with /var/ to /usr/local/hestia/nginx-system/var/
# This pattern matches any absolute path that starts with /var/ anywhere in the line
content = content.gsub(/\/var\//, '/usr/local/hestia/nginx-system/var/')
# Replace pid path from /run/nginx.pid to /run/nginx-system.pid
content = content.gsub(/pid\s+\S+/) { |match| match.gsub('/run/nginx.pid', '/run/nginx-system.pid') }
if content != original_content
File.write(source_path, content)
hestia_print_info_message_to_cli "Updated config: #{source_path}"
modified = true
end
modified
end
def parse_listen(line)
# Попытка найти IP:port
m = line.match(/^\s*listen\s+([^\s:]+):(\d+)/i)
return [m[1], m[2]] if m
# Если только порт после listen
m = line.match(/^\s*listen\s+(\d+);?\s*$/i)
return [nil, m[1]] if m
nil
end
# Helper function to parse and replace ports in listen directives using temp placeholders
def parse_and_replace_listen_directive(line, proxy_port, proxy_ssl_port)
return line unless line.match?(/\blisten\b/i)
new_line = line.dup
# Define target ports (always migrate to these values regardless of input)
target_http_port = '8078'
target_ssl_port = '8079'
result = parse_listen(line)
return line unless result
ip, port = result
if port == proxy_port
if ip
new_line.gsub!("#{ip}:#{port}", "#{ip}:#{target_http_port}")
else
new_line.gsub!(port, target_http_port)
end
elsif port == proxy_ssl_port
if ip
new_line.gsub!("#{ip}:#{port}", "#{ip}:#{target_ssl_port}")
else
new_line.gsub!(port, target_ssl_port)
end
end
new_line
end
hestia_check_privileged_user
load_global_bash_variables "/etc/hestiacp/hestia.conf"
@@ -175,12 +243,25 @@ when :migratenginx
end
end
end
log_migrate_stage('stage4.1') do
# Parse nginx.conf file to replace paths
if migrate_nginx_config_from_file("/usr/local/hestia/nginx-system/etc/nginx/nginx.conf")
hestia_print_info_message_to_cli "Completed migration of nginx.conf paths"
else
hestia_print_error_message_to_cli "Warning: Could not migrate nginx.conf from #{File.expand_path('/usr/local/hestia/nginx-system/etc/nginx/nginx.conf')}"
end
end
log_migrate_stage('stage5') do
nginx_conf_dir = '/usr/local/hestia/nginx-system/etc/nginx'
# Read proxy ports from configuration
proxy_port = $PROXY_PORT.nil? || $PROXY_PORT.empty? ? '80' : $PROXY_PORT
proxy_ssl_port = $PROXY_SSL_PORT.nil? || $PROXY_SSL_PORT.empty? ? '443' : $PROXY_SSL_PORT
#proxy_port = $PROXY_PORT.nil? || $PROXY_PORT.empty? ? '80' : $PROXY_PORT
#proxy_ssl_port = $PROXY_SSL_PORT.nil? || $PROXY_SSL_PORT.empty? ? '443' : $PROXY_SSL_PORT
proxy_port = '80'
proxy_ssl_port = '443'
hestia_print_info_message_to_cli "Migrating ports: #{proxy_port} -> 8078, #{proxy_ssl_port} -> 8079"
# Find and replace port in all .conf files
Dir.glob(File.join(nginx_conf_dir, '**', '*.conf')).each do |conf_file|
@@ -188,52 +269,30 @@ when :migratenginx
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|
modified = true
ip_part = $&.split(':')[0] # Get IP address part
remaining = $POSTMATCH.strip # Get any additional flags (default_server, etc.)
# Process line by line - only replace ports in listen directives
new_lines = []
if remaining.empty?
"#{ip_part}:#{proxy_port}"
content.each_line do |line|
# Check if line is a listen directive (starts with optional whitespace then 'listen')
if /^\s*listen\s+/i.match?(line) || /^listen\s+/i.match?(line)
# This is a listen line - process it
new_line = parse_and_replace_listen_directive(line, proxy_port, proxy_ssl_port)
modified = true unless new_line == line
new_lines << new_line
else
"#{ip_part}:#{proxy_port} #{remaining}"
# Not a listen line, keep as is
new_lines << line
end
end
# 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|
modified = true
flags = $1 || ''
";#{proxy_port}#{flags}"
end
end
# 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|
modified = true
ip_part = $&.split(':')[0] # Get IP address part
"#{ip_part}:#{proxy_ssl_port} ssl;"
end
end
# 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)
modified = true if new_content != content
end
# Write changes back if modified
if modified
File.open(conf_file, 'w') { |f| f.write(new_content) }
File.open(conf_file, 'w') { |f| f.write(new_lines.join) }
hestia_print_info_message_to_cli " Updated: #{conf_file}"
end
end
end
log_migrate_stage('stage6') do
hestia_change_sys_config_value("PROXY_PORT", "8078")
hestia_change_sys_config_value("PROXY_SSL_PORT", "8079")