kpm 0.7.2 → 0.10.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.
Files changed (79) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +138 -0
  4. data/Gemfile +2 -0
  5. data/README.adoc +144 -107
  6. data/Rakefile +2 -1
  7. data/bin/kpm +4 -2
  8. data/kpm.gemspec +11 -8
  9. data/lib/kpm.rb +3 -0
  10. data/lib/kpm/account.rb +268 -338
  11. data/lib/kpm/base_artifact.rb +33 -39
  12. data/lib/kpm/base_installer.rb +69 -83
  13. data/lib/kpm/blob.rb +29 -0
  14. data/lib/kpm/cli.rb +3 -1
  15. data/lib/kpm/coordinates.rb +10 -12
  16. data/lib/kpm/database.rb +94 -113
  17. data/lib/kpm/diagnostic_file.rb +126 -147
  18. data/lib/kpm/formatter.rb +76 -48
  19. data/lib/kpm/inspector.rb +24 -34
  20. data/lib/kpm/installer.rb +53 -46
  21. data/lib/kpm/kaui_artifact.rb +4 -3
  22. data/lib/kpm/killbill_plugin_artifact.rb +10 -7
  23. data/lib/kpm/killbill_server_artifact.rb +13 -12
  24. data/lib/kpm/migrations.rb +26 -11
  25. data/lib/kpm/nexus_helper/actions.rb +52 -9
  26. data/lib/kpm/nexus_helper/cloudsmith_api_calls.rb +83 -0
  27. data/lib/kpm/nexus_helper/github_api_calls.rb +70 -0
  28. data/lib/kpm/nexus_helper/nexus_api_calls_v2.rb +130 -108
  29. data/lib/kpm/nexus_helper/nexus_facade.rb +5 -3
  30. data/lib/kpm/plugins_directory.rb +9 -8
  31. data/lib/kpm/plugins_directory.yml +14 -173
  32. data/lib/kpm/plugins_manager.rb +29 -24
  33. data/lib/kpm/sha1_checker.rb +31 -18
  34. data/lib/kpm/system.rb +104 -135
  35. data/lib/kpm/system_helpers/cpu_information.rb +56 -55
  36. data/lib/kpm/system_helpers/disk_space_information.rb +60 -63
  37. data/lib/kpm/system_helpers/entropy_available.rb +37 -39
  38. data/lib/kpm/system_helpers/memory_information.rb +52 -51
  39. data/lib/kpm/system_helpers/os_information.rb +45 -47
  40. data/lib/kpm/system_helpers/system_proxy.rb +10 -10
  41. data/lib/kpm/tasks.rb +381 -438
  42. data/lib/kpm/tenant_config.rb +68 -83
  43. data/lib/kpm/tomcat_manager.rb +10 -8
  44. data/lib/kpm/trace_logger.rb +18 -16
  45. data/lib/kpm/uninstaller.rb +81 -14
  46. data/lib/kpm/utils.rb +13 -14
  47. data/lib/kpm/version.rb +3 -1
  48. data/packaging/Gemfile +2 -0
  49. data/pom.xml +211 -40
  50. data/spec/kpm/remote/base_artifact_spec.rb +20 -20
  51. data/spec/kpm/remote/base_installer_spec.rb +35 -34
  52. data/spec/kpm/remote/cloudsmith_api_calls_spec.rb +40 -0
  53. data/spec/kpm/remote/github_api_calls_spec.rb +40 -0
  54. data/spec/kpm/remote/installer_spec.rb +80 -79
  55. data/spec/kpm/remote/kaui_artifact_spec.rb +7 -6
  56. data/spec/kpm/remote/killbill_plugin_artifact_spec.rb +25 -30
  57. data/spec/kpm/remote/killbill_server_artifact_spec.rb +17 -16
  58. data/spec/kpm/remote/migrations_spec.rb +12 -11
  59. data/spec/kpm/remote/nexus_facade_spec.rb +32 -28
  60. data/spec/kpm/remote/tenant_config_spec.rb +30 -29
  61. data/spec/kpm/remote/tomcat_manager_spec.rb +4 -3
  62. data/spec/kpm/unit/actions_spec.rb +52 -0
  63. data/spec/kpm/unit/base_artifact_spec.rb +19 -18
  64. data/spec/kpm/unit/cpu_information_spec.rb +67 -0
  65. data/spec/kpm/unit/disk_space_information_spec.rb +47 -0
  66. data/spec/kpm/unit/entropy_information_spec.rb +36 -0
  67. data/spec/kpm/unit/formatter_spec.rb +163 -0
  68. data/spec/kpm/unit/inspector_spec.rb +34 -42
  69. data/spec/kpm/unit/installer_spec.rb +7 -6
  70. data/spec/kpm/unit/memory_information_spec.rb +102 -0
  71. data/spec/kpm/unit/os_information_spec.rb +38 -0
  72. data/spec/kpm/unit/plugins_directory_spec.rb +38 -22
  73. data/spec/kpm/unit/plugins_manager_spec.rb +62 -66
  74. data/spec/kpm/unit/sha1_checker_spec.rb +107 -60
  75. data/spec/kpm/unit/uninstaller_spec.rb +118 -72
  76. data/spec/kpm/unit_mysql/account_spec.rb +127 -142
  77. data/spec/spec_helper.rb +20 -18
  78. data/tasks/package.rake +18 -18
  79. metadata +42 -22
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env rake
2
+ # frozen_string_literal: true
2
3
 
