Skip to content

Commit

Permalink
Fixes #37792 - Safe mode and bootdisk_* template helpers
Browse files Browse the repository at this point in the history
Using :extend_template_helpers instead of :allowed_template_helpers
fix the issue when rendering bootdisk_* template helpers in the Safe mode.
  • Loading branch information
stejskalleos committed Oct 1, 2024
1 parent 035149b commit 415780d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
42 changes: 0 additions & 42 deletions app/lib/foreman_bootdisk/scope/bootdisk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,6 @@
module ForemanBootdisk
module Scope
class Bootdisk < ::Foreman::Renderer::Scope::Provisioning
extend ApipieDSL::Class

apipie :class, 'Macros related to provisioning via boot disk' do
name 'Bootdisk'
sections only: %w[all provisioning]
end

apipie :method, 'Generates URL for boot chain' do
optional :mac, String, 'MAC address of the host', default: 'MAC address of the current host'
optional :action, String, 'Bootloader to use', default: 'iPXE'
returns String, desc: 'URL for boot chain'
example 'bootdisk_chain_url #=> "http://foreman.example.com/unattended/iPXE?mac=00%3A11%3A22%3A33%3A44%3A55"'
example 'bootdisk_chain_url("00:11:22:33:44:55") #=> "http://foreman.example.com/unattended/iPXE?mac=00%3A11%3A22%3A33%3A44%3A55"'
end
def bootdisk_chain_url(mac = host.try(:mac), action = 'iPXE')
url = foreman_url(action)
u = URI.parse(url)
new_query_data = URI.decode_www_form(u.query || '') << ['mac', mac || '']
new_querystring = URI.encode_www_form(new_query_data)
u.query = nil
u.query = new_querystring if new_querystring.present?
u.fragment = nil
u.to_s
end

apipie :method, 'Always raises an error with a description provided as an argument' do
desc 'This method is useful for aborting script execution if some of the conditions are not met'
list :args, desc: 'Description for the error'
raises error: ::Foreman::Exception, desc: 'The error is always being raised'
returns nil, desc: "Doesn't return anything"
example "<%
interface = @host.provision_interface #=> interface = nil
bootdisk_raise(N_('Host has no provisioning interface defined')) unless interface #=> Foreman::Exception is raised and the execution of the script is aborted
%>"
end
def bootdisk_raise(*args)
raise ::Foreman::Exception.new(*args)
end

def template_name
"Foreman Bootdisk"
end
end
end
end
48 changes: 48 additions & 0 deletions app/lib/foreman_bootdisk/template_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module ForemanBootdisk
module TemplateHelpers
extend ApipieDSL::Class

apipie :class, 'Macros related to provisioning via boot disk' do
name 'Bootdisk'
sections only: %w[all provisioning]
end

apipie :method, 'Generates URL for boot chain' do
optional :mac, String, 'MAC address of the host', default: 'MAC address of the current host'
optional :action, String, 'Bootloader to use', default: 'iPXE'
returns String, desc: 'URL for boot chain'
example 'bootdisk_chain_url #=> "http://foreman.example.com/unattended/iPXE?mac=00%3A11%3A22%3A33%3A44%3A55"'
example 'bootdisk_chain_url("00:11:22:33:44:55") #=> "http://foreman.example.com/unattended/iPXE?mac=00%3A11%3A22%3A33%3A44%3A55"'
end
def bootdisk_chain_url(mac = host.try(:mac), action = 'iPXE')
url = foreman_url(action)
u = URI.parse(url)
new_query_data = URI.decode_www_form(u.query || '') << ['mac', mac || '']
new_querystring = URI.encode_www_form(new_query_data)
u.query = nil
u.query = new_querystring if new_querystring.present?
u.fragment = nil
u.to_s
end

apipie :method, 'Always raises an error with a description provided as an argument' do
desc 'This method is useful for aborting script execution if some of the conditions are not met'
list :args, desc: 'Description for the error'
raises error: ::Foreman::Exception, desc: 'The error is always being raised'
returns nil, desc: "Doesn't return anything"
example "<%
interface = @host.provision_interface #=> interface = nil
bootdisk_raise(N_('Host has no provisioning interface defined')) unless interface #=> Foreman::Exception is raised and the execution of the script is aborted
%>"
end
def bootdisk_raise(*args)
raise ::Foreman::Exception.new(*args)
end

def template_name
"Foreman Bootdisk"
end
end
end
4 changes: 3 additions & 1 deletion lib/foreman_bootdisk/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class Engine < ::Rails::Engine
ApipieDSL.configuration.dsl_classes_matchers += [
"#{ForemanBootdisk::Engine.root}/app/lib/foreman_bootdisk/scope/*.rb"
]

provision_method 'bootdisk', N_('Boot disk based')
allowed_template_helpers :bootdisk_chain_url, :bootdisk_raise

extend_template_helpers ForemanBootdisk::TemplateHelpers

extend_page "subnets/index" do |cx|
cx.add_pagelet :subnet_index_action_buttons, name: 'Bootdisk', partial: 'subnets/bootdisk_action_buttons'
Expand Down

0 comments on commit 415780d

Please sign in to comment.