Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): initial version of config search #1560

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions rust/agama-lib/share/examples/storage.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
},
"drives": [
{
"search": {
"name": "/dev/vda"
},
"search": "/dev/vda",
"ptableType": "gpt",
"partitions": [
{
"search": { "name": "/dev/vda2" },
"id": "linux",
"size": "10 GiB",
"encryption": {
"luks1": {
"password": "notsecret"
Expand All @@ -31,12 +30,16 @@
}
},
{
"id": "linux",
"size": "10 GiB",
"search": {
"condition": {
"name": "/dev/vda2"
},
"ifNotFound": "skip"
},
"encryption": {
"luks2": {
"password": "notsecret",
"label": "data"
"label": "home"
}
},
"filesystem": {
Expand All @@ -45,7 +48,7 @@
}
},
{
"size": "2 GiB",
"search": {},
"encryption": "random_swap",
"filesystem": {
"type": "swap",
Expand All @@ -56,7 +59,10 @@
},
{
"search": {
"name": "/dev/vdb"
"condition": {
"name": "/dev/vda"
},
"ifNotFound": "error"
},
"filesystem": {
"type": "ext4",
Expand Down
36 changes: 31 additions & 5 deletions rust/agama-lib/share/profile.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -671,19 +671,45 @@
}
]
},
"search": {
"title": "Search options",
"searchName": {
"title": "Device name",
"type": "string",
"examples": ["/dev/vda", "/dev/disk/by-id/ata-WDC_WD3200AAKS-75L9"]
},
"searchByName": {
"title": "Search by name condition",
"type": "object",
"additionalProperties": false,
"required": ["name"],
"properties": {
"name": {
"title": "Device name",
"type": "string",
"examples": ["/dev/vda"]
"$ref": "#/$defs/searchName"
}
}
},
"search": {
Copy link
Contributor Author

@joseivanlopez joseivanlopez Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The document auto_storage suggests two possible syntaxes for the search condition:

Option A:

{
  "search": {
    "condition": { "property": "name", "value": "/dev/sda2" }
  }
}

Option B:

{
  "search": {
    "condition": { "name": "/dev/sda2" }
  }
}

The option B was chosen in this PR because the following reasons:

  • It is shorter and more expressive.

{"size": { "greater": "1 GiB" } } vs
{ "property": "size", "value": "1 GiB", "operator": "greater" }

  • It is easier to validate the possible properties and their values in the schema.

  • It is easier to define different sets of searches in the schema. For example, a drive could be searched by name, size and model, but a partition only by name and size.

Copy link
Contributor Author

@joseivanlopez joseivanlopez Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a third option to be discussed. It consists on directly use the condition without wrapping with a condition property:

{
  "search": {
    "name": "/dev/vda1",
    "ifNotFound": "skip"
  }
}

{
  "search": {
    "and": [
      { "size": { "greater": "10 GiB"} },
      { "size": { "less": "50 GiB"} }
    ]
  }
}

"anyOf": [
{
"$ref": "#/$defs/searchName"
},
{
"title": "Search options",
"type": "object",
"additionalProperties": false,
"properties": {
"condition": {
"$ref": "#/$defs/searchByName"
},
"ifNotFound": {
"title": "Not found action",
"description": "How to handle the section if the device is not found.",
"enum": ["skip", "error"],
"default": "error"
}
}
}
]
},
"boot": {
"title": "Boot options",
"description": "Allows configuring boot partitions automatically.",
Expand Down
5 changes: 2 additions & 3 deletions service/lib/agama/storage/config_conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@

require "agama/storage/config_conversions/block_device"
require "agama/storage/config_conversions/drive"
require "agama/storage/config_conversions/encrypt"
require "agama/storage/config_conversions/encryption"
require "agama/storage/config_conversions/filesystem"
require "agama/storage/config_conversions/format"
require "agama/storage/config_conversions/from_json"
require "agama/storage/config_conversions/mount"
require "agama/storage/config_conversions/partition"
require "agama/storage/config_conversions/partitionable"
require "agama/storage/config_conversions/search"
require "agama/storage/config_conversions/size"

module Agama
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ def initialize(blk_device_json, settings:, volume_builder:)

# Performs the conversion from Hash according to the JSON schema.
#
# @param config [#encrypt=, #format=, #mount=]
def convert(config)
config.encryption = convert_encrypt
config.filesystem = convert_filesystem
config
# @param default [Configs::Drive, Configs::Partition]
# @return [Configs::Drive, Configs::Partition]
def convert(default)
default.dup.tap do |config|
config.encryption = convert_encrypt
config.filesystem = convert_filesystem
end
end

private
Expand Down
30 changes: 26 additions & 4 deletions service/lib/agama/storage/config_conversions/drive/from_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# find current contact information at www.suse.com.

require "agama/storage/config_conversions/block_device/from_json"
require "agama/storage/config_conversions/search/from_json"
require "agama/storage/config_conversions/partitionable/from_json"
require "agama/storage/configs/drive"

Expand All @@ -42,11 +43,14 @@ def initialize(drive_json, settings:, volume_builder:)

# Performs the conversion from Hash according to the JSON schema.
#
# @param default [Configs::Drive, nil]
# @return [Configs::Drive]
def convert
Configs::Drive.new.tap do |config|
convert_block_device(config)
convert_partitionable(config)
def convert(default = nil)
default_config = default.dup || Configs::Drive.new

convert_drive(default_config).tap do |config|
search = convert_search(config.search)
config.search = search if search
end
end

Expand All @@ -61,6 +65,14 @@ def convert
# @return [VolumeTemplatesBuilder]
attr_reader :volume_builder

# @param config [Configs::Drive]
# @return [Configs::Drive]
def convert_drive(config)
convert_block_device(
convert_partitionable(config)
)
end

# @param config [Configs::Drive]
def convert_block_device(config)
converter = BlockDevice::FromJSON.new(drive_json,
Expand All @@ -76,6 +88,16 @@ def convert_partitionable(config)

converter.convert(config)
end

# @param config [Configs::Search]
# @return [Configs::Search, nil]
def convert_search(config)
search_json = drive_json[:search]
return unless search_json

converter = Search::FromJSON.new(search_json)
converter.convert(config)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def initialize(filesystem_json)
# Performs the conversion from Hash according to the JSON schema.
#
# @param default [Configs::Filesystem, nil]
# @return [Configs::Format]
# @return [Configs::Filesystem]
def convert(default = nil)
default_config = default.dup || Configs::Filesystem.new

Expand Down
54 changes: 37 additions & 17 deletions service/lib/agama/storage/config_conversions/partition/from_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# find current contact information at www.suse.com.

require "agama/storage/config_conversions/block_device/from_json"
require "agama/storage/config_conversions/search/from_json"
require "agama/storage/config_conversions/size/from_json"
require "agama/storage/configs/partition"
require "y2storage/partition_id"
Expand All @@ -43,12 +44,19 @@ def initialize(partition_json, settings:, volume_builder:)

# Performs the conversion from Hash according to the JSON schema.
#
# @param default [Configs::Partition, nil]
# @return [Configs::Partition]
def convert
Configs::Partition.new.tap do |config|
config.id = convert_id
config.size = convert_size
convert_block_device(config)
def convert(default = nil)
default_config = default.dup || Configs::Partition.new

convert_block_device(default_config).tap do |config|
search = convert_search(config.search)
id = convert_id
size = convert_size(config.size)

config.search = search if search
config.id = id if id
config.size = size if size
end
end

Expand All @@ -63,6 +71,25 @@ def convert
# @return [VolumeTemplatesBuilder]
attr_reader :volume_builder

# @param config [Configs::Partition]
# @return [Configs::Partition]
def convert_block_device(config)
converter = BlockDevice::FromJSON.new(partition_json,
settings: settings, volume_builder: volume_builder)

converter.convert(config)
end

# @param config [Configs::Search]
# @return [Configs::Search, nil]
def convert_search(config)
search_json = partition_json[:search]
return unless search_json

converter = Search::FromJSON.new(search_json)
converter.convert(config)
end

# @return [Y2Storage::PartitionId, nil]
def convert_id
value = partition_json[:id]
Expand All @@ -71,20 +98,13 @@ def convert_id
Y2Storage::PartitionId.find(value)
end

# @return [Configs::Size]
def convert_size
# @param config [Configs::Size]
# @return [Configs::Size, nil]
def convert_size(config)
size_json = partition_json[:size]
return Configs::Size.new unless size_json
return unless size_json

Size::FromJSON.new(size_json).convert
end

# @param config [Configs::Partition]
def convert_block_device(config)
converter = BlockDevice::FromJSON.new(partition_json,
settings: settings, volume_builder: volume_builder)

converter.convert(config)
Size::FromJSON.new(size_json).convert(config)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ def initialize(partitionable_json, settings:, volume_builder:)

# Performs the conversion from Hash according to the JSON schema.
#
# @param config [#ptable_type=, #partitions=]
def convert(config)
config.ptable_type = convert_ptable_type
config.partitions = convert_partitions
config
# @param default [Configs::Drive]
# @return [Configs::Drive]
def convert(default)
default.dup.tap do |config|
config.ptable_type = convert_ptable_type
config.partitions = convert_partitions
end
end

private
Expand Down
32 changes: 32 additions & 0 deletions service/lib/agama/storage/config_conversions/search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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/search/from_json"

module Agama
module Storage
module ConfigConversions
# Conversions for search.
module Search
end
end
end
end
Loading
Loading