3
4
  Dir.glob('tasks/*.rake').each { |r| import r }
4
5
 
@@ -33,4 +34,4 @@ namespace :test do
33
34
  end
34
35
 
35
36
  # Run tests by default
36
- task :default => 'test:spec'
37
+ task default: 'test:spec'
data/bin/kpm CHANGED
@@ -1,10 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
- $:.push File.expand_path("../../lib", __FILE__)
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.push File.expand_path('../lib', __dir__)
3
5
  require 'kpm'
4
6
 
5
7
  begin
6
8
  KPM::Cli.start
7
- rescue => e
9
+ rescue StandardError => e
8
10
  KPM.ui.say "#{e.message}\n#{e.backtrace.join("\n")}", :red
9
11
  exit 1
10
12
  end
data/kpm.gemspec CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Copyright 2014 The Billing Project, LLC
3
5
  #
@@ -14,7 +16,7 @@
14
16
  # under the License.
15
17
  #
16
18
 
17
- $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
19
+ $LOAD_PATH.unshift File.expand_path('lib', __dir__)
18
20
  require 'kpm/version'
19
21
 
20
22
  Gem::Specification.new do |s|
@@ -34,17 +36,18 @@ Gem::Specification.new do |s|
34
36
  s.files = `git ls-files`.split("\n")
35
37
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
36
38
  s.bindir = 'bin'
37
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
38
- s.require_paths = ["lib"]
39
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
40
+ s.require_paths = ['lib']
39
41
 
40
42
  s.rdoc_options << '--exclude' << '.'
41
43
 
42
44
  s.add_dependency 'highline', '~> 1.6.21'
43
- s.add_dependency 'thor', '~> 0.19.1'
45
+ s.add_dependency 'killbill-client', '~> 3.2'
44
46
  s.add_dependency 'rubyzip', '~>1.2.0'
45
- s.add_dependency 'killbill-client', '~> 2.5.0'
47
+ s.add_dependency 'thor', '~> 0.19.1'
46
48
 
47
- s.add_development_dependency 'rake', '>= 10.0.0', '< 11.0.0'
48
- s.add_development_dependency 'rspec', '~> 2.12.0'
49
+ s.add_development_dependency 'gem-release', '~> 2.2'
50
+ s.add_development_dependency 'rake', '~> 13.0'
51
+ s.add_development_dependency 'rspec', '~> 3.9'
52
+ s.add_development_dependency 'rubocop', '~> 0.88.0' if RUBY_VERSION >= '2.4'
49
53
  end
50
-
data/lib/kpm.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module KPM
2
4
  autoload :Utils, 'kpm/utils'
3
5
  autoload :BaseArtifact, 'kpm/base_artifact'
@@ -19,6 +21,7 @@ module KPM
19
21
  autoload :Migrations, 'kpm/migrations'
20
22
  autoload :System, 'kpm/system'
21
23
  autoload :Account, 'kpm/account'
24
+ autoload :Blob, 'kpm/blob'
22
25
  autoload :Database, 'kpm/database'
23
26
  autoload :TenantConfig, 'kpm/tenant_config'
24
27
  autoload :DiagnosticFile, 'kpm/diagnostic_file'
data/lib/kpm/account.rb CHANGED
@@ -1,17 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'net/http'
2
4
  require 'tmpdir'
3
5
  require 'yaml'
4
6
  require 'date'
5
7
  require 'securerandom'
8
+ require 'base64'
6
9
  require 'killbill_client'
7
10
 
8
11
  module KPM
9
-
10
12
  class Account
11
-
12
13
  # Killbill server
13
14
  KILLBILL_HOST = ENV['KILLBILL_HOST'] || '127.0.0.1'
14
- KILLBILL_URL = 'http://'.concat(KILLBILL_HOST).concat(':8080')
15
+ KILLBILL_URL = "http://#{KILLBILL_HOST}:8080"
15
16
  KILLBILL_API_VERSION = '1.0'
16
17
 
17
18
  # USER/PWD
@@ -24,7 +25,7 @@ module KPM
24
25
 
25
26
  # Temporary directory
26
27
  TMP_DIR_PEFIX = 'killbill'
27
- TMP_DIR = Dir.mktmpdir(TMP_DIR_PEFIX);
28
+ TMP_DIR = Dir.mktmpdir(TMP_DIR_PEFIX)
28
29
 
29
30
  # Created By
30
31
  WHO = 'kpm_export_import'
@@ -34,76 +35,72 @@ module KPM
34
35
  PLUGIN_NAME_COLUMN = 'plugin_name'
35
36
 
36
37
  # fields to remove from the export files
37
- REMOVE_DATA_FROM = {:accounts => [:name, :address1, :address2, :city, :state_or_province, :phone, :email],
38
- :account_history => [:name, :address1, :address2, :city, :state_or_province, :phone, :email]}
38
+ REMOVE_DATA_FROM = { accounts: %i[name address1 address2 city state_or_province phone email],
39
+ account_history: %i[name address1 address2 city state_or_province phone email] }.freeze
39
40
 
