<commit_before>class Instance < ActiveRecord::Base
  include Provider
  include Aws

  validates_presence_of :name, :os, :subnet
  belongs_to :subnet

  has_many :instance_groups, dependent: :destroy
  has_many :instance_roles, dependent: :destroy
  has_many :groups, through: :instance_groups, dependent: :destroy
  has_many :roles, through: :instance_roles, dependent: :destroy

  before_create :ensure_has_ip
  validate :ip_address_must_be_within_subnet

  def generate_cookbooker

    # template = File.read(Settings.app_path + "lib/templates/cookbook_template_new.rb.erb")
    # template = Erubis::Eruby.new(template)
    cookbook = "# Instance cookbook\n"

    self.roles.each do |role|
        role.recipes.each do |recipe|
          fname = Settings.app_path + "scenarios/recipes/" + recipe + ".rb.erb"
          if File.file?(fname)
            cookbook += Erubis::Eruby.new(File.read(fname)).result + "\n"
          end
        end
      end

      cookbook
  end

  def scenario
    return self.subnet.cloud.scenario
  end

  def owner?(id)
    return self.subnet.cloud.scenario.user_id == id
  end

  def status_check
    puts "\nstatus check\n"
    if self.driver_id
      if AWS::EC2.new.instances[self.driver_id].exists?
        # check if it is running
      else
        self.driver_id = nil
        self.set_stopped
        self.save
      end
    end
  end

  def get_bash_history
    return false if !self.bash_history_page
    s3 = AWS::S3.new
    bucket = s3.buckets[Settings.bucket_name]
    if bucket.objects[self.aws_instance_bash_history_page_name].exists?
      bash_history =  bucket.objects[self.aws_instance_bash_history_page_name].read()
      return bash_history == nil ? "" : bash_history
    end
  end

  def initialized?
    return false if !self.com_page

    s3 = AWS::S3.new
    bucket = s3.buckets[Settings.bucket_name]
    if bucket.objects[self.aws_instance_com_page_name].exists?<commit_msg>fix nil return on instance.get_bash_history<commit_after>class Instance < ActiveRecord::Base
  include Provider
  include Aws

  validates_presence_of :name, :os, :subnet
  belongs_to :subnet

  has_many :instance_groups, dependent: :destroy
  has_many :instance_roles, dependent: :destroy
  has_many :groups, through: :instance_groups, dependent: :destroy
  has_many :roles, through: :instance_roles, dependent: :destroy

  before_create :ensure_has_ip
  validate :ip_address_must_be_within_subnet

  def generate_cookbooker

    # template = File.read(Settings.app_path + "lib/templates/cookbook_template_new.rb.erb")
    # template = Erubis::Eruby.new(template)
    cookbook = "# Instance cookbook\n"

    self.roles.each do |role|
        role.recipes.each do |recipe|
          fname = Settings.app_path + "scenarios/recipes/" + recipe + ".rb.erb"
          if File.file?(fname)
            cookbook += Erubis::Eruby.new(File.read(fname)).result + "\n"
          end
        end
      end

      cookbook
  end

  def scenario
    return self.subnet.cloud.scenario
  end

  def owner?(id)
    return self.subnet.cloud.scenario.user_id == id
  end

  def status_check
    puts "\nstatus check\n"
    if self.driver_id
      if AWS::EC2.new.instances[self.driver_id].exists?
        # check if it is running
      else
        self.driver_id = nil
        self.set_stopped
        self.save
      end
    end
  end

  def get_bash_history
    return false if !self.bash_history_page
    s3 = AWS::S3.new
    bucket = s3.buckets[Settings.bucket_name]
    if bucket.objects[self.aws_instance_bash_history_page_name].exists?
      bash_history =  bucket.objects[self.aws_instance_bash_history_page_name].read()
      return bash_history == nil ? "" : bash_history
    end
    return ""
  end

  def initialized?
    return false if !self.com_page

    s3 = AWS::S3.new
    bucket = s3.buckets[Settings.bucket_name]
    if bucket.objects[self.aws_instance_com_page_name].exists?
{'fraction_non_alphanumeric': 0.09335038363171355, 'fraction_numerical': 0.0035805626598465474, 'mean_word_length': 3.073958333333333, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3672581', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1296, 'n_words': 270}