Skip to content

Commit

Permalink
storage: create LVM config from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Sep 11, 2024
1 parent 9b81ed2 commit ed5805c
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 110 deletions.
11 changes: 8 additions & 3 deletions service/lib/agama/storage/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,22 @@ def calculate_default_sizes(volume_builder)

# return [Array<Configs::Filesystem>]
def filesystems
(drives + partitions).map(&:filesystem).compact
(drives + partitions + logical_volumes).map(&:filesystem).compact
end

# return [Array<Configs::Partition>]
def partitions
drives.flat_map(&:partitions)
end

# return [Array<Configs::Partitions>]
# return [Array<Configs::LogicalVolume>]
def logical_volumes
volume_groups.flat_map(&:logical_volumes)
end

# return [Array<Configs::Partition, Configs::LogicalVolume>]
def default_size_devices
partitions.select { |p| p.size&.default? }
(partitions + logical_volumes).select { |p| p.size&.default? }
end

# Min or max size that should be used for the given partition or logical volume
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
require "agama/storage/config_conversions/from_json_conversions/encryption"
require "agama/storage/config_conversions/from_json_conversions/filesystem"
require "agama/storage/config_conversions/from_json_conversions/filesystem_type"
require "agama/storage/config_conversions/from_json_conversions/logical_volume"
require "agama/storage/config_conversions/from_json_conversions/partition"
require "agama/storage/config_conversions/from_json_conversions/search"
require "agama/storage/config_conversions/from_json_conversions/size"
require "agama/storage/config_conversions/from_json_conversions/volume_group"

module Agama
module Storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require "agama/storage/config_conversions/from_json_conversions/base"
require "agama/storage/config_conversions/from_json_conversions/boot"
require "agama/storage/config_conversions/from_json_conversions/drive"
require "agama/storage/config_conversions/from_json_conversions/volume_group"
require "agama/storage/config"

module Agama
Expand Down Expand Up @@ -56,8 +57,9 @@ def convert(default = nil)
# @return [Hash]
def conversions(default)
{
boot: convert_boot(default.boot),
drives: convert_drives
boot: convert_boot(default.boot),
drives: convert_drives,
volume_groups: convert_volume_groups
}
end

Expand All @@ -83,6 +85,22 @@ def convert_drives
def convert_drive(drive_json)
FromJSONConversions::Drive.new(drive_json, config_builder: config_builder).convert
end

# @return [Array<Configs::VolumeGroup>, nil]
def convert_volume_groups
volume_groups_json = config_json[:volumeGroups]
return unless volume_groups_json

volume_groups_json.map { |v| convert_volume_group(v) }
end

# @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
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# frozen_string_literal: true

# Copyright (c) [2024] 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 "agama/storage/config_conversions/from_json_conversions/base"
require "agama/storage/config_conversions/from_json_conversions/with_encryption"
require "agama/storage/config_conversions/from_json_conversions/with_filesystem"
require "agama/storage/config_conversions/from_json_conversions/with_size"
require "agama/storage/configs/logical_volume"
require "y2storage/disk_size"

module Agama
module Storage
module ConfigConversions
module FromJSONConversions
# Logical volume conversion from JSON hash according to schema.
class LogicalVolume < Base
include WithEncryption
include WithFilesystem
include WithSize

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

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

private

# @return [Hash]
attr_reader :logical_volume_json

# @see Base#conversions
#
# @param default [Configs::LogicalVolume]
# @return [Hash]
def conversions(default)
{
alias: logical_volume_json[:alias],
encryption: convert_encryption(logical_volume_json, default: default.encryption),
filesystem: convert_filesystem(logical_volume_json, default: default.filesystem),
size: convert_size(logical_volume_json, default: default.size),
name: logical_volume_json[:name],
stripes: logical_volume_json[:stripes],
stripe_size: convert_stripe_size,
pool: logical_volume_json[:pool],
used_pool: logical_volume_json[:usedPool]
}
end

# @return [Y2Storage::DiskSize, nil]
def convert_stripe_size
value = logical_volume_json[:stripeSize]
return unless value

Y2Storage::DiskSize.new(value)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

# Copyright (c) [2024] 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 "agama/storage/config_conversions/from_json_conversions/base"
require "agama/storage/config_conversions/from_json_conversions/logical_volume"
require "agama/storage/configs/drive"

module Agama
module Storage
module ConfigConversions
module FromJSONConversions
# Volume group conversion from JSON hash according to schema.
class VolumeGroup < Base
# @param volume_group_json [Hash]
# @param config_builder [ConfigBuilder, nil]
def initialize(volume_group_json, config_builder: nil)
super(config_builder)
@volume_group_json = volume_group_json
end

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

private

# @return [Hash]
attr_reader :volume_group_json

# @see Base#conversions
#
# @param _default [Configs::VolumeGroup]
# @return [Hash]
def conversions(_default)
{
name: volume_group_json[:name],
extend_size: convert_extend_size,
physical_volumes: volume_group_json[:physicalVolumes],
logical_volumes: convert_logical_volumes
}
end

# @return [Y2Storage::DiskSize, nil]
def convert_extend_size
value = volume_group_json[:extendSize]
return unless value

Y2Storage::DiskSize.new(value)
end

# @return [Array<Configs::LogicalVolume>, nil]
def convert_logical_volumes
logical_volumes_json = volume_group_json[:logicalVolumes]
return unless logical_volumes_json

logical_volumes_json.map { |l| convert_logical_volume(l) }
end

# @param logical_volume_json [Hash]
# @return [Configs::LogicalVolume]
def convert_logical_volume(logical_volume_json)
FromJSONConversions::LogicalVolume
.new(logical_volume_json, config_builder: config_builder)
.convert
end
end
end
end
end
end
105 changes: 0 additions & 105 deletions service/lib/agama/storage/config_conversions/volume_group/from_json.rb

This file was deleted.

Loading

0 comments on commit ed5805c

Please sign in to comment.