Skip to content

Commit

Permalink
[service] Improve code for dynamically adding D-Bus interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Feb 20, 2024
1 parent 8e247ff commit 0c364dd
Show file tree
Hide file tree
Showing 27 changed files with 765 additions and 672 deletions.
61 changes: 6 additions & 55 deletions service/lib/agama/dbus/storage/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@

require "dbus"
require "agama/dbus/base_object"
require "agama/dbus/storage/interfaces/drive"
require "agama/dbus/storage/interfaces/raid"
require "agama/dbus/storage/interfaces/multipath"
require "agama/dbus/storage/interfaces/md"
require "agama/dbus/storage/interfaces/block"
require "agama/dbus/storage/interfaces/partition_table"
require "agama/dbus/storage/interfaces/filesystem"
require "agama/dbus/storage/interfaces/component"
require "agama/dbus/storage/interfaces/device"

module Agama
module DBus
Expand Down Expand Up @@ -78,56 +71,14 @@ def storage_device=(value)
# @return [DevicesTree]
attr_reader :tree

# Adds the required interfaces according to the storage object
def add_interfaces # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
interfaces = []
interfaces << Interfaces::Drive if drive?
interfaces << Interfaces::Raid if storage_device.is?(:dm_raid)
interfaces << Interfaces::Md if storage_device.is?(:md)
interfaces << Interfaces::Multipath if storage_device.is?(:multipath)
interfaces << Interfaces::Block if storage_device.is?(:blk_device)
interfaces << Interfaces::PartitionTable if partition_table?
interfaces << Interfaces::Filesystem if filesystem?
interfaces << Interfaces::Component if component?
# Adds the required interfaces according to the storage object.
def add_interfaces
interfaces = Interfaces::Device.constants
.map { |c| Interfaces::Device.const_get(c) }
.select { |c| c.is_a?(Module) && c.respond_to?(:apply?) && c.apply?(storage_device) }

interfaces.each { |i| singleton_class.include(i) }
end

# Whether the storage device is a drive
#
# Drive and disk device are very close concepts, but there are subtle differences. For
# example, a MD RAID is never considered as a drive.
#
# TODO: Revisit the defintion of drive. Maybe some MD devices could implement the drive
# interface if hwinfo provides useful information for them.
#
# @return [Boolean]
def drive?
storage_device.is?(:disk, :dm_raid, :multipath, :dasd) && storage_device.is?(:disk_device)
end

# Whether the storage device has a partition table
#
# @return [Boolean]
def partition_table?
storage_device.is?(:blk_device) &&
storage_device.respond_to?(:partition_table?) &&
storage_device.partition_table?
end

# Whether the storage device is formatted.
#
# @return [Boolean]
def filesystem?
storage_device.is?(:blk_device) && !storage_device.filesystem.nil?
end

# Whether the storage device is component of other devices.
#
# @return [Boolean]
def component?
storage_device.is?(:blk_device) && storage_device.component_of.any?
end
end
end
end
Expand Down
12 changes: 3 additions & 9 deletions service/lib/agama/dbus/storage/interfaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,13 @@
module Agama
module DBus
module Storage
# Module for storage specific D-Bus interfaces
# Module for D-Bus interfaces of storage.
module Interfaces
end
end
end
end

require "agama/dbus/storage/interfaces/drive"
require "agama/dbus/storage/interfaces/raid"
require "agama/dbus/storage/interfaces/multipath"
require "agama/dbus/storage/interfaces/md"
require "agama/dbus/storage/interfaces/block"
require "agama/dbus/storage/interfaces/partition_table"
require "agama/dbus/storage/interfaces/filesystem"
require "agama/dbus/storage/interfaces/component"
require "agama/dbus/storage/interfaces/dasd_manager"
require "agama/dbus/storage/interfaces/device"
require "agama/dbus/storage/interfaces/zfcp_manager"
104 changes: 0 additions & 104 deletions service/lib/agama/dbus/storage/interfaces/block.rb

This file was deleted.

89 changes: 0 additions & 89 deletions service/lib/agama/dbus/storage/interfaces/component.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) [2023-2024] SUSE LLC
# Copyright (c) [2024] SUSE LLC
#
# All Rights Reserved.
#
Expand All @@ -19,35 +19,23 @@
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "dbus"

module Agama
module DBus
module Storage
module Interfaces
# Interface for DM RAID devices
#
# @note This interface is intended to be included by {Device} if needed.
module Raid
RAID_INTERFACE = "org.opensuse.Agama.Storage1.RAID"
private_constant :RAID_INTERFACE

# Paths of the D-Bus objects representing the devices used by the DM RAID.
#
# @return [Array<String>]
def raid_devices
storage_device.parents.map { |p| tree.path_for(p) }
end

def self.included(base)
base.class_eval do
dbus_interface RAID_INTERFACE do
dbus_reader :raid_devices, "ao", dbus_name: "Devices"
end
end
end
# Module for D-Bus interfaces of a device.
module Device
end
end
end
end
end

require "agama/dbus/storage/interfaces/device/block"
require "agama/dbus/storage/interfaces/device/component"
require "agama/dbus/storage/interfaces/device/drive"
require "agama/dbus/storage/interfaces/device/filesystem"
require "agama/dbus/storage/interfaces/device/md"
require "agama/dbus/storage/interfaces/device/multipath"
require "agama/dbus/storage/interfaces/device/partition_table"
require "agama/dbus/storage/interfaces/device/raid"
Loading

0 comments on commit 0c364dd

Please sign in to comment.