|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Facter |
| 4 | + module Resolvers |
| 5 | + module Aix |
| 6 | + class Partitions < BaseResolver |
| 7 | + @log = Facter::Log.new(self) |
| 8 | + @semaphore = Mutex.new |
| 9 | + @fact_list ||= {} |
| 10 | + class << self |
| 11 | + private |
| 12 | + |
| 13 | + MEGABYTES_EXPONENT = 1024**2 |
| 14 | + GIGABYTES_EXPONENT = 1024**3 |
| 15 | + |
| 16 | + def post_resolve(fact_name) |
| 17 | + @fact_list.fetch(fact_name) { query_cudv(fact_name) } |
| 18 | + end |
| 19 | + |
| 20 | + def query_cudv(fact_name) |
| 21 | + @fact_list[:partitions] = {} |
| 22 | + |
| 23 | + odmquery = Facter::ODMQuery.new |
| 24 | + odmquery.equals('PdDvLn', 'logical_volume/lvsubclass/lvtype') |
| 25 | + |
| 26 | + result = odmquery.execute |
| 27 | + |
| 28 | + return unless result |
| 29 | + |
| 30 | + result.each_line do |line| |
| 31 | + next unless line.include?('name') |
| 32 | + |
| 33 | + part_name = line.split('=')[1].strip.delete('"') |
| 34 | + part = "/dev/#{part_name}" |
| 35 | + info = populate_from_lslv(part_name) |
| 36 | + @fact_list[:partitions][part] = info if info |
| 37 | + end |
| 38 | + |
| 39 | + @fact_list[fact_name] |
| 40 | + end |
| 41 | + |
| 42 | + def populate_from_lslv(name) |
| 43 | + stdout, stderr, _status = Open3.capture3("lslv -L #{name}") |
| 44 | + if stdout.empty? |
| 45 | + @log.debug(stderr) |
| 46 | + return |
| 47 | + end |
| 48 | + |
| 49 | + info_hash = extract_info(stdout) |
| 50 | + size_bytes = compute_size(info_hash) |
| 51 | + |
| 52 | + part_info = { |
| 53 | + filesystem: info_hash['TYPE'], |
| 54 | + size_bytes: size_bytes, |
| 55 | + size: Facter::BytesToHumanReadable.convert(size_bytes) |
| 56 | + } |
| 57 | + mount = info_hash['MOUNTPOINT'] |
| 58 | + label = info_hash['LABEL'] |
| 59 | + part_info[:mount] = mount unless %r{N/A} =~ mount |
| 60 | + part_info[:label] = label unless /None/ =~ label |
| 61 | + part_info |
| 62 | + end |
| 63 | + |
| 64 | + def extract_info(lsl_content) |
| 65 | + lsl_content = lsl_content.strip.split("\n").map do |line| |
| 66 | + next unless /PPs:|PP SIZE|TYPE:|LABEL:|MOUNT/ =~ line |
| 67 | + |
| 68 | + line.split(/:|\s\s/).reject(&:empty?) |
| 69 | + end |
| 70 | + |
| 71 | + lsl_content.flatten!.select! { |elem| elem }.map! { |elem| elem.delete("\s") } |
| 72 | + |
| 73 | + Hash[*lsl_content] |
| 74 | + end |
| 75 | + |
| 76 | + def compute_size(info_hash) |
| 77 | + physical_partitions = info_hash['PPs'].to_i |
| 78 | + size_physical_partition = info_hash['PPSIZE'] |
| 79 | + exp = size_physical_partition[/mega/] ? MEGABYTES_EXPONENT : GIGABYTES_EXPONENT |
| 80 | + size_physical_partition.to_i * physical_partitions * exp |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments