Bug 1382735

Summary: [RFE] Allow accessing all template names for a host (in safe mode)
Product: Red Hat Satellite Reporter: Peter Vreman <peter.vreman>
Component: Provisioning TemplatesAssignee: Daniel Lobato Garcia <dlobatog>
Status: CLOSED ERRATA QA Contact: Lukas Zapletal <lzap>
Severity: medium Docs Contact:
Priority: medium    
Version: 6.2.2CC: bbuckingham, bkearney, brubisch, dlobatog, jcallaha, lzap, mhulan, peter.vreman
Target Milestone: UnspecifiedKeywords: EasyFix, FutureFeature, Triaged
Target Release: Unused   
Hardware: Unspecified   
OS: Unspecified   
URL: http://projects.theforeman.org/issues/17680
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2018-02-21 12:35:52 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1122832    

Description Peter Vreman 2016-10-07 14:29:13 UTC
Description of problem:
We have multiple release of RHEL x.y and for each release there is a kickstart template. Each kcikstart template requires a different set of kernel parameters to be supplied.
We do not like to hardcode these parameters in the PXE templates because that is unmaintenable because the template logic and parameters are then separated.

This RFE would like to have the hash of templates_used[<kindname>] available in both:
- Host API
- Template rendering under @host


use case PXE template using @host.templates_used['provisioning'] to include parameters on the kernel command line that are prefixed with the provisioning template name.
----
default linux
label linux
  kernel <%= @kernel %>
<%= @host.inspect %>
  append initrd=<%= @initrd %> ks=<%= foreman_url('provision')%> network <%= @host.params[@host.templates_used['provisioning']+'_kernel_opts'] || "" %>  <%= @host.params['ks_kernel_opts'] || "" %>
  ipappend 2
----


Version-Release number of selected component (if applicable):


How reproducible:


Steps to Reproduce:
1. Set common parameter 'TemplateABC_kernel_options' with value 'ABCDEF'
2. Create provisiong template 'TemplateABC' (content not relevant)
2. Create pxe template 'PXETemplateABC' with the content below
kernel options: XXX<%= @host.params[@host.templates_used['provisioning']+'_kernel_opts'] %>ZZZ
3. Create HostA
4. Associate HostA with TemplateABC and PXETemplateABC
5. Render PXETemplateABC

Actual results:
Output of 'kernel options: XXXZZZ' where the parmaeter is empty

Expected results:
Output of 'kernel options: XXXABCDEFZZZ' where the parameter with the provisioning template name is replaced

Additional info:

Comment 1 Peter Vreman 2016-10-07 14:54:57 UTC
Proposed patch:

--- /usr/share/foreman/app/models/host/managed.rb
+++ /usr/share/foreman/app/models/host/managed.rb
@@ -74,7 +74,7 @@
       :image_build?, :pxe_build?, :otp, :realm, :param_true?, :param_false?, :nil?, :indent, :primary_interface,
       :provision_interface, :interfaces, :bond_interfaces, :bridge_interfaces, :interfaces_with_identifier,
       :managed_interfaces, :facts, :facts_hash, :root_pass, :sp_name, :sp_ip, :sp_mac, :sp_subnet, :use_image,
-      :multiboot, :jumpstart_path, :install_path, :miniroot, :medium
+      :multiboot, :jumpstart_path, :install_path, :miniroot, :medium, :templates_used
   end

   scope :recent,      ->(*args) { where(["last_report > ?", (args.first || (Setting[:puppet_interval] + Setting[:outofsync_interval]).minutes.ago)]) }
@@ -838,6 +838,12 @@
     end.compact
   end

+  def templates_used
+    b = {}
+    available_template_kinds.map { |p| b[p.template_kind_name]=p.name }
+    b
+  end
+
   def render_template(template)
     @host = self
     load_template_vars

Comment 2 Lukas Zapletal 2016-10-13 11:46:40 UTC
Hello,

you could use Operating System - Parameters. You can set arbitrary amount of parameters for each operating system you have and use these in your PXELinux templates, or wherever you like. Example:

* Visit OS - Edit - Parameters
* Enter new parameter with name "test" and arbitrary value
* Edit PXELinux template:

...
KERNEL <%= @kernel %> <%= @host.params['test'] %> 
...

* Save, install an OS

Comment 4 Peter Vreman 2016-10-14 07:59:51 UTC
Lukas,

The global OS level parameters does not support lifecycles as Katello does. The OS parameter is 'Library'. But how to make the OS parameter available for a lifecycle promotion path Library-Development-QAS-Production ?

We solve this challenge of having different releases by using a templates with a version in the name and then using a specified version for each Lifeycle environment.

Kickstart-6.8.1
Kickstart-6.8.2
Kickstart-6.8.3

These are all for the same RHEL6.8, but the content of the %post is different and relies on the kernel parameters.

Comment 5 Lukas Zapletal 2016-10-17 07:07:58 UTC
Peter, I misunderstood your RFE.

Satellite 6.1 contains variable called @template_name which returns the current template name, you could use it in your code to read the parameter.

Under some conditions (when there's snippet included), the variable may contain incorrect name. This has been fixed in 6.3 version (upcoming version of Satellite).

Let me know if this gets you what you need.

Comment 6 Peter Vreman 2016-10-17 07:20:40 UTC
Hoi Lukas,

The @template_name is like you write the current template. When i render a PXE then it is the name of the PXE template.
I would like to know the name of the Provisioning template during the rendering of a PXE template.

Peter

Comment 7 Lukas Zapletal 2016-10-21 13:18:04 UTC
Okay, exposing all template names should not be any problem. We will look into this.

Comment 8 Daniel Lobato Garcia 2016-12-15 07:51:13 UTC
Created redmine issue http://projects.theforeman.org/issues/17680 from this bug

Comment 9 Daniel Lobato Garcia 2016-12-15 08:16:58 UTC
Submitted upstream as https://github.com/theforeman/foreman/pull/4115 - once it's merged I'll set to POST & propose for 6.2. Thanks Peter!

Comment 10 Bryan Kearney 2016-12-15 09:03:17 UTC
Upstream bug component is Provisioning

Comment 11 Bryan Kearney 2016-12-15 15:03:24 UTC
Upstream bug component is Provisioning Templates

Comment 12 Satellite Program 2017-01-04 17:03:14 UTC
Moving this bug to POST for triage into Satellite 6 since the upstream issue http://projects.theforeman.org/issues/17680 has been resolved.

Comment 14 Lukas Zapletal 2017-08-09 10:30:55 UTC
[root@zzzap ~]# rpm -q foreman katello
foreman-1.15.2-1.el7sat.noarch
katello-3.4.4-2.el7sat.noarch

Created PXELinux template:

<%= @host.templates_used %>

Result:

{"PXELinux"=>"Kickstart default PXELinux - template used", "iPXE"=>"Kickstart default iPXE", "provision"=>"Satellite Kickstart Default", "finish"=>"Satellite Kickstart Default Finish", "user_data"=>"Satellite Kickstart Default User Data", "kexec"=>"Discovery Red Hat kexec"}

Safe-mode was enabled. Works.

Comment 18 errata-xmlrpc 2018-02-21 12:35:52 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2018:0336