kpm 0.7.2 → 0.8.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 (74) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +66 -0
  3. data/Gemfile +2 -0
  4. data/README.adoc +111 -109
  5. data/Rakefile +2 -1
  6. data/bin/kpm +4 -2
  7. data/kpm.gemspec +8 -6
  8. data/lib/kpm.rb +3 -0
  9. data/lib/kpm/account.rb +267 -338
  10. data/lib/kpm/base_artifact.rb +33 -39
  11. data/lib/kpm/base_installer.rb +69 -83
  12. data/lib/kpm/blob.rb +29 -0
  13. data/lib/kpm/cli.rb +3 -1
  14. data/lib/kpm/coordinates.rb +6 -9
  15. data/lib/kpm/database.rb +90 -114
  16. data/lib/kpm/diagnostic_file.rb +126 -147
  17. data/lib/kpm/formatter.rb +74 -46
  18. data/lib/kpm/inspector.rb +22 -32
  19. data/lib/kpm/installer.rb +53 -46
  20. data/lib/kpm/kaui_artifact.rb +4 -3
  21. data/lib/kpm/killbill_plugin_artifact.rb +10 -7
  22. data/lib/kpm/killbill_server_artifact.rb +13 -12
  23. data/lib/kpm/migrations.rb +8 -7
  24. data/lib/kpm/nexus_helper/actions.rb +47 -8
  25. data/lib/kpm/nexus_helper/nexus_api_calls_v2.rb +87 -94
  26. data/lib/kpm/nexus_helper/nexus_facade.rb +5 -3
  27. data/lib/kpm/plugins_directory.rb +9 -8
  28. data/lib/kpm/plugins_directory.yml +8 -175
  29. data/lib/kpm/plugins_manager.rb +29 -24
  30. data/lib/kpm/sha1_checker.rb +31 -18
  31. data/lib/kpm/system.rb +105 -136
  32. data/lib/kpm/system_helpers/cpu_information.rb +56 -55
  33. data/lib/kpm/system_helpers/disk_space_information.rb +60 -63
  34. data/lib/kpm/system_helpers/entropy_available.rb +37 -39
  35. data/lib/kpm/system_helpers/memory_information.rb +52 -51
  36. data/lib/kpm/system_helpers/os_information.rb +45 -47
  37. data/lib/kpm/system_helpers/system_proxy.rb +10 -10
  38. data/lib/kpm/tasks.rb +364 -437
  39. data/lib/kpm/tenant_config.rb +68 -83
  40. data/lib/kpm/tomcat_manager.rb +9 -8
  41. data/lib/kpm/trace_logger.rb +18 -16
  42. data/lib/kpm/uninstaller.rb +81 -14
  43. data/lib/kpm/utils.rb +13 -14
  44. data/lib/kpm/version.rb +3 -1
  45. data/packaging/Gemfile +2 -0
  46. data/pom.xml +1 -1
  47. data/spec/kpm/remote/base_artifact_spec.rb +13 -15
  48. data/spec/kpm/remote/base_installer_spec.rb +30 -29
  49. data/spec/kpm/remote/installer_spec.rb +73 -73
  50. data/spec/kpm/remote/kaui_artifact_spec.rb +7 -6
  51. data/spec/kpm/remote/killbill_plugin_artifact_spec.rb +19 -24
  52. data/spec/kpm/remote/killbill_server_artifact_spec.rb +17 -16
  53. data/spec/kpm/remote/migrations_spec.rb +12 -11
  54. data/spec/kpm/remote/nexus_facade_spec.rb +30 -26
  55. data/spec/kpm/remote/tenant_config_spec.rb +27 -26
  56. data/spec/kpm/remote/tomcat_manager_spec.rb +2 -1
  57. data/spec/kpm/unit/actions_spec.rb +52 -0
  58. data/spec/kpm/unit/base_artifact_spec.rb +17 -16
  59. data/spec/kpm/unit/cpu_information_spec.rb +67 -0
  60. data/spec/kpm/unit/disk_space_information_spec.rb +47 -0
  61. data/spec/kpm/unit/entropy_information_spec.rb +36 -0
  62. data/spec/kpm/unit/formatter_spec.rb +163 -0
  63. data/spec/kpm/unit/inspector_spec.rb +34 -42
  64. data/spec/kpm/unit/installer_spec.rb +5 -4
  65. data/spec/kpm/unit/memory_information_spec.rb +102 -0
  66. data/spec/kpm/unit/os_information_spec.rb +38 -0
  67. data/spec/kpm/unit/plugins_directory_spec.rb +34 -18
  68. data/spec/kpm/unit/plugins_manager_spec.rb +61 -65
  69. data/spec/kpm/unit/sha1_checker_spec.rb +107 -60
  70. data/spec/kpm/unit/uninstaller_spec.rb +107 -61
  71. data/spec/kpm/unit_mysql/account_spec.rb +120 -135
  72. data/spec/spec_helper.rb +19 -17
  73. data/tasks/package.rake +18 -18
  74. metadata +17 -8
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'
@@ -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,330 @@ 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') 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
+ if value.to_s == 'true'
320
+ 1
321
+ elsif value.to_s == 'false'
322
+ 0
323
+ else
388
324
  value
389
325
  end
326
+ end
390
327
 
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]
328
+ def fix_dates(value)
329
+ unless value.equal?(:DEFAULT)
407
330
 
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
331
+ dt = DateTime.parse(value)
332
+ return dt.strftime('%F %T').to_s
420
333
 
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]
334
+ end
424
335
 
425
- return new_value
426
- end
336
+ value
337
+ end
427
338
 
