Skip to content
Snippets Groups Projects
Commit a55a42a1 authored by Alexander Olofsson's avatar Alexander Olofsson
Browse files

Clean up install-image/client orchestration

parent 7a5191bf
No related branches found
No related tags found
No related merge requests found
......@@ -79,7 +79,6 @@ module ForemanWds
raise NotImplementedError, 'Not implemented yet'
return unless wds?
wds_server.ensure_unattend(self)
client = wds_server.client(self) || wds_server.create_client(self)
Rails.logger.info client
......@@ -96,8 +95,7 @@ module ForemanWds
client = wds_server.client(self)
return unless client
wds_server.delete_client(client)
wds_server.delete_unattend(self)
wds_server.delete_client(self)
true
rescue ScriptError, StandardError => ex
Rails.logger.error "Failed to remove WDS client, #{ex}"
......
......@@ -76,6 +76,20 @@ class WdsServer < ApplicationRecord
def create_client(host)
raise NotImplementedError, 'Not finished yet'
ensure_unattend(host)
connection.shell(:powershell) do |sh|
# New-WdsClient -DeviceID '#{host.mac.upcase.delete ':'}' -DeviceName '#{host.name}' -WdsClientUnattend '#{unattend_file(host)}' -BootImagePath 'boot\\#{wdsify_architecture(host.architecture)}\\images\\#{(host.wds_boot_image || boot_images.first).file_name}' -PxePromptPolicy 'NoPrompt'
end
end
def delete_client(host)
raise NotImplementedError, 'Not finished yet'
delete_unattend(host)
connection.shell(:powershell) do |sh|
sh.run("Remove-WdsClient -DeviceID '#{host.mac.upcase.delete ':'}'")
end
end
def all_images
......@@ -101,52 +115,6 @@ class WdsServer < ApplicationRecord
nil
end
def ensure_unattend(host)
raise NotImplementedException, 'TODO: Not implemented yet'
connection.shell(:powershell) do |sh|
unattend_path = '' # TODO: find or specify remoteinstall path as a parameter
unattend_file = "#{unattend_path}\\#{host.mac.tr ':', '_'}.xml"
# TODO: render template, send as heredoc
# sh.run("$unattend_render = @'\n#{unattend_template}\n'@")
# sh.run("New-Item -Path '#{unattend_file}' -ItemType 'file' -Value $unattend_render")
if SETTINGS[:wds_unattend_group]
# New-WdsInstallImageGroup -Name #{SETTINGS[:wds_unattend_group]}
# Export-WdsInstallImage -ImageGroup <Group> ...
# Import-WdsInstallImage -ImageGroup #{SETTINGS[:wds_unattend_group]} -UnattendFile '#{unattend_file}' -OverwriteUnattend ...
else
source_image = host.wds_facet.install_image
target_image = ForemanWds::WdsInstallImage.new(
wds_server: self,
file_name: "install-#{host.mac.tr ':', '_'}.wim",
image_group: source_image.image_group,
image_name: "#{source_image.image_name} (for #{host.name})"
)
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}' -OverwriteUnattend")
end
end
end
def delete_unattend(host)
raise NotImplementedException, 'TODO: Not implemented yet'
image = host.wds_facet.install_image
group = SETTINGS[:wds_unattend_group]
if group
image = image.dup.tap { |i| i.image_group = group }
else
image.file_name = "install-#{host.mac.tr ':', '_'}.wim"
image.image_name = "#{image.image_name} (for #{host.name})"
end
unattend_path = '' # TODO: find or specify remoteinstall path as a parameter
unattend_file = "#{unattend_path}\\#{host.mac.tr ':', '_'}.xml"
connection.shell(:powershell) do |sh|
sh.run("Remove-WdsInstallImage -ImageGroup '#{image.image_group}' -ImageName '#{image.image_name}' -FileName '#{image.file_name}'")
sh.run("Remove-Item -Path '#{unattend_file}'")
end
end
def self.bootfile_path(architecture_name, loader = :bios, boot_type = :pxe)
file_name = nil
if boot_type == :local
......@@ -178,6 +146,58 @@ class WdsServer < ApplicationRecord
private
def unattend_path
cache.cache(:unattend_path) do
JSON.parse(connection.shell(:powershell) do |sh|
sh.run('Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\WDSServer\Providers\WDSTFTP -Name RootFolder | select RootFolder | ConvertTo-Json -Compress')
end, symbolize_names: true)[:RootFolder]
end
end
def unattend_file(host)
"#{unattend_path}\\#{host.mac.tr ':', '_'}.xml"
end
def target_image_for(host)
source_image = host.wds_install_image
ForemanWds::WdsInstallImage.new(
wds_server: self,
file_name: "install-#{host.mac.tr ':', '_'}.wim",
image_group: SETTINGS[:wds_unattend_group] || source_image.image_group,
image_name: "#{source_image.image_name} (specialized for #{host.name}/#{host.mac})"
)
end
def ensure_unattend(host)
raise NotImplementedException, 'TODO: Not implemented yet'
connection.shell(:powershell) do |sh|
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]
# New-WdsInstallImageGroup -Name #{SETTINGS[:wds_unattend_group]}
# Export-WdsInstallImage -ImageGroup <Group> ...
# Import-WdsInstallImage -ImageGroup #{SETTINGS[:wds_unattend_group]} -UnattendFile '#{unattend_file(host)}' -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")
end
end
end
def delete_unattend(host)
image = target_image_for(host)
connection.shell(:powershell) do |sh|
sh.run("Remove-WdsInstallImage -ImageGroup '#{image.image_group}' -ImageName '#{image.image_name}' -FileName '#{image.file_name}'")
sh.run("Remove-Item -Path '#{unattend_file(host)}'")
end.errcode.zero?
end
def images(type, name = nil)
raise ArgumentError, 'Type must be :boot or :install' unless %i[boot install].include? type
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment