Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Sep 25, 2024
1 parent a73acce commit 0a2922d
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
9 changes: 9 additions & 0 deletions service/lib/agama/storage/config_conversions/from_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require "agama/config"
require "agama/storage/config_builder"
require "agama/storage/config_conversions/from_json_conversions/config"
require "agama/storage/config_json_solver"

module Agama
module Storage
Expand All @@ -41,6 +42,14 @@ def initialize(config_json, product_config: nil)
# @return [Storage::Config]
def convert
# TODO: Raise error if config_json does not match the JSON schema.
# Implementation idea: ConfigJSONChecker class which reports issues if:
# * The JSON does not match the schema.
# * The JSON contains both "default" and "mandatory" for partitions or logical volumes.
# * The JSON contains "default" or "mandatory" more than once.
ConfigJSONSolver
.new(product_config)
.solve(config_json)

FromJSONConversions::Config
.new(config_json, config_builder: config_builder)
.convert
Expand Down
100 changes: 100 additions & 0 deletions service/lib/agama/storage/config_json_solver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# 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.

module Agama
module Storage
# Class for solving a storage config.
#
# It assigns proper devices and size values according to the product and the system.
class ConfigJSONSolver
# @param devicegraph [Y2Storage::Devicegraph]
# @param product_config [Agama::Config]
def initialize(product_config)
@product_config = product_config
end

# Solves all the search and size configs within a given config.
#
# @note The config object is modified.
#
# @param config [Config]
def solve(config_json)
@config_json = config_json

solve_partitions
solve_logical_volumes
end

private

# @return [Hash]
attr_reader :config_json

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

def solve_partitions
configs_with_partitions_keyword.each { |c| solve_partitions_keyword(c) }
end

def solve_logical_volumes
configs_with_logical_volumes_keyword.each { |c| solve_logical_volumes_keyword(c) }
end

def configs_with_partitions_keyword
configs = drive_configs
end

def explicit_paths
configs_with_filesystem
.select { |c| c.is_a?(Hash) }
.map { |c| c.dig(:filesystem, :path) }
.compact
end

def configs_with_filesystem
drive_configs + partition_configs + logical_volume_configs
end

def drive_configs
config_json[:drives] || []
end

def partition_configs
drive_configs = config_json[:drives]
return [] unless drive_configs

drive_configs
.flat_map { |c| c[:partitions] }
.compact
end

def logical_volume_configs
volume_group_configs = config_json[:volumeGroups]
return [] unless volume_group_configs

volume_group_configs
.flat_map { |c| c[:logicalVolumes] }
.compact
end
end
end
end

0 comments on commit 0a2922d

Please sign in to comment.