kpm 0.7.0 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +73 -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 +268 -337
  10. data/lib/kpm/base_artifact.rb +40 -36
  11. data/lib/kpm/base_installer.rb +71 -84
  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 +92 -103
  16. data/lib/kpm/diagnostic_file.rb +126 -146
  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 +24 -10
  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 +14 -9
  28. data/lib/kpm/plugins_directory.yml +16 -175
  29. data/lib/kpm/plugins_manager.rb +29 -24
  30. data/lib/kpm/sha1_checker.rb +56 -15
  31. data/lib/kpm/system.rb +104 -135
  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 +370 -443
  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 +26 -12
  48. data/spec/kpm/remote/base_installer_spec.rb +30 -29
  49. data/spec/kpm/remote/installer_spec.rb +74 -72
  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 +30 -13
  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 +137 -136
  72. data/spec/spec_helper.rb +19 -17
  73. data/tasks/package.rake +18 -18
  74. metadata +19 -34
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,93 +35,86 @@ 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']
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
110
107
 
111
108
  def import_data(source_file, tenant_record_id, skip_payment_methods, round_trip_export_import = false, generate_record_id = false)
109
+ source_file = File.expand_path(source_file)
112
110
 
113
111
  @generate_record_id = generate_record_id
114
112
  @tenant_record_id = tenant_record_id
115
113
  @round_trip_export_import = round_trip_export_import
116
114
 
117
- if source_file === :import.to_s
118
- raise Interrupt, 'Need to specify a file'
119
- end
115
+ raise Interrupt, 'Need to specify a file' if source_file == :import.to_s
120
116
 
121
- unless File.exist?(source_file)
122
- raise Interrupt, 'Need to specify a valid file'
123
- end
117
+ raise Interrupt, "File #{source_file} does not exist" unless File.exist?(source_file)
124
118
 
125
119
  @delimiter = sniff_delimiter(source_file) || @delimiter
126
120
 
@@ -129,393 +123,330 @@ module KPM
129
123
 
130
124
  private
131
125
 
132
- # export helpers: fetch_export_data; export; process_export_data; remove_export_data;
133
- def fetch_export_data(account_id)
134
- KillBillClient.url = @killbill_url
135
- options = {
136
- :username => @killbill_user,
137
- :password => @killbill_password,
138
- :api_key => @killbill_api_key,
139
- :api_secret => @killbill_api_secrets
140
- }
141
-
142
- begin
143
- account_data = KillBillClient::Model::Export.find_by_account_id(account_id, 'KPM', options)
144
- rescue Exception => e
145
- raise Interrupt, 'Account id not found'
146
- end
147
-
148
- 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'
149
140
  end
150
141
 
151
- def export(export_data)
152
- export_file = TMP_DIR + File::SEPARATOR + 'kbdump'
153
-
154
- open (export_file), 'w' do |io|
155
-
156
- table_name = nil
157
- cols_names = nil
158
- export_data.split("\n").each do |line|
159
- words = line.strip.split(" ")
160
- clean_line = line
161
- if not /--/.match(words[0]).nil?
162
- table_name = words[1]
163
- cols_names = words[2].strip.split(@delimiter)
164
- elsif not table_name.nil?
165
- clean_line = process_export_data(line,table_name,cols_names)
166
- end
167
- io.puts clean_line
142
+ account_data
143
+ end
168
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)
169
159
  end
170
-
160
+ io.puts clean_line
171
161
  end
172
-
173
- export_file
174
162
  end
175
163
 
176
- def process_export_data(line_to_process, table_name, cols_names)
177
- clean_line = line_to_process
178
-
179
- row = []
180
- cols = clean_line.strip.split(@delimiter)
181
- cols_names.each_with_index { |col_name, index|
182
- sanitized_value = remove_export_data(table_name,col_name,cols[index])
183
-
184
- row << sanitized_value
164
+ export_file
165
+ end
185
166
 
186
- }
167
+ def process_export_data(line_to_process, table_name, cols_names)
168
+ clean_line = line_to_process
187
169
 
188
- 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])
189
174
 
190
- clean_line
175
+ row << sanitized_value
191
176
  end
192
177
 
193
- def remove_export_data(table_name,col_name,value)
194
-
195
- if not REMOVE_DATA_FROM[table_name.to_sym].nil?
178
+ row.join(@delimiter)
179
+ end
196
180
 
197
- if REMOVE_DATA_FROM[table_name.to_sym].include? col_name.to_sym
198
- return nil
199
- end
181
+ def remove_export_data(table_name, col_name, value)
182
+ unless REMOVE_DATA_FROM[table_name.to_sym].nil?
200
183
 
201
- end
184
+ return nil if REMOVE_DATA_FROM[table_name.to_sym].include? col_name.to_sym
202
185
 
