Skip to content

Commit 321cf0f

Browse files
author
Bastian Schmidt
committed
Fixes #35269 - Enable boot image download of installation media
* Include proxy.fetch_boot_image * Add bootimage_path variable for template reference * Adapt PXELinux template * Add custom timeout for tftp requests
1 parent 36be84a commit 321cf0f

File tree

7 files changed

+72
-17
lines changed

7 files changed

+72
-17
lines changed

app/models/concerns/orchestration/tftp.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,31 @@ def setTFTPBootFiles
111111
logger.info "Fetching required TFTP boot files for #{host.name}"
112112
valid = []
113113

114-
host.operatingsystem.pxe_files(host.medium_provider).each do |bootfile_info|
115-
bootfile_info.each do |prefix, path|
116-
valid << each_unique_feasible_tftp_proxy do |proxy|
114+
# Check host.medium_provider path for iso image
115+
prefetch_image = File.extname(host.medium_uri.to_s).downcase.end_with?(".iso")
116+
117+
valid << each_unique_feasible_tftp_proxy do |proxy|
118+
bootfiles = host.operatingsystem.pxe_files(host.medium_provider)
119+
# fetch iso image if given
120+
if prefetch_image
121+
raw_files = []
122+
bootfiles.each {|info| raw_files.append(info.values.first.delete_prefix(host.medium_uri.to_s))}
123+
proxy.fetch_boot_image(:url => host.medium_uri.to_s, :path => host.operatingsystem.bootimage_path(host.medium_provider, host, include_suffix=true, include_base_path=false), :files => raw_files)
124+
end
125+
126+
host.operatingsystem.pxe_files(host.medium_provider).each do |bootfile_info|
127+
bootfile_info.each do |prefix, path|
128+
# change path in case of iso download
129+
if prefetch_image
130+
proxy_path = host.operatingsystem.bootimage_path(host.medium_provider, host, false)
131+
proxy_url = "http://#{URI.parse(proxy.url).host}/#{proxy_path}"
132+
path.sub!(host.medium_uri.to_s, proxy_url)
133+
end
117134
proxy.fetch_boot_file(:prefix => prefix.to_s, :path => path)
118135
end
119136
end
120137
end
138+
121139
failure _("Failed to fetch boot files") unless valid.all?
122140
valid.all?
123141
end

app/models/operatingsystem.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Operatingsystem < ApplicationRecord
9292
property :password_hash, String, desc: 'Encrypted hash of the operating system password'
9393
end
9494
class Jail < Safemode::Jail
95-
allow :id, :name, :major, :minor, :family, :to_s, :==, :release, :release_name, :kernel, :initrd, :pxe_type, :boot_files_uri, :password_hash, :mediumpath, :bootfile
95+
allow :id, :name, :major, :minor, :family, :to_s, :==, :release, :release_name, :kernel, :initrd, :pxe_type, :boot_files_uri, :password_hash, :mediumpath, :bootfile, :bootimage_path
9696
end
9797

9898
def self.title_name
@@ -236,6 +236,27 @@ def bootfile(medium_provider, type)
236236
pxe_prefix(medium_provider) + "-" + pxe_file_names(medium_provider)[type.to_sym]
237237
end
238238

239+
apipie :method, 'Returns path to boot image based on given medium provider and (optional) host' do
240+
required :medium_provider, 'MediumProviders::Provider', 'Medium provider responsible to provide location of installation medium for a given entity (host or host group)'
241+
optional :host, 'Host::Managed', 'A specific host which can set custom a boot image path'
242+
returns String, 'Path to the boot image file'
243+
end
244+
def bootimage_path(medium_provider, host = nil, include_suffix = true, include_base_path = true)
245+
unless medium_provider.is_a? MediumProviders::Provider
246+
raise Foreman::Exception.new(N_('Please provide a medium provider. It can be found as @medium_provider in templates, or Foreman::Plugin.medium_providers_registry.find_provider(host)'))
247+
end
248+
include_base_path ? base_path = "/pub/installation_media/" : base_path = ""
249+
include_suffix ? suffix = ".iso" : suffix = ""
250+
unless host.nil?
251+
if host.host_params.has_key? "custom_bootimage_path"
252+
base_path = host.host_params["custom_bootimage_path"]
253+
base_path += "/" unless base_path[-1] == '/'
254+
end
255+
end
256+
257+
"#{base_path}#{name.downcase}/#{medium_provider.unique_id}#{suffix}"
258+
end
259+
239260
# Does this OS family support a build variant that is constructed from a prebuilt archive
240261
def supports_image
241262
false

app/services/foreman/renderer/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class Configuration
9999
:static,
100100
:template_name,
101101
:xen,
102+
:bootimage_path,
102103
]
103104

