Skip to content

Commit

Permalink
WIP: conversion from y2storage
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Jul 21, 2023
1 parent 88e986a commit 102ab2d
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 2 deletions.
2 changes: 1 addition & 1 deletion service/lib/agama/storage/proposal_settings_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# find current contact information at www.suse.com.

require "agama/storage/proposal_settings_conversion/to_y2storage"
#require "agama/storage/proposal_settings_conversion/from_y2storage"
require "agama/storage/proposal_settings_conversion/from_y2storage"

module Agama
module Storage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# frozen_string_literal: true

# Copyright (c) [2023] 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/proposal_settings"
require "agama/storage/volume_conversion"

module Agama
module Storage
module ProposalSettingsConversion
# Utility class offering methods to convert between Y2Storage::ProposalSettings objects and
# Agama::ProposalSettings ones
# Internal class to generate a ProposalSettings object
class FromY2Storage
# Constructor
#
# @param y2storage_settings [Y2Storage::ProposalSettings]
# @param default_specs [Array<Y2Storage::VolumeSpecification>]
# @param devices [Array<Y2Storage::Planned::Device>]
def initialize(settings, config:)
@settings = settings
@config = config
end

# @return [ProposalSettings]
def convert
ProposalSettings.new.tap do |target|
boot_devices_conversion(target)
lvm_conversion(target)
encryption_conversion(target)
space_policy_conversion(target)
volumes_conversion(target)
end
end

private

attr_reader :settings

attr_reader :config

def boot_device_conversion(target)
target.boot_device = settings.root_device
end

def lvm_conversion(target)
target.lvm.enabled = settings.lvm
target.lvm.system_vg_devices = settings.candidate_devices if settings.lvm
end

def encryption_conversion(target)
target.encryption.password = settings.encryption_password
target.encryption.method = settings.encryption_method
target.encryption.pbkd_function = settings.encryption_pbkdf
end

def space_policy_conversion(target)
policy = settings.space_settings.strategy

target.space.policy = policy
target.space.actions = settings.space_settings.actions if policy == :bigger_resize
end

def volumes_conversion(target)
target.volumes = settings.volumes.map do |spec|
VolumeConversion.from_y2storage(spec, config: config)
end

fallbacks_conversion(target)
end

def fallbacks_conversion(target)
target.volumes.each do |volume|
volume.min_size_fallback_for = volumes_with_min_size_fallback(volume.mount_path)
volume.max_size_fallback_for = volumes_with_max_size_fallback(volume.mount_path)
end
end

def volumes_with_min_size_fallback(mount_path)
specs = settings.volumes.select { |s| s.min_size_fallback == mount_path }
specs.map(&:mount_point)
end

def volumes_with_max_size_fallback(mount_path)
specs = settings.volumes.select { |s| s.max_size_fallback == mount_path }
specs.map(&:mount_point)
end
end
end
end
end
2 changes: 1 addition & 1 deletion service/lib/agama/storage/volume_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

require "y2storage"
require "agama/storage/volume_conversion/to_y2storage"
# require "agama/storage/volume_conversion/from_y2storage"
require "agama/storage/volume_conversion/from_y2storage"

module Agama
module Storage
Expand Down
89 changes: 89 additions & 0 deletions service/lib/agama/storage/volume_conversion/from_y2storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# frozen_string_literal: true

# Copyright (c) [2023] 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 "y2storage/storage_manager"
require "agama/storage/volume_templates_builder"

module Agama
module Storage
module VolumeConversion
# Utility class offering methods to convert between Y2Storage::VolumeSpecification objects and
# Agama::Volume ones
# Internal class to generate a Agama volume
class FromY2Storage
# Constructor
#
# @param spec see {#spec}
# @param default_specs see #{default_specs}
# @param devices see {#devices}
def initialize(spec, config:)
@spec = spec
@config = config
end

# @see VolumeConverter#to_y2storage
def convert
volume = VolumeTemplatesBuilder.new_from_config(config).for(spec.mount_point)

volume.tap do |target|
target.device = spec.device
target.separate_vg_name = spec.separate_vg_name
target.mount_options = spec.mount_options
target.fs_type = spec.fs_type

sizes_conversion(target)
btrfs_conversion(target)
end
end

private

attr_reader :spec

attr_reader :config

def sizes_conversion(target)
target.auto_size = !(spec.ignore_fallback_sizes? && spec.ignore_fallback_sizes?)

planned = planned_device_for(spec.mount_point)
target.min_size = planned&.min || spec.min_size
target.max_size = planned&.max || spec.max_size
end

def btrfs_conversion(target)
target.btrfs.snapshots = spec.snapshots?
target.btrfs.subvolumes = spec.subvolumes
target.btrfs.default_subvolume = spec.btrfs_default_subvolume
target.btrfs.read_only = spec.btrfs_read_only
end

def planned_device_for(mount_path)
planned_devices = proposal&.planned_devices || []
planned_devices.find { |d| d.respond_to?(:mount_point) && d.mount_point == mount_path }
end

def proposal
Y2Storage::StorageManager.instance.proposal
end
end
end
end
end

0 comments on commit 102ab2d

Please sign in to comment.