scalingo_databases_rake_tasks 0.1.2 → 0.1.3

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: fef412338d874cf4bc5f097a9d3a95b904308211
4
- data.tar.gz: 5588bdef817f1dc48de78b3d445a8b558eb8b446
3
+ metadata.gz: 269dbd4d78d561cc362f60a0f9025b7fe21f790d
4
+ data.tar.gz: c85800a8026150a9e4ecd92e28df572a574e7762
5
5
  SHA512:
6
- metadata.gz: 4f9d42eedb22b09311d12e7a9ab654051df1b3047e383ada1d760cc14d77347a1a382e85a6c354f73385e2d4579a6a88ed8a2f916b7811eca1be4465a6ba6bfd
7
- data.tar.gz: 1bbab205f75415dbb7d13a8dfb567b182288b9138cfb10ae5a1217283c1215e53db4e6538f865a1cb3650592d52e98f228ca694fea8772b5d0543b7da31b5107
6
+ metadata.gz: 54a07a63f782fbba92d89243a9406c7cb1fdade44fc7e92be17c9ab41b3b8c2acf56296d42c91e5fa8621736f46106fe4c488f323eaf87006f882e9ad4d535e7
7
+ data.tar.gz: 04a5a03bfa75134cb1f4e1327b0de869c65a316ff8075d4cb985d78e3475ab10086794f71d26f77e9843e6fb8a5f4908201ea49361c35fefb7c975c4eaa37246
data/README.md CHANGED
@@ -33,6 +33,10 @@ Backups are stored under the `tmp` folder of your project, the database type is
33
33
 
34
34
  To restore from a specific archive, you'll have to give it the default archive name and put it inside of `tmp` folder before running the rake command.
35
35
 
36
+ ### SSH Identity
37
+
38
+ If you are not using a SSH agent and your default SSH identity file is not `id_rsa` you can specify your custom identity file path with the `SSH_IDENTITY` variable.
39
+
36
40
  ## Installation
37
41
 
38
42
  Add this line to your application's Gemfile:
@@ -1,5 +1,6 @@
1
1
  require 'uri'
2
2
  require 'open3'
3
+ require 'timeout'
3
4
 
4
5
  namespace :scalingo do
5
6
  private
@@ -16,6 +17,10 @@ namespace :scalingo do
16
17
  "#{Rails.root}/tmp/#{name}.tar.gz"
17
18
  end
18
19
 
20
+ def archive_contain_sql path
21
+ `tar tf #{path} | grep "\.sql$" | wc -l`.to_i > 0
22
+ end
23
+
19
24
  def make_tmp_dir
20
25
  FileUtils.mkdir_p 'tmp'
21
26
  end
@@ -31,23 +36,44 @@ namespace :scalingo do
31
36
  end
32
37
 
33
38
  def start_scalingo_tunnel app, variable
34
- cmd = "scalingo -a #{app} db-tunnel -p 27717 #{variable}"
39
+ if ENV['SSH_IDENTITY']
40
+ cmd = "scalingo -a #{app} db-tunnel -i #{ENV['SSH_IDENTITY']} -p 27717 #{variable}"
41
+ else
42
+ cmd = "scalingo -a #{app} db-tunnel -p 27717 #{variable}"
43
+ end
35
44
  puts "*** Executing #{cmd}"
36
45
  i, o, thr = Open3::pipeline_rw cmd
46
+ pid = thr[0].pid
47
+ puts '*** Tunnel opened'
48
+
49
+ close_tunnel = lambda {
50
+ if thr[0].status
51
+ thr[0].kill
52
+ Process.kill("INT", pid) if pid != -1
53
+ puts '*** Tunnel closed'
54
+ end
55
+ }
56
+ at_exit do
57
+ close_tunnel.call
58
+ end
37
59
 
38
- while true
60
+ loop do
39
61
  read_line(o) do |line|
40
62
  # puts line # debug
41
63
  if line.include?("Encrypted")
42
64
  abort "*** Your SSH key is encrypted. This gem is only compatible with SSH agents or unencrypted keys."
43
65
  end
44
66
 
67
+ if line.include?("An error occured")
68
+ abort "*** #{line}"
69
+ end
70
+
45
71
  if line.include?("address already in use")
46
72
  abort "*** Address 127.0.0.1:27717 is already in use."
47
73
  end
48
74
 
49
75
  if line.include?("'127.0.0.1:27717'")
50
- return thr[0].pid
76
+ return [pid, close_tunnel]
51
77
  elsif line.include?("'127.0.0.1:")
52
78
  abort "*** Address 127.0.0.1:27717 is already in use."
53
79
  end
@@ -77,13 +103,17 @@ namespace :scalingo do
77
103
  end
78
104
 
79
105
  database, user, password = remote_credentials(app, variable)
