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 diff --git a/src/lib/registration/storage.rb b/src/lib/registration/storage.rb index 49c7d857..8256f4f5 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 + :base_registered, :custom_url, :imported_cert_sha256_fingerprint, + :yaml_product, :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 06c1642a..102f0e6f 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/storage" require "packager/product_patterns" require "y2packager/medium_type" @@ -62,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 @@ -192,6 +201,9 @@ def self.find_base_product return online_base_product if Stage.initial && Y2Packager::MediumType.online? + yaml_product = Storage::InstallationOptions.instance.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 diff --git a/src/lib/registration/ui/base_system_registration_dialog.rb b/src/lib/registration/ui/base_system_registration_dialog.rb index 97cc84fc..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? + return Empty() if hide_skip_option? Left( RadioButton( Id(:skip_registration), @@ -332,6 +331,18 @@ def skip_option ) end + # 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 # @return [Yast::Term] UI term def product_details_widgets diff --git a/src/lib/registration/yaml_products_reader.rb b/src/lib/registration/yaml_products_reader.rb new file mode 100644 index 00000000..d26e7694 --- /dev/null +++ b/src/lib/registration/yaml_products_reader.rb @@ -0,0 +1,78 @@ +# 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" + +module Registration + Yast.import "Arch" + + # Reads products defined by YAML file + # + # Added for SLED registration on a WSL SLES image (jsc#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| transform(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: + # - 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] A new transformed hash + def transform(product) + arch = Yast::Arch.rpm_arch + + 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["default"] = res["default"]&.casecmp?("true") ? true : false + + res + end + end +end diff --git a/test/fixtures/wsl_products.yml b/test/fixtures/wsl_products.yml new file mode 100644 index 00000000..c8312b16 --- /dev/null +++ b/test/fixtures/wsl_products.yml @@ -0,0 +1,12 @@ +# TODO: references for this file, +# describe what it is +# https://jira.suse.com/browse/PED-1380 +- display_name: "SUSE Linux Enterprise Desktop 15 SP4" + name: "SLED" + register_target: "sle-15-$arch" + version: "15.4" +- display_name: "SUSE Linux Enterprise Server 15 SP4" + name: "SLES" + register_target: "sle-15-$arch" + 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..319b19c1 --- /dev/null +++ b/test/registration/yaml_products_reader_test.rb @@ -0,0 +1,49 @@ +# 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 + + 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 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