Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37792 - Safe mode and bootdisk_* template helpers #167

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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