Sha256: 259a1c4d7f3d70d1455207cf17f1e1b3dc29cee4d1ea02c5996c1fe56ebc5a31

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

module BjnInventory
  def self.get_groups_data(inventory, ansible_spec)
    ansible_inventory = inventory.to_ansible(ansible_spec)
    groups_data = ansible_inventory.reject {|group, devices| group == '_meta'}
    groups_data
  end

  def self.refresh_inventory_data(type, data, opt)
    type_dir = "#{opt[:datadir]}/#{type}"
    IO.write("#{opt[:datadir]}/#{type}.json", JSON.pretty_generate(data))
    if !Dir.exist?(type_dir)
      Dir.mkdir(type_dir)
    end
    refresh_inventory_entries(type, data, type_dir)
  end

  def self.refresh_inventory_entries(type, data, type_dir)
    previous_entries = Dir.entries(type_dir).reject {|listing| (listing == '.' || listing == '..' || listing == "#{type}.json")}
    previous_entries = previous_entries.map { |file| file.sub('.json', '') }
    current_entries = data.keys

    remove_entries = previous_entries - current_entries

    remove_entries.each do |name|
      entry = "#{type_dir}/#{name}.json"
      File.delete(entry)
    end

    current_entries.each do |name|
      node_name = name
      includes_slash = node_name.include?('/')
      if includes_slash
        split_name = node_name.split('/')
        tmp_name = split_name.join('-')
        node_name = tmp_name
      end
      entry = "#{type_dir}/#{node_name}.json"
      tmp_file = "#{type_dir}/.#{node_name}.#{Process.pid}.tmp"
      begin
        IO.write(tmp_file, JSON.pretty_generate(data[name]))
      rescue
        puts "could not create temp file: #{tmp_file}"
      end
      begin
        IO.copy_stream(tmp_file, entry)
      rescue
        puts "could not copy #{tmp_file} to #{entry}"
      end
      File.delete(tmp_file)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bjn_inventory-1.7.1 lib/bjn_inventory/data_files.rb
bjn_inventory-1.7.0 lib/bjn_inventory/data_files.rb
bjn_inventory-1.6.1 lib/bjn_inventory/data_files.rb
bjn_inventory-1.5.1 lib/bjn_inventory/data_files.rb
bjn_inventory-1.3.1 lib/bjn_inventory/data_files.rb