Skip to content

Commit

Permalink
use #fetch_as_{hash,array} whenever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
wfeldt committed Apr 7, 2021
1 parent 019c43d commit 9d39af3
Show file tree
Hide file tree
Showing 10 changed files with 876 additions and 66 deletions.
22 changes: 11 additions & 11 deletions src/lib/autoinstall/autosetup_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def suse_register
#
# @return [Boolean]
def semi_auto?(name)
general_section = Yast::Profile.current["general"] || {}
general_section = Yast::Profile.current.fetch_as_hash("general")
!!general_section["semi-automatic"]&.include?(name)
end

Expand All @@ -126,13 +126,13 @@ def autosetup_network
# Prevent to be called twice in case of already configured
return if @network_configured

networking_section = Yast::Profile.current.fetch("networking", {})
networking_section = Yast::Profile.current.fetch_as_hash("networking")
Yast::WFM.CallFunction("lan_auto", ["Import", networking_section])

# Import also the host section in order to resolve hosts only available
# with the network configuration and the host entry
if Yast::Profile.current["host"]
Yast::WFM.CallFunction("host_auto", ["Import", Yast::Profile.current["host"]])
if Yast::Profile.current.fetch_as_hash("host", nil)
Yast::WFM.CallFunction("host_auto", ["Import", Yast::Profile.current.fetch_as_hash("host")])
end

if semi_auto?("networking")
Expand All @@ -158,15 +158,15 @@ def autosetup_network
def network_before_proposal?
return @network_before_proposal unless @network_before_proposal.nil?

networking_section = Yast::Profile.current["networking"] || {}
networking_section = Yast::Profile.current.fetch_as_hash("networking")

@network_before_proposal = networking_section.fetch("setup_before_proposal", false)
end

# Import and configure country specific data (timezone, language and
# keyboard)
def autosetup_country
Yast::Language.Import(Yast::Profile.current["language"] || {})
Yast::Language.Import(Yast::Profile.current.fetch_as_hash("language"))

# Set Console font
Yast::Installation.encoding = Yast::Console.SelectFont(Yast::Language.language)
Expand All @@ -178,16 +178,16 @@ def autosetup_country
end

if Yast::Profile.current.key?("timezone")
Yast::Timezone.Import(Yast::Profile.current["timezone"] || {})
Yast::Timezone.Import(Yast::Profile.current.fetch_as_hash("timezone"))
Yast::Profile.remove_sections("timezone")
end

# bnc#891808: infer keyboard from language if needed
if Yast::Profile.current.key?("keyboard")
Yast::Keyboard.Import(Yast::Profile.current["keyboard"] || {}, :keyboard)
Yast::Keyboard.Import(Yast::Profile.current.fetch_as_hash("keyboard"), :keyboard)
Yast::Profile.remove_sections("keyboard")
elsif Yast::Profile.current.key?("language")
Yast::Keyboard.Import(Yast::Profile.current["language"] || {}, :language)
Yast::Keyboard.Import(Yast::Profile.current.fetch_as_hash("language"), :language)
end

Yast::Profile.remove_sections("language") if Yast::Profile.current.key?("language")
Expand All @@ -199,14 +199,14 @@ def profile_checker

# Invokes autoyast setup for firewall
def autosetup_firewall
return if !Yast::Profile.current["firewall"]
return if !Yast::Profile.current.fetch_as_hash("firewall", nil)

# in some cases we need to postpone firewall configuration to the second stage
# we also have to guarantee that firewall is not blocking second stage in this case
firewall_section = if need_second_stage_run?
{ "enable_firewall" => false }
else
Yast::Profile.current["firewall"]
Yast::Profile.current.fetch_as_hash("firewall")
end

log.info("Importing Firewall settings from AY profile")
Expand Down
18 changes: 9 additions & 9 deletions src/lib/autoinstall/clients/inst_autoinit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ def run
return @ret if @ret == :abort
end

if Yast::Profile.current["iscsi-client"]
if Yast::Profile.current.fetch_as_hash("iscsi-client", nil)
log.info "iscsi-client found"
Yast::WFM.CallFunction(
"iscsi-client_auto",
["Import", Yast::Profile.current["iscsi-client"]]
["Import", Yast::Profile.current.fetch_as_hash("iscsi-client")]
)
Yast::WFM.CallFunction("iscsi-client_auto", ["Write"])
end

