Skip to content

Commit

Permalink
Merge pull request #581 from yast/wsl_product_selection
Browse files Browse the repository at this point in the history
YAML products reader
  • Loading branch information
joseivanlopez authored Oct 21, 2022
2 parents 77f0079 + 5fc9537 commit 428c0c0
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 12 deletions.
9 changes: 9 additions & 0 deletions package/yast2-registration.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
-------------------------------------------------------------------
Wed Oct 19 08:13:54 UTC 2022 - José Iván López González <jlopez@suse.com>

- 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 <igonzalezsosa@suse.com>

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-registration.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/lib/registration/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 15 additions & 3 deletions src/lib/registration/sw_mgmt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions src/lib/registration/ui/base_system_registration_dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down
78 changes: 78 additions & 0 deletions src/lib/registration/yaml_products_reader.rb
Original file line number Diff line number Diff line change
@@ -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<Hash>]
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
12 changes: 12 additions & 0 deletions test/fixtures/wsl_products.yml
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions test/registration/yaml_products_reader_test.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 23 additions & 5 deletions test/sw_mgmt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 428c0c0

Please sign in to comment.