40
- DATE_COLUMNS_TO_FIX = ['created_date','updated_date','processing_available_date','effective_date',
41
- 'boot_date','start_timestamp','last_access_time','payment_date','original_created_date',
42
- 'last_sys_update_date','charged_through_date','bundle_start_date','start_date', 'reference_time']
41
+ DATE_COLUMNS_TO_FIX = %w[created_date updated_date processing_available_date effective_date
42
+ boot_date start_timestamp last_access_time payment_date original_created_date
43
+ last_sys_update_date charged_through_date bundle_start_date start_date catalog_effective_date reference_time].freeze
43
44
 
44
45
  # round trip constants duplicate record
45
- ROUND_TRIP_EXPORT_IMPORT_MAP = {:accounts => {:id => :accounts_id, :external_key => :accounts_id}, :all => {:account_id => :accounts_id},
46
- :account_history => {:id => :account_history_id, :external_key => :accounts_id, :payment_method_id => :payment_methods_id},
47
- :account_emails => {:id => :account_emails_id}, :account_email_history => {:id => :account_email_history_id},
48
- :subscription_events => {:id => :subscription_events_id},:subscriptions => {:id => :subscriptions_id},
49
- :bundles => {:id => :bundles_id},:blocking_states => {:id => :blocking_states_id, :blockable_id => nil},
50
- :invoice_items => {:id => :invoice_items_id, :child_account_id => nil, :invoice_id => :invoices_id, :bundle_id => :bundles_id, :subscription_id => :subscriptions_id },
51
- :invoices => {:id => :invoices_id},
52
- :invoice_payments => {:id => :invoice_payments_id, :invoice_id => :invoices_id, :payment_id => :payments_id},
53
- :invoice_parent_children => {:id => :invoice_parent_children_id, :parent_invoice_id => nil, :child_invoice_id => nil, :child_account_id => nil},
54
- :payment_attempts => {:id => :payment_attempts_id, :payment_method_id => :payment_methods_id, :transaction_id => :payment_transactions_id},
55
- :payment_attempt_history => {:id => :payment_attempt_history_id, :payment_method_id => :payment_methods_id, :transaction_id => :payment_transactions_id},
56
- :payment_methods => {:id => :payment_methods_id, :external_key => :generate},:payment_method_history => {:id => :payment_method_history_id},
57
- :payments => {:id => :payments_id, :payment_method_id => :payment_methods_id},
58
- :payment_history => {:id => :payment_history_id, :payment_method_id => :payment_methods_id},
59
- :payment_transactions => {:id => :payment_transactions_id, :payment_id => :payments_id},
60
- :payment_transaction_history => {:id => :payment_transaction_history_id, :payment_id => :payments_id},
61
- :_invoice_payment_control_plugin_auto_pay_off => {:payment_method_id => :payment_methods_id, :payment_id => :payments_id},
62
- :rolled_up_usage => {:id => :rolled_up_usage_id, :subscription_id => :subscriptions_id, :tracking_id => nil},
63
- :custom_fields => {:id => :custom_fields_id},:custom_field_history => {:id => :custom_field_history_id},
64
- :tag_definitions => {:id => :tag_definitions_id},:tag_definition_history => {:id => :tag_definition_history_id},
65
- :tags => {:id => :tags_id, :object_id => nil},
66
- :tag_history => {:id => :tag_history_id, :object_id => nil},
67
- :audit_log => {:id => :audit_log_id}
68
- }
69
-
70
- #delimeters to sniff
71
- DELIMITERS = [',','|']
72
- DEFAULT_DELIMITER = "|"
46
+ ROUND_TRIP_EXPORT_IMPORT_MAP = { accounts: { id: :accounts_id, external_key: :accounts_id }, all: { account_id: :accounts_id },
47
+ account_history: { id: :account_history_id, external_key: :accounts_id, payment_method_id: :payment_methods_id },
48
+ account_emails: { id: :account_emails_id }, account_email_history: { id: :account_email_history_id },
49
+ subscription_events: { id: :subscription_events_id }, subscriptions: { id: :subscriptions_id },
50
+ bundles: { id: :bundles_id }, blocking_states: { id: :blocking_states_id, blockable_id: nil },
51
+ invoice_items: { id: :invoice_items_id, child_account_id: nil, invoice_id: :invoices_id, bundle_id: :bundles_id, subscription_id: :subscriptions_id },
52
+ invoices: { id: :invoices_id },
53
+ invoice_payments: { id: :invoice_payments_id, invoice_id: :invoices_id, payment_id: :payments_id },
54
+ invoice_parent_children: { id: :invoice_parent_children_id, parent_invoice_id: nil, child_invoice_id: nil, child_account_id: nil },
55
+ payment_attempts: { id: :payment_attempts_id, payment_method_id: :payment_methods_id, transaction_id: :payment_transactions_id },
56
+ payment_attempt_history: { id: :payment_attempt_history_id, payment_method_id: :payment_methods_id, transaction_id: :payment_transactions_id },
57
+ payment_methods: { id: :payment_methods_id, external_key: :generate }, payment_method_history: { id: :payment_method_history_id },
58
+ payments: { id: :payments_id, payment_method_id: :payment_methods_id },
59
+ payment_history: { id: :payment_history_id, payment_method_id: :payment_methods_id },
60
+ payment_transactions: { id: :payment_transactions_id, payment_id: :payments_id },
61
+ payment_transaction_history: { id: :payment_transaction_history_id, payment_id: :payments_id },
62
+ _invoice_payment_control_plugin_auto_pay_off: { payment_method_id: :payment_methods_id, payment_id: :payments_id },
63
+ rolled_up_usage: { id: :rolled_up_usage_id, subscription_id: :subscriptions_id, tracking_id: nil },
64
+ custom_fields: { id: :custom_fields_id }, custom_field_history: { id: :custom_field_history_id },
65
+ tag_definitions: { id: :tag_definitions_id }, tag_definition_history: { id: :tag_definition_history_id },
66
+ tags: { id: :tags_id, object_id: nil },
67
+ tag_history: { id: :tag_history_id, object_id: nil },
68
+ audit_log: { id: :audit_log_id } }.freeze
69
+
70
+ # delimeters to sniff
71
+ DELIMITERS = [',', '|'].freeze
72
+ DEFAULT_DELIMITER = '|'
73
+
74
+ B64_REGEX = %r{^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$}.freeze
73
75
 
