Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Sep 30, 2024
1 parent a4593ce commit 2b776f6
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 200 deletions.
29 changes: 13 additions & 16 deletions service/lib/agama/storage/config_conversions/from_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# find current contact information at www.suse.com.

require "agama/config"
require "agama/storage/config_builder"
require "agama/storage/config_conversions/from_json_conversions/config"
require "agama/storage/config_json_solver"

Expand All @@ -33,10 +32,10 @@ class FromJSON
#
# @param config_json [Hash]
# @param product_config [Agama::Config, nil]
def initialize(config_json, product_config: nil)
# Copies the JSON hash to avoid changes in the given parameter, see {ConfigJSONSolver}.
@config_json = json_dup(config_json)
@product_config = product_config || Agama::Config.new
def initialize(config_json, default_paths: [], mandatory_paths: [])
@config_json = config_json
@default_paths = default_paths
@mandatory_paths = mandatory_paths
end

# Performs the conversion from Hash according to the JSON schema.
Expand All @@ -48,22 +47,25 @@ def convert
# * The JSON does not match the schema.
# * The JSON contains more than one "generate" for partitions and logical volumes.
# * The JSON contains invalid aliases (now checked by ConfigChecker).

# Copies the JSON hash to avoid changes in the given config, see {ConfigJSONSolver}.
config_json = json_dup(self.config_json)

ConfigJSONSolver
.new(product_config)
.new(default_paths, mandatory_paths)
.solve(config_json)

FromJSONConversions::Config
.new(config_json, config_builder: config_builder)
.convert
FromJSONConversions::Config.new(config_json).convert
end

private

# @return [Hash]
attr_reader :config_json

# @return [Agama::Config]
attr_reader :product_config
attr_reader :default_paths

attr_reader :mandatory_paths

# Deep dup of the given JSON.
#
Expand All @@ -72,11 +74,6 @@ def convert
def json_dup(json)
Marshal.load(Marshal.dump(json))
end

# @return [ConfigBuilder]
def config_builder
@config_builder ||= ConfigBuilder.new(product_config)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,30 @@ module ConfigConversions
module FromJSONConversions
# Base class for conversions from JSON hash according to schema.
class Base
# @param config_builder [ConfigBuilder, nil]
def initialize(config_builder = nil)
@config_builder = config_builder
def initialize(config_json)
@config_json = config_json
end

# Performs the conversion from Hash according to the JSON schema.
#
# @param default [Object] A {Config} or any its configs from {Storage::Configs}.
# @param config [Object] A {Config} or any of its configs from {Storage::Configs}.
# @return [Object] A {Config} or any its configs from {Storage::Configs}.
def convert(default)
default.dup.tap do |config|
conversions(config).each do |property, value|
next if value.nil?
def convert(config)
conversions.each do |property, value|
next if value.nil?

config.public_send("#{property}=", value)
end
config.public_send("#{property}=", value)
end
end

private

# @return [ConfigBuilder, nil]
attr_reader :config_builder
attr_reader :config_json

# Values to apply to the config.
#
# @param _default [Object] A {Config} or any its configs from {Storage::Configs}.
# @return [Hash] e.g., { name: "/dev/vda" }.
def conversions(_default)
def conversions
{}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,14 @@ module ConfigConversions
module FromJSONConversions
# Boot conversion from JSON hash according to schema.
class Boot < Base
# @param boot_json [Hash]
def initialize(boot_json)
super()
@boot_json = boot_json
end

# @see Base#convert
#
# @param default [Configs::Boot, nil]
# @param boot_json [Hash]
# @return [Configs::Boot]
def convert(default = nil)
super(default || Configs::Boot.new)
def convert(boot_json)
@boot_json = boot_json

super(Configs::Boot.new)
end

private
Expand All @@ -49,9 +45,9 @@ def convert(default = nil)