203
- value
204
186
  end
205
187
 
206
- # import helpers: sanitize_and_import; import; sanitize; replace_tenant_record_id; replace_account_record_id; replace_boolean;
207
- # fix_dates; fill_empty_column;
208
- def sanitize_and_import(source_file, skip_payment_methods)
209
- tables = Hash.new
210
- error_importing_data = false
211
-
212
- open (source_file), 'r' do |data|
213
-
214
- rows = nil;
215
- table_name = nil;
216
- cols_names = nil;
217
-
218
- data.each_line do |line|
219
- words = line.strip.split(" ")
220
-
221
- if /--/.match(words[0])
222
- unless table_name.nil?
223
- if @generate_record_id
224
- cols_names.shift
225
- end
188
+ value
189
+ end
226
190
 
227
- tables[table_name] = { :col_names => cols_names, :rows => rows};
228
- 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
229
196
 
230
- table_name = words[1]
231
- 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
232
201
 
233
- rows = []
234
- elsif not table_name.nil?
235
- row = process_import_data(line, table_name,cols_names, skip_payment_methods, rows)
202
+ data.each_line do |line|
203
+ words = line.strip.split(' ')
236
204
 
237
- next if row.nil?
205
+ if /--/.match(words[0])
206
+ unless table_name.nil?
207
+ cols_names.shift if @generate_record_id
238
208
 
239
- rows.push(row)
240
- else
241
- error_importing_data = true
242
- break
209
+ tables[table_name] = { col_names: cols_names, rows: rows }
243
210
  end
244
- end
245
211
 
246
- if not ( table_name.nil? || error_importing_data )
247
- if @generate_record_id
248
- cols_names.shift
249
- end
212
+ table_name = words[1]
213
+ cols_names = words[2].strip.split(@delimiter)
250
214
 
251
- tables[table_name] = { :col_names => cols_names, :rows => rows};
252
- end
215
+ rows = []
216
+ elsif !table_name.nil?
217
+ row = process_import_data(line, table_name, cols_names, skip_payment_methods, rows)
253
218
 
254
- if tables.empty?
219
+ next if row.nil?
220
+
221
+ rows.push(row)
222
+ else
255
223
  error_importing_data = true
224
+ break
256
225
  end
257
226
  end
258
227
 
259
- unless error_importing_data
260
- import(tables)
261
- else
262
- 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 }
263
232
  end
264
233
 
234
+ error_importing_data = true if tables.empty?
265
235
  end
266
236
 
267
- def process_import_data(line, table_name, cols_names, skip_payment_methods, rows)
268
- # to make sure that the last column is not omitted if is empty
269
- cols = line.strip.split(@delimiter,line.count(@delimiter)+1)
270
-
271
- if cols_names.size != cols.size
272
- @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"
273
- return nil
274
- end
237
+ raise Interrupt, "Data on #{source_file} is invalid" if error_importing_data
275
238
 
276
- row = []
239
+ import(tables)
240
+ end
277
241
 
278
- cols_names.each_with_index do |col_name, index|
279
- 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)
280
245
 
281
- unless sanitized_value.nil?
282
- row << sanitized_value
283
- end
284
- end
285
-
286
- 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
287
249
  end
288
250
 
289
- def import(tables)
290
- record_id = nil;
291
- statements = Database.generate_insert_statement(tables)
292
- statements.each do |statement|
293
- response = Database.execute_insert_statement(statement[:table_name],statement[:query], statement[:qty_to_insert], statement[:table_data],record_id)
251
+ row = []
294
252
 
295
- if statement[:table_name] == 'accounts' && response.is_a?(String)
296
- record_id = {:variable => '@account_record_id', :value => response}
297
- end
298
-
299
- if response === false
300
- break
301
- end
302
- 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)
303
256
 
257
+ row << sanitized_value unless sanitized_value.nil?
304
258
  end
305
259
 
306
- def sanitize(table_name,column_name,value,skip_payment_methods)
307
- sanitized_value = replace_boolean(value)
308
- sanitized_value = fill_empty_column(sanitized_value)
260
+ row
261
+ end
309
262
 
310
- if table_name == 'payment_methods' && skip_payment_methods && column_name == PLUGIN_NAME_COLUMN
311
- sanitized_value = SAFE_PAYMENT_METHOD
312
- 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)
313
268
 
314
- if DATE_COLUMNS_TO_FIX.include? column_name
315
- sanitized_value = fix_dates(sanitized_value)
316
- end
269
+ record_id = { variable: '@account_record_id', value: response } if statement[:table_name] == 'accounts' && response.is_a?(String)
317
270
 