339
+ def fill_empty_column(value)
340
+ if value.to_s.strip.empty?
341
+ :DEFAULT
342
+ else
428
343
  value
429
344
  end
345
+ end
430
346
 
431
- def sniff_delimiter(file)
432
-
433
- return nil if File.size?(file).nil?
434
-
435
- first_line = File.open(file) {|f| f.readline}
347
+ def replace_uuid(table_name, column_name, value)
348
+ @tables_id["#{table_name}_id"] = SecureRandom.uuid if column_name == 'id'
436
349
 
437
- return nil if first_line.nil?
350
+ if ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym] && ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym][column_name.to_sym]
351
+ key = ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym][column_name.to_sym]
438
352
 
439
- sniff = {}
353
+ new_value = if key.equal?(:generate)
354
+ SecureRandom.uuid
355
+ else
356
+ @tables_id[key.to_s]
357
+ end
440
358
 
441
- DELIMITERS.each do |delimiter|
442
- sniff[delimiter] = first_line.count(delimiter)
359
+ if new_value.nil?
360
+ new_value = SecureRandom.uuid
361
+ @tables_id[key.to_s] = new_value
443
362
  end
444
-
445
- sniff = sniff.sort {|a,b| b[1]<=>a[1]}
446
- sniff.size > 0 ? sniff[0][0] : nil
363
+ return new_value
447
364
  end
448
365
 
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)
366
+ unless ROUND_TRIP_EXPORT_IMPORT_MAP[:all][column_name.to_sym].nil?
367
+ key = ROUND_TRIP_EXPORT_IMPORT_MAP[:all][column_name.to_sym]
368
+ new_value = @tables_id[key.to_s]
452
369
 
453
- set_config(config_file)
370
+ return new_value
371
+ end
454
372
 
455
- if not @config.nil?
456
- config_killbill = @config['killbill']
373
+ value
374
+ end
457
375
 
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
376
+ def b64_decode_if_needed(input)
377
+ # Exclude nil or non string
378
+ return input if input.nil? || !input.is_a?(String)
379
+ # Apply regex to check that string is built as a B64 string: the character set is [A-Z, a-z, 0-9, and + /]
380
+ # and if the rest length is less than 4, the string is padded with '=' characters.
381
+ return input if input.match(B64_REGEX).nil?
463
382
 
464
- config_db = @config['database']
383
+ # Decode
384
+ result = Base64.decode64(input)
385
+ # Verify encoded of the decoded value == input prior return result
386
+ return input if Base64.strict_encode64(result) != input
465
387
 
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)
388
+ Blob.new(result, TMP_DIR)
389
+ end
470
390
 
471
- end
472
- end
473
- end
391
+ def sniff_delimiter(file)
392
+ return nil if File.size?(file).nil?
474
393
 
475
- def set_config(config_file = nil)
476
- @config = nil
394
+ first_line = File.open(file, &:readline)
477
395
 
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
396
+ return nil if first_line.nil?
483
397
 
398
+ sniff = {}
399
+
400
+ DELIMITERS.each do |delimiter|
401
+ sniff[delimiter] = first_line.count(delimiter)
484
402
  end
485
403
 
486
- def set_database_options(database_host = nil, database_port = nil, database_name = nil, database_credentials = nil, logger)
404
+ sniff = sniff.sort { |a, b| b[1] <=> a[1] }
405
+ !sniff.empty? ? sniff[0][0] : nil
406
+ end
487
407
 
488
- Database.set_logger(logger)
408
+ def load_config_from_file(config_file)
409
+ self.config = config_file
489
410
 
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?
411
+ return if @config.nil?
494
412
 
495
- Database.set_mysql_command_line
413
+ config_killbill = @config['killbill']
414
+
415
+ unless config_killbill.nil?
416
+ set_killbill_options([config_killbill['api_key'], config_killbill['api_secret']],
417
+ [config_killbill['user'], config_killbill['password']],
418
+ "http://#{config_killbill['host']}:#{config_killbill['port']}")
496
419
  end
497
420
 
498
- def set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
421
+ config_db = @config['database']
499
422
 
500
- if not killbill_api_credentials.nil?
423
+ @database = Database.new(config_db['name'], config_db['host'], config_db['port'], config_db['username'], config_db['password'], @logger) unless config_db.nil?
424
+ end
501
425
 
502
- @killbill_api_key = killbill_api_credentials[0]
503
- @killbill_api_secrets = killbill_api_credentials[1]
426
+ def config=(config_file = nil)
427
+ @config = nil
504
428
 
505
- end
429
+ return if config_file.nil?
506
430
 
507
- if not killbill_credentials.nil?
431
+ @config = YAML.load_file(config_file) unless Dir[config_file][0].nil?
432
+ end
508
433
 
509
- @killbill_user = killbill_credentials[0]
510
- @killbill_password = killbill_credentials[1]
434
+ def set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
435
+ unless killbill_api_credentials.nil?
511
436
 
512
- end
437
+ @killbill_api_key = killbill_api_credentials[0]
438
+ @killbill_api_secret = killbill_api_credentials[1]
513
439
 
514
- if not killbill_url.nil?
440
+ end
515
441
 
516
- @killbill_url = killbill_url
442
+ unless killbill_credentials.nil?
443
+
444
+ @killbill_user = killbill_credentials[0]
445
+ @killbill_password = killbill_credentials[1]
517
446
 
518
- end
519
447
  end
520
448
 
449
+ @killbill_url = killbill_url unless killbill_url.nil?
450
+ end
521
451
  end
522
-
523
- end
452
+ end