class PulpProxy::DiskUsage

Constants

SIZE

Attributes

command_path[R]
path[R]
size[R]
stat[R]

Public Class Methods

new(opts ={}) click to toggle source
# File lib/smart_proxy_pulp_plugin/disk_usage.rb, line 9
def initialize(opts ={})
  raise(::Proxy::Error::ConfigurationError, 'Unable to continue - must provide a path.') if opts[:path].nil?
  @paths_hash = validate_path(path_hash(opts[:path]))
  @path = @paths_hash.values
  @size = SIZE[opts[:size]] || SIZE[:kilobyte]
  @stat = {}
  find_df
  get_stat
end

Public Instance Methods

to_json() click to toggle source
# File lib/smart_proxy_pulp_plugin/disk_usage.rb, line 19
def to_json
  stat.to_json
end

Private Instance Methods

command() click to toggle source
# File lib/smart_proxy_pulp_plugin/disk_usage.rb, line 31
def command
  [command_path, "-B", "#{size}", *path]
end
find_df() click to toggle source
# File lib/smart_proxy_pulp_plugin/disk_usage.rb, line 27
def find_df
  @command_path = which('df') || raise(::Proxy::Error::ConfigurationError, 'df command was not found unable to retrieve usage information.')
end
get_stat() click to toggle source
# File lib/smart_proxy_pulp_plugin/disk_usage.rb, line 35
def get_stat
  raw = Open3::popen3({"LC_ALL" => "C"}, *command) do |stdin, stdout, stderr, thread|
    unless stderr.read.empty?
      error_line = stderr.read
      logger.error "[#{command_path}] #{error_line}"
      raise(::Proxy::Error::ConfigurationError, "#{command_path} raised an error: #{error_line}")
    end
    stdout.read.split("\n")
  end
  logger.debug "[#{command_path}] #{raw.to_s}"

  titles = normalize_titles(raw)
  raw.each_with_index do |line, index|
    mount_path = path[index]
    values = normalize_values(line.split)
    @stat[@paths_hash.key(mount_path)] = Hash[titles.zip(values)].merge({:path => mount_path, :size => SIZE.key(size)})
  end
end
normalize_titles(raw) click to toggle source
# File lib/smart_proxy_pulp_plugin/disk_usage.rb, line 58
def normalize_titles(raw)
  replacers = {"mounted on" => :mounted, "use%" => :percent}
  raw.shift.downcase.gsub(/(use%|mounted on)/) { |m| replacers.fetch(m,m)}.split.map(&:to_sym)
end
normalize_values(values) click to toggle source
# File lib/smart_proxy_pulp_plugin/disk_usage.rb, line 63
def normalize_values(values)
  values.each_with_index do |value, index|
    is_int = Integer(value) rescue false
    values[index] = is_int if is_int
  end
  values
end
path_hash(path) click to toggle source
# File lib/smart_proxy_pulp_plugin/disk_usage.rb, line 54
def path_hash(path)
  path.is_a?(Hash) ? path : Hash[path, path]
end
validate_path(path_hash) click to toggle source
# File lib/smart_proxy_pulp_plugin/disk_usage.rb, line 71
def validate_path(path_hash)
  path_hash.each do |key, value|
    unless File.readable?(value)
      logger.warn "File at #{value} defined in #{key} parameter doesn't exist or is unreadable"
      path_hash.delete(key)
    end
  end
  path_hash
end