Module: Cloner::MongoDB
Instance Method Summary collapse
- #clone_mongodb ⇒ Object
- #mongodb_bin_path(util) ⇒ Object
- #mongodb_conf ⇒ Object
- #mongodb_dump_copy ⇒ Object
- #mongodb_dump_extra ⇒ Object
- #mongodb_dump_remote ⇒ Object
- #mongodb_dump_restore ⇒ Object
- #mongodb_local_auth ⇒ Object
- #mongodb_local_bin_path(util) ⇒ Object
- #mongodb_path ⇒ Object
- #mongodb_r_conf ⇒ Object
- #mongodb_remote_bin_path(util) ⇒ Object
- #mongodb_to ⇒ Object
Instance Method Details
#clone_mongodb ⇒ Object
128 129 130 131 132 |
# File 'lib/cloner/mongodb.rb', line 128 def clone_mongodb mongodb_dump_remote() mongodb_dump_copy() mongodb_dump_restore() end |
#mongodb_bin_path(util) ⇒ Object
57 58 59 |
# File 'lib/cloner/mongodb.rb', line 57 def mongodb_bin_path(util) util end |
#mongodb_conf ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/cloner/mongodb.rb', line 4 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 |
#mongodb_dump_copy ⇒ Object
123 124 125 126 |
# File 'lib/cloner/mongodb.rb', line 123 def mongodb_dump_copy FileUtils.mkdir_p(mongodb_path) rsync("#{remote_dump_path}/#{mongodb_r_conf['database']}", mongodb_path) end |
#mongodb_dump_extra ⇒ Object
53 54 55 |
# File 'lib/cloner/mongodb.rb', line 53 def mongodb_dump_extra "" end |
#mongodb_dump_remote ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cloner/mongodb.rb', line 87 def mongodb_dump_remote puts "backup remote DB via ssh" do_ssh 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 = "#{mongodb_remote_bin_path 'mongodump'} -u #{e username} -p #{e password} -d #{e mongodb_r_conf['database']} --authenticationDatabase #{e mongodb_r_conf['database']} -o #{e remote_dump_path} #{mongodb_dump_extra}" puts dump if verbose? ret = ssh_exec!(ssh, dump) check_ssh_err(ret) end end |
#mongodb_dump_restore ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/cloner/mongodb.rb', line 105 def mongodb_dump_restore puts "restoring DB" restore = "#{mongodb_local_bin_path 'mongorestore'} --drop -d #{e mongodb_to} #{mongodb_local_auth} #{e mongodb_path}" puts restore if verbose? pipe = IO.popen(restore) while (line = pipe.gets) print line if verbose? end ret = $?.to_i if ret != 0 puts "Error: local command exited with #{ret}" end end |
#mongodb_local_auth ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/cloner/mongodb.rb', line 43 def mongodb_local_auth if mongodb_conf['options'].present? && mongodb_conf['options']['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 |
#mongodb_local_bin_path(util) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cloner/mongodb.rb', line 61 def mongodb_local_bin_path(util) if local_docker_compose? && local_docker_compose_service # Build docker compose exec command compose_cmd = local_docker_compose_exec( local_docker_compose_service, util, no_tty: true ) return compose_cmd end mongodb_bin_path(util) end |
#mongodb_path ⇒ Object
119 120 121 |
# File 'lib/cloner/mongodb.rb', line 119 def mongodb_path Rails.root.join("tmp", "dump", mongodb_to).to_s end |
#mongodb_r_conf ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cloner/mongodb.rb', line 19 def mongodb_r_conf @r_conf ||= begin do_ssh do |ssh| ret = ssh_exec!(ssh, "cat #{e(remote_app_path + '/config/mongoid.yml')}") check_ssh_err(ret) begin yml = YAML.load(ret[0])[env_from] raise 'no data' if yml.blank? rescue Exception => e puts "unable to read remote database.yml for env #{env_from}." puts "Remote file contents:" puts ret[0] end if yml.key?('sessions') yml['sessions']['default'] else yml['clients']['default'] end end end end |
#mongodb_remote_bin_path(util) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cloner/mongodb.rb', line 74 def mongodb_remote_bin_path(util) if remote_docker_compose? && remote_docker_compose_service # Build docker compose exec command for remote compose_cmd = remote_docker_compose_exec( remote_docker_compose_service, util, no_tty: true ) return compose_cmd end mongodb_bin_path(util) end |
#mongodb_to ⇒ Object
15 16 17 |
# File 'lib/cloner/mongodb.rb', line 15 def mongodb_to mongodb_conf['database'] end |