<commit_before>module Cloner::MongoDB
  extend ActiveSupport::Concern

  def mongodb_conf
    @conf ||= begin
      yml = YAML.load_file(Rails.root.join('config', 'mongoid.yml'))[Rails.env]
      if yml.key?('sessions')
        yml['sessions']['default']
      else
        yml['clients']['default']
      end
    end
  end

  def mongodb_to
    mongodb_conf['database']
  end

  def mongodb_r_conf
    @r_conf ||= begin
      Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
        ret = ssh_exec!(ssh, "cat #{e(remote_app_path + '/config/mongoid.yml')}")
        check_ssh_err(ret)
        yml = YAML.load(ret[0])[env_from]
        if yml.key?('sessions')
          yml['sessions']['default']
        else
          yml['clients']['default']
        end
      end
    end
  end

  def mongodb_local_auth
    if mongodb_conf['options'].present? && mongodb_conf['password'].present?
      "-u #{e mongodb_conf['options']['user']} -p #{e mongodb_conf['options']['password']}"
    elsif mongodb_conf['password'].present?
      "-u #{e mongodb_conf['username']} -p #{e mongodb_conf['password']}"
    else
      ""
    end
  end

  def mongodb_dump_remote
    puts "backup remote DB via ssh"
    Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
      ssh.exec!("rm -R #{remote_dump_path}")
      ret = ssh_exec!(ssh, "mkdir -p #{remote_dump_path}")
      check_ssh_err(ret)
      dump = "mongodump -u #{e mongodb_r_conf['username']} -p #{e mongodb_r_conf['password']} -d #{e mongodb_r_conf['database']} --authenticationDatabase #{e mongodb_r_conf['database']} -o #{e remote_dump_path}"
      puts dump if verbose?
      ret = ssh_exec!(ssh, dump)
      check_ssh_err(ret)
    end
  end
<commit_msg>fix mondoid 5.0 auth<commit_after>module Cloner::MongoDB
  extend ActiveSupport::Concern

  def mongodb_conf
    @conf ||= begin
      yml = YAML.load_file(Rails.root.join('config', 'mongoid.yml'))[Rails.env]
      if yml.key?('sessions')
        yml['sessions']['default']
      else
        yml['clients']['default']
      end
    end
  end

  def mongodb_to
    mongodb_conf['database']
  end

  def mongodb_r_conf
    @r_conf ||= begin
      Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
        ret = ssh_exec!(ssh, "cat #{e(remote_app_path + '/config/mongoid.yml')}")
        check_ssh_err(ret)
        yml = YAML.load(ret[0])[env_from]
        if yml.key?('sessions')
          yml['sessions']['default']
        else
          yml['clients']['default']
        end
      end
    end
  end

  def mongodb_local_auth
    if mongodb_conf['options'].present? && mongodb_conf['password'].present?
      "-u #{e mongodb_conf['options']['user']} -p #{e mongodb_conf['options']['password']}"
    elsif mongodb_conf['password'].present?
      "-u #{e mongodb_conf['username']} -p #{e mongodb_conf['password']}"
    else
      ""
    end
  end

  def mongodb_dump_remote
    puts "backup remote DB via ssh"
    Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
      ssh.exec!("rm -R #{remote_dump_path}")
      ret = ssh_exec!(ssh, "mkdir -p #{remote_dump_path}")
      check_ssh_err(ret)
      if mongodb_r_conf['options'].present? && mongodb_r_conf['options']['password'].present?
        username, password = mongodb_r_conf['options']['user'], mongodb_r_conf['options']['password']
      else
        username, password = mongodb_r_conf['username'], mongodb_r_conf['password']
      end
      dump = "mongodump -u #{e username} -p #{e password} -d #{e mongodb_r_conf['database']} --authenticationDatabase #{e mongodb_r_conf['database']} -o #{e remote_dump_path}"
      puts dump if verbose?
      ret = ssh_exec!(ssh, dump)
      check_ssh_err(ret)
    end
  end

{'fraction_non_alphanumeric': 0.15507876154263986, 'fraction_numerical': 0.0010863661053775121, 'mean_word_length': 3.152198421645998, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7110758', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1271, 'n_words': 256}