cloner 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 084c4eb25834594c2bf7ab958a545f6b85a918c7
4
- data.tar.gz: 653be534290038354984642b050a681c6bece755
3
+ metadata.gz: 766c1d83045885a8d0583a7d68996e10e285f445
4
+ data.tar.gz: 30a73229c201aaee192ff864d34b986209c78530
5
5
  SHA512:
6
- metadata.gz: 9887f526f47f8482302a99d0e203d88e792369867dba64fc1c7985f5bc05a726157fc2eb2fb9c118f2e2ebfc1a00770a135b1a55b0a3b02991df8d33b307837b
7
- data.tar.gz: 86e4e6c4fe618380ae1aff54f4d3406527b6170fc41d2cbecacd3323743cf4cff09598d959fec25105f591fa5d611aa2e579e7b1d9ca0879b46cdda4578da5e4
6
+ metadata.gz: 10ae7e947d725997f0e382107aa2db7eeeb32c9fe097b341b8ad3f8e49cf3d1d7e01a4fea20bedf94d18df74bad91cd69693bc86d009f02c5a18951f4245c981
7
+ data.tar.gz: 63d80240f8d443a070ebbf6f7a699ef1587ab1159e6bd65ac256641cf5b85e34c15e98d1c5c1eccda0a33fd31fc8e4129457b31783c99342a5af39cb4374da4e
@@ -1,6 +1,7 @@
1
1
  module Cloner::Internal
2
2
  extend ActiveSupport::Concern
3
3
  include Cloner::MongoDB
4
+ include Cloner::Postgres
4
5
  include Cloner::SSH
5
6
  include Cloner::RSync
6
7
 
@@ -19,7 +20,11 @@ module Cloner::Internal
19
20
  end
20
21
 
21
22
  def clone_db
22
- clone_mongodb
23
+ if defined?(Mongoid)
24
+ clone_mongodb
25
+ else
26
+ clone_pg
27
+ end
23
28
  end
24
29
  end
25
30
 
@@ -61,7 +61,7 @@ module Cloner::MongoDB
61
61
  end
62
62
 
63
63
  def mongodb_dump_copy
64
- `mkdir -p #{mongodb_path}`
64
+ FileUtils.mkdir_p(mongodb_path)
65
65
  rsync("#{remote_dump_path}/#{mongodb_r_conf['database']}", mongodb_path)
66
66
  end
67
67
 
@@ -0,0 +1,82 @@
1
+ module Cloner::Postgres
2
+ extend ActiveSupport::Concern
3
+
4
+ def ar_conf
5
+ @conf ||= begin
6
+ YAML.load_file(Rails.root.join('config', 'database.yml'))[Rails.env]
7
+ end
8
+ end
9
+
10
+ def ar_to
11
+ ar_conf['database']
12
+ end
13
+
14
+ def ar_r_conf
15
+ @ar_r_conf ||= begin
16
+ Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
17
+ ret = ssh_exec!(ssh, "cat #{remote_app_path}/config/database.yml")
18
+ check_ssh_err(ret)
19
+ YAML.load(ret[0])[env_from]
20
+ end
21
+ end
22
+ end
23
+
24
+ def pg_local_auth
25
+ if ar_conf['password'].nil?
26
+ ""
27
+ else
28
+ "PGPASSWORD='#{ar_conf['password']}' "
29
+ end
30
+ end
31
+
32
+ def pg_remote_auth
33
+ if ar_r_conf['password'].nil?
34
+ ""
35
+ else
36
+ "PGPASSWORD='#{ar_r_conf['password']}' "
37
+ end
38
+ end
39
+
40
+ def pg_dump_remote
41
+ puts "backup remote DB via ssh"
42
+ Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
43
+ ssh.exec!("rm -R #{remote_dump_path}")
44
+ ret = ssh_exec!(ssh, "mkdir -p #{remote_dump_path}")
45
+ check_ssh_err(ret)
46
+ dump = pg_remote_auth + "pg_dump -Fc -U #{ar_r_conf['username']} #{ar_r_conf['database']} > #{remote_dump_path}/tmp.bak"
47
+ puts dump if verbose?
48
+ ret = ssh_exec!(ssh, dump)
49
+ check_ssh_err(ret)
50
+ end
51
+ end
52
+
53
+ def pg_dump_restore
54
+ puts "restoring DB"
55
+ restore = pg_local_auth + "pg_restore -Fc -U #{ar_conf['username']} -d #{ar_to} #{pg_path}/tmp.bak"
56
+ puts restore if verbose?
57
+ pipe = IO.popen(restore)
58
+ while (line = pipe.gets)
59
+ print line if verbose?
60
+ end
61
+ ret = $?.to_i
62
+ if ret != 0
63
+ puts "Error: local command exited with #{ret}"
64
+ end
65
+ end
66
+
67
+ def pg_path
68
+ Rails.root.join("tmp", "dump").to_s
69
+ end
70
+
71
+ def pg_dump_copy
72
+ FileUtils.mkdir_p(pg_path)
73
+ `mkdir -p #{pg_path}`
74
+ rsync("#{remote_dump_path}/", pg_path)
75
+ end
76
+
77
+ def clone_pg
78
+ pg_dump_remote()
79
+ pg_dump_copy()
80
+ pg_dump_restore()
81
+ end
82
+ end
@@ -1,3 +1,3 @@
1
1
  module Cloner
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/cloner.rb CHANGED
@@ -2,6 +2,7 @@ require "cloner/version"
2
2
  require 'thor'
3
3
  require 'active_support/concern'
4
4
  require "cloner/mongodb"
5
+ require "cloner/postgres"
5
6
  require "cloner/ssh"
6
7
  require "cloner/rsync"
7
8
  require "cloner/internal"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - glebtv
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-01 00:00:00.000000000 Z
11
+ date: 2014-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -99,6 +99,7 @@ files:
99
99
  - lib/cloner/base.rb
100
100
  - lib/cloner/internal.rb
101
101
  - lib/cloner/mongodb.rb
102
+ - lib/cloner/postgres.rb
102
103
  - lib/cloner/rsync.rb
103
104
  - lib/cloner/ssh.rb
104
105
  - lib/cloner/version.rb