318
- if not @tenant_record_id.nil?
319
- sanitized_value = replace_tenant_record_id(table_name,column_name,sanitized_value)
320
- end
271
+ break unless response
272
+ end
273
+ end
321
274
 
322
- if @generate_record_id
323
- sanitized_value = replace_account_record_id(table_name,column_name,sanitized_value)
324
- end
275
+ def sanitize(table_name, column_name, value, skip_payment_methods)
276
+ sanitized_value = replace_boolean(value)
325
277
 
326
- if @round_trip_export_import
327
- sanitized_value = replace_uuid(table_name,column_name,sanitized_value)
328
- end
278
+ sanitized_value = fill_empty_column(sanitized_value)
329
279
 
330
- sanitized_value
331
- end
280
+ sanitized_value = SAFE_PAYMENT_METHOD if table_name == 'payment_methods' && skip_payment_methods && column_name == PLUGIN_NAME_COLUMN
332
281
 
333
- def replace_tenant_record_id(table_name,column_name,value)
334
- return @tenant_record_id if column_name == 'tenant_record_id' || column_name == 'search_key2'
335
- value
336
- end
282
+ sanitized_value = fix_dates(sanitized_value) if DATE_COLUMNS_TO_FIX.include? column_name
337
283
 
338
- 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?
339
285
 
340
- if column_name == 'account_record_id'
286
+ sanitized_value = replace_account_record_id(table_name, column_name, sanitized_value) if @generate_record_id
341
287
 
342
- return :@account_record_id
343
- end
288
+ sanitized_value = replace_uuid(table_name, column_name, sanitized_value) if @round_trip_export_import
344
289
 
345
- if column_name == 'record_id'
346
- return nil
347
- end
290
+ sanitized_value = b64_decode_if_needed(sanitized_value) if column_name == 'billing_events'
348
291
 
349
- if column_name == 'target_record_id'
292
+ sanitized_value
293
+ end
350
294
 
351
- if table_name == 'account_history'
352
- return :@account_record_id
353
- end
354
- 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)
355
297
 
356
- if column_name == 'search_key1' && table_name == 'bus_ext_events_history'
357
- return :@account_record_id
358
- end
298
+ value
299
+ end
359
300
 
360
- if column_name == 'search_key1' && table_name == 'bus_events_history'
361
- return :@account_record_id
362
- end
301
+ def replace_account_record_id(table_name, column_name, value)
302
+ return :@account_record_id if column_name == 'account_record_id'
363
303
 
364
- value
304
+ return nil if column_name == 'record_id'
365
305
 
366
- end
306
+ if column_name == 'target_record_id'
367
307
 
368
- def replace_boolean(value)
369
- if value.to_s === 'true'
370
- return 1
371
- elsif value.to_s === 'false'
372
- return 0
373
- else
374
- return value
375
- end
308
+ return :@account_record_id if table_name == 'account_history'
376
309
  end
377
310
 
378
- def fix_dates(value)
379
- if !value.equal?(:DEFAULT)
311
+ return :@account_record_id if column_name == 'search_key1' && table_name == 'bus_ext_events_history'
380
312
 
381
- dt = DateTime.parse(value)
382
- return dt.strftime('%F %T').to_s
313
+ return :@account_record_id if column_name == 'search_key1' && table_name == 'bus_events_history'
383
314
 
384
- end
315
+ value
316
+ end
385
317
 
318
+ def replace_boolean(value)
319
+ if value.to_s == 'true'
320
+ 1
321
+ elsif value.to_s == 'false'
322
+ 0
323
+ else
386
324
  value
387
325
  end
326
+ end
388
327
 
389
- def fill_empty_column(value)
390
- if value.to_s.strip.empty?
391
- return :DEFAULT
392
- else
393
- return value
394
- end
395
- end
396
-
397
- def replace_uuid(table_name,column_name,value)
398
-
399
- if column_name == 'id'
400
- @tables_id["#{table_name}_id"] = SecureRandom.uuid
401
- end
402
-
403
- if ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym] && ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym][column_name.to_sym]
404
- key = ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym][column_name.to_sym]
328
+ def fix_dates(value)
329
+ unless value.equal?(:DEFAULT)
405
330
 
406
- if key.equal?(:generate)
407
- new_value = SecureRandom.uuid
408
- else
409
- new_value = @tables_id[key.to_s]
410
- end
411
-
412
- if new_value.nil?
413
- new_value = SecureRandom.uuid
414
- @tables_id[key.to_s] = new_value
415
- end
416
- return new_value
417
- end
331
+ dt = DateTime.parse(value)
332
+ return dt.strftime('%F %T').to_s
418
333
 
419
- if not ROUND_TRIP_EXPORT_IMPORT_MAP[:all][column_name.to_sym].nil?
420
- key = ROUND_TRIP_EXPORT_IMPORT_MAP[:all][column_name.to_sym]
421
- new_value = @tables_id[key.to_s]
334
+ end
422
335
 