74
76
  def initialize(config_file = nil, killbill_api_credentials = nil, killbill_credentials = nil, killbill_url = nil,
75
77
  database_name = nil, database_credentials = nil, database_host = nil, database_port = nil, data_delimiter = nil, logger = nil)
76
78
  @killbill_api_key = KILLBILL_API_KEY
77
- @killbill_api_secrets = KILLBILL_API_SECRET
79
+ @killbill_api_secret = KILLBILL_API_SECRET
78
80
  @killbill_url = KILLBILL_URL
79
81
  @killbill_user = KILLBILL_USER
80
82
  @killbill_password = KILLBILL_PASSWORD
81
83
  @delimiter = data_delimiter || DEFAULT_DELIMITER
82
84
  @logger = logger
83
- @tables_id = Hash.new
85
+ @tables_id = {}
84
86
 
87
+ set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
85
88
 
86
- set_killbill_options(killbill_api_credentials,killbill_credentials,killbill_url)
87
- set_database_options(database_host,database_port,database_name,database_credentials,logger)
89
+ database_credentials ||= [nil, nil]
90
+ @database = Database.new(database_name, database_host, database_port, database_credentials[0], database_credentials[1], logger)
88
91
 
89
92
  load_config_from_file(config_file)
90
-
91
93
  end
92
94
 
93
95
  def export_data(account_id = nil)
94
-
95
- if account_id === :export.to_s
96
- raise Interrupt, 'Need to specify an account id'
97
- end
96
+ raise Interrupt, 'Need to specify an account id' if account_id == :export.to_s
98
97
 
99
98
  export_data = fetch_export_data(account_id)
100
99
  export_file = export(export_data)
101
100
 
102
- unless File.exist?(export_file)
103
- raise Interrupt, 'Account id not found'
104
- else
105
- @logger.info "\e[32mData exported under #{export_file}\e[0m"
106
- end
101
+ raise Interrupt, 'Account id not found' unless File.exist?(export_file)
102
+
103
+ @logger.info "\e[32mData exported under #{export_file}\e[0m"
107
104
 
108
105
  export_file
109
106
  end
@@ -115,13 +112,9 @@ module KPM
115
112
  @tenant_record_id = tenant_record_id
116
113
  @round_trip_export_import = round_trip_export_import
117
114
 
118
- if source_file === :import.to_s
119
- raise Interrupt, 'Need to specify a file'
120
- end
115
+ raise Interrupt, 'Need to specify a file' if source_file == :import.to_s
121
116
 
122
- unless File.exist?(source_file)
123
- raise Interrupt, "File #{source_file} does not exist"
124
- end
117
+ raise Interrupt, "File #{source_file} does not exist" unless File.exist?(source_file)
125
118
 
126
119
  @delimiter = sniff_delimiter(source_file) || @delimiter
127
120
 
@@ -130,394 +123,331 @@ module KPM
130
123
 
131
124
  private
132
125
 
133
- # export helpers: fetch_export_data; export; process_export_data; remove_export_data;
134
- def fetch_export_data(account_id)
135
- KillBillClient.url = @killbill_url
136
- options = {
137
- :username => @killbill_user,
138
- :password => @killbill_password,
139
- :api_key => @killbill_api_key,
140
- :api_secret => @killbill_api_secrets
141
- }
142
-
143
- begin
144
- account_data = KillBillClient::Model::Export.find_by_account_id(account_id, 'KPM', options)
145
- rescue Exception => e
146
- raise Interrupt, 'Account id not found'
147
- end
148
-
149
- account_data
126
+ # export helpers: fetch_export_data; export; process_export_data; remove_export_data;
127
+ def fetch_export_data(account_id)
128
+ KillBillClient.url = @killbill_url
129
+ options = {
130
+ username: @killbill_user,
131
+ password: @killbill_password,
132
+ api_key: @killbill_api_key,
133
+ api_secret: @killbill_api_secret
134
+ }
135
+
136
+ begin
137
+ account_data = KillBillClient::Model::Export.find_by_account_id(account_id, 'KPM', options)
138
+ rescue StandardError
139
+ raise Interrupt, 'Account id not found'
150
140
  end
151
141
 
