diff --git a/app/models/concerns/foreman_wds/host_extensions.rb b/app/models/concerns/foreman_wds/host_extensions.rb index 69e186c0cdb32278ca487df49c45850ec2fa8768..84ba02578e1cc379bf9e48ce99b427840fffffef 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 33df3c556a510d7a73239a6712242a2ab3aafaea..36eddf9eadd0201d3e232ab3ac91a8e0352a7880 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