Skip to content

Commit

Permalink
[service] Export space actions on D-Bus
Browse files Browse the repository at this point in the history
- Moreover, the list of actions is always recovered from Y2Storage instead of
  remembering the actions passed in the settings to calculate a proposal.
  • Loading branch information
joseivanlopez committed Feb 7, 2024
1 parent f3aadda commit fd4d7d2
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 31 deletions.
11 changes: 10 additions & 1 deletion service/lib/agama/dbus/storage/proposal.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) [2022-2023] SUSE LLC
# Copyright (c) [2022-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -58,6 +58,8 @@ def initialize(backend, logger)

dbus_reader :space_policy, "s"

dbus_reader :space_actions, "aa{sv}"

dbus_reader :volumes, "aa{sv}"

dbus_reader :actions, "aa{sv}"
Expand Down Expand Up @@ -109,6 +111,13 @@ def space_policy
dbus_settings.fetch("SpacePolicy", "")
end

# Space actions
#
# @return [Array<Hash>]
def space_actions
dbus_settings.fetch("SpaceActions", [])
end

# Volumes used to calculate the storage proposal
#
# @return [Array<Hash>]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) [2023] SUSE LLC
# Copyright (c) [2023-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -130,9 +130,11 @@ def space_policy_conversion(target, value)
end

# @param target [Agama::Storage::ProposalSettings]
# @param value [Hash]
# @param value [Array<Hash>]
def space_actions_conversion(target, value)
target.space.actions = value
target.space.actions = value.each_with_object({}) do |v, result|
result[v["Device"]] = v["Action"].to_sym
end
end

# @param target [Agama::Storage::ProposalSettings]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) [2023] SUSE LLC
# Copyright (c) [2023-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -35,7 +35,7 @@ def initialize(settings)
# Performs the conversion to D-Bus format.
#
# @return [Hash]
def convert # rubocop:disable Metrics/AbcSize
def convert
{
"BootDevice" => settings.boot_device.to_s,
"LVM" => settings.lvm.enabled?,
Expand All @@ -44,7 +44,7 @@ def convert # rubocop:disable Metrics/AbcSize
"EncryptionMethod" => settings.encryption.method.id.to_s,
"EncryptionPBKDFunction" => settings.encryption.pbkd_function&.value || "",
"SpacePolicy" => settings.space.policy.to_s,
"SpaceActions" => settings.space.actions,
"SpaceActions" => space_actions_conversion,
"Volumes" => settings.volumes.map { |v| VolumeConversion.to_dbus(v) }
}
end
Expand All @@ -53,6 +53,13 @@ def convert # rubocop:disable Metrics/AbcSize

# @return [Agama::Storage::ProposalSettings]
attr_reader :settings

# @return [Array<Hash>]
def space_actions_conversion
settings.space.actions.each_with_object([]) do |(device, action), actions|
actions << { "Device" => device, "Action" => action.to_s }
end
end
end
end
end
Expand Down
7 changes: 3 additions & 4 deletions service/lib/agama/storage/proposal.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) [2022-2023] SUSE LLC
# Copyright (c) [2022-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -85,10 +85,9 @@ def settings
proposal.settings,
config: config
).tap do |settings|
# FIXME: The conversion from Y2Storage cannot infer the space policy. Copying space
# settings from the original settings.
# The conversion from Y2Storage cannot infer the space policy. Copying space policy from
# the original settings.
settings.space.policy = original_settings.space.policy
settings.space.actions = original_settings.space.actions
# FIXME: The conversion from Y2Storage cannot reliably infer the system VG devices in all
# cases. Copying system VG devices from the original settings.
settings.lvm.system_vg_devices = original_settings.lvm.system_vg_devices
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) [2023] SUSE LLC
# Copyright (c) [2023-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -82,9 +82,10 @@ def encryption_conversion(target)
target.encryption.pbkd_function = settings.encryption_pbkdf
end

# @note The space policy cannot be inferred from Y2Storage settings.
# @param target [Agama::Storage::ProposalSettings]
def space_settings_conversion(target)
# FIXME: No way to infer space settings from Y2Storage.
target.space.actions = settings.space_settings.actions
end

# @param target [Agama::Storage::ProposalSettings]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def find_max_size_fallback(mount_path)

