Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Nov 14, 2024
1 parent a4ed02e commit 1047992
Show file tree
Hide file tree
Showing 11 changed files with 458 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def conversions

# @return [Array<Hash>]
def convert_drives
found_drives.map { |d| ToModelConversions::Drive.new(d).convert }
valid_drives.map { |d| ToModelConversions::Drive.new(d).convert }
end

def found_drives
config.drives.reject { |d| d.found_device.nil? }
def valid_drives
config.drives.select(&:found_device)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,25 @@

require "agama/storage/config_conversions/to_model_conversions/base"
# require "agama/storage/config_conversions/to_json_conversions/with_encryption"
# require "agama/storage/config_conversions/to_json_conversions/with_filesystem"
# require "agama/storage/config_conversions/to_json_conversions/with_partitions"
require "agama/storage/config_conversions/to_model_conversions/with_filesystem"
require "agama/storage/config_conversions/to_model_conversions/with_space_policy"
require "agama/storage/config_conversions/to_model_conversions/with_partitions"
# require "agama/storage/config_conversions/to_json_conversions/with_ptable_type"
# require "agama/storage/config_conversions/to_json_conversions/with_search"
require "agama/storage/configs/drive"

module Agama
module Storage
module ConfigConversions
module ToJSONConversions
module ToModelConversions
# Drive conversion to JSON hash according to schema.
class Drive < Base
# include WithSearch
# include WithEncryption
# include WithFilesystem
include WithFilesystem
include WithSpacePolicy
# include WithPtableType
# include WithPartitions
include WithPartitions

# @see Base
def self.config_type
Expand All @@ -50,11 +52,12 @@ def self.config_type
def conversions
{
name: config.found_device&.name,
alias: config.alias
# encryption: convert_encryption,
# filesystem: convert_filesystem,
# ptableType: convert_ptable_type,
# partitions: convert_partitions
alias: config.alias,
mountPath: config.filesystem&.path,
filesystem: convert_filesystem,
spacePolicy: convert_space_policy,
ptableType: config.ptable_type&.to_s,
partitions: convert_partitions
}
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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/to_model_conversions/base"
require "agama/storage/configs/filesystem"

module Agama
module Storage
module ConfigConversions
module ToModelConversions
# Drive conversion to JSON hash according to schema.
class Filesystem < Base
# @see Base
def self.config_type
Configs::Filesystem
end

private

# @see Base#conversions
def conversions
{
# TODO
default: false,
type: config.type&.fs_type&.to_s,
snapshots: config.btrfs_snapshots?
}
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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/to_model_conversions/base"
# require "agama/storage/config_conversions/to_model_conversions/with_encryption"
require "agama/storage/config_conversions/to_model_conversions/with_filesystem"
require "agama/storage/configs/partition"

module Agama
module Storage
module ConfigConversions
module ToModelConversions
# Partition conversion to JSON hash according to schema.
class Partition < Base
# include WithEncryption
include WithFilesystem

# @see Base
def self.config_type
Configs::Partition
end

private

# @see Base#conversions
def conversions
{
name: config.found_device&.name,
alias: config.alias,
id: config.id&.to_s,
mountPath: config.filesystem&.path,
filesystem: convert_filesystem,
# size: convert_size,
delete: config.delete?,
deleteIfNeeded: config.delete_if_needed?,
# resize: convert_resize,
# resizeIfNeeded: convert_resize_if_needed
}
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 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
module ConfigConversions
module ToModelConversions
# Drive conversion to JSON hash according to schema.
class SpacePolicy
# TODO: make it work with volume groups and raids too?
#
# @param config [Configs::Drive]
def initialize(config)
@config = config
end

def convert
return "delete" if config.filesystem || delete_all_partition?
return "resize" if shrink_all_partition?
return "custom" if delete_partition? || resize_partition?

"keep"
end

private

attr_reader :config

def delete_all_partition?
config.partitions
.select(&:delete?)
.any? { |p| search_all?(p) }
end

def shrink_all_partition?
config.partitions.any? { |p| shrink_all?(p) }
end

def delete_partition?
config.partitions
.select(&:found_device)
.any? { |p| p.delete? || p.delete_if_needed? }
end

def resize_partition?
config.partitions
.select(&:found_device)
.any? { |p| !p.size.default? }
end

def search_all?(partition_config)
partition_config.search &&
partition_config.search.always_match? &&
partition_config.search.max.nil?
end

def shrink_all?(partition_config)
partition_config.size && partition_config.size.min.to_i == 0
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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/to_model_conversions/filesystem"

module Agama
module Storage
module ConfigConversions
module ToModelConversions
# Mixin for filesystem conversion to JSON.
module WithFilesystem
# @return [Hash, nil]
def convert_filesystem
filesystem = config.filesystem
return unless filesystem

ToModelConversions::Filesystem.new(filesystem).convert
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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/to_model_conversions/partition"

module Agama
module Storage
module ConfigConversions
module ToModelConversions
# Mixin for partitions conversion to JSON.
module WithPartitions
# @return [Array<Hash>]
def convert_partitions
valid_partitions
.map { |p| ToModelConversions::Partition.new(p).convert }
.compact
end

def valid_partitions
partitions_to_create + partitions_to_reuse
end

def partitions_to_create
config.partitions.reject(&:search) +
config.partitions.select { |p| p.search&.create_device? }
end

def partitions_to_reuse
config.partitions.select(&:found_device)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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/to_model_conversions/space_policy"

module Agama
module Storage
module ConfigConversions
module ToModelConversions
# Mixin for filesystem conversion to JSON.
module WithSpacePolicy
# @return [Hash, nil]
def convert_space_policy
return unless config.respond_to?(:partitions)

ToModelConversions::SpacePolicy.new(config).convert
end
end
end
end
end
end
Loading

0 comments on commit 1047992

Please sign in to comment.