Class: Mool::Disk

Inherits:
Base
  • Object
show all
Defined in:
lib/mool/disk.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attrs

Constructor Details

#initialize(dev_name) ⇒ Disk

Returns a new instance of Disk.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mool/disk.rb', line 19

def initialize(dev_name)
  @path = nil

  Mool::Command.dev_block_command.each do |entry|
    logical_name = Mool::Command.logical_name_command(entry)
    next unless Mool::Command.dev_name_command(entry).include?(dev_name) ||
                Mool::Command.uevent_command(entry).include?(dev_name) ||
                (logical_name.present? && logical_name.include?(dev_name))
    @path = entry
    break
  end

  raise "Does't exist #{dev_name}!" if @path.nil?
  read_uevent
  logical_name
  swap
  capacity
  @unity = Mool::BYTES
  mounting_point
  file_system
end

Instance Attribute Details

#block_freeObject

Returns the value of attribute block_free.



3
4
5
# File 'lib/mool/disk.rb', line 3

def block_free
  @block_free
end

#block_usedObject

Returns the value of attribute block_used.



3
4
5
# File 'lib/mool/disk.rb', line 3

def block_used
  @block_used
end

#devnameObject

Returns the value of attribute devname.



3
4
5
# File 'lib/mool/disk.rb', line 3

def devname
  @devname
end

#devtypeObject

Returns the value of attribute devtype.



3
4
5
# File 'lib/mool/disk.rb', line 3

def devtype
  @devtype
end

#file_systemObject

Returns the value of attribute file_system.



3
4
5
# File 'lib/mool/disk.rb', line 3

def file_system
  @file_system
end

#majorObject

Returns the value of attribute major.



3
4
5
# File 'lib/mool/disk.rb', line 3

def major
  @major
end

#minorObject

Returns the value of attribute minor.



3
4
5
# File 'lib/mool/disk.rb', line 3

def minor
  @minor
end

#mount_pointObject

Returns the value of attribute mount_point.



3
4
5
# File 'lib/mool/disk.rb', line 3

def mount_point
  @mount_point
end

#partitionsObject

Returns the value of attribute partitions.



3
4
5
# File 'lib/mool/disk.rb', line 3

def partitions
  @partitions
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/mool/disk.rb', line 3

def path
  @path
end

#sizeObject

Returns the value of attribute size.



3
4
5
# File 'lib/mool/disk.rb', line 3

def size
  @size
end

#slavesObject

Returns the value of attribute slaves.



3
4
5
# File 'lib/mool/disk.rb', line 3

def slaves
  @slaves
end

#swapObject

Returns the value of attribute swap.



3
4
5
# File 'lib/mool/disk.rb', line 3

def swap
  @swap
end

#total_blockObject

Returns the value of attribute total_block.



3
4
5
# File 'lib/mool/disk.rb', line 3

def total_block
  @total_block
end

#unityObject

Returns the value of attribute unity.



3
4
5
# File 'lib/mool/disk.rb', line 3

def unity
  @unity
end

Class Method Details

.allObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/mool/disk.rb', line 166

def self.all
  disks = []

  Mool::Command.dev_block_command.each do |entry|
    real_path = Mool::Command.real_path_block_command(entry)
    next unless !real_path.include?('virtual') &&
                !real_path.include?('/sr') &&
                !Mool::Command.real_path_command_exist?(real_path) &&
                Mool::Command.slaves_command(real_path).empty?

    disks << Mool::Disk.new(entry.split('/').last)
  end

  disks.each { |disk| disk.partitions.each { |part| part.partitions && part.slaves }}
  disks.each { |disk| disk.slaves.each { |part| part.partitions && part.slaves }}
  disks
end

.all_usableObject



189
190
191
192
193
194
195
196
197
# File 'lib/mool/disk.rb', line 189

def self.all_usable
  result = Mool::Disk.all
  result.each do |disk|
    result += (disk.partitions +
               disk.slaves +
               (disk.partitions + disk.slaves).collect { |p| p.partitions + p.slaves }.flatten)
  end
  result.reject(&:blank?).select { |d| (d.partitions + d.slaves).blank? }
end

.swapObject



184
185
186
187
# File 'lib/mool/disk.rb', line 184

def self.swap
  result = Mool::Command.swap_command.scan(%r{/.*\n\/dev\/(\S+)/}).flatten.first
  Mool::Disk.new(result) unless result.nil?
end

Instance Method Details

#capacityObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/mool/disk.rb', line 91

def capacity
  return if defined?(@total_block) && defined?(@block_used) && defined?(@block_free)
  result = Mool::Command.df_command.scan(
    /(#{@logical_name}|#{@devname})\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/
  ).flatten
  @total_block = Mool::Command.capacity_partition_command(@path).chomp.to_f *
                 Mool::BLOCK_SIZE
  @block_used  = result[2].to_f * Mool::BLOCK_SIZE
  @block_free  = @total_block - @block_used
end

#devObject



75
76
77
# File 'lib/mool/disk.rb', line 75

def dev
  @major + @minor
end

#free_percentObject



130
131
132
# File 'lib/mool/disk.rb', line 130

def free_percent
  @block_free / @total_block
end

#is_disk?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/mool/disk.rb', line 79

def is_disk?
  @devtype == 'disk'
end

#is_partition?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/mool/disk.rb', line 83

def is_partition?
  @devtype == 'partition'
end

#logical_nameObject



57
58
59
60
# File 'lib/mool/disk.rb', line 57

def logical_name
  lname = Mool::Command.logical_name_command(@path)
  @logical_name = lname.present? ? lname : @devname
end

#mounting_pointObject



41
42
43
44
45
46
# File 'lib/mool/disk.rb', line 41

def mounting_point
  Mool::Command.mount_command.include?(@logical_name) &&
    @mount_point ||= Mool::Command.mount_command.scan(
    /#{@logical_name} (\S+)/
  ).flatten.first
end

#read_ueventObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mool/disk.rb', line 62

def read_uevent
  @major,
  @minor,
  @devname,
  @devtype =
  Mool::Command.uevent_command(@path).split("\n").map do |result|
    result.split('=').last
  end
  # Mool::Command.uevent_command(@path).scan(
  #   /.*=(\d+)\n.*=(\d+)\n.*=(\S+)\n.*=(\w+)\n/
  # ).flatten
end

#to_bObject



134
135
136
137
138
139
140
# File 'lib/mool/disk.rb', line 134

def to_b
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::BYTES)
end

#to_gbObject



158
159
160
161
162
163
164
# File 'lib/mool/disk.rb', line 158

def to_gb
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::GBYTES)
end

#to_kbObject



142
143
144
145
146
147
148
# File 'lib/mool/disk.rb', line 142

def to_kb
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::KBYTES)
end

#to_mbObject



150
151
152
153
154
155
156
# File 'lib/mool/disk.rb', line 150

def to_mb
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::MBYTES)
end

#used_percentObject



126
127
128
# File 'lib/mool/disk.rb', line 126

def used_percent
  @block_used / @total_block
end