Module: Cloner::Postgres

Extended by:
ActiveSupport::Concern
Included in:
Internal
Defined in:
lib/cloner/postgres.rb

Instance Method Summary collapse

Instance Method Details

#ar_confObject



4
5
6
7
8
# File 'lib/cloner/postgres.rb', line 4

def ar_conf
  @conf ||= begin
    YAML.load_file(Rails.root.join('config', 'database.yml'))[Rails.env]
  end
end

#ar_r_confObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cloner/postgres.rb', line 14

def ar_r_conf
  @ar_r_conf ||= begin
    do_ssh do |ssh|
      ret = ssh_exec!(ssh, "cat #{e(remote_app_path + '/config/database.yml')}")
      check_ssh_err(ret)
      begin
        res = YAML.load(ret[0])[env_from]
        raise 'no data' if res.blank?
        res['host'] ||= '127.0.0.1'
      rescue Exception => e
        puts "unable to read remote database.yml for env #{env_from}."
        puts "Remote file contents:"
        puts ret[0]
      end
      res
    end
  end
end

#ar_toObject



10
11
12
# File 'lib/cloner/postgres.rb', line 10

def ar_to
  ar_conf['database']
end

#clone_pgObject



88
89
90
91
92
# File 'lib/cloner/postgres.rb', line 88

def clone_pg
  pg_dump_remote()
  pg_dump_copy()
  pg_dump_restore()
end

#pg_dump_copyObject



82
83
84
85
86
# File 'lib/cloner/postgres.rb', line 82

def pg_dump_copy
  FileUtils.mkdir_p(pg_path)
  `mkdir -p #{e pg_path}`
  rsync(remote_dump_path + '/', pg_path)
end

#pg_dump_remoteObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cloner/postgres.rb', line 49

def pg_dump_remote
  puts "backup remote DB via ssh"
  do_ssh do |ssh|
    ssh.exec!("rm -R #{e remote_dump_path}")
    ret = ssh_exec!(ssh, "mkdir -p #{e remote_dump_path}")
    check_ssh_err(ret)
    host = ar_r_conf['host'].present? ? "-h #{e ar_r_conf['host']}" : ""
    dump = pg_remote_auth + "pg_dump -Fc -U #{e ar_r_conf['username']} #{host} #{e ar_r_conf['database']} > #{e(remote_dump_path + '/tmp.bak')}"
    puts dump if verbose?
    ret = ssh_exec!(ssh, dump)
    check_ssh_err(ret)
  end
end

#pg_dump_restoreObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloner/postgres.rb', line 63

def pg_dump_restore
  puts "restoring DB"
  host = ar_conf['host'].present? ? "-h #{e ar_conf['host']}" : ""
  restore = pg_local_auth + "pg_restore --no-owner -Fc -c -U #{e ar_conf['username']} #{host} -d #{e ar_to} #{e(pg_path + '/tmp.bak')}"
  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

#pg_local_authObject



33
34
35
36
37
38
39
# File 'lib/cloner/postgres.rb', line 33

def pg_local_auth
  if ar_conf['password'].nil?
    ""
  else
    "PGPASSWORD='#{ar_conf['password']}' "
  end
end

#pg_pathObject



78
79
80
# File 'lib/cloner/postgres.rb', line 78

def pg_path
  Rails.root.join("tmp", "dump").to_s
end

#pg_remote_authObject



41
42
43
44
45
46
47
# File 'lib/cloner/postgres.rb', line 41

def pg_remote_auth
  if ar_r_conf['password'].nil?
    ""
  else
    "PGPASSWORD='#{ar_r_conf['password']}' "
  end
end