<commit_before>require 'yaml'
module Subspace
  class Inventory
    attr_accessor :group_vars, :hosts, :global_vars, :path
    def initialize
      @hosts = {}
      @group_vars = {}
      @global_vars = {}
    end

    # Find all the hosts in the host/group or exit
    def find_hosts!(host_spec)
      if self.groups[host_spec]
        return self.groups[host_spec].host_list.map { |m| self.hosts[m] }
      elsif self.hosts[host_spec]
        return [self.hosts[host_spec]]
      else
        say "No inventory matching: '#{host_spec}' found. "
        say (["Available hosts:"] + self.hosts.keys).join("\n\t")
        say (["Available groups:"] + self.groups.keys).join("\n\t")
        exit
      end
    end

    def self.read(path)
      inventory = new
      inventory.path = path

      yml = YAML.load(File.read(path)).to_h

      # Run through all hosts
      yml["all"]["hosts"].each do |name, vars|
        inventory.hosts[name] = Host.new(name, vars: vars || {})
      end

      # Run through all children (groups)
      # This does NOT handle sub-groups yet
      yml["all"]["children"].each do |name, group|
        next unless group["hosts"]

        # Each group defines its host membership
        group["hosts"].each do |host, vars|
          inventory.hosts[host] ||= Host.new(host, vars: vars || {})
          inventory.hosts[host].group_list.push name
        end

        if group["vars"]
          inventory.group_vars[name] = group["vars"]
        end
      end

      # Capture global variables
      inventory.global_vars = yml["all"]["vars"] || {}

      inventory
    end

    def write(path=nil)
      File.write(@path || path, self.to_yml)
    end

    def merge(inventory_json)
      inventory_json["inventory"]["value"]["hostnames"].each_with_index do |host, i|
        if hosts[host]
          old_ip = hosts[host].vars["ansible_host"]
          new_ip = inventory_json["inventory"]["value"]["ip_addresses"][i]
          if old_ip != new_ip
            say "  * Host '#{host}' IP address changed! You may need to update the inventory! (#{old_ip} => #{new_ip})"
          end
          next
        end
        hosts[host] = Host.new(host)
        hosts[host].vars["ansible_host"] = inventory_json["inventory"]["value"]["ip_addresses"][i]
        hosts[host].vars["ansible_user"] = inventory_json["inventory"]["value"]["users"][i]
        hosts[host].vars["hostname"] = host
        hosts[host].group_list = inventory_json["inventory"]["value"]["groups"][i].split(/\s/)<commit_msg>no value<commit_after>require 'yaml'
module Subspace
  class Inventory
    attr_accessor :group_vars, :hosts, :global_vars, :path
    def initialize
      @hosts = {}
      @group_vars = {}
      @global_vars = {}
    end

    # Find all the hosts in the host/group or exit
    def find_hosts!(host_spec)
      if self.groups[host_spec]
        return self.groups[host_spec].host_list.map { |m| self.hosts[m] }
      elsif self.hosts[host_spec]
        return [self.hosts[host_spec]]
      else
        say "No inventory matching: '#{host_spec}' found. "
        say (["Available hosts:"] + self.hosts.keys).join("\n\t")
        say (["Available groups:"] + self.groups.keys).join("\n\t")
        exit
      end
    end

    def self.read(path)
      inventory = new
      inventory.path = path

      yml = YAML.load(File.read(path)).to_h

      # Run through all hosts
      yml["all"]["hosts"].each do |name, vars|
        inventory.hosts[name] = Host.new(name, vars: vars || {})
      end

      # Run through all children (groups)
      # This does NOT handle sub-groups yet
      yml["all"]["children"].each do |name, group|
        next unless group["hosts"]

        # Each group defines its host membership
        group["hosts"].each do |host, vars|
          inventory.hosts[host] ||= Host.new(host, vars: vars || {})
          inventory.hosts[host].group_list.push name
        end

        if group["vars"]
          inventory.group_vars[name] = group["vars"]
        end
      end

      # Capture global variables
      inventory.global_vars = yml["all"]["vars"] || {}

      inventory
    end

    def write(path=nil)
      File.write(@path || path, self.to_yml)
    end

    def merge(inventory_json)
      inventory_json["inventory"]["hostnames"].each_with_index do |host, i|
        if hosts[host]
          old_ip = hosts[host].vars["ansible_host"]
          new_ip = inventory_json["inventory"]["ip_addresses"][i]
          if old_ip != new_ip
            say "  * Host '#{host}' IP address changed! You may need to update the inventory! (#{old_ip} => #{new_ip})"
          end
          next
        end
        hosts[host] = Host.new(host)
        hosts[host].vars["ansible_host"] = inventory_json["inventory"]["ip_addresses"][i]
        hosts[host].vars["ansible_user"] = inventory_json["inventory"]["users"][i]
        hosts[host].vars["hostname"] = host
        hosts[host].group_list = inventory_json["inventory"]["groups"][i].split(/\s/)
{'fraction_non_alphanumeric': 0.14457831325301204, 'fraction_numerical': 0.0, 'mean_word_length': 2.5176553672316384, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5597957', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1565, 'n_words': 348}