Skip to content

Commit

Permalink
Merge 7cb89c5 into 32ce1b5
Browse files Browse the repository at this point in the history
  • Loading branch information
ancorgs committed Mar 21, 2016
2 parents 32ce1b5 + 7cb89c5 commit f49c29f
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 53 deletions.
104 changes: 104 additions & 0 deletions src/lib/storage/devices_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# encoding: utf-8

# Copyright (c) [2016] 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 Yast
module Storage
class DevicesList
include Enumerable

class << self
attr_accessor :device_class
attr_accessor :default_delegate

def list_of(klass)
self.device_class = klass
end

def by_default_delegate_to(list)
self.default_delegate = list
end
end

attr_reader :devicegraph

def initialize(devicegraph, list: nil)
@devicegraph = devicegraph
@list = list || full_list
end

def with(attrs = {})
new_list = list.select do |element|
attrs.all? { |attr, value| match?(element, attr, value) }
end
if block_given?
new_list.select!(&Proc.new)
end
self.class.new(devicegraph, list: new_list)
end

def each(&block)
@list.each(&block)
end

def dup
self.class.new(devicegraph, @list.dup)
end

# Returns true if the list contains no elements
#
# @return [Boolean]
def empty?
list.empty?
end

# Number of elements in the list
#
# @return [Fixnum]
def length
list.length
end
alias_method :size, :length

def method_missing(meth, *args, &block)
delegate_list = self.class.default_delegate
if delegate_list
delegate_list.send(meth, *args, &block)
else
super
end
end

protected

attr_accessor :list

def full_list
self.class.device_class.all(devicegraph).to_a
end

def match?(element, attr, value)
real_value = element.send(attr)
return true if real_value == value
value.is_a?(Array) && value.include?(real_value)
end
end
end
end
58 changes: 58 additions & 0 deletions src/lib/storage/disks_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# encoding: utf-8

# Copyright (c) [2016] 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 "storage"
require "storage/devices_list"
require "storage/partitions_list"
require "storage/filesystems_list"
#require "storage/free_disk_spaces_list"
require "storage/refinements/disk"

module Yast
module Storage
class DisksList < DevicesList
list_of ::Storage::Disk
by_default_delegate_to :partitions

using Refinements::Disk

def partitions
part_list = list.reduce([]) { |sum, disk| sum + disk.all_partitions }
PartitionsList.new(devicegraph, list: part_list)
end

def filesystems
fs_list = partitions.filesystems.to_a
# Add filesystems not included in #partitions (directly on disk)
list.each do |disk|
fs_list << disk.filesystem if !disk.partition_table && disk.filesystem
end
FilesystemsList.new(devicegraph, list: fs_list)
end

def free_disk_spaces
spaces_list = list.reduce([]) { |sum, disk| sum + disk.free_spaces }
FreeDiskSpacesList.new(devicegraph, list: spaces_list)
end
end
end
end
32 changes: 32 additions & 0 deletions src/lib/storage/filesystems_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# encoding: utf-8

# Copyright (c) [2016] 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 "storage"
require "storage/devices_list"

module Yast
module Storage
class FilesystemsList < DevicesList
list_of ::Storage::Filesystem
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,36 @@
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "storage"
require "storage/free_disk_space"
require "storage/disk_size"
require "storage/devices_list"
require "storage/refinements/disk"

module Yast
module Storage
# Prototype of a class to allow querying a devigraph for its elements
#
# The class is now used in the RSpec tests and in the proposal code but the
# API will likely change a lot depending on the discussion on
# https://lists.opensuse.org/yast-devel/2016-03/msg00053.html
# Thus the lack of documentation for the concrete methods
class DevicegraphQuery
class FreeDiskSpacesList < DevicesList
list_of FreeDiskSpace

using Refinements::Disk

# Free disk space below this size will be disregarded
TINY_FREE_CHUNK = DiskSize.MiB(30)

attr_reader :devicegraph

def initialize(devicegraph, disk_names: nil)
@devicegraph = devicegraph
@disk_names = disk_names
end

def disks
if @disk_names
@disk_names.map { |disk_name| ::Storage::Disk.find(@devicegraph, disk_name) }
else
devicegraph.all_disks.to_a
end
def useful
new_list = list.select { |space| space.size >= TINY_FREE_CHUNK }
self.class.new(devicegraph, list: new_list)
end

def available_size
useful_free_spaces.map(&:size).reduce(DiskSize.zero, :+)
def disk_size
list.map(&:size).reduce(DiskSize.zero, :+)
end

def useful_free_spaces
free_spaces.select { |space| space.size >= TINY_FREE_CHUNK }
end

def free_spaces
protected

def full_list
disks = devicegraph.all_disks.to_a
disks.reduce([]) { |sum, disk| sum + disk.free_spaces }
end

def partitions
disks.reduce([]) { |sum, disk| sum + disk.all_partitions }
end
end
end
end
50 changes: 50 additions & 0 deletions src/lib/storage/partitions_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# encoding: utf-8

# Copyright (c) [2016] 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 "storage"
require "storage/devices_list"
require "storage/filesystems_list"

module Yast
module Storage
class PartitionsList < DevicesList

list_of ::Storage::Partition
by_default_delegate_to :filesystems

def filesystems
FilesystemsList.new(devicegraph, list: list.map(&:filesystem).compact)
end

protected

def full_list
# There is no ::Storage::Partition.all
devicegraph.all_disks.to_a.reduce([]) do |sum, disk|
if disk.partition_table
sum + disk.partition_table.partitions.to_a
end
end
end
end
end
end
13 changes: 7 additions & 6 deletions src/lib/storage/proposal/partition_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
require "storage/planned_volumes_list"
require "storage/disk_size"
require "storage/refinements/devicegraph"
require "storage/devicegraph_query"
require "storage/refinements/devicegraph_lists"

module Yast
module Storage
Expand All @@ -34,6 +34,7 @@ class Proposal
# SpaceMaker.
class PartitionCreator
using Refinements::Devicegraph
using Refinements::DevicegraphLists
include Yast::Logger

attr_accessor :settings
Expand Down Expand Up @@ -84,15 +85,15 @@ def create_partitions(volumes, target_size)
# @return [DiskSize] sum
#
def total_free_size
devgraph_query.available_size
free_spaces.disk_size
end

# List of free spaces in the devicegraph
#
# @return [Array<FreeDiskSpace>]
#
def free_spaces
devgraph_query.useful_free_spaces
candidate_disks.free_disk_spaces.useful
end

# @return [Array<String>]
Expand All @@ -102,9 +103,9 @@ def candidate_disk_names

# Query in the target devicegraph restricted to the candidate disks
#
# @return [DevicegraphQuery]
def devgraph_query
@devgraph_query ||= DevicegraphQuery.new(devicegraph, disk_names: candidate_disk_names)
# @return [DisksList]
def candidate_disks
@candidate_disks ||= devicegraph.disks.with(name: candidate_disk_names)
end

# Create volumes on LVM.
Expand Down

0 comments on commit f49c29f

Please sign in to comment.