Class: OsCtl::Lib::Zfs::DatasetTree

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/zfs/dataset_tree.rb

Overview

Encapsulates a tree of datasets and their properties

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil) ⇒ DatasetTree

Returns a new instance of DatasetTree.

Parameters:

  • name (String) (defaults to: nil)


14
15
16
17
18
# File 'lib/libosctl/zfs/dataset_tree.rb', line 14

def initialize(name: nil)
  @name = name
  @properties = {}
  @datasets = {}
end

Instance Attribute Details

#datasetsHash<String, Zfs::DatasetTree> (readonly)

Returns:



11
12
13
# File 'lib/libosctl/zfs/dataset_tree.rb', line 11

def datasets
  @datasets
end

#nameString? (readonly)

Returns full name.

Returns:

  • (String, nil)

    full name



5
6
7
# File 'lib/libosctl/zfs/dataset_tree.rb', line 5

def name
  @name
end

#propertiesHash<String, String> (readonly)

Returns:

  • (Hash<String, String>)


8
9
10
# File 'lib/libosctl/zfs/dataset_tree.rb', line 8

def properties
  @properties
end

Instance Method Details

#[](dataset) ⇒ Zfs::DatasetTree?

Lookup dataset in the subtree

Parameters:

  • dataset (String)

    full name

Returns:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/libosctl/zfs/dataset_tree.rb', line 32

def [](dataset)
  parts = dataset.split('/')
  tree = self

  parts.each do |name|
    tree = tree.datasets[name]
    return nil if tree.nil?
  end

  tree
end

#add_property(dataset, property, value) ⇒ Object

Add property to a dataset

Parameters:

  • dataset (String)
  • property (String)
  • value (String)


24
25
26
27
# File 'lib/libosctl/zfs/dataset_tree.rb', line 24

def add_property(dataset, property, value)
  parts = dataset.split('/')
  do_add_property([], parts.first, parts[1..], property, value)
end

#as_dataset(base: '') ⇒ Zfs::Dataset

Returns:



52
53
54
# File 'lib/libosctl/zfs/dataset_tree.rb', line 52

def as_dataset(base: '')
  Zfs::Dataset.new(name, base:, properties:)
end

#do_add_property(path, name, parts, property, value) ⇒ Object (protected)



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/libosctl/zfs/dataset_tree.rb', line 71

def do_add_property(path, name, parts, property, value)
  datasets[name] ||= self.class.new(name: (path + [name]).join('/'))

  if parts.empty?
    datasets[name].properties[property] = value
  else
    datasets[name].do_add_property(
      path + [name],
      parts.first,
      parts[1..],
      property,
      value
    )
  end
end

#each_tree_dataset {|tree| ... } ⇒ Object

Iterate over all datasets in the subtree

Yield Parameters:



46
47
48
49
# File 'lib/libosctl/zfs/dataset_tree.rb', line 46

def each_tree_dataset(&block)
  block.call(self)
  datasets.each_value { |ds| ds.each_tree_dataset(&block) }
end

Print the tree to the console



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/libosctl/zfs/dataset_tree.rb', line 57

def print(indent: 0)
  puts "#{' ' * indent}#{name}:"

  properties.each do |k, v|
    puts "#{' ' * indent}  #{k}=#{v}"
  end

  datasets.each_value do |tree|
    tree.print(indent: indent + 2)
  end
end