152
- def export(export_data)
153
- export_file = TMP_DIR + File::SEPARATOR + 'kbdump'
154
-
155
- open (export_file), 'w' do |io|
156
-
157
- table_name = nil
158
- cols_names = nil
159
- export_data.split("\n").each do |line|
160
- words = line.strip.split(" ")
161
- clean_line = line
162
- if not /--/.match(words[0]).nil?
163
- table_name = words[1]
164
- cols_names = words[2].strip.split(@delimiter)
165
- elsif not table_name.nil?
166
- clean_line = process_export_data(line,table_name,cols_names)
167
- end
168
- io.puts clean_line
142
+ account_data
143
+ end
169
144
 
145
+ def export(export_data)
146
+ export_file = TMP_DIR + File::SEPARATOR + 'kbdump'
147
+
148
+ File.open(export_file, 'w') do |io|
149
+ table_name = nil
150
+ cols_names = nil
151
+ export_data.split("\n").each do |line|
152
+ words = line.strip.split(' ')
153
+ clean_line = line
154
+ if !/--/.match(words[0]).nil?
155
+ table_name = words[1]
156
+ cols_names = words[2].strip.split(@delimiter)
157
+ elsif !table_name.nil?
158
+ clean_line = process_export_data(line, table_name, cols_names)
170
159
  end
171
-
160
+ io.puts clean_line
172
161
  end
173
-
174
- export_file
175
162
  end
176
163
 
177
- def process_export_data(line_to_process, table_name, cols_names)
178
- clean_line = line_to_process
179
-
180
- row = []
181
- cols = clean_line.strip.split(@delimiter)
182
- cols_names.each_with_index { |col_name, index|
183
- sanitized_value = remove_export_data(table_name,col_name,cols[index])
184
-
185
- row << sanitized_value
164
+ export_file
165
+ end
186
166
 
187
- }
167
+ def process_export_data(line_to_process, table_name, cols_names)
168
+ clean_line = line_to_process
188
169
 
189
- clean_line = row.join(@delimiter)
170
+ row = []
171
+ cols = clean_line.strip.split(@delimiter)
172
+ cols_names.each_with_index do |col_name, index|
173
+ sanitized_value = remove_export_data(table_name, col_name, cols[index])
190
174
 
191
- clean_line
175
+ row << sanitized_value
192
176
  end
193
177
 
194
- def remove_export_data(table_name,col_name,value)
195
-
196
- if not REMOVE_DATA_FROM[table_name.to_sym].nil?
178
+ row.join(@delimiter)
179
+ end
197
180
 
198
- if REMOVE_DATA_FROM[table_name.to_sym].include? col_name.to_sym
199
- return nil
200
- end
181
+ def remove_export_data(table_name, col_name, value)
182
+ unless REMOVE_DATA_FROM[table_name.to_sym].nil?
201
183
 
202
- end
184
+ return nil if REMOVE_DATA_FROM[table_name.to_sym].include? col_name.to_sym
203
185
 
204
- value
205
186
  end
206
187
 
207
- # import helpers: sanitize_and_import; import; sanitize; replace_tenant_record_id; replace_account_record_id; replace_boolean;
208
- # fix_dates; fill_empty_column;
209
- def sanitize_and_import(source_file, skip_payment_methods)
210
- tables = Hash.new
211
- error_importing_data = false
212
-
213
- open (source_file), 'r' do |data|
214
-
215
- rows = nil;
216
- table_name = nil;
217
- cols_names = nil;
218
-
219
- data.each_line do |line|
220
- words = line.strip.split(" ")
221
-
222
- if /--/.match(words[0])
223
- unless table_name.nil?
224
- if @generate_record_id
225
- cols_names.shift
226
- end
188
+ value
189
+ end
227
190
 
228
- tables[table_name] = { :col_names => cols_names, :rows => rows};
229
- end
191
+ # import helpers: sanitize_and_import; import; sanitize; replace_tenant_record_id; replace_account_record_id; replace_boolean;
192
+ # fix_dates; fill_empty_column;
193
+ def sanitize_and_import(source_file, skip_payment_methods)
194
+ tables = {}
195
+ error_importing_data = false
230
196
 
231
- table_name = words[1]
232
- cols_names = words[2].strip.split(@delimiter)
197
+ File.open(source_file, 'r:UTF-8') do |data|
198
+ rows = nil
199
+ table_name = nil
200
+ cols_names = nil
233
201
 
234
- rows = []
235
- elsif not table_name.nil?
236
- row = process_import_data(line, table_name,cols_names, skip_payment_methods, rows)
202
+ data.each_line do |line|
203
+ words = line.strip.split(' ')
237
204
 
238
- next if row.nil?
205
+ if /--/.match(words[0])
206
+ unless table_name.nil?
207
+ cols_names.shift if @generate_record_id
239
208
 
240
- rows.push(row)
241
- else
242
- error_importing_data = true
243
- break
209
+ tables[table_name] = { col_names: cols_names, rows: rows }
244
210
  end
245
- end
246
211
 
247
- if not ( table_name.nil? || error_importing_data )
248
- if @generate_record_id
249
- cols_names.shift
250
- end
212
+ table_name = words[1]
213
+ cols_names = words[2].strip.split(@delimiter)
251
214
 
252
- tables[table_name] = { :col_names => cols_names, :rows => rows};
253
- end
215
+ rows = []
216
+ elsif !table_name.nil?
217
+ row = process_import_data(line, table_name, cols_names, skip_payment_methods, rows)
254
218
 
