Skip to content

Commit

Permalink
WIP: Use AgamaProposal for the initial storage proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
ancorgs committed Oct 25, 2024
1 parent fd2433e commit 8694e5f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 10 deletions.
112 changes: 112 additions & 0 deletions service/lib/agama/storage/config_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# 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"

module Agama
module Storage
# Reader for the initial storage config
class ConfigReader
# @param agama_config [Agama::Config]
def initialize(agama_config)
@agama_config = agama_config
end

# Generates a storage config from the Agama control file.
#
# @return [Storage::Config]
def read
ConfigConversions::FromJSON.new(json, default_paths: default_paths).convert
end

private

# @return [Agama::Config]
attr_reader :agama_config

def default_paths
@default_paths ||= agama_config.default_paths
end

def space_policy
@space_policy ||= agama_config.data.dig("storage", "space_policy")
end

def lvm?
return @lvm unless @lvm.nil?

@lvm = !!agama_config.data.dig("storage", "lvm")
end

def json
lvm? ? json_for_lvm : json_for_disk
end

def json_for_disk
{
drives: [
{ partitions: [partition_for_existing, volumes_generator].compact }
]
}
end

def json_for_lvm
{
drives: [
{
alias: "target",
partitions: [partition_for_existing].compact
}
],
volume_groups: [
{
name: "system",
physicalVolumes: [pvs_generator],
logicalVolumes: [volumes_generator]
}
]
}
end

def volumes_generator
{ generate: "default" }
end

def pvs_generator
{ generate: ["target"] }
end

def partition_for_existing
return unless ["delete", "resize"].include?(space_policy)

partition = { search: "*" }

if space_policy == "delete"
partition[:delete] = true
else
partition[:size] = { min: 0, max: "current" }
end

partition
end
end
end
end
6 changes: 3 additions & 3 deletions service/lib/agama/storage/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
require "agama/storage/callbacks"
require "agama/storage/iscsi/manager"
require "agama/storage/finisher"
require "agama/storage/proposal_settings_reader"
require "agama/storage/config_reader"
require "agama/issue"
require "agama/with_locale"
require "agama/with_issues"
Expand Down Expand Up @@ -216,8 +216,8 @@ def probe_devices

# Calculates the proposal using the settings from the config file.
def calculate_proposal
settings = ProposalSettingsReader.new(config).read
proposal.calculate_guided(settings)
settings = ConfigReader.new(config).read
proposal.calculate_agama(settings)
end

# Adds the required packages to the list of resolvables to install
Expand Down
12 changes: 5 additions & 7 deletions service/test/agama/storage/manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,16 @@

allow(proposal).to receive(:issues).and_return(proposal_issues)
allow(proposal).to receive(:available_devices).and_return(devices)
allow(proposal).to receive(:settings).and_return(settings)
allow(proposal).to receive(:calculate_guided)
allow(proposal).to receive(:calculate_agama)

allow(config).to receive(:pick_product)
allow(iscsi).to receive(:activate)
allow(y2storage_manager).to receive(:activate)
allow(iscsi).to receive(:probe)
allow(y2storage_manager).to receive(:probe)

allow_any_instance_of(Agama::Storage::ProposalSettingsReader).to receive(:read)
.and_return(config_settings)
allow_any_instance_of(Agama::Storage::ConfigReader).to receive(:read)
.and_return(storage_config)
end

let(:raw_devicegraph) do
Expand All @@ -186,8 +185,7 @@

let(:devices) { [disk1, disk2] }

let(:settings) { Agama::Storage::ProposalSettings.new }
let(:config_settings) { Agama::Storage::ProposalSettings.new }
let(:storage_config) { Agama::Storage::Config.new }

let(:disk1) { instance_double(Y2Storage::Disk, name: "/dev/vda") }
let(:disk2) { instance_double(Y2Storage::Disk, name: "/dev/vdb") }
Expand All @@ -206,7 +204,7 @@
end
expect(iscsi).to receive(:probe)
expect(y2storage_manager).to receive(:probe)
expect(proposal).to receive(:calculate_guided).with(config_settings)
expect(proposal).to receive(:calculate_agama).with(storage_config)
storage.probe
end

Expand Down

0 comments on commit 8694e5f

Please sign in to comment.