<commit_before>class EngineYardCloudInstance
  CURRENT_INSTANCE_ID_CACHE_PATH = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/engine_yard_cloud_instance_id" : '/etc/engine_yard_cloud_instance_id'
  CURRENT_SECURITY_GROUPS_CACHE_PATH = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/engine_yard_cloud_security_groups" : '/etc/engine_yard_cloud_security_groups'
  INSTANCE_DESCRIPTIONS_CACHE_PATH = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/engine_yard_cloud_instance_descriptions.yml" : '/etc/engine_yard_cloud_instance_descriptions.yml'
  DNA_PATH = '/etc/chef/dna.json'
  
  attr_reader :instance_id
  def initialize(instance_id)
    @instance_id = instance_id.to_s
  end
  
  def valid?
    data.present?
  end
  
  def clear
    self.class.clear
  end
  
  def to_hash
    data.deep_copy
  end
  
  def data
    self.class.data[instance_id.to_sym]
  end
  
  def method_missing(name, *args, &block)
    name = name.to_sym
    if data and data.has_key?(name)
      data[name]
    else
      super
    end
  end
  
  class_inheritable_accessor :proxy
  
  class << self
    def to_hash
      clear
      data.deep_copy
    end
    
    def from_hash(hash)
      self.proxy = true
      @_data = hash.recursive_symbolize_keys!
      self
    end
    
    def app
      find_all_by_instance_roles :app, :app_master
    end
    
    def db
      find_all_by_instance_roles :db
    end
    
    def utility
      find_all_by_instance_roles :utility
    end
    
    def all
      data.map { |k, _| new k }
    end
    
    def current
      new cached_current_instance_id
    end
    
    def first
      new data.to_a.first.first
    end
    
    def find_by_instance_id(instance_id)
      new instance_id
    end
    
    def find_all_by_instance_roles(*args)
      data.select { |_, v| Array.wrap(args).map(&:to_s).include? v[:instance_role] }.map { |k, _| new k }
    end
    
    def clear
      raise "[EY CLOUD AWARENESS GEM] Can't clear if we used from_hash" if self.proxy
      @_data = nil
      @_dna = nil
      cached_instance_descriptions true
      cached_current_security_groups true
      cached_current_instance_id true
    end
    
    def data
      return @_data if @_data
      raise "[EY CLOUD AWARENESS GEM] Can't calculate data if we used from_hash" if self.proxy
      hash = Hash.new
      cached_instance_descriptions.each do |instance_description|
        next unless Set.new(Array.wrap(cached_current_security_groups)).superset? Set.new(instance_description[:aws_groups])
        hash[instance_description[:aws_instance_id]] ||= Hash.new
        current = hash[instance_description[:aws_instance_id]]
        # using current as a pointer
        if dna[:db_host] == instance_description[:dns_name] or dna[:db_host] == instance_description[:private_dns_name]
          current[:instance_role] = 'db'
        elsif Array.wrap(dna[:utility_instances]).include? instance_description[:private_dns_name]
          current[:instance_role] = 'utility'
        elsif dna[:master_app_server][:private_dns_name] == instance_description[:private_dns_name]
          current[:instance_role] = 'app_master'
        elsif instance_description[:aws_state] == 'running'
          current[:instance_role] = 'app'
        end
        current[:private_dns_name] = instance_description[:private_dns_name]
        current[:dns_name] = instance_description[:dns_name]
        current[:aws_state] = instance_description[:aws_state]
        current[:aws_groups] = instance_description[:aws_groups]
        current[:aws_instance_id] = instance_description[:aws_instance_id]
      end
      @_data = hash.recursive_symbolize_keys!
    end
    
    def dna
      raise "[EY CLOUD AWARENESS GEM] Can't see DNA if we used from_hash" if self.proxy<commit_msg>Get user information.<commit_after>class EngineYardCloudInstance
  CURRENT_INSTANCE_ID_CACHE_PATH = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/engine_yard_cloud_instance_id" : '/etc/engine_yard_cloud_instance_id'
  CURRENT_SECURITY_GROUPS_CACHE_PATH = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/engine_yard_cloud_security_groups" : '/etc/engine_yard_cloud_security_groups'
  INSTANCE_DESCRIPTIONS_CACHE_PATH = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/engine_yard_cloud_instance_descriptions.yml" : '/etc/engine_yard_cloud_instance_descriptions.yml'
  DNA_PATH = '/etc/chef/dna.json'
  
  attr_reader :instance_id
  def initialize(instance_id)
    @instance_id = instance_id.to_s
  end
  
  def valid?
    data.present?
  end
  
  def clear
    self.class.clear
  end
  
  def to_hash
    data.deep_copy
  end
  
  def data
    self.class.data[instance_id.to_sym]
  end
  
  def method_missing(name, *args, &block)
    name = name.to_sym
    if data and data.has_key?(name)
      data[name]
    else
      super
    end
  end
  
  class_inheritable_accessor :proxy
  
  class << self
    def to_hash
      clear
      data.deep_copy
    end
    
    def from_hash(hash)
      self.proxy = true
      @_data = hash.recursive_symbolize_keys!
      self
    end
    
    def app
      find_all_by_instance_roles :app, :app_master
    end
    
    def db
      find_all_by_instance_roles :db
    end
    
    def utility
      find_all_by_instance_roles :utility
    end
    
    def all
      data.map { |k, _| new k }
    end
    
    def current
      new cached_current_instance_id
    end
    
    def first
      new data.to_a.first.first
    end
    
    def find_by_instance_id(instance_id)
      new instance_id
    end
    
    def find_all_by_instance_roles(*args)
      data.select { |_, v| Array.wrap(args).map(&:to_s).include? v[:instance_role] }.map { |k, _| new k }
    end
    
    def clear
      raise "[EY CLOUD AWARENESS GEM] Can't clear if we used from_hash" if self.proxy
      @_data = nil
      @_dna = nil
      cached_instance_descriptions true
      cached_current_security_groups true
      cached_current_instance_id true
    end
    
    def data
      return @_data if @_data
      raise "[EY CLOUD AWARENESS GEM] Can't calculate data if we used from_hash" if self.proxy
      hash = Hash.new
      cached_instance_descriptions.each do |instance_description|
        next unless Set.new(Array.wrap(cached_current_security_groups)).superset? Set.new(instance_description[:aws_groups])
        hash[instance_description[:aws_instance_id]] ||= Hash.new
        current = hash[instance_description[:aws_instance_id]]
        # using current as a pointer
        if dna[:db_host] == instance_description[:dns_name] or dna[:db_host] == instance_description[:private_dns_name]
          current[:instance_role] = 'db'
        elsif Array.wrap(dna[:utility_instances]).include? instance_description[:private_dns_name]
          current[:instance_role] = 'utility'
        elsif dna[:master_app_server][:private_dns_name] == instance_description[:private_dns_name]
          current[:instance_role] = 'app_master'
        elsif instance_description[:aws_state] == 'running'
          current[:instance_role] = 'app'
        end
        current[:private_dns_name] = instance_description[:private_dns_name]
        current[:dns_name] = instance_description[:dns_name]
        current[:aws_state] = instance_description[:aws_state]
        current[:aws_groups] = instance_description[:aws_groups]
        current[:aws_instance_id] = instance_description[:aws_instance_id]
        current[:users] = instance_description[:users]
      end
      @_data = hash.recursive_symbolize_keys!
    end
    
    def dna
      raise "[EY CLOUD AWARENESS GEM] Can't see DNA if we used from_hash" if self.proxy
{'fraction_non_alphanumeric': 0.0847748580483296, 'fraction_numerical': 0.0, 'mean_word_length': 3.1230266739248775, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '3847386', 'n_tokens_mistral': 2514, 'n_tokens_neox': 2515, 'n_words': 479}