if Yast::Profile.current["fcoe-client"]
if Yast::Profile.current.fetch_as_hash("fcoe-client", nil)
log.info "fcoe-client found"
Yast::WFM.CallFunction(
"fcoe-client_auto",
["Import", Yast::Profile.current["fcoe-client"]]
["Import", Yast::Profile.current.fetch_as_hash("fcoe-client")]
)
Yast::WFM.CallFunction("fcoe-client_auto", ["Write"])
end
Expand Down Expand Up @@ -153,7 +153,7 @@ def run
# many times as needed.
def autoinit_scripts
# Pre-Scripts
Yast::AutoinstScripts.Import(Yast::Profile.current["scripts"] || {})
Yast::AutoinstScripts.Import(Yast::Profile.current.fetch_as_hash("scripts"))
Yast::AutoinstScripts.Write("pre-scripts", false)

# Reread Profile in case it was modified in pre-script
Expand All @@ -167,7 +167,7 @@ def autoinit_scripts
loop do
askDialog
# Pre-Scripts
Yast::AutoinstScripts.Import(Yast::Profile.current["scripts"] || {})
Yast::AutoinstScripts.Import(Yast::Profile.current.fetch_as_hash("scripts"))
Yast::AutoinstScripts.Write("pre-scripts", false)
ret = readModified
return :abort if ret == :abort
Expand All @@ -184,9 +184,9 @@ def autoinit_scripts
# Imports the initial profile configuration (report, general and
# pre-scripts sections)
def import_initial_config
Yast::Report.Import(Yast::Profile.current.fetch("report", {}))
Yast::AutoinstGeneral.Import(Yast::Profile.current.fetch("general", {}))
Yast::AutoinstScripts.Import(Yast::Profile.current.fetch("scripts", {}))
Yast::Report.Import(Yast::Profile.current.fetch_as_hash("report"))
Yast::AutoinstGeneral.Import(Yast::Profile.current.fetch_as_hash("general"))
Yast::AutoinstScripts.Import(Yast::Profile.current.fetch_as_hash("scripts"))
end

# Checking profile for unsupported sections.
Expand Down
8 changes: 4 additions & 4 deletions src/lib/autoinstall/clients/inst_autosetup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def main
AutoinstSoftware.merge_product(AutoinstFunctions.selected_product)

# configure general settings
general_section = Profile.current["general"] || {}
networking_section = Profile.current["networking"] || {}
general_section = Profile.current.fetch_as_hash("general")
networking_section = Profile.current.fetch_as_hash("networking")
pkg_list = networking_section["managed"] ? ["NetworkManager"] : []
AutoinstGeneral.Import(general_section)
log.info("general: #{general_section}")
Expand Down Expand Up @@ -238,7 +238,7 @@ def main

return :abort unless WFM.CallFunction(
"bootloader_auto",
["Import", Ops.get_map(Profile.current, "bootloader", {})]
["Import", Profile.current.fetch_as_hash("bootloader")]
)

Progress.NextStage
Expand Down Expand Up @@ -369,7 +369,7 @@ def main
# Checking Base Product licenses
#
Progress.NextStage
if general_section["mode"]&.fetch("confirm_base_product_license", false)
if general_section.fetch_as_hash("mode").fetch("confirm_base_product_license", false)
result = nil
while result != :next
result = WFM.CallFunction("inst_product_license", [{ "enable_back"=>false }])
Expand Down
21 changes: 11 additions & 10 deletions src/lib/autoinstall/pkg_gpg_check_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,27 @@ def handle_error
# If the add-on has its own specific configuration, those settings
# will override to general settings.
#
# @param [Hash] profile AutoYaST profile
# @param [Yast::ProfileHash] profile AutoYaST profile
# @param [String] url Repository URL
# @return [Hash] Signature handling settings for the given add-on.
# @return [Yast::ProfileHash] Signature handling settings for the given add-on.
def get_addon_config(profile, url)
addon_config = addons_config(profile).find { |c| c["media_url"] == url } || {}
general_config = profile.fetch("general", {})
signature_handling = general_config.fetch("signature-handling", {})
signature_handling = {} unless signature_handling.is_a?(Hash)
signature_handling.merge(addon_config.fetch("signature-handling", {}))
addon_config =
addons_config(profile).find { |c| c["media_url"] == url } ||
Yast::ProfileHash.new
general_config = profile.fetch_as_hash("general")
general_config.fetch_as_hash("signature-handling")
.merge(addon_config.fetch_as_hash("signature-handling"))
end

