Fixes
This commit is contained in:
@@ -62,7 +62,7 @@ def log_migrate_stage(stage)
|
|||||||
yield
|
yield
|
||||||
# Record the successful stage
|
# Record the successful stage
|
||||||
File.open(log_file, 'a') { |f| f.puts 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
|
rescue => e
|
||||||
hestia_print_error_message_to_cli "Stage #{stage} failed: #{e.message}"
|
hestia_print_error_message_to_cli "Stage #{stage} failed: #{e.message}"
|
||||||
exit 1
|
exit 1
|
||||||
@@ -165,10 +165,12 @@ when :migratenginx
|
|||||||
# Find all files under the nginx-system directory
|
# Find all files under the nginx-system directory
|
||||||
nginx_conf_dir = '/usr/local/hestia/nginx-system/etc/nginx'
|
nginx_conf_dir = '/usr/local/hestia/nginx-system/etc/nginx'
|
||||||
Dir.glob(File.join(nginx_conf_dir, '**', '*')).each do |path|
|
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)
|
next if File.directory?(path)
|
||||||
content = File.read(path)
|
content = File.read(path)
|
||||||
new_content = content.gsub(/(?<!\/usr\/local\/hestia\/nginx-system)\/etc\/nginx/, '/usr/local/hestia/nginx-system/etc/nginx')
|
new_content = content.gsub(/(?<!\/usr\/local\/hestia\/nginx-system)\/etc\/nginx/, '/usr/local/hestia/nginx-system/etc/nginx')
|
||||||
if new_content != content
|
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) }
|
File.open(path, 'w') { |f| f.write(new_content) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -182,12 +184,13 @@ when :migratenginx
|
|||||||
|
|
||||||
# Find and replace port in all .conf files
|
# Find and replace port in all .conf files
|
||||||
Dir.glob(File.join(nginx_conf_dir, '**', '*.conf')).each do |conf_file|
|
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)
|
content = File.read(conf_file)
|
||||||
modified = false
|
modified = false
|
||||||
|
|
||||||
# Replace HTTP port pattern: listen <IP>:#{$PROXY_PORT} [flags];
|
# 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;
|
# 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
|
modified = true
|
||||||
ip_part = $&.split(':')[0] # Get IP address part
|
ip_part = $&.split(':')[0] # Get IP address part
|
||||||
remaining = $POSTMATCH.strip # Get any additional flags (default_server, etc.)
|
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 first substitution didn't match, try simpler pattern for ports without IP
|
||||||
if new_content == content
|
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
|
modified = true
|
||||||
flags = $1 || ''
|
flags = $1 || ''
|
||||||
";#{proxy_port}#{flags}"
|
";#{proxy_port}#{flags}"
|
||||||
@@ -210,7 +213,7 @@ when :migratenginx
|
|||||||
|
|
||||||
# Replace SSL port pattern: listen <IP>:#{$PROXY_SSL_PORT} ssl;
|
# Replace SSL port pattern: listen <IP>:#{$PROXY_SSL_PORT} ssl;
|
||||||
if new_content == content
|
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
|
modified = true
|
||||||
ip_part = $&.split(':')[0] # Get IP address part
|
ip_part = $&.split(':')[0] # Get IP address part
|
||||||
"#{ip_part}:#{proxy_ssl_port} ssl;"
|
"#{ip_part}:#{proxy_ssl_port} ssl;"
|
||||||
@@ -219,8 +222,8 @@ when :migratenginx
|
|||||||
|
|
||||||
# Simple fallback: replace the port variable values directly
|
# Simple fallback: replace the port variable values directly
|
||||||
if new_content == content
|
if new_content == content
|
||||||
new_content = content.gsub("#\{$PROXY_PORT\}", proxy_port)
|
new_content = content.gsub("#{$PROXY_PORT}", proxy_port)
|
||||||
new_content = new_content.gsub("#\{$PROXY_SSL_PORT\}", proxy_ssl_port)
|
new_content = new_content.gsub("#{$PROXY_SSL_PORT}", proxy_ssl_port)
|
||||||
modified = true if new_content != content
|
modified = true if new_content != content
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -230,34 +233,6 @@ when :migratenginx
|
|||||||
hestia_print_info_message_to_cli " Updated: #{conf_file}"
|
hestia_print_info_message_to_cli " Updated: #{conf_file}"
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
log_migrate_stage('stage6') do
|
log_migrate_stage('stage6') do
|
||||||
hestia_change_sys_config_value("PROXY_PORT", "8078")
|
hestia_change_sys_config_value("PROXY_PORT", "8078")
|
||||||
|
|||||||
@@ -327,11 +327,20 @@ def hestia_change_sys_config_value(key, value)
|
|||||||
config_file = "/usr/local/hestia/conf/hestia.conf"
|
config_file = "/usr/local/hestia/conf/hestia.conf"
|
||||||
|
|
||||||
if File.exist?(config_file)
|
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
|
# Check if key exists in the configuration file
|
||||||
existing_line_index = -1
|
existing_line_index = -1
|
||||||
|
|
||||||
f.each_with_index do |line, idx|
|
lines.each_with_index do |line, idx|
|
||||||
line_stripped = line.strip
|
line_stripped = line.strip
|
||||||
# Skip comment lines
|
# Skip comment lines
|
||||||
next if line_stripped.start_with?("#")
|
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
|
# Use temporary file for safety and atomic replacement
|
||||||
temp_file = "#{config_file}.tmp"
|
temp_file = "#{config_file}.tmp"
|
||||||
|
|
||||||
File.open(config_file, "r") do |input_f|
|
# Second pass: rebuild the content with updated value
|
||||||
lines = []
|
new_lines = []
|
||||||
|
|
||||||
input_f.each do |line|
|
lines.each do |line|
|
||||||
line_stripped = line.strip
|
line_stripped = line.strip
|
||||||
# Skip comment lines
|
# Skip comment lines
|
||||||
next if line_stripped.start_with?("#")
|
next if line_stripped.start_with?("#")
|
||||||
next if line_stripped.empty?
|
next if line_stripped.empty?
|
||||||
|
|
||||||
# Match and replace the key-value pair
|
# Match and replace the key-value pair
|
||||||
if line.match(/^\s*#{Regexp.escape(key)}='[^']*'/)
|
if line.match(/^\s*#{Regexp.escape(key)}='[^']*'/)
|
||||||
lines << "#{key}='#{value}'"
|
new_lines << "#{key}='#{value}'"
|
||||||
else
|
else
|
||||||
lines << line
|
new_lines << line
|
||||||
end
|
|
||||||
end
|
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
|
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
|
end
|
||||||
|
else
|
||||||
|
OK
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
check_result error_code: E_NOTEXIST, error_message: "Configuration file #{config_file} does not exist"
|
check_result error_code: E_NOTEXIST, error_message: "Configuration file #{config_file} does not exist"
|
||||||
|
|||||||
Reference in New Issue
Block a user