423
- return new_value
424
- end
336
+ value
337
+ end
425
338
 
339
+ def fill_empty_column(value)
340
+ if value.to_s.strip.empty?
341
+ :DEFAULT
342
+ else
426
343
  value
427
344
  end
345
+ end
428
346
 
429
- def sniff_delimiter(file)
430
-
431
- return nil if File.size?(file).nil?
432
-
433
- 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'
434
349
 
435
- 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]
436
352
 
437
- sniff = {}
353
+ new_value = if key.equal?(:generate)
354
+ SecureRandom.uuid
355
+ else
356
+ @tables_id[key.to_s]
357
+ end
438
358
 
439
- DELIMITERS.each do |delimiter|
440
- sniff[delimiter] = first_line.count(delimiter)
359
+ if new_value.nil?
360
+ new_value = SecureRandom.uuid
361
+ @tables_id[key.to_s] = new_value
441
362
  end
442
-
443
- sniff = sniff.sort {|a,b| b[1]<=>a[1]}
444
- sniff.size > 0 ? sniff[0][0] : nil
363
+ return new_value
445
364
  end
446
365
 
447
- # helper methods that set up killbill and database options: load_config_from_file; set_config; set_database_options;
448
- # set_killbill_options;
449
- 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]
450
369
 
451
- set_config(config_file)
370
+ return new_value
371
+ end
452
372
 
453
- if not @config.nil?
454
- config_killbill = @config['killbill']
373
+ value
374
+ end
455
375
 
456
- if not config_killbill.nil?
457
- set_killbill_options([config_killbill['api_key'],config_killbill['api_secret']],
458
- [config_killbill['user'],config_killbill['password']],
459
- "http://#{config_killbill['host']}:#{config_killbill['port']}")
460
- 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?
461
382
 
462
- 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
463
387
 
464
- if not config_db.nil?
465
- set_database_options(config_db['host'],config_db['name'],
466
- [config_db['username'],config_db['password']],
467
- @logger)
388
+ Blob.new(result, TMP_DIR)
389
+ end
468
390
 
469
- end
470
- end
471
- end
391
+ def sniff_delimiter(file)
392
+ return nil if File.size?(file).nil?
472
393
 
473
- def set_config(config_file = nil)
474
- @config = nil
394
+ first_line = File.open(file, &:readline)
475
395
 
476
- if not config_file.nil?
477
- if not Dir[config_file][0].nil?
478
- @config = YAML::load_file(config_file)
479
- end
480
- end
396
+ return nil if first_line.nil?
481
397
 
398
+ sniff = {}
399
+
400
+ DELIMITERS.each do |delimiter|
401
+ sniff[delimiter] = first_line.count(delimiter)
482
402
  end
483
403
 
484
- 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
485
407
 
486
- Database.set_logger(logger)
408
+ def load_config_from_file(config_file)
409
+ self.config = config_file
487
410
 
488
- Database.set_credentials(database_credentials[0],database_credentials[1]) unless database_credentials.nil?
489
- Database.set_database_name(database_name) unless database_name.nil?
490
- Database.set_host(database_host) unless database_host.nil?
491
- Database.set_port(database_port) unless database_port.nil?
411
+ return if @config.nil?
492
412
 
493
- 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']}")
494
419
  end
495
420
 
496
- def set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
421
+ config_db = @config['database']
497
422
 
498
- 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
499
425
 
500
- @killbill_api_key = killbill_api_credentials[0]
501
- @killbill_api_secrets = killbill_api_credentials[1]
426
+ def config=(config_file = nil)
427
+ @config = nil
502
428
 
503
- end
429
+ return if config_file.nil?
504
430
 
505
- if not killbill_credentials.nil?
431
+ @config = YAML.load_file(config_file) unless Dir[config_file][0].nil?
432
+ end
506
433
 
507
- @killbill_user = killbill_credentials[0]
508
- @killbill_password = killbill_credentials[1]
434
+ def set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
435
+ unless killbill_api_credentials.nil?
509
436
 
510
- end
437
+ @killbill_api_key = killbill_api_credentials[0]
438
+ @killbill_api_secret = killbill_api_credentials[1]
511
439
 
512
- if not killbill_url.nil?
440
+ end
513
441
 
514
- @killbill_url = killbill_url
442
+ unless killbill_credentials.nil?
443
+
444
+ @killbill_user = killbill_credentials[0]
445
+ @killbill_password = killbill_credentials[1]
515
446
 
516
- end
517
447
  end
518
448
 
449
+ @killbill_url = killbill_url unless killbill_url.nil?
450
+ end
519
451
  end
520
-
521
- end
452
+ end