# All block devices affected by the space policy.
#
# This includes the partitions from the root device, the candidate devices and from the
# This includes the partitions from the boot device, the candidate devices and from the
# devices directly assigned to a volume as target device. If a device is not partitioned,
# then the device itself is included.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) [2023] SUSE LLC
# Copyright (c) [2023-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -69,7 +69,16 @@
"EncryptionMethod" => "luks1",
"EncryptionPBKDFunction" => "pbkdf2",
"SpacePolicy" => "custom",
"SpaceActions" => { "/dev/sda" => "force_delete" },
"SpaceActions" => [
{
"Device" => "/dev/sda",
"Action" => "force_delete"
},
{
"Device" => "/dev/sdb1",
"Action" => "resize"
}
],
"Volumes" => [
{ "MountPath" => "/" },
{ "MountPath" => "/test" }
Expand All @@ -87,7 +96,9 @@
expect(settings.encryption.method).to eq(Y2Storage::EncryptionMethod::LUKS1)
expect(settings.encryption.pbkd_function).to eq(Y2Storage::PbkdFunction::PBKDF2)
expect(settings.space.policy).to eq(:custom)
expect(settings.space.actions).to eq({ "/dev/sda" => "force_delete" })
expect(settings.space.actions).to eq({
"/dev/sda" => :force_delete, "/dev/sdb1" => :resize
})
expect(settings.volumes.map(&:mount_path)).to contain_exactly("/", "/test")
end

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

# Copyright (c) [2023] SUSE LLC
# Copyright (c) [2024] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -38,7 +38,7 @@
settings.encryption.method = Y2Storage::EncryptionMethod::LUKS2
settings.encryption.pbkd_function = Y2Storage::PbkdFunction::ARGON2ID
settings.space.policy = :custom
settings.space.actions = { "/dev/sda" => :force_delete }
settings.space.actions = { "/dev/sda" => :force_delete, "/dev/sdb1" => "resize" }
settings.volumes = [Agama::Storage::Volume.new("/test")]
end
end
Expand All @@ -53,7 +53,7 @@
"EncryptionMethod" => "luks2",
"EncryptionPBKDFunction" => "pbkdf2",
"SpacePolicy" => "keep",
"SpaceActions" => {},
"SpaceActions" => [],
"Volumes" => []
)

Expand All @@ -65,7 +65,16 @@
"EncryptionMethod" => "luks2",
"EncryptionPBKDFunction" => "argon2id",
"SpacePolicy" => "custom",
"SpaceActions" => { "/dev/sda" => :force_delete },
"SpaceActions" => [
{
"Device" => "/dev/sda",
"Action" => "force_delete"
},
{
"Device" => "/dev/sdb1",
"Action" => "resize"
}
],
"Volumes" => [
{
"MountPath" => "/test",
Expand Down
30 changes: 29 additions & 1 deletion service/test/agama/dbus/storage/proposal_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) [2022-2023] SUSE LLC
# Copyright (c) [2022-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -181,6 +181,34 @@
end
end

describe "#space_actions" do
context "if a proposal has not been calculated yet" do
let(:settings) { nil }

it "returns an empty list" do
expect(subject.space_actions).to eq([])
end
end

context "if a proposal has been calculated" do
let(:settings) do
Agama::Storage::ProposalSettings.new.tap do |settings|
settings.space.actions = {
"/dev/vda1" => :force_delete,
"/dev/vda2" => :resize
}
end
end

it "return a list with a hash for each action" do
expect(subject.space_actions).to contain_exactly(
{ "Device" => "/dev/vda1", "Action" => "force_delete" },
{ "Device" => "/dev/vda2", "Action" => "resize" }
)
end
end
end

describe "#volumes" do
let(:settings) do
Agama::Storage::ProposalSettings.new.tap { |s| s.volumes = calculated_volumes }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) [2023] SUSE LLC
# Copyright (c) [2023-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -38,6 +38,10 @@
settings.encryption_password = "notsecret"
settings.encryption_method = Y2Storage::EncryptionMethod::LUKS2
settings.encryption_pbkdf = Y2Storage::PbkdFunction::ARGON2ID
settings.space_settings.actions = {
"/dev/sda" => :force_delete,
"/dev/sdb1" => :resize
}
settings.volumes = []
end
end
Expand All @@ -57,6 +61,9 @@
method: Y2Storage::EncryptionMethod::LUKS2,
pbkd_function: Y2Storage::PbkdFunction::ARGON2ID
),
space: an_object_having_attributes(
actions: { "/dev/sda" => :force_delete, "/dev/sdb1" => :resize }
),
volumes: []
)
end
Expand Down
13 changes: 5 additions & 8 deletions service/test/agama/storage/proposal_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) [2022-2023] SUSE LLC
# Copyright (c) [2022-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -155,13 +155,10 @@
volumes: contain_exactly(
an_object_having_attributes(mount_path: "/")
),
# Checking space settings explicitly here because the settings converter cannot infer the
# space settings from the Y2Storage settings. The space settings are directly recovered
# from the original settings passed to #calculate.
space: an_object_having_attributes(
policy: :custom,
actions: { "/dev/sda" => :force_delete }
)
# Checking space policy explicitly here because the settings converter cannot infer the
# space policy from the Y2Storage settings. The space policy is directly recovered from
# the original settings passed to #calculate.
space: an_object_having_attributes(policy: :custom)
)
end

Expand Down

0 comments on commit fd4d7d2

Please sign in to comment.