active_sanitization 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +2 -2
- data/README.md +2 -2
- data/active_sanitization.gemspec +1 -1
- data/lib/active_sanitization.rb +28 -16
- data/lib/active_sanitization/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05cb2fd58e6ef73cdebaf8a54f53f97b61471fbe
|
4
|
+
data.tar.gz: bbc7fdc1d6a622195e58617740beac87968ab30a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91ce1939251bd21312769b9f76172c8272c634a6532ff808eaeb41bcca67addd553e20dfdb709c542eaaac9c48288dba163e7821b32b600296cac0446647ad3c
|
7
|
+
data.tar.gz: 874c6d3d1969022aa545905acb9ff9f0ce94b10658e34078486b79d7041ef8d674d752742b0389db2e4637fd38721b3a5d09c3a7aeeb71addba3d84e710c19f8
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
active_sanitization (0.
|
4
|
+
active_sanitization (0.2.0)
|
5
5
|
aws-sdk (~> 2.0.33)
|
6
6
|
|
7
7
|
GEM
|
@@ -147,7 +147,7 @@ PLATFORMS
|
|
147
147
|
DEPENDENCIES
|
148
148
|
active_sanitization!
|
149
149
|
activerecord (~> 4.2.1)
|
150
|
-
bundler (~> 1.
|
150
|
+
bundler (~> 1.9.1)
|
151
151
|
byebug (~> 4.0.4)
|
152
152
|
mysql2 (~> 0.3.18)
|
153
153
|
pry (~> 0.10.1)
|
data/README.md
CHANGED
@@ -106,9 +106,9 @@ ActiveSanitization.configure do |config|
|
|
106
106
|
# The name of your app
|
107
107
|
config.app_name = 'super_secret_app'
|
108
108
|
|
109
|
-
# The
|
109
|
+
# The loggers that the gem should use.
|
110
110
|
# This will default to STOUT if non is provided
|
111
|
-
config.
|
111
|
+
config.loggers = [Rails.logger]
|
112
112
|
|
113
113
|
# The path to the root of your project
|
114
114
|
# This is required so the database dump can be put in a tmp folder
|
data/active_sanitization.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_development_dependency "bundler", "~> 1.
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9.1"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.2.0"
|
25
25
|
spec.add_development_dependency "rails", "~> 4.2.1"
|
data/lib/active_sanitization.rb
CHANGED
@@ -15,7 +15,7 @@ module ActiveSanitization
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class Configuration
|
18
|
-
attr_accessor :tables_to_sanitize, :tables_to_truncate, :tables_to_ignore, :sanitization_columns, :s3_bucket, :app_name, :aws_access_key_id, :aws_secret_access_key, :env, :active_record_connection, :db_config, :custom_sanitization, :
|
18
|
+
attr_accessor :tables_to_sanitize, :tables_to_truncate, :tables_to_ignore, :sanitization_columns, :s3_bucket, :app_name, :aws_access_key_id, :aws_secret_access_key, :env, :active_record_connection, :db_config, :custom_sanitization, :loggers, :root, :s3_bucket_region
|
19
19
|
|
20
20
|
def initialize
|
21
21
|
@tables_to_sanitize = {}
|
@@ -26,7 +26,7 @@ module ActiveSanitization
|
|
26
26
|
@env = ENV['RACK_ENV'] || ENV['RAILS_ENV']
|
27
27
|
@active_record_connection = ActiveRecord::Base.connection
|
28
28
|
@root = File.dirname(File.dirname(__FILE__))
|
29
|
-
@
|
29
|
+
@loggers = [Logger.new(STDOUT)]
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -60,7 +60,9 @@ module ActiveSanitization
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def self.log(output)
|
63
|
-
self.configuration.
|
63
|
+
self.configuration.loggers.each do |logger|
|
64
|
+
logger.info(output)
|
65
|
+
end unless self.configuration.env == 'test'
|
64
66
|
end
|
65
67
|
|
66
68
|
def self.pre_sanitization_checks
|
@@ -106,7 +108,7 @@ module ActiveSanitization
|
|
106
108
|
raise "Failed to load DB #{self.configuration.db_config} into temp DB #{temp_db}."
|
107
109
|
end
|
108
110
|
|
109
|
-
self.log("mysqldump -h #{self.configuration.db_config['host']} -u #{self.configuration.db_config['username']} --password
|
111
|
+
self.log("mysqldump -h #{self.configuration.db_config['host']} -u #{self.configuration.db_config['username']} --password=XXXXXXXXX --no-data #{self.configuration.db_config['database']} #{self.configuration.tables_to_truncate.keys.join(' ')} | mysql -h #{self.configuration.db_config['host']} -u #{self.configuration.db_config['username']} --password=XXXXXXXXX -D #{temp_db}")
|
110
112
|
system("mysqldump -h #{self.configuration.db_config['host']} -u #{self.configuration.db_config['username']} --password=#{self.configuration.db_config['password']} --no-data #{self.configuration.db_config['database']} #{self.configuration.tables_to_truncate.keys.join(' ')} | mysql -h #{self.configuration.db_config['host']} -u #{self.configuration.db_config['username']} --password=#{self.configuration.db_config['password']} -D #{temp_db}")
|
111
113
|
if $?.exitstatus == 0
|
112
114
|
self.log("Temp DB created and populated")
|
@@ -169,16 +171,20 @@ module ActiveSanitization
|
|
169
171
|
system("gzip '#{dump_file}'")
|
170
172
|
end
|
171
173
|
|
172
|
-
def self.
|
174
|
+
def self.get_s3_client
|
173
175
|
creds = Aws::Credentials.new(self.configuration.aws_access_key_id, self.configuration.aws_secret_access_key)
|
174
|
-
|
175
|
-
|
176
|
+
Aws::S3::Client.new(credentials: creds, region: self.configuration.s3_bucket_region)
|
177
|
+
end
|
178
|
+
|
179
|
+
def self.get_s3_bucket
|
180
|
+
resource = Aws::S3::Resource.new(client: get_s3_client)
|
176
181
|
resource.bucket(self.configuration.s3_bucket)
|
177
182
|
end
|
178
183
|
|
179
184
|
def self.upload(compressed_dump_file)
|
180
185
|
timestamp = DateTime.now.strftime('%Y%m%d%H%M%S')
|
181
186
|
name = "#{self.configuration.app_name}/#{self.configuration.env}/mysql/#{timestamp}/#{File.basename(compressed_dump_file)}"
|
187
|
+
self.log("Uploading to bucket: #{self.configuration.s3_bucket}, path: #{name}")
|
182
188
|
file = File.open(compressed_dump_file, 'r')
|
183
189
|
|
184
190
|
bucket = get_s3_bucket
|
@@ -232,9 +238,15 @@ module ActiveSanitization
|
|
232
238
|
|
233
239
|
self.clean_up_temp_db(temp_db)
|
234
240
|
end
|
241
|
+
|
235
242
|
self.gzip(dump_file)
|
236
|
-
|
237
|
-
|
243
|
+
|
244
|
+
if self.configuration.s3_bucket && self.configuration.aws_access_key_id && self.configuration.aws_secret_access_key
|
245
|
+
self.upload(compressed_dump_file)
|
246
|
+
else
|
247
|
+
self.clean_up_files(dump_file, compressed_dump_file)
|
248
|
+
end
|
249
|
+
|
238
250
|
self.log("-- DONE --")
|
239
251
|
else
|
240
252
|
self.log(checks[:error])
|
@@ -256,7 +268,7 @@ module ActiveSanitization
|
|
256
268
|
return
|
257
269
|
end
|
258
270
|
|
259
|
-
self.log('WARNING: this rake task will dump your MySQL DB to
|
271
|
+
self.log('WARNING: this rake task will dump your MySQL DB to a file, then wipe your DB before importing a snapshot')
|
260
272
|
local_dump_file = "#{File.join(self.configuration.root, "tmp")}/local_data.dump"
|
261
273
|
|
262
274
|
# Make copy of local DB just in case something goes wrong
|
@@ -267,13 +279,12 @@ module ActiveSanitization
|
|
267
279
|
raise "Failed to create a local DB dump. If a previous local dump exists, please delete it and try again."
|
268
280
|
end
|
269
281
|
|
270
|
-
# get all the files in the snapshot
|
271
|
-
objects = bucket.objects("#{prefix}/#{timestamp}")
|
272
282
|
dump_file = "#{File.join(self.configuration.root, "tmp")}/data.dump"
|
273
283
|
compressed_dump_file = "#{dump_file}.gz"
|
274
|
-
|
275
|
-
|
276
|
-
|
284
|
+
|
285
|
+
name = "#{prefix}/#{timestamp}/data.dump.gz"
|
286
|
+
self.log("Downloading dump from bucket: #{self.configuration.s3_bucket}, path: #{name}")
|
287
|
+
get_s3_client.get_object({ bucket:self.configuration.s3_bucket , key: name }, target: compressed_dump_file)
|
277
288
|
|
278
289
|
# reset db
|
279
290
|
self.log("Recreating your local DB")
|
@@ -282,7 +293,8 @@ module ActiveSanitization
|
|
282
293
|
|
283
294
|
# Import data
|
284
295
|
self.log("Unzipping and importing data...")
|
285
|
-
|
296
|
+
self.log("gunzip < #{compressed_dump_file} | mysql -u root #{self.configuration.db_config['database']}")
|
297
|
+
system("gunzip < #{compressed_dump_file} | mysql -u root #{self.configuration.db_config['database']}")
|
286
298
|
if $?.exitstatus == 0
|
287
299
|
File.delete(compressed_dump_file) if File.exist?(compressed_dump_file)
|
288
300
|
else
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_sanitization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Haley
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-04-
|
12
|
+
date: 2015-04-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 1.
|
20
|
+
version: 1.9.1
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: 1.
|
27
|
+
version: 1.9.1
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rake
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|