255
- if tables.empty?
219
+ next if row.nil?
220
+
221
+ rows.push(row)
222
+ else
256
223
  error_importing_data = true
224
+ break
257
225
  end
258
226
  end
259
227
 
260
- unless error_importing_data
261
- import(tables)
262
- else
263
- raise Interrupt, "Data on #{source_file} is invalid"
228
+ unless table_name.nil? || error_importing_data
229
+ cols_names.shift if @generate_record_id
230
+
231
+ tables[table_name] = { col_names: cols_names, rows: rows }
264
232
  end
265
233
 
234
+ error_importing_data = true if tables.empty?
266
235
  end
267
236
 
268
- def process_import_data(line, table_name, cols_names, skip_payment_methods, rows)
269
- # to make sure that the last column is not omitted if is empty
270
- cols = line.strip.split(@delimiter,line.count(@delimiter)+1)
271
-
272
- if cols_names.size != cols.size
273
- @logger.warn "\e[32mWARNING!!! On #{table_name} table there is a mismatch on column count[#{cols.size}] versus header count[#{cols_names.size}]\e[0m"
274
- return nil
275
- end
237
+ raise Interrupt, "Data on #{source_file} is invalid" if error_importing_data
276
238
 
277
- row = []
239
+ import(tables)
240
+ end
278
241
 
279
- @logger.debug "Processing table_name=#{table_name}, line=#{line}"
280
- cols_names.each_with_index do |col_name, index|
281
- sanitized_value = sanitize(table_name,col_name,cols[index], skip_payment_methods)
242
+ def process_import_data(line, table_name, cols_names, skip_payment_methods, _rows)
243
+ # to make sure that the last column is not omitted if is empty
244
+ cols = line.strip.split(@delimiter, line.count(@delimiter) + 1)
282
245
 
283
- unless sanitized_value.nil?
284
- row << sanitized_value
285
- end
286
- end
287
-
288
- return row
246
+ if cols_names.size != cols.size
247
+ @logger.warn "\e[32mWARNING!!! On #{table_name} table there is a mismatch on column count[#{cols.size}] versus header count[#{cols_names.size}]\e[0m"
248
+ return nil
289
249
  end
290
250
 
291
- def import(tables)
292
- record_id = nil;
293
- statements = Database.generate_insert_statement(tables)
294
- statements.each do |statement|
295
- response = Database.execute_insert_statement(statement[:table_name],statement[:query], statement[:qty_to_insert], statement[:table_data],record_id)
251
+ row = []
296
252
 
297
- if statement[:table_name] == 'accounts' && response.is_a?(String)
298
- record_id = {:variable => '@account_record_id', :value => response}
299
- end
300
-
301
- if response === false
302
- break
303
- end
304
- end
253
+ @logger.debug "Processing table_name=#{table_name}, line=#{line}"
254
+ cols_names.each_with_index do |col_name, index|
255
+ sanitized_value = sanitize(table_name, col_name, cols[index], skip_payment_methods)
305
256
 
257
+ row << sanitized_value unless sanitized_value.nil?
306
258
  end
307
259
 
308
- def sanitize(table_name,column_name,value,skip_payment_methods)
309
- sanitized_value = replace_boolean(value)
310
- sanitized_value = fill_empty_column(sanitized_value)
260
+ row
261
+ end
311
262
 
312
- if table_name == 'payment_methods' && skip_payment_methods && column_name == PLUGIN_NAME_COLUMN
313
- sanitized_value = SAFE_PAYMENT_METHOD
314
- end
263
+ def import(tables)
264
+ record_id = nil
265
+ statements = @database.generate_insert_statement(tables)
266
+ statements.each do |statement|
267
+ response = @database.execute_insert_statement(statement[:table_name], statement[:query], statement[:qty_to_insert], statement[:table_data], record_id)
315
268
 
316
- if DATE_COLUMNS_TO_FIX.include? column_name
317
- sanitized_value = fix_dates(sanitized_value)
318
- end
269
+ record_id = { variable: '@account_record_id', value: response } if statement[:table_name] == 'accounts' && response.is_a?(String)
319
270
 
320
- if not @tenant_record_id.nil?
321
- sanitized_value = replace_tenant_record_id(table_name,column_name,sanitized_value)
322
- end
271
+ break unless response
272
+ end
273
+ end
323
274
 
324
- if @generate_record_id
325
- sanitized_value = replace_account_record_id(table_name,column_name,sanitized_value)
326
- end
275
+ def sanitize(table_name, column_name, value, skip_payment_methods)
276
+ sanitized_value = replace_boolean(value)
327
277
 
328
- if @round_trip_export_import
329
- sanitized_value = replace_uuid(table_name,column_name,sanitized_value)
330
- end
278
+ sanitized_value = fill_empty_column(sanitized_value)
331
279
 
332
- sanitized_value
333
- end
280
+ sanitized_value = SAFE_PAYMENT_METHOD if table_name == 'payment_methods' && skip_payment_methods && column_name == PLUGIN_NAME_COLUMN
334
281
 
335
- def replace_tenant_record_id(table_name,column_name,value)
336
- return @tenant_record_id if column_name == 'tenant_record_id' || column_name == 'search_key2'
337
- value
338
- end
282
+ sanitized_value = fix_dates(sanitized_value) if DATE_COLUMNS_TO_FIX.include? column_name
339
283
 
