ey_cloud_server 1.4.5 → 1.4.26

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.
Files changed (42) hide show
  1. data/bin/binary_log_purge +10 -2
  2. data/bin/ey-snapshots +7 -1
  3. data/bin/eybackup +13 -1
  4. data/lib/ey-flex.rb +18 -7
  5. data/lib/ey-flex/big-brother.rb +2 -2
  6. data/lib/ey-flex/bucket_minder.rb +46 -160
  7. data/lib/ey-flex/ec2.rb +17 -0
  8. data/lib/ey-flex/snapshot_minder.rb +93 -171
  9. data/lib/ey_backup.rb +84 -48
  10. data/lib/ey_backup/backend.rb +34 -0
  11. data/lib/ey_backup/backup_set.rb +70 -63
  12. data/lib/ey_backup/base.rb +0 -5
  13. data/lib/ey_backup/cli.rb +26 -6
  14. data/lib/ey_backup/database.rb +48 -0
  15. data/lib/ey_backup/dumper.rb +15 -31
  16. data/lib/ey_backup/engine.rb +7 -17
  17. data/lib/ey_backup/engines/mysql_engine.rb +24 -16
  18. data/lib/ey_backup/engines/postgresql_engine.rb +26 -20
  19. data/lib/ey_backup/loader.rb +13 -33
  20. data/lib/ey_backup/processors/gpg_encryptor.rb +3 -20
  21. data/lib/ey_backup/processors/gzipper.rb +0 -29
  22. data/lib/ey_backup/processors/splitter.rb +22 -34
  23. data/lib/ey_backup/spawner.rb +7 -13
  24. data/lib/ey_cloud_server.rb +1 -1
  25. data/lib/{ey-flex → ey_cloud_server}/version.rb +1 -1
  26. data/spec/big-brother_spec.rb +12 -0
  27. data/spec/bucket_minder_spec.rb +113 -0
  28. data/spec/config-example.yml +11 -0
  29. data/spec/ey_api_spec.rb +63 -0
  30. data/spec/ey_backup/backend_spec.rb +12 -0
  31. data/spec/ey_backup/backup_spec.rb +54 -0
  32. data/spec/ey_backup/cli_spec.rb +35 -0
  33. data/spec/ey_backup/mysql_backups_spec.rb +208 -0
  34. data/spec/ey_backup/postgres_backups_spec.rb +106 -0
  35. data/spec/ey_backup/spec_helper.rb +5 -0
  36. data/spec/fakefs_hax.rb +50 -0
  37. data/spec/gpg.public +0 -0
  38. data/spec/gpg.sekrit +0 -0
  39. data/spec/helpers.rb +270 -0
  40. data/spec/snapshot_minder_spec.rb +68 -0
  41. data/spec/spec_helper.rb +31 -0
  42. metadata +286 -53
