This commit is contained in:
Alexey Berezhok
2026-05-15 00:34:14 +03:00
parent e0419fea4b
commit af9e6bfb82

View File

@@ -28,6 +28,9 @@ require "HestiaBunkerWebApi"
require 'json' unless defined?(JSON)
require 'fileutils'
require 'time'
require 'pathname'
def copy_nginx_files(src_root, dest_root)
FileUtils.mkdir_p(dest_root)
@@ -90,6 +93,55 @@ check_hestia_demo_mode
case v_command.to_sym
when :migratenginx
log_migrate_stage('stage0') do
# Create backup of /etc/nginx with timestamp
timestamp = Time.now.strftime('%Y%m%d_%H%M%S')
backup_root = "/etc/nginx_backup_#{timestamp}"
FileUtils.mkdir_p(backup_root)
src_root = '/etc/nginx'
dest_root = backup_root
# Custom copy function to handle symlinks in conf.d/domains
def copy_with_symlinks(src, dest)
Dir.foreach(src) do |entry|
next if entry == '.' || entry == '..'
src_path = File.join(src, entry)
dest_path = File.join(dest, entry)
if File.symlink?(src_path)
# Check if symlink is inside conf.d/domains
if src_path.include?(File.join('conf.d', 'domains'))
# Resolve the target of the symlink
target_path = File.readlink(src_path)
# Resolve relative symlink paths
unless Pathname.new(target_path).absolute?
target_path = File.expand_path(target_path, File.dirname(src_path))
end
if File.exist?(target_path) && File.file?(target_path)
content = File.read(target_path)
new_file_name = "#{entry}_content.conf"
new_file_path = File.join(dest, new_file_name)
File.write(new_file_path, content)
end
else
# Preserve the symlink as is
FileUtils.mkdir_p(File.dirname(dest_path))
target = File.readlink(src_path)
FileUtils.ln_s(target, dest_path)
end
elsif File.directory?(src_path)
FileUtils.mkdir_p(dest_path)
copy_with_symlinks(src_path, dest_path)
else
FileUtils.cp(src_path, dest_path)
end
end
end
copy_with_symlinks(src_root, dest_root)
end
log_migrate_stage('stage1') do
if $BUNKERWEB.nil?
hestia_change_sys_config_value("BUNKERWEB", "yes")
@@ -103,19 +155,146 @@ when :migratenginx
end
end
log_migrate_stage('stage3') do
hestia_change_sys_config_value("PROXY_PORT", "8078")
hestia_change_sys_config_value("PROXY_SSL_PORT", "8079")
end
log_migrate_stage('stage4') do
FileUtils.rm_f Dir.glob('/usr/local/hestia/nginx-system/etc/nginx/conf.d/*.conf')
src_root = '/etc/nginx'
dest_root = '/usr/local/hestia/nginx-system/etc/nginx'
copy_nginx_files(src_root, dest_root)
end
#stage5 замена /etc/nginx на /usr/local/hestia/nginx-system/etc/nginx
#stage6 замена портов на 8078
#stage7 проверить, нужно ли что-то менять в шаблонах
log_migrate_stage('stage4') do
# 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|
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
File.open(path, 'w') { |f| f.write(new_content) }
end
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
# Find and replace port in all .conf files
Dir.glob(File.join(nginx_conf_dir, '**', '*.conf')).each do |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|
modified = true
ip_part = $&.split(':')[0] # Get IP address part
remaining = $POSTMATCH.strip # Get any additional flags (default_server, etc.)
if remaining.empty?
"#{ip_part}:#{proxy_port}"
else
"#{ip_part}:#{proxy_port} #{remaining}"
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) }
puts " 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) }
puts " Updated: #{default_conf}"
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")
end
log_migrate_stage('stage7') do
if system('/usr/local/hestia/bin/v-ext-modules enable update_module')
output = IO.popen("/usr/local/hestia/bin/v-ext-modules state update_module json").read
begin
parsed = JSON.parse(output)
if parsed.is_a?(Array) && parsed.first && parsed.first['STATE'] == 'enabled'
system('/usr/local/hestia/bin/v-ext-modules-run update_module synctemplates')
else
hestia_print_error_message_to_cli "update_module not enabled after enable command"
exit 1
end
rescue JSON::ParserError => e
hestia_print_error_message_to_cli "Failed to parse JSON from state command: #{e.message}"
exit 1
end
else
hestia_print_error_message_to_cli "Failed to enable update_module"
exit 1
end
end
log_migrate_stage('stage8') do
hestia_change_sys_config_value("PROXY_PORT", "8078")
hestia_change_sys_config_value("PROXY_SSL_PORT", "8079")
end
#stage8 активация из запуск nginx-system
log_migrate_stage('stage9') do
# Delete all contents inside /etc/nginx
FileUtils.rm_rf Dir.glob('/etc/nginx/*')
# Stop nginx service
system('systemctl stop nginx')
# Start nginx-system service
system('systemctl start nginx-system')
end
when :migratetobunkerweb
#TODO настройка bunkerweb
else