From b264ed1c7e189e74e5bd0d1ed53360a3f61b1666 Mon Sep 17 00:00:00 2001
From: Alexander Olofsson <alexander.olofsson@liu.se>
Date: Mon, 25 Oct 2021 09:17:34 +0200
Subject: [PATCH] Sort Windows installimages for clients

See theforeman/foreman#8866
---
 app/views/wds_servers/_image_select.html.erb | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/app/views/wds_servers/_image_select.html.erb b/app/views/wds_servers/_image_select.html.erb
index c4e23ba..e06089b 100644
--- a/app/views/wds_servers/_image_select.html.erb
+++ b/app/views/wds_servers/_image_select.html.erb
@@ -6,7 +6,15 @@
     images = images.select { |img| img.matches_architecture?(host.architecture) && img.enabled } if host.architecture
     boot_images = images.select { |img| img.is_a?(ForemanWds::WdsBootImage) }
     install_images = images.select { |img| img.is_a?(ForemanWds::WdsInstallImage) }
-    install_images = install_images.select { |img| img.version == "#{host.operatingsystem.major}.#{host.operatingsystem.minor}" } if host.operatingsystem
+
+    if host.operatingsystem
+      host_is_client = host.operatingsystem.name.end_with?('_client')
+      client_sort_order = host_is_client ? 0 : 1
+      server_sort_order = host_is_client ? 1 : 0
+
+      install_images = install_images.select { |img| img.version == "#{host.operatingsystem.major}.#{host.operatingsystem.minor}" }
+                                     .sort_by { |img| "#{img.name =~ /server/i ? server_sort_order : client_sort_order}#{img.name}" }
+    end
   -%>
   <%= select_f f, :install_image_name, install_images, :name, :name,
     { selected: item.install_image_name },
-- 
GitLab