# @see Base#conversions
#
# @param _default [Configs::Boot]
# @param _config [Configs::Boot]
# @return [Hash]
def conversions(_default)
def conversions(_config)
{
configure: boot_json[:configure],
device: boot_json[:device]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,14 @@ module ConfigConversions
module FromJSONConversions
# Btrfs conversion from JSON hash according to schema.
class Btrfs < Base
# @param btrfs_json [Hash]
def initialize(btrfs_json)
super()
@btrfs_json = btrfs_json
end

# @see Base#convert
#
# @param default [Configs::Btrfs, nil]
# @param btrfs_json [Hash]
# @return [Configs::Btrfs]
def convert(default = nil)
super(default || Configs::Btrfs.new)
def convert(btrfs_json)
@btrfs_json = btrfs_json

super(Configs::Btrfs.new)
end

private
Expand All @@ -49,9 +45,9 @@ def convert(default = nil)

# @see Base#conversions
#
# @param _default [Configs::Btrfs]
# @param _config [Configs::Btrfs]
# @return [Hash]
def conversions(_default)
def conversions(_config)
{
snapshots: btrfs_json[:snapshots]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,14 @@ module ConfigConversions
module FromJSONConversions
# Config conversion from JSON hash according to schema.
class Config < Base
# @param config_json [Hash]
# @param config_builder [ConfigBuilder, nil]
def initialize(config_json, config_builder: nil)
super(config_builder)
@config_json = config_json
end

# @see Base#convert
#
# @param default [Config, nil]
# @param config_json [Hash]
# @return [Config]
def convert(default = nil)
super(default || Storage::Config.new)
def convert(config_json)
@config_json = config_json

super(Storage::Config.new)
end

private
Expand All @@ -55,21 +50,20 @@ def convert(default = nil)
#
# @param default [Config]
# @return [Hash]
def conversions(default)
def conversions(config)
{
boot: convert_boot(default.boot),
boot: convert_boot,
drives: convert_drives,
volume_groups: convert_volume_groups
}
end

# @param default [Configs::Boot, nil]
# @return [Configs::Boot, nil]
def convert_boot(default = nil)
def convert_boot
boot_json = config_json[:boot]
return unless boot_json

FromJSONConversions::Boot.new(boot_json).convert(default)
FromJSONConversions::Boot.new.convert(boot_json)
end

# @return [Array<Configs::Drive>, nil]
Expand All @@ -83,7 +77,7 @@ def convert_drives
# @param drive_json [Hash]
# @return [Configs::Drive]
def convert_drive(drive_json)
FromJSONConversions::Drive.new(drive_json, config_builder: config_builder).convert
FromJSONConversions::Drive.new.convert(drive_json)
end

# @return [Array<Configs::VolumeGroup>, nil]
Expand All @@ -97,9 +91,7 @@ def convert_volume_groups
# @param volume_group_json [Hash]
# @return [Configs::VolumeGroup]
def convert_volume_group(volume_group_json)
FromJSONConversions::VolumeGroup
.new(volume_group_json, config_builder: config_builder)
.convert
FromJSONConversions::VolumeGroup.new.convert(volume_group_json)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,14 @@ class Drive < Base
include WithPtableType
include WithPartitions

# @param drive_json [Hash]
# @param config_builder [ConfigBuilder, nil]
def initialize(drive_json, config_builder: nil)
super(config_builder)
@drive_json = drive_json
end

# @see Base#convert
#
# @param default [Configs::Drive, nil]
# @param drive_json [Hash]
# @return [Configs::Drive]
def convert(default = nil)
super(default || Configs::Drive.new)
def convert(drive_json)
@drive_json = drive_json

super(Configs::Drive.new)
end

private
Expand All @@ -61,14 +56,14 @@ def convert(default = nil)

# @see Base#conversions
#
# @param default [Configs::Drive]
# @param _config [Configs::Drive]
# @return [Hash]
def conversions(default)
def conversions(_config)
{
search: convert_search(drive_json, default: default.search),
search: convert_search(drive_json),
alias: drive_json[:alias],
encryption: convert_encryption(drive_json, default: default.encryption),
filesystem: convert_filesystem(drive_json, default: default.filesystem),
encryption: convert_encryption(drive_json),
filesystem: convert_filesystem(drive_json),
ptable_type: convert_ptable_type(drive_json),
partitions: convert_partitions(drive_json)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,26 @@ module ConfigConversions
module FromJSONConversions
# Encryption conversion from JSON hash according to schema.
class Encryption < Base
# @param encryption_json [Hash, String]
# @param config_builder [ConfigBuilder, nil]
def initialize(encryption_json, config_builder: nil)
super(config_builder)
@encryption_json = encryption_json
end

# @see Base#convert
#
# @param default [Configs::Encryption, nil]
# @param encryption_json [Hash, String]
# @return [Configs::Encryption]
def convert(default = nil)
super(default || self.default)
def convert(encryption_json)
@encryption_json = encryption_json

super(Configs::Encryption.new)
end

private

# @return [Hash, String]
attr_reader :encryption_json

# @return [Configs::Encryption]
attr_reader :default_config

# @see Base#conversions
#
# @param _default [Configs::Encryption]
# @param _config [Configs::Encryption]
# @return [Hash]
def conversions(_default)
def conversions(_config)
return luks1_conversions if luks1?
return luks2_conversions if luks2?
return pervasive_luks2_conversions if pervasive_luks2?
Expand Down Expand Up @@ -159,15 +151,6 @@ def convert_label
def convert_pbkd_function
Y2Storage::PbkdFunction.find(encryption_json.dig(:luks2, :pbkdFunction))
end

# Default encryption config.
#
# @return [Configs::Encryption]
def default
return Configs::Encryption.new unless config_builder

config_builder.default_encryption
end
end
end
end
Expand Down
Loading

0 comments on commit 2b776f6

Please sign in to comment.