From 8ac0b81be91e6e4d678268aa1de5cc13246cfc52 Mon Sep 17 00:00:00 2001
From: Alexander Olofsson <alexander.olofsson@liu.se>
Date: Wed, 28 Jul 2021 13:07:12 +0200
Subject: [PATCH] Work towards WDS clients

---
 .../concerns/foreman_wds/host_extensions.rb   |  5 +--
 app/models/wds_server.rb                      | 37 +++++++++++++++----
 2 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/app/models/concerns/foreman_wds/host_extensions.rb b/app/models/concerns/foreman_wds/host_extensions.rb
index 69e186c..84ba025 100644
--- a/app/models/concerns/foreman_wds/host_extensions.rb
+++ b/app/models/concerns/foreman_wds/host_extensions.rb
@@ -103,7 +103,7 @@ module ForemanWds
       raise NotImplementedError, 'Not implemented yet'
       return unless wds?
 
-      client = wds_server.client(self) || wds_server.create_client(self)
+      client = wds_server.ensure_client(self)
 
       Rails.logger.info client
       true
@@ -116,9 +116,6 @@ module ForemanWds
       raise NotImplementedError, 'Not implemented yet'
       return unless wds?
 
-      client = wds_server.client(self)
-      return unless client
-
       wds_server.delete_client(self)
       true
     rescue ScriptError, StandardError => ex
diff --git a/app/models/wds_server.rb b/app/models/wds_server.rb
index 33df3c5..36eddf9 100644
--- a/app/models/wds_server.rb
+++ b/app/models/wds_server.rb
@@ -183,21 +183,36 @@ class WdsServer < ApplicationRecord
   end
 
   def ensure_unattend(host)
+    iface = host&.provision_interface
+    raise 'No provisioning interface available' unless iface
     raise NotImplementedException, 'TODO: Not implemented yet'
+
+    # TODO: render template, send as heredoc
+    template = host.operatingsystem.provisioning_templates.find { |t| t.template_kind.name == 'wds_unattend' }
+    unless template
+      logger.warn 'No WDS Unattend template specified, falling back to provision template'
+      template ||= host.operatingsystem.provisioning_templates.find { |t| t.template_kind.name == 'provision' }
+    end
+    raise 'No provisioning template available' unless template
+
+    template_data = host.render_template template: template
+
     connection.shell(:powershell) do |sh|
+      file_path = unattend_file(host)
+
+      sh.run("$unattend_render = @'\n#{template_data}\n'@")
+      sh.run("New-Item -Path '#{file_path}' -ItemType 'file' -Value $unattend_render")
+
+      source_image = host.wds_facet.install_image
       target_image = target_image_for(host)
-      # TODO: render template, send as heredoc
-      # sh.run("$unattend_render = @'\n#{unattend_template}\n'@")
-      # sh.run("New-Item -Path '#{unattend_file(host)}' -ItemType 'file' -Value $unattend_render")
       if SETTINGS[:wds_unattend_group]
+        raise NotImplementedException, 'TODO: Not implemented yet'
         # New-WdsInstallImageGroup -Name #{SETTINGS[:wds_unattend_group]}
         # Export-WdsInstallImage -ImageGroup <Group> ...
-        # Import-WdsInstallImage -ImageGroup #{SETTINGS[:wds_unattend_group]} -UnattendFile '#{unattend_file(host)}' -OverwriteUnattend ...
+        # Import-WdsInstallImage -ImageGroup #{SETTINGS[:wds_unattend_group]} -UnattendFile '#{file_path}' -OverwriteUnattend ...
       else
-        source_image = host.wds_facet.install_image
-
         sh.run("Copy-WdsInstallImage -ImageGroup '#{source_image.image_group}' -FileName '#{source_image.file_name}' -ImageName '#{source_image.image_name}' -NewFileName '#{target_image.file_name}' -NewImageName '#{target_image.image_name}'")
-        sh.run("Set-WdsInstallImage -ImageGroup '#{target_image.image_group}' -FileName '#{target_image.file_name}' -ImageName '#{target_image.image_name}' -DisplayOrder 99999 -UnattendFile '#{unattend_file(host)}' -OverwriteUnattend")
+        sh.run("Set-WdsInstallImage -ImageGroup '#{target_image.image_group}' -FileName '#{target_image.file_name}' -ImageName '#{target_image.image_name}' -DisplayOrder 99999 -UnattendFile '#{file_path}' -OverwriteUnattend")
       end
     end
   end
@@ -211,6 +226,14 @@ class WdsServer < ApplicationRecord
     end.errcode.zero?
   end
 
+  def ensure_client(host)
+    raise NotImplementedException, 'TODO: Not implemented yet'
+  end
+
+  def delete_client(host)
+    raise NotImplementedException, 'TODO: Not implemented yet'
+  end
+
   def images(type, name = nil)
     raise ArgumentError, 'Type must be :boot or :install' unless %i[boot install].include? type
 
-- 
GitLab