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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 621371200d0de25b03a94a5cc486bbfd8438950f
4
- data.tar.gz: 96dd6d55756ac5d0cfa26c0eda7aa24099f31e1b
3
+ metadata.gz: 05cb2fd58e6ef73cdebaf8a54f53f97b61471fbe
4
+ data.tar.gz: bbc7fdc1d6a622195e58617740beac87968ab30a
5
5
  SHA512:
6
- metadata.gz: 83d4f4053ac906dd77c8c2805f1e03ad524a54205bd8484086b2ade857ad48b8a7f90b54769a6f0a304c4df49719f99a2ef5c4d7861e64f31f4a61d8c05fbac8
7
- data.tar.gz: f0b10f1d68af8e9e2028007dac8f1a4ad26b2692a287b4f2c3d465998e28da66760bb82e887eac2e6337d45b4c4be6b570f338f817693dcdeacecaf635798b09
6
+ metadata.gz: 91ce1939251bd21312769b9f76172c8272c634a6532ff808eaeb41bcca67addd553e20dfdb709c542eaaac9c48288dba163e7821b32b600296cac0446647ad3c
7
+ data.tar.gz: 874c6d3d1969022aa545905acb9ff9f0ce94b10658e34078486b79d7041ef8d674d752742b0389db2e4637fd38721b3a5d09c3a7aeeb71addba3d84e710c19f8
@@ -3,3 +3,9 @@ Initial release
3
3
  - Sets up gem
4
4
  - Adds dependencies
5
5
  - Exposes rake tasks
6
+
7
+ # 0.2.0
8
+ - Add extra logging around S3 upload
9
+ - Fix S3 download
10
+ - Stop logging DB passwords
11
+ - Allow multiple loggers to be used
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_sanitization (0.1.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.8.3)
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 logger that the gem should use.
109
+ # The loggers that the gem should use.
110
110
  # This will default to STOUT if non is provided
111
- config.logger = Rails.logger
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
@@ -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.8.3"
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"
@@ -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, :logger, :root, :s3_bucket_region
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
- @logger = Logger.new(STDOUT)
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.logger.info(output) unless self.configuration.env == 'test'
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=#{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
+ 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.get_s3_bucket
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
- client = Aws::S3::Client.new(credentials: creds, region: self.configuration.s3_bucket_region)
175
- resource = Aws::S3::Resource.new(client: client)
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
- self.upload(compressed_dump_file) if self.configuration.s3_bucket && self.configuration.aws_access_key_id && self.configuration.aws_secret_access_key
237
- self.clean_up_files(dump_file, compressed_dump_file) unless self.configuration.s3_bucket && self.configuration.aws_access_key_id && self.configuration.aws_secret_access_key
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 tmp, then wipe your DB before importing a snapshot')
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
- self.log("Downloading file to #{compressed_dump_file}")
275
- url = objects.first.object.presigned_url(:get, expires_in: 600)
276
- system("curl -o #{compressed_dump_file} '#{url}'")
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
- system("gunzip -c '#{compressed_dump_file}' | mysql -uroot #{self.configuration.db_config['database']}")
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
@@ -1,3 +1,3 @@
1
1
  module ActiveSanitization
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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-03 00:00:00.000000000 Z
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.8.3
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.8.3
27
+ version: 1.9.1
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement