From a2ed34d38943e1a69ca126201948c105c221e8a9 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 11 Oct 2022 07:48:31 +0200 Subject: [PATCH 01/22] add initial code for products defined in YAML --- src/lib/registration/sw_mgmt.rb | 3 +++ src/lib/registration/yaml_product.rb | 35 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/lib/registration/yaml_product.rb diff --git a/src/lib/registration/sw_mgmt.rb b/src/lib/registration/sw_mgmt.rb index 06c1642a..55df9e11 100644 --- a/src/lib/registration/sw_mgmt.rb +++ b/src/lib/registration/sw_mgmt.rb @@ -31,6 +31,7 @@ require "registration/helpers" require "registration/url_helpers" require "registration/repo_state" +require "registration/yaml_product" require "packager/product_patterns" require "y2packager/medium_type" @@ -192,6 +193,8 @@ def self.find_base_product return online_base_product if Stage.initial && Y2Packager::MediumType.online? + return YamlProduct.selected_product if YamlProduct.selected_product + # use the selected product if a product has been already selected selected = product_selected? if Stage.initial installed = product_installed? if Stage.initial diff --git a/src/lib/registration/yaml_product.rb b/src/lib/registration/yaml_product.rb new file mode 100644 index 00000000..c72bcbdd --- /dev/null +++ b/src/lib/registration/yaml_product.rb @@ -0,0 +1,35 @@ +# allow to register against products defined in yaml. Very similar concept to online medium just this time +# for first boot purpose + +require "yaml" + +module Registration + class YamlProduct + PATH = "/etc/YaST2/products.yaml" + + # check if yaml products are defined at all + def self.exist? + ::File.exist?(PATH) + end + + def self.available_products + return @products if @products + return nil unless exist? + + @products = YAML.load_file(PATH) + end + + def self.selected_product + return nil unless exist? + + # TODO: really select + { + "display_name" => "SUSE Linux Enterprise Desktop 15 SP5" + "name" => "SLED", + "arch" => "x86_64", + "version" => "15.4", + "register_target" => "sle-15-x86_64" + } + end + end +end From 0c1d45d8d74cc60b0c24d21a8e08b1b3328b7a12 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 11 Oct 2022 10:01:02 +0200 Subject: [PATCH 02/22] fix typo --- src/lib/registration/yaml_product.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/registration/yaml_product.rb b/src/lib/registration/yaml_product.rb index c72bcbdd..2b5e62c5 100644 --- a/src/lib/registration/yaml_product.rb +++ b/src/lib/registration/yaml_product.rb @@ -24,7 +24,7 @@ def self.selected_product # TODO: really select { - "display_name" => "SUSE Linux Enterprise Desktop 15 SP5" + "display_name" => "SUSE Linux Enterprise Desktop 15 SP5", "name" => "SLED", "arch" => "x86_64", "version" => "15.4", From d859d9e3f8134ce503a41bbc4efa6505ac44d409 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 11 Oct 2022 22:14:09 +0200 Subject: [PATCH 03/22] fix passing version --- src/lib/registration/yaml_product.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/registration/yaml_product.rb b/src/lib/registration/yaml_product.rb index 2b5e62c5..1aa4f9bc 100644 --- a/src/lib/registration/yaml_product.rb +++ b/src/lib/registration/yaml_product.rb @@ -27,7 +27,8 @@ def self.selected_product "display_name" => "SUSE Linux Enterprise Desktop 15 SP5", "name" => "SLED", "arch" => "x86_64", - "version" => "15.4", + "version" => "15.4-0", + "version_version" => "15.4", "register_target" => "sle-15-x86_64" } end From 840f851a3d736f5e2b05f54c40a63c6df2e29f21 Mon Sep 17 00:00:00 2001 From: Martin Vidner Date: Wed, 12 Oct 2022 10:56:25 +0200 Subject: [PATCH 04/22] Use SLED15-SP4 for the FAKE_BASE_PRODUCT This is useful because Registration::YamlProduct is not working yet. Once it works, this commit can be dropped. --- src/lib/registration/sw_mgmt.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/lib/registration/sw_mgmt.rb b/src/lib/registration/sw_mgmt.rb index 55df9e11..183cc8d2 100644 --- a/src/lib/registration/sw_mgmt.rb +++ b/src/lib/registration/sw_mgmt.rb @@ -63,9 +63,17 @@ class SwMgmt ZYPP_DIR = "/etc/zypp".freeze - FAKE_BASE_PRODUCT = { "name" => "SLES", "arch" => "x86_64", "version" => "12-0", - "flavor" => "DVD", "version_version" => "12", "register_release" => "", - "register_target" => "sle-12-x86_64" }.freeze + FAKE_BASE_PRODUCT = { + "arch" => "x86_64", + "display_name" => "SUSE Linux Enterprise Desktop 15 SP4", + "flavor" => "", + "name" => "SLED", + "product_line" => "sled", + "register_release" => "", + "register_target" => "sle-15-x86_64", + "version" => "15.4-0", + "version_version" => "15.4" + }.freeze OEM_DIR = "/var/lib/suseRegister/OEM".freeze From 27ff108368d1bf169430452c0bb81c31df6adc11 Mon Sep 17 00:00:00 2001 From: Martin Vidner Date: Wed, 12 Oct 2022 12:12:22 +0200 Subject: [PATCH 05/22] A testing wsl_products.yml TODO: fix the naming, everywhere in this branch Also compare with other YAML fixtures in this directory. Add comments about schema, YAML allows comments. On the testing machine: ```sh sudo ln -s /home/linux/svn/yast/registration/test/fixtures/wsl_products.yml /etc/YaST2/products.yaml irb -I src/lib -r registration/sw_mgmt ``` ```rb Registration::SwMgmt.find_base_product ``` --- test/fixtures/wsl_products.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test/fixtures/wsl_products.yml diff --git a/test/fixtures/wsl_products.yml b/test/fixtures/wsl_products.yml new file mode 100644 index 00000000..8955ea18 --- /dev/null +++ b/test/fixtures/wsl_products.yml @@ -0,0 +1,19 @@ +# TODO: references for this file, +# describe what it is +# https://jira.suse.com/browse/PED-1380 +- arch: "$arch" + display_name: "SUSE Linux Enterprise Desktop 15 SP4" + name: "SLED" + product_line: "sled" + register_target: "sle-15-$arch" + version: "15.4-0" + version_version: "15.4" +- arch: "$arch" + display_name: "SUSE Linux Enterprise Server 15 SP4" + name: "SLES" + product_line: "sles" + register_target: "sle-15-$arch" + version: "15.4-0" + # we will convert a YAML number to a string when reading + # because it is easy to forget the quotes + version_version: 15.4 From 8c9947ca8a5c4ec66d31acfca9c9a9946bb010de Mon Sep 17 00:00:00 2001 From: Martin Vidner Date: Wed, 12 Oct 2022 14:46:01 +0200 Subject: [PATCH 06/22] available_products: Expand $arch in the data --- src/lib/registration/yaml_product.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/lib/registration/yaml_product.rb b/src/lib/registration/yaml_product.rb index 1aa4f9bc..3e874b56 100644 --- a/src/lib/registration/yaml_product.rb +++ b/src/lib/registration/yaml_product.rb @@ -2,8 +2,13 @@ # for first boot purpose require "yaml" +require "yast" module Registration + Yast.import "Arch" + + # Added for SLED registration on a WSL SLES image, see + # https://jira.suse.com/browse/PED-1380 class YamlProduct PATH = "/etc/YaST2/products.yaml" @@ -17,6 +22,7 @@ def self.available_products return nil unless exist? @products = YAML.load_file(PATH) + @products = @products.map { |p| expand_variables(p) } end def self.selected_product @@ -32,5 +38,22 @@ def self.selected_product "register_target" => "sle-15-x86_64" } end + + private + + # For all values: + # - convert them to String (to allow writing "15.4" as 15.4) + # - replace $arch substrings with the architecture + # @param product [Hash] + # @return [Hash] new hash + def self.expand_variables(product) + arch = Yast::Arch.architecture + + product.map do |key, val| + val_s = val.to_s + val_s.gsub!("$arch", arch) + [key, val_s] + end.to_h + end end end From 04e3f62e5c8e75db4a81ac9199b7961a35efa002 Mon Sep 17 00:00:00 2001 From: Martin Vidner Date: Wed, 12 Oct 2022 14:47:34 +0200 Subject: [PATCH 07/22] selected_product is just a class level attr_accessor --- src/lib/registration/yaml_product.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/lib/registration/yaml_product.rb b/src/lib/registration/yaml_product.rb index 3e874b56..f8607fc4 100644 --- a/src/lib/registration/yaml_product.rb +++ b/src/lib/registration/yaml_product.rb @@ -7,6 +7,9 @@ module Registration Yast.import "Arch" + # UI should use {.available_products} and pass the user's choice to + # {.selected_product=} + # # Added for SLED registration on a WSL SLES image, see # https://jira.suse.com/browse/PED-1380 class YamlProduct @@ -25,18 +28,12 @@ def self.available_products @products = @products.map { |p| expand_variables(p) } end - def self.selected_product - return nil unless exist? + def self.selected_product=(product) + @selected_product = product + end - # TODO: really select - { - "display_name" => "SUSE Linux Enterprise Desktop 15 SP5", - "name" => "SLED", - "arch" => "x86_64", - "version" => "15.4-0", - "version_version" => "15.4", - "register_target" => "sle-15-x86_64" - } + def self.selected_product + @selected_product end private From 44d43e9336da52f6f232307131221838df62818a Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Thu, 13 Oct 2022 10:07:22 +0200 Subject: [PATCH 08/22] add more real code --- src/lib/registration/yaml_product.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/lib/registration/yaml_product.rb b/src/lib/registration/yaml_product.rb index f8607fc4..80aa3bed 100644 --- a/src/lib/registration/yaml_product.rb +++ b/src/lib/registration/yaml_product.rb @@ -8,7 +8,7 @@ module Registration Yast.import "Arch" # UI should use {.available_products} and pass the user's choice to - # {.selected_product=} + # {.select_product} # # Added for SLED registration on a WSL SLES image, see # https://jira.suse.com/browse/PED-1380 @@ -28,12 +28,21 @@ def self.available_products @products = @products.map { |p| expand_variables(p) } end - def self.selected_product=(product) + # @param product_name [String, nil] nil in case no product is selected, so no registration + def self.select_product(product_name) + product = available_products.find{ |p| p["name"] == product_name } + + @manually_selected = true @selected_product = product end def self.selected_product - @selected_product + return @selected_product if @manually_selected + + product = available_products.find{ |p| p["default"] } + product ||= available_products.first # use first if none have default + + select_product(product["name"]) end private From c706a1b89a262df204fc23e199fe466bb18a0ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Thu, 13 Oct 2022 15:58:37 +0100 Subject: [PATCH 09/22] Add reader --- src/lib/registration/sw_mgmt.rb | 3 - src/lib/registration/yaml_product.rb | 65 -------------------- src/lib/registration/yaml_products_reader.rb | 51 +++++++++++++++ 3 files changed, 51 insertions(+), 68 deletions(-) delete mode 100644 src/lib/registration/yaml_product.rb create mode 100644 src/lib/registration/yaml_products_reader.rb diff --git a/src/lib/registration/sw_mgmt.rb b/src/lib/registration/sw_mgmt.rb index 183cc8d2..c2dab758 100644 --- a/src/lib/registration/sw_mgmt.rb +++ b/src/lib/registration/sw_mgmt.rb @@ -31,7 +31,6 @@ require "registration/helpers" require "registration/url_helpers" require "registration/repo_state" -require "registration/yaml_product" require "packager/product_patterns" require "y2packager/medium_type" @@ -201,8 +200,6 @@ def self.find_base_product return online_base_product if Stage.initial && Y2Packager::MediumType.online? - return YamlProduct.selected_product if YamlProduct.selected_product - # use the selected product if a product has been already selected selected = product_selected? if Stage.initial installed = product_installed? if Stage.initial diff --git a/src/lib/registration/yaml_product.rb b/src/lib/registration/yaml_product.rb deleted file mode 100644 index 80aa3bed..00000000 --- a/src/lib/registration/yaml_product.rb +++ /dev/null @@ -1,65 +0,0 @@ -# allow to register against products defined in yaml. Very similar concept to online medium just this time -# for first boot purpose - -require "yaml" -require "yast" - -module Registration - Yast.import "Arch" - - # UI should use {.available_products} and pass the user's choice to - # {.select_product} - # - # Added for SLED registration on a WSL SLES image, see - # https://jira.suse.com/browse/PED-1380 - class YamlProduct - PATH = "/etc/YaST2/products.yaml" - - # check if yaml products are defined at all - def self.exist? - ::File.exist?(PATH) - end - - def self.available_products - return @products if @products - return nil unless exist? - - @products = YAML.load_file(PATH) - @products = @products.map { |p| expand_variables(p) } - end - - # @param product_name [String, nil] nil in case no product is selected, so no registration - def self.select_product(product_name) - product = available_products.find{ |p| p["name"] == product_name } - - @manually_selected = true - @selected_product = product - end - - def self.selected_product - return @selected_product if @manually_selected - - product = available_products.find{ |p| p["default"] } - product ||= available_products.first # use first if none have default - - select_product(product["name"]) - end - - private - - # For all values: - # - convert them to String (to allow writing "15.4" as 15.4) - # - replace $arch substrings with the architecture - # @param product [Hash] - # @return [Hash] new hash - def self.expand_variables(product) - arch = Yast::Arch.architecture - - product.map do |key, val| - val_s = val.to_s - val_s.gsub!("$arch", arch) - [key, val_s] - end.to_h - end - end -end diff --git a/src/lib/registration/yaml_products_reader.rb b/src/lib/registration/yaml_products_reader.rb new file mode 100644 index 00000000..a4a67918 --- /dev/null +++ b/src/lib/registration/yaml_products_reader.rb @@ -0,0 +1,51 @@ +# allow to register against products defined in yaml. Very similar concept to online medium just this time +# for first boot purpose + +require "yaml" +require "yast" + +module Registration + Yast.import "Arch" + + # Added for SLED registration on a WSL SLES image, see + # https://jira.suse.com/browse/PED-1380 + class YamlProductsReader + attr_reader :path + + def initialize(path = DEFAULT_PATH) + @path = path + end + + # @return [Array] + def read + return [] unless yaml_exist? + + YAML.load_file(path).map { |p| expand_variables(p) } + end + + private + + DEFAULT_PATH = "/etc/YaST2/products.yaml".freeze + private_constant :DEFAULT_PATH + + # check if yaml products are defined at all + def yaml_exist? + ::File.exist?(path) + end + + # For all values: + # - convert them to String (to allow writing "15.4" as 15.4) + # - replace $arch substrings with the architecture + # @param product [Hash] + # @return [Hash] new hash + def expand_variables(product) + arch = Yast::Arch.architecture + + product.map do |key, val| + val_s = val.to_s + val_s.gsub!("$arch", arch) + [key, val_s] + end.to_h + end + end +end From 8aebe833fa0ccd3491e0aa39dc95c26e1e3d7bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Thu, 13 Oct 2022 17:12:46 +0100 Subject: [PATCH 10/22] WIP --- src/lib/registration/storage.rb | 2 +- src/lib/registration/sw_mgmt.rb | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib/registration/storage.rb b/src/lib/registration/storage.rb index 49c7d857..e5b21115 100644 --- a/src/lib/registration/storage.rb +++ b/src/lib/registration/storage.rb @@ -51,7 +51,7 @@ class InstallationOptions include Singleton attr_accessor :install_updates, :email, :reg_code, :selected_addons, - :base_registered, :custom_url, :imported_cert_sha256_fingerprint + :base_registered, :custom_url, :imported_cert_sha256_fingerprint, :product def initialize @email = "" diff --git a/src/lib/registration/sw_mgmt.rb b/src/lib/registration/sw_mgmt.rb index c2dab758..87b8f548 100644 --- a/src/lib/registration/sw_mgmt.rb +++ b/src/lib/registration/sw_mgmt.rb @@ -31,6 +31,8 @@ require "registration/helpers" require "registration/url_helpers" require "registration/repo_state" +require "registration/storage" +require "registration/yaml_products_reader" require "packager/product_patterns" require "y2packager/medium_type" @@ -200,6 +202,9 @@ def self.find_base_product return online_base_product if Stage.initial && Y2Packager::MediumType.online? + yaml_product = find_yaml_product + return yaml_product if yaml_product + # use the selected product if a product has been already selected selected = product_selected? if Stage.initial installed = product_installed? if Stage.initial @@ -766,6 +771,13 @@ def self.target_distribution(destdir) target_distro end - private_class_method :init_target, :target_distribution + def self.find_yaml_product + product_name = Storage::InstallationOptions.instance.product + return nil unless product_name + + YamlProductsReader.new.read.find { |p| p["name"] == product_name } + end + + private_class_method :init_target, :target_distribution, :find_yaml_product end end From 5a673947fcbabad0e6d2090b690f99f1e7e323ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Fri, 14 Oct 2022 09:54:10 +0100 Subject: [PATCH 11/22] Do not skip --- src/lib/registration/sw_mgmt.rb | 4 ++++ .../ui/base_system_registration_dialog.rb | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/lib/registration/sw_mgmt.rb b/src/lib/registration/sw_mgmt.rb index 87b8f548..544225d8 100644 --- a/src/lib/registration/sw_mgmt.rb +++ b/src/lib/registration/sw_mgmt.rb @@ -307,6 +307,10 @@ def self.installed_products products.map { |p| resolvable_to_h(p) } end + def self.base_installed_product + Y2Packager::Resolvable.find(kind: :product, status: :installed, category: "base").first + end + # convert a libzypp Product Hash to a SUSE::Connect::Remote::Product object # @param product [Hash] product Hash obtained from pkg-bindings # @return [SUSE::Connect::Remote::Product] the remote product diff --git a/src/lib/registration/ui/base_system_registration_dialog.rb b/src/lib/registration/ui/base_system_registration_dialog.rb index 97cc84fc..0a34daa2 100644 --- a/src/lib/registration/ui/base_system_registration_dialog.rb +++ b/src/lib/registration/ui/base_system_registration_dialog.rb @@ -321,7 +321,7 @@ def register_local_option # @return [Yast::Term] UI term def skip_option # do not display it in an installed system or when already registered - return Empty() if Stage.normal || Registration.is_registered? + return Empty() if Stage.normal || Registration.is_registered? || product_switched? Left( RadioButton( Id(:skip_registration), @@ -332,6 +332,15 @@ def skip_option ) end + def product_switched? + installed_product = SwMgmt.base_installed_product + product = Storage::InstallationOptions.instance.product + + return false unless installed_product && product + + installed_product.name != product + end + # part of the main dialog definition - the base product details # @return [Yast::Term] UI term def product_details_widgets From f0cadb4a97e2a1a0c48191ed687f703e9f80bf12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Fri, 14 Oct 2022 12:16:03 +0100 Subject: [PATCH 12/22] Allow force registration --- src/lib/registration/storage.rb | 4 +++- src/lib/registration/sw_mgmt.rb | 4 ---- .../ui/base_system_registration_dialog.rb | 20 ++++++++++--------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/lib/registration/storage.rb b/src/lib/registration/storage.rb index e5b21115..94b5da16 100644 --- a/src/lib/registration/storage.rb +++ b/src/lib/registration/storage.rb @@ -51,13 +51,15 @@ class InstallationOptions include Singleton attr_accessor :install_updates, :email, :reg_code, :selected_addons, - :base_registered, :custom_url, :imported_cert_sha256_fingerprint, :product + :base_registered, :custom_url, :imported_cert_sha256_fingerprint, + :force_registration def initialize @email = "" @reg_code = "" @selected_addons = [] @base_registered = false + @force_registration = false end end diff --git a/src/lib/registration/sw_mgmt.rb b/src/lib/registration/sw_mgmt.rb index 544225d8..87b8f548 100644 --- a/src/lib/registration/sw_mgmt.rb +++ b/src/lib/registration/sw_mgmt.rb @@ -307,10 +307,6 @@ def self.installed_products products.map { |p| resolvable_to_h(p) } end - def self.base_installed_product - Y2Packager::Resolvable.find(kind: :product, status: :installed, category: "base").first - end - # convert a libzypp Product Hash to a SUSE::Connect::Remote::Product object # @param product [Hash] product Hash obtained from pkg-bindings # @return [SUSE::Connect::Remote::Product] the remote product diff --git a/src/lib/registration/ui/base_system_registration_dialog.rb b/src/lib/registration/ui/base_system_registration_dialog.rb index 0a34daa2..bf330738 100644 --- a/src/lib/registration/ui/base_system_registration_dialog.rb +++ b/src/lib/registration/ui/base_system_registration_dialog.rb @@ -320,8 +320,7 @@ def register_local_option # widget for skipping the registration # @return [Yast::Term] UI term def skip_option - # do not display it in an installed system or when already registered - return Empty() if Stage.normal || Registration.is_registered? || product_switched? + return Empty() if hide_skip_option? Left( RadioButton( Id(:skip_registration), @@ -332,13 +331,16 @@ def skip_option ) end - def product_switched? - installed_product = SwMgmt.base_installed_product - product = Storage::InstallationOptions.instance.product - - return false unless installed_product && product - - installed_product.name != product + # Whether skip option should be hidden + # + # Do not display it in an installed system or when already registered or when registration + # is mandatory. + # + # @return [Boolean] + def hide_skip_option? + Stage.normal || + Registration.is_registered? || + Storage::InstallationOptions.instance.force_registration end # part of the main dialog definition - the base product details From 644ed32c3bbefdf6bfd59b7312272dfaf5d4b2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Fri, 14 Oct 2022 12:59:51 +0100 Subject: [PATCH 13/22] Storage yaml product --- src/lib/registration/storage.rb | 2 +- src/lib/registration/sw_mgmt.rb | 12 ++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/lib/registration/storage.rb b/src/lib/registration/storage.rb index 94b5da16..8256f4f5 100644 --- a/src/lib/registration/storage.rb +++ b/src/lib/registration/storage.rb @@ -52,7 +52,7 @@ class InstallationOptions attr_accessor :install_updates, :email, :reg_code, :selected_addons, :base_registered, :custom_url, :imported_cert_sha256_fingerprint, - :force_registration + :yaml_product, :force_registration def initialize @email = "" diff --git a/src/lib/registration/sw_mgmt.rb b/src/lib/registration/sw_mgmt.rb index 87b8f548..102f0e6f 100644 --- a/src/lib/registration/sw_mgmt.rb +++ b/src/lib/registration/sw_mgmt.rb @@ -32,7 +32,6 @@ require "registration/url_helpers" require "registration/repo_state" require "registration/storage" -require "registration/yaml_products_reader" require "packager/product_patterns" require "y2packager/medium_type" @@ -202,7 +201,7 @@ def self.find_base_product return online_base_product if Stage.initial && Y2Packager::MediumType.online? - yaml_product = find_yaml_product + yaml_product = Storage::InstallationOptions.instance.yaml_product return yaml_product if yaml_product # use the selected product if a product has been already selected @@ -771,13 +770,6 @@ def self.target_distribution(destdir) target_distro end - def self.find_yaml_product - product_name = Storage::InstallationOptions.instance.product - return nil unless product_name - - YamlProductsReader.new.read.find { |p| p["name"] == product_name } - end - - private_class_method :init_target, :target_distribution, :find_yaml_product + private_class_method :init_target, :target_distribution end end From 374737b8e24e682cb36189af30b6cfde6350dd4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez?= Date: Fri, 14 Oct 2022 13:01:10 +0100 Subject: [PATCH 14/22] Use rpm_arch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ladislav Slezák --- src/lib/registration/yaml_products_reader.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/registration/yaml_products_reader.rb b/src/lib/registration/yaml_products_reader.rb index a4a67918..99babd6c 100644 --- a/src/lib/registration/yaml_products_reader.rb +++ b/src/lib/registration/yaml_products_reader.rb @@ -39,7 +39,7 @@ def yaml_exist? # @param product [Hash] # @return [Hash] new hash def expand_variables(product) - arch = Yast::Arch.architecture + arch = Yast::Arch.rpm_arch product.map do |key, val| val_s = val.to_s From 25d61c726d0503fd8288b71e52e3a7981bc553d0 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Oct 2022 09:59:55 +0200 Subject: [PATCH 15/22] simplify yaml file --- src/lib/registration/yaml_products_reader.rb | 12 +++++++++--- test/fixtures/wsl_products.yml | 16 ++++------------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/lib/registration/yaml_products_reader.rb b/src/lib/registration/yaml_products_reader.rb index 99babd6c..8f3b742f 100644 --- a/src/lib/registration/yaml_products_reader.rb +++ b/src/lib/registration/yaml_products_reader.rb @@ -20,7 +20,7 @@ def initialize(path = DEFAULT_PATH) def read return [] unless yaml_exist? - YAML.load_file(path).map { |p| expand_variables(p) } + YAML.load_file(path).map { |p| transform(p) } end private @@ -36,16 +36,22 @@ def yaml_exist? # For all values: # - convert them to String (to allow writing "15.4" as 15.4) # - replace $arch substrings with the architecture + # - replace version with version_version as registration expects + # - add arch key if not defined # @param product [Hash] # @return [Hash] new hash - def expand_variables(product) + def transform(product) arch = Yast::Arch.rpm_arch - product.map do |key, val| + res = product.map do |key, val| val_s = val.to_s val_s.gsub!("$arch", arch) [key, val_s] end.to_h + res["version_version"] ||= res["version"] + res["arch"] ||= arch + + res end end end diff --git a/test/fixtures/wsl_products.yml b/test/fixtures/wsl_products.yml index 8955ea18..0460a4d3 100644 --- a/test/fixtures/wsl_products.yml +++ b/test/fixtures/wsl_products.yml @@ -1,19 +1,11 @@ # TODO: references for this file, # describe what it is # https://jira.suse.com/browse/PED-1380 -- arch: "$arch" - display_name: "SUSE Linux Enterprise Desktop 15 SP4" +- display_name: "SUSE Linux Enterprise Desktop 15 SP4" name: "SLED" - product_line: "sled" register_target: "sle-15-$arch" - version: "15.4-0" - version_version: "15.4" -- arch: "$arch" - display_name: "SUSE Linux Enterprise Server 15 SP4" + version: 15.4 +- display_name: "SUSE Linux Enterprise Server 15 SP4" name: "SLES" - product_line: "sles" register_target: "sle-15-$arch" - version: "15.4-0" - # we will convert a YAML number to a string when reading - # because it is easy to forget the quotes - version_version: 15.4 + version: 15.4 From a3c1ae9d4dfe2fc3ce5beedde5530d0af9b2a151 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Oct 2022 10:02:18 +0200 Subject: [PATCH 16/22] add missing default --- test/fixtures/wsl_products.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/fixtures/wsl_products.yml b/test/fixtures/wsl_products.yml index 0460a4d3..7331afa7 100644 --- a/test/fixtures/wsl_products.yml +++ b/test/fixtures/wsl_products.yml @@ -9,3 +9,4 @@ name: "SLES" register_target: "sle-15-$arch" version: 15.4 + default: true From a681754b9a2f8839ade400257a06d13c06e9e4d7 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Oct 2022 16:48:59 +0200 Subject: [PATCH 17/22] add quotes and tests --- test/fixtures/wsl_products.yml | 4 +- .../registration/yaml_products_reader_test.rb | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 test/registration/yaml_products_reader_test.rb diff --git a/test/fixtures/wsl_products.yml b/test/fixtures/wsl_products.yml index 7331afa7..88efabff 100644 --- a/test/fixtures/wsl_products.yml +++ b/test/fixtures/wsl_products.yml @@ -4,9 +4,9 @@ - display_name: "SUSE Linux Enterprise Desktop 15 SP4" name: "SLED" register_target: "sle-15-$arch" - version: 15.4 + version: "15.4" - display_name: "SUSE Linux Enterprise Server 15 SP4" name: "SLES" register_target: "sle-15-$arch" - version: 15.4 + version: "15.4" default: true diff --git a/test/registration/yaml_products_reader_test.rb b/test/registration/yaml_products_reader_test.rb new file mode 100644 index 00000000..be953023 --- /dev/null +++ b/test/registration/yaml_products_reader_test.rb @@ -0,0 +1,43 @@ +# Copyright (c) [2022] SUSE LLC +# +# All Rights Reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of version 2 of the GNU General Public License as published +# by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, contact SUSE LLC. +# +# To contact SUSE LLC about this file by physical or electronic mail, you may +# find current contact information at www.suse.com. + +require_relative "../spec_helper" +require "registration/yaml_products_reader" + +describe Registration::YamlProductsReader do + subject { described_class.new(File.expand_path("#{__dir__}/../fixtures/wsl_products.yml")) } + + describe "#read" do + it "reads content of yaml file" do + expect(subject.read.first["name"]).to eq "SLED" + end + + it "adds arch key if not defined" do + expect(subject.read.first["arch"]).to eq Yast::Arch.rpm_arch + end + + it "converts version to version_version" do + expect(subject.read.first["version_version"]).to eq "15.4" + end + + it "expands properly arch variable" do + expect(subject.read.first["register_target"]).to eq "sle-15-#{Yast::Arch.rpm_arch}" + end + end +end From 3b82148ec2149fb398bf9e64511048ff467a810a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Tue, 18 Oct 2022 16:47:55 +0100 Subject: [PATCH 18/22] Add unit tests --- test/sw_mgmt_spec.rb | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/test/sw_mgmt_spec.rb b/test/sw_mgmt_spec.rb index 8249f099..7b199ec7 100644 --- a/test/sw_mgmt_spec.rb +++ b/test/sw_mgmt_spec.rb @@ -418,12 +418,30 @@ end context "in installed system" do - let(:products) { load_resolvable("products_legacy_installation.yml") } - it "returns installed products" do + before do allow(Yast::Stage).to receive(:initial).and_return(false) - expect(Y2Packager::Resolvable).to receive(:find).and_return(products) - # the SLES product in the list is installed - expect(subject.find_base_product["name"]).to eq(products[1].name) + allow(Y2Packager::Resolvable).to receive(:find).and_return(products) + allow(Registration::Storage::InstallationOptions.instance).to receive(:yaml_product) + .and_return(yaml_product) + end + + let(:products) { load_resolvable("products_legacy_installation.yml") } + + context "if a YAML product is selected" do + let(:yaml_product) { { "name" => "SLES", "version" => "15.4" } } + + it "returns the YAML product" do + expect(subject.find_base_product).to eq(yaml_product) + end + end + + context "if no YAML product is selected" do + let(:yaml_product) { nil } + + it "returns installed products" do + # the SLES product in the list is installed + expect(subject.find_base_product["name"]).to eq(products[1].name) + end end end From 8c53ac2c869ae6f3edb56a8e2b65618c3a43e262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Tue, 18 Oct 2022 16:48:10 +0100 Subject: [PATCH 19/22] Rubocop and license --- src/lib/registration/yaml_products_reader.rb | 27 ++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/lib/registration/yaml_products_reader.rb b/src/lib/registration/yaml_products_reader.rb index 8f3b742f..6ec070f0 100644 --- a/src/lib/registration/yaml_products_reader.rb +++ b/src/lib/registration/yaml_products_reader.rb @@ -1,5 +1,21 @@ -# allow to register against products defined in yaml. Very similar concept to online medium just this time -# for first boot purpose +# Copyright (c) [2022] SUSE LLC +# +# All Rights Reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of version 2 of the GNU General Public License as published +# by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, contact SUSE LLC. +# +# To contact SUSE LLC about this file by physical or electronic mail, you may +# find current contact information at www.suse.com. require "yaml" require "yast" @@ -7,8 +23,9 @@ module Registration Yast.import "Arch" - # Added for SLED registration on a WSL SLES image, see - # https://jira.suse.com/browse/PED-1380 + # Reads products defined by YAML file + # + # Added for SLED registration on a WSL SLES image (jsc#PED-1380). class YamlProductsReader attr_reader :path @@ -23,7 +40,7 @@ def read YAML.load_file(path).map { |p| transform(p) } end - private + private DEFAULT_PATH = "/etc/YaST2/products.yaml".freeze private_constant :DEFAULT_PATH From 691425deba963d6259f1a28ed75b1c9408552d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Wed, 19 Oct 2022 09:17:05 +0100 Subject: [PATCH 20/22] Changelog and version --- package/yast2-registration.changes | 9 +++++++++ package/yast2-registration.spec | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/package/yast2-registration.changes b/package/yast2-registration.changes index ed4d6351..388af2a1 100644 --- a/package/yast2-registration.changes +++ b/package/yast2-registration.changes @@ -1,3 +1,12 @@ +------------------------------------------------------------------- +Wed Oct 19 08:13:54 UTC 2022 - José Iván López González + +- Add reader for products defined in a YAML file. +- Allow forcing registration and configuring a YAML product as + installed product. +- Related to jsc#PED-1380 and jsc#PM-3439. +- 4.4.23 + ------------------------------------------------------------------- Tue Jun 7 12:37:04 UTC 2022 - Imobach Gonzalez Sosa diff --git a/package/yast2-registration.spec b/package/yast2-registration.spec index acc37a88..9eae6291 100644 --- a/package/yast2-registration.spec +++ b/package/yast2-registration.spec @@ -17,7 +17,7 @@ Name: yast2-registration -Version: 4.4.22 +Version: 4.4.23 Release: 0 Summary: YaST2 - Registration Module License: GPL-2.0-only From 222b051699e504b3b3bd8f34826b0b4048bd11e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Thu, 20 Oct 2022 14:27:52 +0100 Subject: [PATCH 21/22] Convert default to boolean --- src/lib/registration/yaml_products_reader.rb | 14 +++++++++----- test/registration/yaml_products_reader_test.rb | 6 ++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/lib/registration/yaml_products_reader.rb b/src/lib/registration/yaml_products_reader.rb index 6ec070f0..3d3c5608 100644 --- a/src/lib/registration/yaml_products_reader.rb +++ b/src/lib/registration/yaml_products_reader.rb @@ -51,12 +51,15 @@ def yaml_exist? end # For all values: - # - convert them to String (to allow writing "15.4" as 15.4) - # - replace $arch substrings with the architecture - # - replace version with version_version as registration expects - # - add arch key if not defined + # - converts them to String (to allow writing "15.4" as 15.4) + # - replaces $arch substring with the current architecture + # And also: + # - replaces version with version_version as registration expects + # - adds arch key if not defined + # - converts value of default key to boolean + # # @param product [Hash] - # @return [Hash] new hash + # @return [Hash] A new transformed hash def transform(product) arch = Yast::Arch.rpm_arch @@ -67,6 +70,7 @@ def transform(product) end.to_h res["version_version"] ||= res["version"] res["arch"] ||= arch + res["default"] = res["default"] == "true" res end diff --git a/test/registration/yaml_products_reader_test.rb b/test/registration/yaml_products_reader_test.rb index be953023..319b19c1 100644 --- a/test/registration/yaml_products_reader_test.rb +++ b/test/registration/yaml_products_reader_test.rb @@ -39,5 +39,11 @@ it "expands properly arch variable" do expect(subject.read.first["register_target"]).to eq "sle-15-#{Yast::Arch.rpm_arch}" end + + it "converts default to boolean" do + products = subject.read + expect(products[0]["default"]).to eq(false) + expect(products[1]["default"]).to eq(true) + end end end From 5fc95373082387ab907974e538eb87303258d54b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez?= Date: Thu, 20 Oct 2022 14:33:48 +0100 Subject: [PATCH 22/22] Use casecmp? Co-authored-by: Josef Reidinger --- src/lib/registration/yaml_products_reader.rb | 2 +- test/fixtures/wsl_products.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/registration/yaml_products_reader.rb b/src/lib/registration/yaml_products_reader.rb index 3d3c5608..d26e7694 100644 --- a/src/lib/registration/yaml_products_reader.rb +++ b/src/lib/registration/yaml_products_reader.rb @@ -70,7 +70,7 @@ def transform(product) end.to_h res["version_version"] ||= res["version"] res["arch"] ||= arch - res["default"] = res["default"] == "true" + res["default"] = res["default"]&.casecmp?("true") ? true : false res end diff --git a/test/fixtures/wsl_products.yml b/test/fixtures/wsl_products.yml index 88efabff..c8312b16 100644 --- a/test/fixtures/wsl_products.yml +++ b/test/fixtures/wsl_products.yml @@ -9,4 +9,4 @@ name: "SLES" register_target: "sle-15-$arch" version: "15.4" - default: true + default: True