@@ -0,0 +1,208 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "MySQL Backups" do
4
+ before(:each) do
5
+ @db_name = create_mysql_database('first')
6
+ end
7
+
8
+ it "makes a backup" do
9
+ EY::Backup.run(["-c", backup_config_file ])
10
+
11
+ reset_logger
12
+
13
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name])
14
+
15
+ stdout.should match(/0:#{@db_name}/)
16
+ end
17
+
18
+ it "makes a split backup" do
19
+ EY::Backup.run(["-c", backup_config_file, "-s", "100"])
20
+
21
+ backup = EY::Backup.run(["-c", backup_config_file, "-l", @db_name]).first
22
+ backup.parts.should > 1
23
+ end
24
+
25
+ it "makes GPG encrypted backups" do
26
+ EY::Backup.run(["-c", backup_config_file, '-k', Helpers::PUBLIC_KEY_ID])
27
+
28
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name])
29
+
30
+ stdout.should match(/0:#{@db_name}/)
31
+ end
32
+
33
+ it "restores a backup" do
34
+ run_sql("CREATE TABLE `foo` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
35
+ run_sql("CREATE TABLE `bar` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
36
+
37
+ EY::Backup.run(["-c", backup_config_file ])
38
+
39
+ run_sql("DROP TABLE `bar`;", @db_name).should be_true
40
+ run_sql("SELECT * FROM `bar`;", @db_name).should be_false
41
+
42
+ reset_logger
43
+
44
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name])
45
+
46
+ stdout.should include("1 backup(s) found")
47
+
48
+ EY::Backup.run(["-c", backup_config_file, "-r", "0:#{@db_name}"])
49
+
50
+ run_sql("SELECT * FROM `foo`;", @db_name).should be_true
51
+ run_sql("SELECT * FROM `bar`;", @db_name).should be_true
52
+ end
53
+
54
+ it "restores a split backup" do
55
+ run_sql("CREATE TABLE `foo` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
56
+ run_sql("CREATE TABLE `bar` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
57
+
58
+ EY::Backup.run(["-c", backup_config_file, '-s', '100'])
59
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name]).first.parts.should > 1
60
+
61
+ run_sql("DROP TABLE `bar`;", @db_name).should be_true
62
+ run_sql("SELECT * FROM `bar`;", @db_name).should be_false
63
+
64
+ reset_logger
65
+
66
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name])
67
+
68
+ stdout.should include("1 backup(s) found")
69
+
70
+ EY::Backup.run(["-c", backup_config_file, "-r", "0:#{@db_name}"])
71
+
72
+ run_sql("SELECT * FROM `foo`;", @db_name).should be_true
73
+ run_sql("SELECT * FROM `bar`;", @db_name).should be_true
74
+ end
75
+
76
+ it "downloads a split backup to 1 file" do
77
+ run_sql("CREATE TABLE `foo` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
78
+ run_sql("CREATE TABLE `bar` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
79
+
80
+ EY::Backup.run(["-c", backup_config_file, '-s', '100'])
81
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name]).first.parts.should > 1
82
+ EY::Backup.run(["-c", backup_config_file, "--download", "0:#{@db_name}"])
83
+ files = Dir["#{EY::Backup.tmp_dir}/*#{@db_name}*"]
84
+
85
+ files.size.should == 1
86
+ FileUtils.rm(files.first)
87
+ end
88
+
89
+ it 'manually restores an encrypted backup' do
90
+ run_sql("CREATE TABLE `foo` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
91
+ run_sql("CREATE TABLE `bar` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
92
+
93
+ import_gpg
94
+
95
+ EY::Backup.run(["-c", backup_config_file, '-k', Helpers::PUBLIC_KEY_ID])
96
+
97
+ run_sql("DROP TABLE `bar`;", @db_name).should be_true
98
+ run_sql("SELECT * FROM `bar`;", @db_name).should be_false
99
+
100
+ reset_logger
101
+
102
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name])
103
+
104
+ stdout.should include("1 backup(s) found")
105
+
106
+ EY::Backup.run(["-c", backup_config_file, "--download", "0:#{@db_name}"])
107
+ file = Dir["#{EY::Backup.tmp_dir}/*#{@db_name}*"].first
108
+ stdout = StringIO.new
109
+ EY::Backup::Spawner.spawn("gpg --no-default-keyring --keyring #{Helpers::PUBLIC_KEY_PATH} --secret-keyring #{Helpers::PRIVATE_KEY_PATH} --decrypt", stdout, file)
110
+
111
+ stdout.rewind
112
+
113
+ run_sql_pipe(stdout, @db_name)
114
+
115
+ run_sql("SELECT * FROM `foo`;", @db_name).should be_true
116
+ run_sql("SELECT * FROM `bar`;", @db_name).should be_true
117
+ FileUtils.rm(file)
118
+ end
119
+ end
120
+
121
+ describe "MySQL Backups" do
122
+ before(:each) do
123
+ @dbs = [create_mysql_database('first'), create_mysql_database('second')]
124
+ end
125
+
126
+ it "makes a backup" do
127
+ EY::Backup.run(["-c", backup_config_file ])
128
+
129
+ reset_logger
130
+
131
+ @dbs.each do |db_name|
132
+ EY::Backup.run(["-c", backup_config_file, "-l", db_name])
133
+ stdout.should match(/0:#{db_name}/)
134
+ end
135
+ end
136
+ end
137
+
138
+ describe "a multi-region backup" do
139
+ describe "in japan!" do
140
+ before(:each) do
141
+ @db_name = create_mysql_database('speaking_japanese', 'ap-northeast-1')
142
+ end
143
+
144
+ it "makes a split backup" do
145
+ EY::Backup.run(["-c", backup_config_file, "-s", "100"])
146
+
147
+ backup = EY::Backup.run(["-c", backup_config_file, "-l", @db_name]).first
148
+ backup.parts.should > 1
149
+ end
150
+
151
+ it "restores a split backup" do
152
+ run_sql("CREATE TABLE `foo` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
153
+ run_sql("CREATE TABLE `bar` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
154
+
155
+ EY::Backup.run(["-c", backup_config_file, '-s', '100'])
156
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name]).first.parts.should > 1
157
+
158
+ run_sql("DROP TABLE `bar`;", @db_name).should be_true
159
+ run_sql("SELECT * FROM `bar`;", @db_name).should be_false
160
+
161
+ reset_logger
162
+
163
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name])
164
+
165
+ stdout.should include("1 backup(s) found")
166
+
167
+ EY::Backup.run(["-c", backup_config_file, "-r", "0:#{@db_name}"])
168
+
169
+ run_sql("SELECT * FROM `foo`;", @db_name).should be_true
170
+ run_sql("SELECT * FROM `bar`;", @db_name).should be_true
171
+ end
172
+ end
173
+
174
+ describe "in europe!" do
175
+ before(:each) do
176
+ @db_name = create_mysql_database('speaking_esperanto', 'eu-west-1')
177
+ end
178
+
179
+ it "makes a split backup" do
180
+ EY::Backup.run(["-c", backup_config_file, "-s", "100"])
181
+
182
+ backup = EY::Backup.run(["-c", backup_config_file, "-l", @db_name]).first
183
+ backup.parts.should > 1
184
+ end
185
+
186
+ it "restores a split backup" do
187
+ run_sql("CREATE TABLE `foo` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
188
+ run_sql("CREATE TABLE `bar` (`id` int(11) NOT NULL auto_increment, PRIMARY KEY(`id`));", @db_name).should be_true
189
+
190
+ EY::Backup.run(["-c", backup_config_file, '-s', '100'])
191
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name]).first.parts.should > 1
192
+
193
+ run_sql("DROP TABLE `bar`;", @db_name).should be_true
194
+ run_sql("SELECT * FROM `bar`;", @db_name).should be_false
195
+
196
+ reset_logger
197
+
198
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name])
199
+
200
+ stdout.should include("1 backup(s) found")
201
+
202
+ EY::Backup.run(["-c", backup_config_file, "-r", "0:#{@db_name}"])
203
+
204
+ run_sql("SELECT * FROM `foo`;", @db_name).should be_true
205
+ run_sql("SELECT * FROM `bar`;", @db_name).should be_true
206
+ end
207
+ end
208
+ end
@@ -0,0 +1,106 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "Postgres Backups" do
4
+ before(:each) do
5
+ @db_name = create_postgresql_database('first')
6
+ end
7
+
8
+ it "makes a custom format backup" do
9
+ EY::Backup.run(["-c", backup_config_file, "-e", "postgresql"])
10
+
11
+ reset_logger
12
+
13
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name, "-e", "postgresql"])
14
+
15
+ stdout.should match(/0:#{@db_name}.*\.pgz$/)
16
+ end
17
+
18
+ it "makes a split backup" do
19
+ EY::Backup.run(["-c", backup_config_file, "-e", "postgresql", "-s", "100"])
20
+
21
+ backup = EY::Backup.run(["-c", backup_config_file, "-e", "postgresql", "-l", @db_name]).first
22
+ backup.parts.should > 1
23
+ end
24
+
25
+ it "makes GPG encrypted backups" do
26
+ import_gpg
27
+
28
+ EY::Backup.run(["-c", backup_config_file, '-e', 'postgresql', '-k', Helpers::PUBLIC_KEY_ID])
29
+
30
+ EY::Backup.run(["-c", backup_config_file, '-e', 'postgresql', "-l", @db_name])
31
+
32
+ stdout.should match(/0:#{@db_name}/)
33
+ end
34
+
35
+ it "restores a backup" do
36
+ run_psql("CREATE TABLE foo (id integer NOT NULL);", @db_name).should be_true
37
+ run_psql("CREATE TABLE bar (id integer NOT NULL);", @db_name).should be_true
38
+
39
+ EY::Backup.run(["-c", backup_config_file, '-e', 'postgresql'])
40
+
41
+ run_psql("DROP TABLE bar;", @db_name).should be_true
42
+ run_psql("SELECT * FROM bar;", @db_name).should be_false
43
+
44
+ reset_logger
45
+
46
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name, '-e', 'postgresql'])
47
+
48
+ stdout.should include("1 backup(s) found")
49
+
50
+ EY::Backup.run(["-c", backup_config_file, "-r", "0:#{@db_name}", '-e', 'postgresql'])
51
+
52
+ run_psql("SELECT * FROM foo;", @db_name).should be_true
53
+ run_psql("SELECT * FROM bar;", @db_name).should be_true
54
+ end
55
+
56
+ it "restores split backups" do
57
+ run_psql("CREATE TABLE foo (id integer NOT NULL);", @db_name).should be_true
58
+ run_psql("CREATE TABLE bar (id integer NOT NULL);", @db_name).should be_true
59
+
60
+ EY::Backup.run(["-c", backup_config_file, "-e", "postgresql", "-s", "100"])
61
+ EY::Backup.run(["-c", backup_config_file, "-e", "postgresql", "-l", @db_name]).first.parts.should > 1
62
+
63
+ run_psql("DROP TABLE bar;", @db_name).should be_true
64
+ run_psql("SELECT * FROM bar;", @db_name).should be_false
65
+
66
+ EY::Backup.run(["-c", backup_config_file, "-l", @db_name, '-e', 'postgresql'])
67
+
68
+ stdout.should include("1 backup(s) found")
69
+
70
+ EY::Backup.run(["-c", backup_config_file, "-r", "0:#{@db_name}", '-e', 'postgresql'])
71
+
72
+ run_psql("SELECT * FROM foo;", @db_name).should be_true
73
+ run_psql("SELECT * FROM bar;", @db_name).should be_true
74
+ end
75
+
76
+ it "downloads a split backup to 1 file" do
77
+ run_psql("CREATE TABLE foo (id integer NOT NULL);", @db_name).should be_true
78
+ run_psql("CREATE TABLE bar (id integer NOT NULL);", @db_name).should be_true
79
+
80
+ EY::Backup.run(["-c", backup_config_file, "-e", "postgresql", "-s", "100"])
81
+ EY::Backup.run(["-c", backup_config_file, "-e", "postgresql", "-l", @db_name]).first.parts.should > 1
82
+ EY::Backup.run(["-c", backup_config_file, "-e", 'postgresql', "--download", "0:#{@db_name}"])
83
+ files = Dir["#{EY::Backup.tmp_dir}/*#{@db_name}*"]
84
+
85
+ files.size.should == 1
86
+ FileUtils.rm(files.first)
87
+ end
88
+
89
+ end
90
+
91
+ describe "Postgresql Backups" do
92
+ before(:each) do
93
+ @dbs = [create_postgresql_database('first'), create_postgresql_database('second')]
94
+ end
95
+
96
+ it "makes multiple backup" do
97
+ EY::Backup.run(["-c", backup_config_file, "-e", "postgresql"])
98
+
99
+ reset_logger
100
+
101
+ @dbs.each do |db_name|
102
+ EY::Backup.run(["-c", backup_config_file, "-l", db_name, '-e', 'postgresql'])
103
+ stdout.should match(/0:#{db_name}/)
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,5 @@
1
+ require File.dirname(__FILE__) + "/../../lib/ey_backup"
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ EY::Backup.logger = EY::Backup::Logger.new(StringIO.new, StringIO.new)
5
+
@@ -0,0 +1,50 @@
1
+ module FakeFS
2
+ class Dir
3
+ def self.tmpdir
4
+ RealDir.tmpdir
5
+ end
6
+
7
+ def self.mkdir(path)
8
+ FileUtils.mkdir_p(path)
9
+ end
10
+
11
+ def self.rmdir(path)
12
+ FileUtils.rm_rf(path)
13
+ end
14
+ end
15
+
16
+ class File
17
+ class << self
18
+ alias open_with_fail open
19
+ end
20
+ def self.open(path, mode = nil, perm = nil, &block)
21
+ open_with_fail(path, mode, &block)
22
+ end
23
+
24
+ def self.size(file)
25
+ self.open(file, 'r') do |f|
26
+ f.size
27
+ end
28
+ end
29
+
30
+ def size
31
+ @file.content.size
32
+ end
33
+
34
+ alias read_without_hax read
35
+ def read(length = nil)
36
+ if length
37
+ @position ||= 0
38
+ data = @file.content[@position, length]
39
+ @position += length
40
+ data
41
+ else
42
+ read_without_hax
43
+ end
44
+ end
45
+
46
+ def rewind
47
+ @position = 0
48
+ end
49
+ end
50
+ end
data/spec/gpg.public ADDED
Binary file
data/spec/gpg.sekrit ADDED
Binary file
data/spec/helpers.rb ADDED
@@ -0,0 +1,270 @@
1
+ module Helpers
2
+ PUBLIC_KEY_PATH = File.dirname(__FILE__) + '/gpg.public'
3
+ PRIVATE_KEY_PATH = File.dirname(__FILE__) + '/gpg.sekrit'
4
+ PUBLIC_KEY_ID = '4D5AA1CE74A97ADB'
5
+
6
+ def run_before
7
+ public_key, private_key = read_keys
8
+
9
+ FakeWeb.clean_registry
10
+ FakeWeb.passthrough_uri(:any, %r{^https?://s3.amazonaws.com/})
11
+
12
+ FakeWeb.register_uri(:get, "http://169.254.169.254/latest/meta-data/local-hostname", :body => "localhost")
13
+ FakeWeb.register_uri(:get, "http://169.254.169.254/latest/meta-data/instance-id", :body => "i-awesome")
14
+
15
+ FakeWeb.register_uri(:get, %r{https://ec2.amazonaws.com/.*}, :status => 200)
16
+ @mock_environment_name = 'myenv'
17
+
18
+ FakeWeb.register_uri(:post, "http://example.org/api/environments", :body => JSON.generate({
19
+ @mock_environment_name => {:id => 1}
20
+ }))
21
+
22
+ FileUtils.mkdir_p(tmp_dir)
23
+ Dir.glob("#{tmp_dir}/*").each do |f|
24
+ FileUtils.rm(f)
25
+ end
26
+
27
+ stub_configs
28
+ end
29
+
30
+ def stub_configs
31
+ # print "in stub_configs: #{YAML::dump(@database_config)}, #{YAML::dump(spec_config)}\n"
32
+ config = @database_config || spec_config
33
+ config = config.merge({ :tmp_dir => tmp_dir })
34
+ # print "in stub_configs2: #{YAML::dump(config)}\n"
35
+ File.open(backup_config_file, "w") do |f|
36
+ f.puts YAML.dump(config )
37
+ end
38
+ end
39
+
40
+ def import_gpg
41
+ system("gpg --import #{PUBLIC_KEY_PATH}") || raise("Could not import public key")
42
+ end
43
+
44
+ def write_keys(public_key, private_key)
45
+ File.open('gpg.public', 'w') do |f|
46
+ f << public_key
47
+ end
48
+
49
+ File.open('gpg.sekrit', 'w') do |f|
50
+ f << private_key
51
+ end
52
+ end
53
+
54
+ def read_keys
55
+ return File.read(PUBLIC_KEY_PATH), File.read(PRIVATE_KEY_PATH)
56
+ end
57
+
58
+ def run_after
59
+ FileUtils.rm_f(backup_config_file)
60
+ filenames = Dir.glob("#{tmp_dir}/*")
61
+ if filenames.any?
62
+ raise "failed to remove #{filenames.join(' ')}"
63
+ end
64
+ end
65
+
66
+ def teardown_dna(data)
67
+ FileUtils.rm_f("#{tmp_dir}/dna.json")
68
+ end
69
+
70
+ def setup_dna(data)
71
+ FileUtils.mkdir_p("#{tmp_dir}")
72
+ if File.exist?("#{tmp_dir}/dna.json")
73
+ FileUtils.rm("#{tmp_dir}/dna.json")
74
+ end
75
+ File.open("#{tmp_dir}/dna.json", "w") do |f|
76
+ f.puts data.to_json
77
+ end
78
+ end
79
+
80
+ def backup_config_file
81
+ "#{tmp_dir}/spec_backups.yml"
82
+ end
83
+
84
+ def mysql_password_option
85
+ mysql_password.blank? ? "" : "-p#{mysql_password}"
86
+ end
87
+
88
+ def generate_database_name(type)
89
+ "ey_flex_#{type}_db_#{/\w{5,7}/.gen}"
90
+ end
91
+
92
+ def create_mysql_database(db_key, region = 'us-east-1')
93
+ db_name = generate_database_name('mysql')
94
+ system(%Q{mysql -u#{mysql_user} -h#{mysql_host} #{mysql_password_option} -e "drop database if exists #{db_name};"})
95
+ system(%Q{mysql -u#{mysql_user} -h#{mysql_host} #{mysql_password_option} -e "create database #{db_name};"}) || raise("Could not create db: #{db_name}")
96
+ created_mysql_dbs[db_key] = db_name
97
+ write_database_config('mysql', mysql_user, mysql_password, mysql_host, created_mysql_dbs.values, region)
98
+ db_name
99
+ end
100
+
101
+ def drop_postgresql_database(db_key)
102
+ db_name = created_postgresql_dbs[db_key]
103
+ # system("PGPASSWORD='#{postgresql_password}' dropdb -U #{postgresql_user} -h #{postgresql_host} #{db_name}")
104
+ command=%Q{ssh -F /dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -i /home/#{postgresql_user}/.ssh/internal root@#{postgresql_host} "dropdb -U postgres -h localhost #{db_name}"}
105
+ system(command)
106
+ end
107
+
108
+ def check_postgresql_database(db_name)
109
+ command=%Q{ssh -F /dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -i /home/#{postgresql_user}/.ssh/internal root@#{postgresql_host} "psql -l -U #{postgresql_user} -h localhost | grep '#{db_name}'"}
110
+ system(command) || raise("Could not find db: #{db_name} with command:\n #{command}")
111
+ db_name
112
+ end
113
+
114
+ def create_postgresql_database(db_key, region = 'us-east-1')
115
+ db_name = generate_database_name('postgresql')
116
+ created_postgresql_dbs[db_key] = db_name
117
+ drop_postgresql_database(db_key)
118
+ # ssh -F /dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -i /home/$USER/.ssh/internal root@$DB_HOST "createdb -U postgres -h localhost -O $USER testdbreallytestingweee8"
119
+ # system("PGPASSWORD='#{postgresql_password}' createdb -U #{postgresql_user} -h #{postgresql_host} #{db_name}") || raise("Could not create db: #{db_name}")
120
+ command=%Q{ssh -F /dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -i /home/#{postgresql_user}/.ssh/internal root@#{postgresql_host} "createdb -U postgres -h localhost -O #{postgresql_user} #{db_name}"}
121
+ system(command) || raise("Could not create db: #{db_name} with command:\n #{command}")
122
+ write_database_config('postgresql', postgresql_user, postgresql_password, postgresql_host, created_postgresql_dbs.values, region)
123
+ db_name
124
+ end
125
+
126
+ def write_database_config(type, root_user, root_password, host, databases, region)
127
+ @database_config = {
128
+ :env => 'production',
129
+ :dbuser => root_user,
130
+ :dbpass => root_password,
131
+ :dbhost => host,
132
+ :aws_secret_id => spec_config['aws_secret_id'] || spec_config[:aws_secret_id],
133
+ :aws_secret_key => spec_config['aws_secret_key'] || spec_config[:aws_secret_key],
134
+ :databases => databases,
135
+ :keep => keep,
136
+ :region => region,
137
+ :backup_bucket => "ey-backup-test-#{region}",
138
+ }
139
+ stub_configs
140
+ end
141
+
142
+ def set_keep(keep)
143
+ @keep = keep
144
+ end
145
+
146
+ def keep
147
+ @keep || 5
148
+ end
149
+
150
+ def run_sql(command, database)
151
+ system("echo '#{command}' |mysql -u#{mysql_user} -h#{mysql_host} #{mysql_password_option} -D#{database}")
152
+ end
153
+
154
+ def run_sql_pipe(io, database)
155
+ pid, stdin, _, stderr = Open4.popen4("mysql -u#{mysql_user} -h#{mysql_host} #{mysql_password_option} -D #{database}")
156
+
157
+ stdin << io.read
158
+ stdin.flush
159
+ stdin.close
160
+
161
+ ignored, status = Process::waitpid2 pid
162
+
163
+ status
164
+ end
165
+
166
+ def run_psql(command, database)
167
+ system("PGPASSWORD='#{postgresql_password}' psql -c '#{command}' -h #{postgresql_host} -U #{postgresql_user} #{database}")
168
+ end
169
+
170
+ def tmp_dir
171
+ if spec_config['tmp_dir'].blank?
172
+ File.dirname(__FILE__) + "/tmp/"
173
+ else
174
+ spec_config['tmp_dir'] || File.dirname("./tmp/")
175
+ end
176
+ end
177
+
178
+ def mysql_user
179
+ spec_config['mysql_user'] || raise("No mysql user provided")
180
+ end
181
+
182
+ def postgresql_user
183
+ spec_config['postgresql_user'] || raise("No postgresql user provided")
184
+ end
185
+
186
+ def mysql_password
187
+ spec_config['mysql_password']
188
+ end
189
+
190
+ def postgresql_password
191
+ spec_config['postgresql_password']
192
+ end
193
+
194
+ def mysql_host
195
+ spec_config['mysql_host']
196
+ end
197
+
198
+ def postgresql_host
199
+ spec_config['postgresql_host']
200
+ end
201
+
202
+ def created_mysql_dbs
203
+ @created_mysql_dbs ||= {}
204
+ end
205
+
206
+ def created_postgresql_dbs
207
+ @created_postgresql_dbs ||= {}
208
+ end
209
+
210
+ def spec_config
211
+ Helpers.spec_config
212
+ end
213
+
214
+ def self.spec_config
215
+ @spec_config
216
+ end
217
+
218
+ def self.load_spec_config
219
+ filename = File.dirname(__FILE__) + '/config.yml'
220
+ if File.exist?(filename)
221
+ @spec_config = YAML.load_file(filename)
222
+ else
223
+ abort "Please setup your spec/config.yml file"
224
+ end
225
+ end
226
+
227
+ def last_stdout
228
+ @last_stdout || raise("Run a command with `capturing_stdio' before checking `last_stdout'")
229
+ end
230
+
231
+ def last_stderr
232
+ @last_stderr || raise("Run a command with `capturing_stdio' before checking `last_stderr'")
233
+ end
234
+
235
+ def last_exit_code
236
+ @last_exit_code || raise("Run a command with `capturing_stdio' before checking `last_exit_code'")
237
+ end
238
+
239
+ def io_logger
240
+ @io_logger ||= Logger.new(RealFile.dirname(__FILE__) + '/../tmp/io.log')
241
+ end
242
+
243
+ def capturing_stdio(&block)
244
+ io_logger.debug "beginning to capture IO"
245
+ @last_stdout, @last_stderr = "", ""
246
+ old_stdout, old_stderr = $stdout, $stderr
247
+ new_stdout, new_stderr = StringIO.new(@last_stdout), StringIO.new(@last_stderr)
248
+ $stdout, $stderr = new_stdout, new_stderr
249
+ yield
250
+ exit 0
251
+ rescue SystemExit => e
252
+ @last_exit_code = e.status
253
+ [new_stdout.string.split("\n"), new_stderr.string.split("\n")]
254
+ ensure
255
+ io_logger.debug "stdout: \n#{new_stdout.string}"
256
+ io_logger.debug "stderr: \n#{new_stderr.string}"
257
+ io_logger.debug "finished capturing IO"
258
+ $stdout, $stderr = old_stdout, old_stderr
259
+ end
260
+
261
+ def reset_logger
262
+ EY::Backup.logger = EY::Backup::Logger.new(StringIO.new, StringIO.new)
263
+ end
264
+
265
+ def stdout
266
+ EY::Backup.logger.stdout.string
267
+ end
268
+
269
+ load_spec_config
270
+ end