This commit is contained in:
Alexey Berezhok
2026-05-03 19:47:42 +03:00
parent 8b911372af
commit 3c5a5924d6

View File

@@ -269,8 +269,18 @@ class HestiaBunkerWebApi
raise BunkerWebApiError.new("Service '#{service_name}' not found") raise BunkerWebApiError.new("Service '#{service_name}' not found")
end end
# Extract current variables from the service configuration # Extract variables from config - API stores them in 'config' not 'variables'
current_vars = get_service_response[:body]["variables"] || {} # Each variable is a hash with 'value' as the actual setting value
current_vars = {}
if get_service_response[:body]["config"]
get_service_response[:body]["config"].each do |key, value_hash|
if value_hash.is_a?(Hash) && value_hash.key?("value")
current_vars[key] = value_hash["value"]
else
current_vars[key] = value_hash
end
end
end
# Update SSL settings # Update SSL settings
updated_vars = { updated_vars = {
@@ -312,8 +322,23 @@ class HestiaBunkerWebApi
raise BunkerWebApiError.new("Service '#{service_name}' not found") raise BunkerWebApiError.new("Service '#{service_name}' not found")
end end
# Extract current variables from the service configuration # Save the entire current configuration (except server_name which will be replaced)
current_vars = get_service_response[:body]["variables"] || {} current_config = get_service_response[:body]
# Extract variables from config - API stores them in 'config' not 'variables'
# Each variable is a hash with 'value' as the actual setting value
current_vars = {}
if current_config["config"]
current_config["config"].each do |key, value_hash|
if key != "SERVER_NAME"
if value_hash.is_a?(Hash) && value_hash.key?("value")
current_vars[key] = value_hash["value"]
else
current_vars[key] = value_hash
end
end
end
end
# Clean the list_aliases string according to the rules # Clean the list_aliases string according to the rules
cleaned_aliases = list_aliases.to_s cleaned_aliases = list_aliases.to_s
@@ -329,40 +354,39 @@ class HestiaBunkerWebApi
# Ensure the main service name appears first in the alias list # Ensure the main service name appears first in the alias list
aliases_array = cleaned_aliases.split(' ') aliases_array = cleaned_aliases.split(' ')
unless aliases_array.include?(service_name) if aliases_array.include?(service_name)
# If service_name not present, add it at the front
aliases_array.unshift(service_name)
else
# If present but not first, reorder to make it first
aliases_array.delete(service_name) aliases_array.delete(service_name)
aliases_array.unshift(service_name)
end end
# Rebuild cleaned string # Rebuild cleaned string
cleaned_aliases = aliases_array.join(' ') cleaned_aliases = aliases_array.join(' ')
current_vars["SERVER_NAME"]=cleaned_aliases
# Update alias settings # Step 1: Delete the old service
updated_vars = { delete_response = api_call("DELETE", "/services/#{service_name}")
"SERVER_NAME" => cleaned_aliases if delete_response[:status] != 200 && delete_response[:status] != 204
} raise BunkerWebApiError.new("Failed to delete service '#{service_name}': status=#{delete_response[:status]}")
end
# Merge with existing variables (keep non-SSL settings) puts "[INFO] Service '#{service_name}' deleted"
final_vars = current_vars.merge(updated_vars)
# Step 2: Create a new service with the cleaned alias list as server_name
# and preserve all existing configuration variables
service_body = { service_body = {
server_name: nil, # Not changing name server_name: service_name, # Use the full alias list including service_name first
is_draft: false, # Keep as online is_draft: current_config["is_draft"] || false,
variables: final_vars variables: current_vars # Preserve all existing variables from the original service
} }
response = api_call("PATCH", "/services/#{service_name}", {}, JSON.generate(service_body)) post_response = api_call("POST", "/services", {}, JSON.generate(service_body))
if response[:status] == 200 if post_response[:status] == 200 || post_response[:status] == 201
puts "[INFO] SSL configuration updated for service '#{service_name}'" puts "[INFO] Service recreated successfully with aliases: #{cleaned_aliases}"
elsif post_response[:status] == 409
raise BunkerWebApiError.new("Service '#{service_name}' already exists")
else else
raise BunkerWebApiError.new("Failed to update SSL configuration: status=#{response[:status]}, body=#{response[:raw_body]}") raise BunkerWebApiError.new("Failed to create service: status=#{post_response[:status]}, body=#{post_response[:raw_body]}")
end end
return response[:body] || {} return post_response || {}
end end
@@ -377,8 +401,18 @@ class HestiaBunkerWebApi
raise BunkerWebApiError.new("Service '#{service_name}' not found") raise BunkerWebApiError.new("Service '#{service_name}' not found")
end end
# Extract current variables from the service configuration # Extract variables from config - API stores them in 'config' not 'variables'
current_vars = get_service_response[:body]["variables"] || {} # Each variable is a hash with 'value' as the actual setting value
current_vars = {}
if get_service_response[:body]["config"]
get_service_response[:body]["config"].each do |key, value_hash|
if value_hash.is_a?(Hash) && value_hash.key?("value")
current_vars[key] = value_hash["value"]
else
current_vars[key] = value_hash
end
end
end
# Update SSL settings # Update SSL settings
updated_vars = { updated_vars = {