This commit is contained in:
Alexey Berezhok
2026-05-23 22:45:01 +03:00
parent 4f999094f3
commit 317e3e215e
2 changed files with 351 additions and 33 deletions

View File

@@ -328,6 +328,7 @@ when :migratenginx
# Stop nginx service
system('systemctl stop nginx')
# Start nginx-system service
system('systemctl enable nginx-system')
system('systemctl start nginx-system')
end
when :migratetobunkerweb
@@ -354,6 +355,8 @@ when :migratetobunkerweb
hestia_print_error_message_to_cli "Failed to enable bunkerweb_module"
exit 1
end
hestia_print_info_message_to_cli "Ожидаем минуту для перезапуска сервиса bunkerweb..."
sleep 60
end
LIST_DOMAINS=[]
@@ -394,6 +397,10 @@ when :migratetobunkerweb
ssl_status = domain_info['SSL'] || 'no'
is_ssl = ssl_status == 'yes'
# Get IP from domain info - should be available in the parsed JSON
proxy_host = domain_info['IP'] || (domain_info['IP6'].present? ? domain_info['IP6'].strip : "127.0.0.1")
proxy_host = proxy_host.nil? || proxy_host.empty? ? "127.0.0.1" : proxy_host
# Create alias list from domain info (ALIAS field contains comma-separated aliases)
raw_aliases = domain_info['ALIAS'] || ''
if raw_aliases && !raw_aliases.empty?
@@ -403,12 +410,23 @@ when :migratetobunkerweb
aliases_list = domain_name # No aliases, use domain name only
end
hestia_print_info_message_to_cli "Processing domain: #{domain_name} (SSL: #{is_ssl}, Aliases: #{aliases_list})"
hestia_print_info_message_to_cli "Processing domain: #{domain_name} (SSL: #{is_ssl}, IP: #{proxy_host})"
begin
# Add domain to BunkerWeb via module script (without SSL)
output = IO.popen("/usr/local/hestia/bin/v-bunkerweb-module add #{domain_name} #{$PROXY_HOST}")
hestia_print_info_message_to_cli " Added #{domain_name}: #{output.strip}"
cmd = "/usr/local/hestia/bin/v-bunkerweb-module add #{domain_name} #{proxy_host}"
puts_cmd = " Added #{domain_name}: Executing command: #{cmd}"
hestia_print_info_message_to_cli puts_cmd
result = system(cmd)
if result
# Команда успешно выполнена
hestia_print_info_message_to_cli " Status: Success"
else
hestia_print_error_message_to_cli " Failed to add domain #{domain_name}"
next
end
rescue => e
hestia_print_error_message_to_cli "Failed to add domain #{domain_name} to BunkerWeb: #{e.message}"
next
@@ -416,8 +434,18 @@ when :migratetobunkerweb
# Add aliases to the domain via module script
begin
output = IO.popen("/usr/local/hestia/bin/v-bunkerweb-module alias #{domain_name} \"#{aliases_list}\"")
hestia_print_info_message_to_cli " Added aliases for #{domain_name}: #{output.strip}"
cmd = "/usr/local/hestia/bin/v-bunkerweb-module alias #{domain_name} \"#{aliases_list}\""
puts_cmd = " Added aliases for #{domain_name}: Executing command: #{cmd}"
hestia_print_info_message_to_cli puts_cmd
result = system(cmd)
if result
hestia_print_info_message_to_cli " Status: Success"
else
hestia_print_error_message_to_cli " Failed to set aliases for #{domain_name}"
next
end
rescue => e
hestia_print_error_message_to_cli "Failed to set aliases for #{domain_name}: #{e.message}"
next
@@ -425,7 +453,7 @@ when :migratetobunkerweb
# Handle SSL configuration if enabled
if is_ssl
original_ssl_dir = "/home/#{username}/conf/#{domain_name}/web/ssl"
original_ssl_dir = "/home/#{username}/conf/web/#{domain_name}/ssl"
# Check if SSL directory and files exist
unless Dir.exist?(original_ssl_dir) || File.exist?("#{original_ssl_dir}/#{domain_name}.pem")
@@ -445,9 +473,10 @@ when :migratetobunkerweb
begin
# Create bunkerweb SSL directory if it doesn't exist
FileUtils.mkdir_p(bunkerweb_ssl_dir)
Dir.chmod(bunkerweb_ssl_dir, 0755)
FileUtils.chmod(0755, bunkerweb_ssl_dir)
# Define paths in bunkerweb directory
crt_path_bunkerweb = "#{bunkerweb_ssl_dir}/#{domain_name}.crt"
cert_path_bunkerweb = "#{bunkerweb_ssl_dir}/#{domain_name}.pem"
key_path_bunkerweb = "#{bunkerweb_ssl_dir}/#{domain_name}.key"
@@ -459,43 +488,53 @@ when :migratetobunkerweb
# Copy .crt file if exists
if File.exist?(original_crt)
FileUtils.cp("-f", original_crt, cert_path_bunkerweb)
elsif File.exist?(original_pem)
FileUtils.cp(original_crt, crt_path_bunkerweb)
end
if File.exist?(original_pem)
# Only use .pem if .crt doesn't exist (fallback like the backup script)
FileUtils.cp("-f", original_pem, cert_path_bunkerweb)
FileUtils.cp(original_pem, cert_path_bunkerweb)
end
# Copy .key file if exists
if File.exist?(original_key)
FileUtils.cp("-f", original_key, key_path_bunkerweb)
end
# Copy .pem file if exists (in addition to .crt for backup purposes)
if File.exist?(original_pem)
FileUtils.cp("-f", original_pem, cert_path_bunkerweb)
FileUtils.cp(original_key, key_path_bunkerweb)
end
# Copy .ca file if exists
if File.exist?(original_ca)
FileUtils.cp("-f", original_ca, "#{bunkerweb_ssl_dir}/#{domain_name}.ca")
FileUtils.cp(original_ca, "#{bunkerweb_ssl_dir}/#{domain_name}.ca")
end
# Set ownership and permissions (nginx user can read, others cannot)
Dir.chown(bunkerweb_ssl_dir, 'root', 'nginx')
Dir.chmod(bunkerweb_ssl_dir, 0755)
FileUtils.chown('root', 'nginx', bunkerweb_ssl_dir)
FileUtils.chmod(0755, bunkerweb_ssl_dir)
File.chown(cert_path_bunkerweb, 'root', 'nginx') if File.exist?(cert_path_bunkerweb)
File.chmod(cert_path_bunkerweb, 0640) if File.exist?(cert_path_bunkerweb)
FileUtils.chown('root', 'nginx', cert_path_bunkerweb) if File.exist?(cert_path_bunkerweb)
FileUtils.chmod(0640, cert_path_bunkerweb) if File.exist?(cert_path_bunkerweb)
File.chown(key_path_bunkerweb, 'root', 'nginx') if File.exist?(key_path_bunkerweb)
File.chmod(key_path_bunkerweb, 0640) if File.exist?(key_path_bunkerweb)
FileUtils.chown('root', 'nginx', key_path_bunkerweb) if File.exist?(key_path_bunkerweb)
FileUtils.chmod(0640, key_path_bunkerweb) if File.exist?(key_path_bunkerweb)
# Add SSL configuration to BunkerWeb via module script
output = IO.popen("/usr/local/hestia/bin/v-bunkerweb-module addssl #{domain_name} #{cert_path_bunkerweb} #{key_path_bunkerweb}")
hestia_print_error_message_to_cli " Added SSL for #{domain_name}: #{output.strip}"
FileUtils.chown('root', 'nginx', crt_path_bunkerweb) if File.exist?(crt_path_bunkerweb)
FileUtils.chmod(0640, crt_path_bunkerweb) if File.exist?(crt_path_bunkerweb)
# Add SSL configuration to BunkerWeb via module script (use proxy_host from earlier)
cmd = "/usr/local/hestia/bin/v-bunkerweb-module addssl #{domain_name} #{crt_path_bunkerweb} #{key_path_bunkerweb}"
puts_cmd = " Added SSL for #{domain_name}: Executing command: #{cmd}"
hestia_print_info_message_to_cli puts_cmd
result = system(cmd)
if result
hestia_print_info_message_to_cli " Status: Success"
else
hestia_print_error_message_to_cli " Failed to configure SSL for #{domain_name}"
end
# Update path_to_ssl to point to bunkerweb directory
ssl_cert_path = cert_path_bunkerweb
ssl_cert_path = crt_path_bunkerweb
rescue => e
hestia_print_error_message_to_cli "Failed to configure SSL for #{domain_name}: #{e.message}"
end
@@ -505,7 +544,8 @@ when :migratetobunkerweb
LIST_DOMAINS << {
user: username,
domain: domain_name,
is_ssl: is_ssl,
proxy_host: proxy_host.to_s,
is_ssl: is_ssl == true ? "yes" : "no", # Convert boolean/string to proper string format
path_to_ssl: ssl_cert_path ? ssl_cert_path : nil
}
@@ -519,10 +559,6 @@ when :migratetobunkerweb
LIST_DOMAINS.each_with_index do |entry, idx|
hestia_print_info_message_to_cli "#{idx + 1}. user: #{entry[:user]}, domain: #{entry[:domain]}, is_ssl: #{entry[:is_ssl].to_s}, path_to_ssl: #{entry[:path_to_ssl]}"
end
# Output as JSON for potential use in downstream scripts
hestia_print_info_message_to_cli "\n=== LIST_DOMAINS (JSON) ==="
hestia_print_info_message_to_cli JSON.generate(LIST_DOMAINS, { indent: 2 })
else
hestia_print_error_message_to_cli "No domains were migrated to BunkerWeb"
end
@@ -530,7 +566,7 @@ when :migratetobunkerweb
hestia_print_info_message_to_cli "Stage 11 completed."
end
else
hestia_print_error_message_to_cli "unknown command"
hestia_print_error_message_to_cli "unknown command (use migratetobunkerweb or migratenginx)"
log_event E_ARGS, $ARGUMENTS
exit 1
end