340
- def replace_account_record_id(table_name,column_name,value)
284
+ sanitized_value = replace_tenant_record_id(table_name, column_name, sanitized_value) unless @tenant_record_id.nil?
341
285
 
342
- if column_name == 'account_record_id'
286
+ sanitized_value = replace_account_record_id(table_name, column_name, sanitized_value) if @generate_record_id
343
287
 
344
- return :@account_record_id
345
- end
288
+ sanitized_value = replace_uuid(table_name, column_name, sanitized_value) if @round_trip_export_import
346
289
 
347
- if column_name == 'record_id'
348
- return nil
349
- end
290
+ sanitized_value = b64_decode_if_needed(sanitized_value) if column_name == 'billing_events'
350
291
 
351
- if column_name == 'target_record_id'
292
+ sanitized_value
293
+ end
352
294
 
353
- if table_name == 'account_history'
354
- return :@account_record_id
355
- end
356
- end
295
+ def replace_tenant_record_id(_table_name, column_name, value)
296
+ return @tenant_record_id if %w[tenant_record_id search_key2].include?(column_name)
357
297
 
358
- if column_name == 'search_key1' && table_name == 'bus_ext_events_history'
359
- return :@account_record_id
360
- end
298
+ value
299
+ end
361
300
 
362
- if column_name == 'search_key1' && table_name == 'bus_events_history'
363
- return :@account_record_id
364
- end
301
+ def replace_account_record_id(table_name, column_name, value)
302
+ return :@account_record_id if column_name == 'account_record_id'
365
303
 
366
- value
304
+ return nil if column_name == 'record_id'
367
305
 
368
- end
306
+ if column_name == 'target_record_id'
369
307
 
370
- def replace_boolean(value)
371
- if value.to_s === 'true'
372
- return 1
373
- elsif value.to_s === 'false'
374
- return 0
375
- else
376
- return value
377
- end
308
+ return :@account_record_id if table_name == 'account_history'
378
309
  end
379
310
 
380
- def fix_dates(value)
381
- if !value.equal?(:DEFAULT)
311
+ return :@account_record_id if column_name == 'search_key1' && table_name == 'bus_ext_events_history'
382
312
 
383
- dt = DateTime.parse(value)
384
- return dt.strftime('%F %T').to_s
313
+ return :@account_record_id if column_name == 'search_key1' && table_name == 'bus_events_history'
385
314
 
386
- end
315
+ value
316
+ end
387
317
 
318
+ def replace_boolean(value)
319
+ case value.to_s
320
+ when 'true'
321
+ 1
322
+ when 'false'
323
+ 0
324
+ else
388
325
  value
389
326
  end
327
+ end
390
328
 
391
- def fill_empty_column(value)
392
- if value.to_s.strip.empty?
393
- return :DEFAULT
394
- else
395
- return value
396
- end
397
- end
398
-
399
- def replace_uuid(table_name,column_name,value)
400
-
401
- if column_name == 'id'
402
- @tables_id["#{table_name}_id"] = SecureRandom.uuid
403
- end
404
-
405
- if ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym] && ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym][column_name.to_sym]
406
- key = ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym][column_name.to_sym]
329
+ def fix_dates(value)
330
+ unless value.equal?(:DEFAULT)
407
331
 
408
- if key.equal?(:generate)
409
- new_value = SecureRandom.uuid
410
- else
411
- new_value = @tables_id[key.to_s]
412
- end
413
-
414
- if new_value.nil?
415
- new_value = SecureRandom.uuid
416
- @tables_id[key.to_s] = new_value
417
- end
418
- return new_value
419
- end
332
+ dt = DateTime.parse(value)
333
+ return dt.strftime('%F %T').to_s
420
334
 
421
- if not ROUND_TRIP_EXPORT_IMPORT_MAP[:all][column_name.to_sym].nil?
422
- key = ROUND_TRIP_EXPORT_IMPORT_MAP[:all][column_name.to_sym]
423
- new_value = @tables_id[key.to_s]
335
+ end
424
336
 
425
- return new_value
426
- end
337
+ value
338
+ end
427
339
 
340
+ def fill_empty_column(value)
341
+ if value.to_s.strip.empty?
342
+ :DEFAULT
343
+ else
428
344
  value
429
345
  end
346
+ end
430
347
 
431
- def sniff_delimiter(file)
432
-
433
- return nil if File.size?(file).nil?
434
-
435
- first_line = File.open(file) {|f| f.readline}
348
+ def replace_uuid(table_name, column_name, value)
349
+ @tables_id["#{table_name}_id"] = SecureRandom.uuid if column_name == 'id'
436
350
 
437
- return nil if first_line.nil?
351
+ if ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym] && ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym][column_name.to_sym]
352
+ key = ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym][column_name.to_sym]
438
353
 
439
- sniff = {}
354
+ new_value = if key.equal?(:generate)
355
+ SecureRandom.uuid
356
+ else
357
+ @tables_id[key.to_s]
358
+ end
440
359
 
441
- DELIMITERS.each do |delimiter|
442
- sniff[delimiter] = first_line.count(delimiter)
360
+ if new_value.nil?
361
+ new_value = SecureRandom.uuid
362
+ @tables_id[key.to_s] = new_value
443
363
  end