80
- pid = start_scalingo_tunnel(app, variable)
81
- at_exit do
82
- Process.kill("INT", pid) if pid != -1
83
- puts '*** Tunnel closed'
106
+
107
+ begin
108
+ pid, close_tunnel = Timeout::timeout(15) do
109
+ start_scalingo_tunnel(app, variable)
110
+ end
111
+ rescue Timeout::Error => e
112
+ abort "*** Error in tunnel: #{e}"
84
113
  end
85
- puts '*** Tunnel opened'
114
+
86
115
  yield(database, user, password)
116
+ close_tunnel.call
87
117
  end
88
118
 
89
119
  class VariableError < StandardError
@@ -53,7 +53,9 @@ namespace :scalingo do
53
53
  password_cmd = "PGPASSWORD=#{password}"
54
54
  end
55
55
  end
56
- base_cmd = "pg_dump -O -n public --format=c #{user_cmd} -h #{host} -p #{port} -d #{database}"
56
+ exclude_cmd = "-N 'information_schema' -N '^pg_*'"
57
+ base_cmd = "pg_dump --no-owner --no-privileges --clean #{exclude_cmd} --format=c #{user_cmd} -h #{host} -p #{port} -d #{database}"
58
+ base_cmd << " --if-exists" if pg_restore_after_9_4?
57
59
  output = "rm -rf #{DUMP_PATH} 2>/dev/null && /usr/bin/env PGPASSWORD=[FILTERED] #{base_cmd}"
58
60
  cmd = "rm -rf #{DUMP_PATH} 2>/dev/null && /usr/bin/env #{password_cmd} #{base_cmd}"
59
61
 
@@ -67,27 +69,45 @@ namespace :scalingo do
67
69
  end
68
70
 
69
71
  def self.restore database, user, password, host, port
70
- user_cmd = ""
71
- password_cmd = ""
72
- if not user.blank?
73
- user_cmd = " -U #{user}"
74
- if not password.blank?
75
- password_cmd = "PGPASSWORD=#{password}"
72
+ if archive_contain_sql(archive_name DUMP_NAME)
73
+ user_cmd = ""
74
+ password_cmd = ""
75
+ if not user.blank?
76
+ user_cmd = " -U #{user}"
77
+ if not password.blank?
78
+ password_cmd = "PGPASSWORD=#{password}"
79
+ end
76
80
  end
77
- end
78
81
 
79
- base_cmd = "tar xvzOf #{archive_name DUMP_NAME} | "
80
- pg_cmd = "pg_restore -O -n public --clean"
81
- pg_cmd << " --if-exists" if pg_restore_after_9_4
82
- pg_cmd << " #{user_cmd} -h #{host} -p #{port} -d #{database}"
83
- output = "#{base_cmd} PGPASSWORD=[FILTERED] #{pg_cmd}"
84
- cmd = "#{base_cmd} #{password_cmd} #{pg_cmd}"
82
+ base_cmd = "tar xvzOf #{archive_name DUMP_NAME} | "
83
+ pg_cmd = "psql #{user_cmd} -h #{host} -p #{port} -d #{database} -f -"
84
+ output = "#{base_cmd} PGPASSWORD=[FILTERED] #{pg_cmd}"
85
+ cmd = "#{base_cmd} #{password_cmd} #{pg_cmd}"
85
86
 
86
- puts "*** Executing #{output}"
87
- system(cmd)
87
+ puts "*** Executing #{output}"
88
+ system(cmd)
89
+ else
90
+ user_cmd = ""
91
+ password_cmd = ""
92
+ if not user.blank?
93
+ user_cmd = " -U #{user}"
94
+ if not password.blank?
95
+ password_cmd = "PGPASSWORD=#{password}"
96
+ end
97
+ end
98
+ base_cmd = "tar xvzOf #{archive_name DUMP_NAME} | "
99
+ pg_cmd = "pg_restore --no-owner --no-privileges --clean"
100
+ pg_cmd << " --if-exists" if pg_restore_after_9_4?
101
+ pg_cmd << " #{user_cmd} -h #{host} -p #{port} -d #{database}"
102
+ output = "#{base_cmd} PGPASSWORD=[FILTERED] #{pg_cmd}"
103
+ cmd = "#{base_cmd} #{password_cmd} #{pg_cmd}"
104
+
105
+ puts "*** Executing #{output}"
106
+ system(cmd)
107
+ end
88
108
  end
89
109
 
90
- def pg_restore_after_9_4?
110
+ def self.pg_restore_after_9_4?
91
111
  version = `pg_restore --version`.split.last
92
112
  major, minor = version.split('.')
93
113
  major.to_i >= 9 && minor.to_i >= 4
@@ -1,3 +1,3 @@
1
1
  module ScalingoDbTasks
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scalingo_databases_rake_tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scalingo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-19 00:00:00.000000000 Z
11
+ date: 2016-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler