Class: OsCtl::Cli::ZfsProperties

Inherits:
Object
  • Object
show all
Includes:
Lib::Utils::Humanize, Lib::Utils::Log, Lib::Utils::System
Defined in:
lib/osctl/cli/zfs_properties.rb

Constant Summary collapse

ABBREVIATIONS =
{
  'avail' => 'available',
  'compress' => 'compression',
  'dnsize' => 'dnodesize',
  'lrefer' => 'logicalreferenced',
  'lused' => 'logicalused',
  'rdonly' => 'readonly',
  'recsize' => 'recordsize',
  'refer' => 'referenced',
  'refreserv' => 'refreservation',
  'reserv' => 'reservation',
  'volsize' => 'volblocksize'
}.freeze

Instance Method Summary collapse

Instance Method Details

#add_container_values(data, props, precise: false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/osctl/cli/zfs_properties.rb', line 48

def add_container_values(data, props, precise: false)
  zfs_props = props.select { |v| is_prop?(v) }.map { |v| name_to_zfs(v) }
  return if zfs_props.empty?

  if data.is_a?(::Hash)
    index = { data[:dataset] => data }
  elsif data.is_a?(::Array)
    index = data.to_h { |ct| [ct[:dataset], ct] }
  end

  add_property_values(index, zfs_props, precise)
end

#add_property_values(index, zfs_props, precise) ⇒ Object (protected)



63
64
65
66
67
68
69
70
71
72
# File 'lib/osctl/cli/zfs_properties.rb', line 63

def add_property_values(index, zfs_props, precise)
  reader = OsCtl::Lib::Zfs::PropertyReader.new
  tree = reader.read(index.keys, zfs_props)

  tree.each_tree_dataset do |ds|
    ds.properties.each do |k, v|
      index[ds.name][:"#{name_to_cli(k)}"] = prop_value(k, v, precise)
    end
  end
end

#is_prop?(v) ⇒ Boolean (protected)

Returns:

  • (Boolean)


82
83
84
# File 'lib/osctl/cli/zfs_properties.rb', line 82

def is_prop?(v)
  v.start_with?('zfs.')
end

#list_property_namesObject



23
24
25
26
27
28
29
30
# File 'lib/osctl/cli/zfs_properties.rb', line 23

def list_property_names
  zpools = `zpool list -H -o name`.strip.split("\n")
  return [] if zpools.empty?

  zfs(:get, '-H -o property all', zpools.first).output.split.map do |v|
    name_to_cli(v)
  end
end

#name_to_cli(v) ⇒ Object (protected)



78
79
80
# File 'lib/osctl/cli/zfs_properties.rb', line 78

def name_to_cli(v)
  "zfs.#{v}"
end

#name_to_zfs(v) ⇒ Object (protected)



74
75
76
# File 'lib/osctl/cli/zfs_properties.rb', line 74

def name_to_zfs(v)
  v[4..]
end

#prop_value(prop, v, precise) ⇒ Object (protected)



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/osctl/cli/zfs_properties.rb', line 86

def prop_value(prop, v, precise)
  case prop
  # Timestamp
  when 'creation'
    i = v.to_i
    OsCtl::Lib::Cli::Presentable.new(
      i,
      formatted: precise ? nil : Time.at(i).strftime('%Y-%m-%d %H:%M:%S %Z')
    )

  # Data units
  when 'avail', 'available', 'logicalreferenced', 'logicalused', 'quota',
       'recordsize', 'referenced', 'refquota', 'refreservation',
       'reservation', 'used', 'usedbychildren', 'usedbydataset',
       'usedbyrefreservation', 'usedbysnapshots', 'volblocksize', 'written'
    i = v.to_i
    OsCtl::Lib::Cli::Presentable.new(i, formatted: precise ? nil : humanize_data(i))

  # Other
  else
    v
  end
end

#validate_property_names(props) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/osctl/cli/zfs_properties.rb', line 32

def validate_property_names(props)
  props.map do |v|
    next(v) unless is_prop?(v)

    n = name_to_zfs(v)
    real_name =
      if ABBREVIATIONS.has_key?(n)
        ABBREVIATIONS[n]
      else
        n
      end

    name_to_cli(real_name).to_sym
  end
end