# Get add-ons configuration
#
# This is just a helper method that returns the //add-ons/add_on_products section
# of an AutoYaST profile.
#
# @param [Hash] profile AutoYaST profile.
# @return [Hash] Add-ons section from profile.
# @param [Yast::ProfileHash] profile AutoYaST profile.
# @return [Yast::ProfileHash] Add-ons section from profile.
def addons_config(profile)
profile.fetch("add-on", {}).fetch("add_on_products", [])
profile.fetch_as_hash("add-on").fetch_as_array("add_on_products")
end

# Find the key ID for the package
Expand Down
4 changes: 2 additions & 2 deletions src/lib/autoinstall/profile_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ def run_scripts(filename)
# we need to import at least scripts if not all of them are imported
if !@import_all
Yast::Profile.ReadXML(filename)
Yast::AutoinstScripts.Import(Yast::Profile.current.fetch("scripts", {}))
log.info "importing scripts #{Yast::Profile.current.fetch("scripts", {})}"
Yast::AutoinstScripts.Import(Yast::Profile.current.fetch_as_hash("scripts"))
log.info "importing scripts #{Yast::Profile.current.fetch_as_hash("scripts")}"
end
SCRIPTS_PARAMS.each do |type, special|
# pre-scripts has some expectations where profile lives
Expand Down
12 changes: 6 additions & 6 deletions src/modules/AutoinstFunctions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ def identify_product
# @return [Y2Packager::Product] a product if exactly one product matches
# the criteria, nil otherwise
def identify_product_by_patterns(profile)
software = profile["software"] || {}
software = profile.fetch_as_hash("software")

identify_product do |name|
software.fetch("patterns", []).any? { |p| p =~ /#{name.downcase}-.*/ }
software.fetch_as_array("patterns").any? { |p| p =~ /#{name.downcase}-.*/ }
end
end

Expand All @@ -206,10 +206,10 @@ def identify_product_by_patterns(profile)
# @return [Y2Packager::Product] a product if exactly one product matches
# the criteria, nil otherwise
def identify_product_by_packages(profile)
software = profile["software"] || {}
software = profile.fetch_as_hash("software")

identify_product do |name|
software.fetch("packages", []).any? { |p| p =~ /#{name.downcase}-release/ }
software.fetch_as_array("packages").any? { |p| p =~ /#{name.downcase}-release/ }
end
end

Expand All @@ -235,15 +235,15 @@ def identify_product_by_selection(profile)
# @param profile [Hash] AutoYaST profile
# @return [String] product name
def base_product_name(profile)
software = profile["software"]
software = profile.fetch_as_hash("software", nil)

if software.nil?
log.info("Error: given profile has not a valid software section")

return nil
end

software.fetch("products", []).first
software.fetch_as_array("products").first
end
end

Expand Down
12 changes: 6 additions & 6 deletions src/modules/AutoinstGeneral.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,19 @@ def Summary
# @param [Hash] settings
# @return booelan
def Import(settings)
settings = deep_copy(settings)
settings = Yast::ProfileHash.new(settings)
SetModified()
log.info "General import: #{settings.inspect}"
@mode = settings.fetch("mode", {})
@mode = settings.fetch_as_hash("mode")
@cio_ignore = settings.fetch("cio_ignore", true)
@signature_handling = settings.fetch("signature-handling", {})
@askList = settings.fetch("ask-list", [])
@proposals = settings.fetch("proposals", [])
@signature_handling = settings.fetch_as_hash("signature-handling")
@askList = settings.fetch_as_array("ask-list")
@proposals = settings.fetch_as_array("proposals")
AutoinstStorage.import_general_settings(settings["storage"])
@minimal_configuration = settings.fetch("minimal_configuration", false)
@self_update = settings["self_update"] # no default as we want to know if it is explicit
@self_update_url = settings["self_update_url"]
@wait = settings.fetch("wait", {})
@wait = settings.fetch_as_hash("wait")

SetSignatureHandling()

Expand Down
4 changes: 2 additions & 2 deletions src/modules/AutoinstScripts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ def Write(type, special)
private

# Checking if the script has the right format
# @param tree [Hash] scripts section of the AutoYast configuration
# @param tree [Yast::ProfileHash] scripts section of the AutoYast configuration
# @param key [String] kind of script (pre, post,..)
# @return [Array<String>] of scripts
def valid_scripts_for(tree, key)
tree.fetch(key, []).select do |h|
tree.fetch_as_array(key).select do |h|
next true if h.is_a?(Hash)

log.warn "Cannot evaluate #{key}: #{h.inspect}"
Expand Down

0 comments on commit 9d39af3

Please sign in to comment.