104105
DEFAULT_ALLOWED_GLOBAL_SETTINGS = [

app/services/foreman/renderer/scope/variables/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def self.included(base)
1212
delegate :diskLayout, :disk_layout_source, :medium, :architecture, :ptable, :use_image, :arch,
1313
:image_file, :default_image_file, to: :host, allow_nil: true
1414
delegate :mediumpath, :additional_media, :supports_image, :major, :preseed_path, :preseed_server,
15-
:xen, :kernel, :initrd, to: :operatingsystem, allow_nil: true
15+
:xen, :kernel, :initrd, :bootimage_path, to: :operatingsystem, allow_nil: true
1616
delegate :name, to: :architecture, allow_nil: true, prefix: true
1717
delegate :content, to: :disk_layout_source, allow_nil: true, prefix: true
1818

@@ -97,6 +97,7 @@ def xenserver_attributes
9797

9898
def pxe_config
9999
return unless @medium_provider
100+
@bootimage_path = bootimage_path(@medium_provider, host)
100101
@kernel = kernel(@medium_provider)
101102
@initrd = initrd(@medium_provider)
102103
@kernel_uri, @initrd_uri = operatingsystem.boot_files_uri(@medium_provider)

app/services/proxy_api/resource.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ def initialize(args)
2828

2929
attr_reader :connect_params
3030

31-
def resource
31+
def resource(timeout = nil)
3232
# Required in order to ability to mock the resource
33+
unless timeout.nil?
34+
custom_params = connect_params.merge(timeout: timeout)
35+
return RestClient::Resource.new(url, custom_params)
36+
end
3337
@resource ||= RestClient::Resource.new(url, connect_params)
3438
end
3539

@@ -65,7 +69,7 @@ def parse(response)
6569
end
6670

6771
# Perform GET operation on the supplied path
68-
def get(path = nil, payload = {})
72+
def get(path = nil, payload = {}, timeout = nil)
6973
query = payload.delete(:query)
7074
Foreman::Deprecation.deprecation_warning("3.3", "passing additional headers to ProxyApi resource GET action") unless payload.empty?
7175
final_uri = path || ""
@@ -78,39 +82,39 @@ def get(path = nil, payload = {})
7882
telemetry_duration_histogram(:proxy_api_duration, :ms, method: 'get') do
7983
# This ensures that an extra "/" is not generated
8084
if path
81-
resource[final_uri].get payload
85+
resource(timeout)[final_uri].get payload
8286
else
83-
resource.get payload
87+
resource(timeout).get payload
8488
end
8589
end
8690
end
8791
end
8892

8993
# Perform POST operation with the supplied payload on the supplied path
90-
def post(payload, path = "")
94+
def post(payload, path = "", timeout = nil)
9195
logger.debug("POST request payload: #{payload}")
9296
with_logger do
9397
telemetry_duration_histogram(:proxy_api_duration, :ms, method: 'post') do
94-
resource[path].post payload
98+
resource(timeout)[path].post payload
9599
end
96100
end
97101
end
98102

99103
# Perform PUT operation with the supplied payload on the supplied path
100-
def put(payload, path = "")
104+
def put(payload, path = "", timeout = nil)
101105
logger.debug("PUT request payload: #{payload}")
102106
with_logger do
103107
telemetry_duration_histogram(:proxy_api_duration, :ms, method: 'put') do
104-
resource[path].put payload
108+
resource(timeout)[path].put payload
105109
end
106110
end
107111
end
108112

109113
# Perform DELETE operation on the supplied path
110-
def delete(path)
114+
def delete(path, timeout = nil)
111115
with_logger do
112116
telemetry_duration_histogram(:proxy_api_duration, :ms, method: 'delete') do
113-
resource[path].delete
117+
resource(timeout)[path].delete
114118
end
115119
end
116120
end

app/services/proxy_api/tftp.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ def fetch_boot_file(args)
3838
raise ProxyException.new(url, e, N_("Unable to fetch TFTP boot file"))
3939
end
4040

41+
# Requests that the proxy downloads and extracts an image from the media's source
42+
# [+args+] : Hash containing
43+
# :path => String containing the location on the smart proxy to store the image
44+
# :url => String containing the URL of the image to download
45+
# Returns : Boolean status
46+
def fetch_boot_image(args)
47+
parse(post(args, "fetch_boot_image", 180)) # Set 180 seconds timeout for large image download
48+
rescue => e
49+
raise ProxyException.new(url, e, N_("Unable to fetch and extract TFTP boot image"))
50+
end
51+
4152
# returns the TFTP boot server for this proxy
4253
def bootServer
4354
if (response = parse(get("serverName"))) && response["serverName"].present?

app/views/unattended/provisioning_templates/PXELinux/preseed_default_pxelinux_autoinstall.erb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ test_on:
3434
end
3535
options << "locale=#{host_param('lang') || 'en_US'}"
3636
options = options.join(' ')
37-
image_path = @preseed_path.sub(/\/?$/, '.iso')
3837
-%>
3938
#
4039
# WARNING
@@ -47,6 +46,6 @@ DEFAULT linux cloud-init autoinstall
4746
LABEL linux cloud-init autoinstall
4847
KERNEL <%= @kernel %>
4948
INITRD <%= @initrd %>
50-
APPEND ip=dhcp url=http://<%= @preseed_server %><%= image_path %> autoinstall ds=nocloud-net;s=http://<%= foreman_request_addr %>/userdata/ root=/dev/ram0 ramdisk_size=1500000 fsck.mode=skip
49+
APPEND ip=dhcp url=http://<%= foreman_request_addr.split(':').first %><%= @bootimage_path %> autoinstall ds=nocloud-net;s=http://<%= foreman_request_addr %>/userdata/ root=/dev/ram0 ramdisk_size=1500000 fsck.mode=skip
5150

5251
<%= snippet_if_exists(template_name + " custom menu") %>

0 commit comments

Comments
 (0)