444
-
445
- sniff = sniff.sort {|a,b| b[1]<=>a[1]}
446
- sniff.size > 0 ? sniff[0][0] : nil
364
+ return new_value
447
365
  end
448
366
 
449
- # helper methods that set up killbill and database options: load_config_from_file; set_config; set_database_options;
450
- # set_killbill_options;
451
- def load_config_from_file(config_file)
367
+ unless ROUND_TRIP_EXPORT_IMPORT_MAP[:all][column_name.to_sym].nil?
368
+ key = ROUND_TRIP_EXPORT_IMPORT_MAP[:all][column_name.to_sym]
369
+ new_value = @tables_id[key.to_s]
452
370
 
453
- set_config(config_file)
371
+ return new_value
372
+ end
454
373
 
455
- if not @config.nil?
456
- config_killbill = @config['killbill']
374
+ value
375
+ end
457
376
 
458
- if not config_killbill.nil?
459
- set_killbill_options([config_killbill['api_key'],config_killbill['api_secret']],
460
- [config_killbill['user'],config_killbill['password']],
461
- "http://#{config_killbill['host']}:#{config_killbill['port']}")
462
- end
377
+ def b64_decode_if_needed(input)
378
+ # Exclude nil or non string
379
+ return input if input.nil? || !input.is_a?(String)
380
+ # Apply regex to check that string is built as a B64 string: the character set is [A-Z, a-z, 0-9, and + /]
381
+ # and if the rest length is less than 4, the string is padded with '=' characters.
382
+ return input if input.match(B64_REGEX).nil?
463
383
 
464
- config_db = @config['database']
384
+ # Decode
385
+ result = Base64.decode64(input)
386
+ # Verify encoded of the decoded value == input prior return result
387
+ return input if Base64.strict_encode64(result) != input
465
388
 
466
- if not config_db.nil?
467
- set_database_options(config_db['host'],config_db['name'],
468
- [config_db['username'],config_db['password']],
469
- @logger)
389
+ Blob.new(result, TMP_DIR)
390
+ end
470
391
 
471
- end
472
- end
473
- end
392
+ def sniff_delimiter(file)
393
+ return nil if File.size?(file).nil?
474
394
 
475
- def set_config(config_file = nil)
476
- @config = nil
395
+ first_line = File.open(file, &:readline)
477
396
 
478
- if not config_file.nil?
479
- if not Dir[config_file][0].nil?
480
- @config = YAML::load_file(config_file)
481
- end
482
- end
397
+ return nil if first_line.nil?
483
398
 
399
+ sniff = {}
400
+
401
+ DELIMITERS.each do |delimiter|
402
+ sniff[delimiter] = first_line.count(delimiter)
484
403
  end
485
404
 
486
- def set_database_options(database_host = nil, database_port = nil, database_name = nil, database_credentials = nil, logger)
405
+ sniff = sniff.sort { |a, b| b[1] <=> a[1] }
406
+ !sniff.empty? ? sniff[0][0] : nil
407
+ end
487
408
 
488
- Database.set_logger(logger)
409
+ def load_config_from_file(config_file)
410
+ self.config = config_file
489
411
 
490
- Database.set_credentials(database_credentials[0],database_credentials[1]) unless database_credentials.nil?
491
- Database.set_database_name(database_name) unless database_name.nil?
492
- Database.set_host(database_host) unless database_host.nil?
493
- Database.set_port(database_port) unless database_port.nil?
412
+ return if @config.nil?
494
413
 
495
- Database.set_mysql_command_line
414
+ config_killbill = @config['killbill']
415
+
416
+ unless config_killbill.nil?
417
+ set_killbill_options([config_killbill['api_key'], config_killbill['api_secret']],
418
+ [config_killbill['user'], config_killbill['password']],
419
+ "http://#{config_killbill['host']}:#{config_killbill['port']}")
496
420
  end
497
421
 
498
- def set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
422
+ config_db = @config['database']
499
423
 
500
- if not killbill_api_credentials.nil?
424
+ @database = Database.new(config_db['name'], config_db['host'], config_db['port'], config_db['username'], config_db['password'], @logger) unless config_db.nil?
425
+ end
501
426
 
502
- @killbill_api_key = killbill_api_credentials[0]
503
- @killbill_api_secrets = killbill_api_credentials[1]
427
+ def config=(config_file = nil)
428
+ @config = nil
504
429
 
505
- end
430
+ return if config_file.nil?
506
431
 
507
- if not killbill_credentials.nil?
432
+ @config = YAML.load_file(config_file) unless Dir[config_file][0].nil?
433
+ end
508
434
 
509
- @killbill_user = killbill_credentials[0]
510
- @killbill_password = killbill_credentials[1]
435
+ def set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
436
+ unless killbill_api_credentials.nil?
511
437
 
512
- end
438
+ @killbill_api_key = killbill_api_credentials[0]
439
+ @killbill_api_secret = killbill_api_credentials[1]
513
440
 
514
- if not killbill_url.nil?
441
+ end
515
442
 
516
- @killbill_url = killbill_url
443
+ unless killbill_credentials.nil?
444
+
445
+ @killbill_user = killbill_credentials[0]
446
+ @killbill_password = killbill_credentials[1]
517
447
 
518
- end
519
448
  end
520
449
 
450
+ @killbill_url = killbill_url unless killbill_url.nil?
451
+ end
521
452
  end
522
-
523
- end
453
+ end