From 52b080365bdc5183ea77ebcedcbff580089e13b5 Mon Sep 17 00:00:00 2001 From: Bastian Schmidt Date: Tue, 19 Jul 2022 09:32:15 +0200 Subject: [PATCH] Fixes #35269 - Support system image download for installation media * Include proxy.fetch_system_image * Add system_image_path variable for template reference * Adapt PXELinux template * Add custom timeout for tftp requests * Add tftp_http_port setting --- app/models/concerns/orchestration/tftp.rb | 46 +++++++++++++++++-- app/models/operatingsystem.rb | 22 ++++++++- app/models/smart_proxy.rb | 12 ++++- .../foreman/renderer/configuration.rb | 1 + .../foreman/renderer/scope/variables/base.rb | 3 +- app/services/proxy_api/resource.rb | 24 ++++++---- app/services/proxy_api/tftp.rb | 14 ++++++ .../preseed_default_pxelinux_autoinstall.erb | 11 ++++- 8 files changed, 114 insertions(+), 19 deletions(-) diff --git a/app/models/concerns/orchestration/tftp.rb b/app/models/concerns/orchestration/tftp.rb index 2665cab80f53..c371a987ccf8 100644 --- a/app/models/concerns/orchestration/tftp.rb +++ b/app/models/concerns/orchestration/tftp.rb @@ -111,13 +111,34 @@ def setTFTPBootFiles logger.info "Fetching required TFTP boot files for #{host.name}" valid = [] - host.operatingsystem.pxe_files(host.medium_provider).each do |bootfile_info| - bootfile_info.each do |prefix, path| - valid << each_unique_feasible_tftp_proxy do |proxy| - proxy.fetch_boot_file(:prefix => prefix.to_s, :path => path) + # Check host.medium_provider path for iso image + prefetch_image = File.extname(host.medium_uri.to_s).downcase.end_with?(".iso") + + valid << each_unique_feasible_tftp_proxy do |proxy| + bootfiles = host.operatingsystem.pxe_files(host.medium_provider) + # fetch iso image if given + if prefetch_image + retries = 10 + pause_until_retry = 18.seconds + host_url = host.medium_uri.to_s + file_paths, tftp_base_path = pxe_url_to_path(bootfiles, host_url) + system_image_path = host.operatingsystem.system_image_path(host.medium_provider, host, true, false) + image_status = proxy.fetch_system_image(:url => host_url, :path => system_image_path, :files => file_paths, :tftp_path => tftp_base_path) + until retries <= 0 || image_status == 200 + sleep(pause_until_retry) + image_status = proxy.fetch_system_image(:url => host_url, :path => system_image_path, :files => file_paths, :tftp_path => tftp_base_path) + retries = retries - 1 + end + image_status == 200 ? true : false + else + bootfiles.each do |bootfile_info| + bootfile_info.each do |prefix, path| + proxy.fetch_boot_file(:prefix => prefix.to_s, :path => path) + end end end end + failure _("Failed to fetch boot files") unless valid.all? valid.all? end @@ -197,4 +218,21 @@ def each_unique_feasible_tftp_proxy end results.all? end + + def pxe_url_to_path(pxe_urls, host_url) + pxe_paths = [] + pxe_urls.each {|pxe_url| pxe_paths.append(pxe_url.values.first.delete_prefix(host_url))} +<<<<<<< HEAD + pxe_paths + end + + def pxe_replace_proxy_addr(url, host, proxy) + proxy_path = host.operatingsystem.system_image_path(host.medium_provider, host, false) + proxy_url = "http://#{URI.parse(proxy.url).host}/#{proxy_path}" + url.sub(host.medium_uri.to_s, proxy_url) +======= + return pxe_paths, pxe_urls[0].keys[0] +>>>>>>> 893b12df3 (Fixes #35269 - Support system image download as installation media) + end + end diff --git a/app/models/operatingsystem.rb b/app/models/operatingsystem.rb index 0d97e87e2907..6f2fd6151ff1 100644 --- a/app/models/operatingsystem.rb +++ b/app/models/operatingsystem.rb @@ -92,7 +92,7 @@ class Operatingsystem < ApplicationRecord property :password_hash, String, desc: 'Encrypted hash of the operating system password' end class Jail < Safemode::Jail - allow :id, :name, :major, :minor, :family, :to_s, :==, :release, :release_name, :kernel, :initrd, :pxe_type, :boot_files_uri, :password_hash, :mediumpath, :bootfile + allow :id, :name, :major, :minor, :family, :to_s, :==, :release, :release_name, :kernel, :initrd, :pxe_type, :boot_files_uri, :password_hash, :mediumpath, :bootfile, :system_image_path end def self.title_name @@ -236,6 +236,26 @@ def bootfile(medium_provider, type) pxe_prefix(medium_provider) + "-" + pxe_file_names(medium_provider)[type.to_sym] end + apipie :method, 'Returns path to boot image based on given medium provider and (optional) host' do + required :medium_provider, 'MediumProviders::Provider', 'Medium provider responsible to provide location of installation medium for a given entity (host or host group)' + optional :host, 'Host::Managed', 'A specific host which can set custom a boot image path' + returns String, 'Path to the boot image file' + end + def system_image_path(medium_provider, host = nil, include_suffix = true, include_base_path = true) + unless medium_provider.is_a? MediumProviders::Provider + 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)')) + end + include_base_path ? base_path = system_image_base_path : base_path = "" + include_suffix ? suffix = ".iso" : suffix = "" + + "#{base_path}#{name.downcase}/#{medium_provider.unique_id}#{suffix}" + end + + # Base path for system_image url + def system_image_base_path + "/tftp/system_image/" + end + # Does this OS family support a build variant that is constructed from a prebuilt archive def supports_image false diff --git a/app/models/smart_proxy.rb b/app/models/smart_proxy.rb index 1d1ae8077b06..e4fa6ef7f521 100644 --- a/app/models/smart_proxy.rb +++ b/app/models/smart_proxy.rb @@ -109,6 +109,14 @@ def setting(feature, setting) smart_proxy_feature_by_name(feature).try(:settings).try(:[], setting) end + def tftp_http_port + setting(:TFTP, 'http_port') + end + + def tftp_http_port! + tftp_http_port || raise(::Foreman::Exception.new(N_("HTTP boot requires proxy with httpboot feature and http_port exposed setting"))) + end + def httpboot_http_port setting(:HTTPBoot, 'http_port') end @@ -202,12 +210,14 @@ def get_features sections only: %w[all additional] prop_group :basic_model_props, ApplicationRecord, meta: { friendly_name: 'Smart Proxy' } property :hostname, String, desc: 'Returns name of the host with proxy' + property :tftp_http_port, Integer, desc: 'Returns proxy port for TFTP boot images' + property :tftp_http_port!, Integer, desc: 'Same as tftp_http_port, but raises Foreman::Exception if no port is set' property :httpboot_http_port, Integer, desc: 'Returns proxy port for HTTP boot' property :httpboot_http_port!, Integer, desc: 'Same as httpboot_http_port, but raises Foreman::Exception if no port is set' property :httpboot_https_port, Integer, desc: 'Returns proxy port for HTTPS boot' property :httpboot_https_port!, Integer, desc: 'Same as httpboot_https_port, but raises Foreman::Exception if no port is set' end class Jail < ::Safemode::Jail - allow :id, :name, :hostname, :httpboot_http_port, :httpboot_https_port, :httpboot_http_port!, :httpboot_https_port!, :url + allow :id, :name, :hostname, :tftp_http_port, :httpboot_http_port, :httpboot_https_port, :tftp_http_port!, :httpboot_http_port!, :httpboot_https_port!, :url end end diff --git a/app/services/foreman/renderer/configuration.rb b/app/services/foreman/renderer/configuration.rb index 4d8b65ed108c..3aa615dd0eb3 100644 --- a/app/services/foreman/renderer/configuration.rb +++ b/app/services/foreman/renderer/configuration.rb @@ -100,6 +100,7 @@ class Configuration :static, :template_name, :xen, + :system_image_path, ] DEFAULT_ALLOWED_GLOBAL_SETTINGS = [ diff --git a/app/services/foreman/renderer/scope/variables/base.rb b/app/services/foreman/renderer/scope/variables/base.rb index f211eb177ade..ff206d4daf08 100644 --- a/app/services/foreman/renderer/scope/variables/base.rb +++ b/app/services/foreman/renderer/scope/variables/base.rb @@ -12,7 +12,7 @@ def self.included(base) delegate :diskLayout, :disk_layout_source, :medium, :architecture, :ptable, :use_image, :arch, :image_file, :default_image_file, to: :host, allow_nil: true delegate :mediumpath, :additional_media, :supports_image, :major, :preseed_path, :preseed_server, - :xen, :kernel, :initrd, to: :operatingsystem, allow_nil: true + :xen, :kernel, :initrd, :system_image_path, to: :operatingsystem, allow_nil: true delegate :name, to: :architecture, allow_nil: true, prefix: true delegate :content, to: :disk_layout_source, allow_nil: true, prefix: true @@ -97,6 +97,7 @@ def xenserver_attributes def pxe_config return unless @medium_provider + @system_image_path = system_image_path(@medium_provider, host) @kernel = kernel(@medium_provider) @initrd = initrd(@medium_provider) @kernel_uri, @initrd_uri = operatingsystem.boot_files_uri(@medium_provider) diff --git a/app/services/proxy_api/resource.rb b/app/services/proxy_api/resource.rb index 088853400eb3..fed7adc6333d 100644 --- a/app/services/proxy_api/resource.rb +++ b/app/services/proxy_api/resource.rb @@ -28,8 +28,12 @@ def initialize(args) attr_reader :connect_params - def resource + def resource(timeout = nil) # Required in order to ability to mock the resource + unless timeout.nil? + custom_params = connect_params.merge(timeout: timeout) + return RestClient::Resource.new(url, custom_params) + end @resource ||= RestClient::Resource.new(url, connect_params) end @@ -65,7 +69,7 @@ def parse(response) end # Perform GET operation on the supplied path - def get(path = nil, payload = {}) + def get(path = nil, payload = {}, timeout = nil) query = payload.delete(:query) Foreman::Deprecation.deprecation_warning("3.3", "passing additional headers to ProxyApi resource GET action") unless payload.empty? final_uri = path || "" @@ -78,39 +82,39 @@ def get(path = nil, payload = {}) telemetry_duration_histogram(:proxy_api_duration, :ms, method: 'get') do # This ensures that an extra "/" is not generated if path - resource[final_uri].get payload + resource(timeout)[final_uri].get payload else - resource.get payload + resource(timeout).get payload end end end end # Perform POST operation with the supplied payload on the supplied path - def post(payload, path = "") + def post(payload, path = "", timeout = nil) logger.debug("POST request payload: #{payload}") with_logger do telemetry_duration_histogram(:proxy_api_duration, :ms, method: 'post') do - resource[path].post payload + resource(timeout)[path].post payload end end end # Perform PUT operation with the supplied payload on the supplied path - def put(payload, path = "") + def put(payload, path = "", timeout = nil) logger.debug("PUT request payload: #{payload}") with_logger do telemetry_duration_histogram(:proxy_api_duration, :ms, method: 'put') do - resource[path].put payload + resource(timeout)[path].put payload end end end # Perform DELETE operation on the supplied path - def delete(path) + def delete(path, timeout = nil) with_logger do telemetry_duration_histogram(:proxy_api_duration, :ms, method: 'delete') do - resource[path].delete + resource(timeout)[path].delete end end end diff --git a/app/services/proxy_api/tftp.rb b/app/services/proxy_api/tftp.rb index dc4f0582ca7d..8dec46feff98 100644 --- a/app/services/proxy_api/tftp.rb +++ b/app/services/proxy_api/tftp.rb @@ -38,6 +38,20 @@ def fetch_boot_file(args) raise ProxyException.new(url, e, N_("Unable to fetch TFTP boot file")) end + # Requests that the proxy downloads and extracts an image from the media's source + # [+args+] : Hash containing + # :path => String containing the location on the smart proxy to store the image + # :url => String containing the URL of the image to download + # Returns : Integer response status + def fetch_system_image(args) + response = post(args, "fetch_system_image") + response.code + rescue RestClient::Locked + 423 + rescue => e + raise ProxyException.new(url, e, N_("Unable to fetch and extract TFTP system image")) + end + # returns the TFTP boot server for this proxy def bootServer if (response = parse(get("serverName"))) && response["serverName"].present? diff --git a/app/views/unattended/provisioning_templates/PXELinux/preseed_default_pxelinux_autoinstall.erb b/app/views/unattended/provisioning_templates/PXELinux/preseed_default_pxelinux_autoinstall.erb index eb030c320be9..e3b34bbd6dfa 100644 --- a/app/views/unattended/provisioning_templates/PXELinux/preseed_default_pxelinux_autoinstall.erb +++ b/app/views/unattended/provisioning_templates/PXELinux/preseed_default_pxelinux_autoinstall.erb @@ -42,7 +42,14 @@ test_on: options << "locale=#{host_param('lang') || 'en_US'}" options = options.join(' ') - image_path = @preseed_path.sub(/\/?$/, '.iso') + if @preseed_path.downcase.end_with?('.iso') + image_path = @system_image_path + tftp = @host.subnet.tftp + image_host = "#{tftp}:#{tftp.tftp_http_port}" + else + image_path = @preseed_path.sub(/\/?$/, '.iso') + image_host = foreman_request_addr.split(':').first + end -%> # # WARNING @@ -55,6 +62,6 @@ DEFAULT linux cloud-init autoinstall LABEL linux cloud-init autoinstall KERNEL <%= @kernel %> INITRD <%= @initrd %> - APPEND 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 <%= options %> + APPEND url=http://<%= image_host %><%= image_path %> autoinstall ds=nocloud-net;s=http://<%= foreman_request_addr %>/userdata/ root=/dev/ram0 ramdisk_size=1500000 fsck.mode=skip <%= options %> <%= snippet_if_exists(template_name + " custom menu") %>