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.
- checksums.yaml +4 -4
- data/.rubocop.yml +66 -0
- data/Gemfile +2 -0
- data/README.adoc +111 -109
- data/Rakefile +2 -1
- data/bin/kpm +4 -2
- data/kpm.gemspec +8 -6
- data/lib/kpm.rb +3 -0
- data/lib/kpm/account.rb +267 -338
- data/lib/kpm/base_artifact.rb +33 -39
- data/lib/kpm/base_installer.rb +69 -83
- data/lib/kpm/blob.rb +29 -0
- data/lib/kpm/cli.rb +3 -1
- data/lib/kpm/coordinates.rb +6 -9
- data/lib/kpm/database.rb +90 -114
- data/lib/kpm/diagnostic_file.rb +126 -147
- data/lib/kpm/formatter.rb +74 -46
- data/lib/kpm/inspector.rb +22 -32
- data/lib/kpm/installer.rb +53 -46
- data/lib/kpm/kaui_artifact.rb +4 -3
- data/lib/kpm/killbill_plugin_artifact.rb +10 -7
- data/lib/kpm/killbill_server_artifact.rb +13 -12
- data/lib/kpm/migrations.rb +8 -7
- data/lib/kpm/nexus_helper/actions.rb +47 -8
- data/lib/kpm/nexus_helper/nexus_api_calls_v2.rb +87 -94
- data/lib/kpm/nexus_helper/nexus_facade.rb +5 -3
- data/lib/kpm/plugins_directory.rb +9 -8
- data/lib/kpm/plugins_directory.yml +8 -175
- data/lib/kpm/plugins_manager.rb +29 -24
- data/lib/kpm/sha1_checker.rb +31 -18
- data/lib/kpm/system.rb +105 -136
- data/lib/kpm/system_helpers/cpu_information.rb +56 -55
- data/lib/kpm/system_helpers/disk_space_information.rb +60 -63
- data/lib/kpm/system_helpers/entropy_available.rb +37 -39
- data/lib/kpm/system_helpers/memory_information.rb +52 -51
- data/lib/kpm/system_helpers/os_information.rb +45 -47
- data/lib/kpm/system_helpers/system_proxy.rb +10 -10
- data/lib/kpm/tasks.rb +364 -437
- data/lib/kpm/tenant_config.rb +68 -83
- data/lib/kpm/tomcat_manager.rb +9 -8
- data/lib/kpm/trace_logger.rb +18 -16
- data/lib/kpm/uninstaller.rb +81 -14
- data/lib/kpm/utils.rb +13 -14
- data/lib/kpm/version.rb +3 -1
- data/packaging/Gemfile +2 -0
- data/pom.xml +1 -1
- data/spec/kpm/remote/base_artifact_spec.rb +13 -15
- data/spec/kpm/remote/base_installer_spec.rb +30 -29
- data/spec/kpm/remote/installer_spec.rb +73 -73
- data/spec/kpm/remote/kaui_artifact_spec.rb +7 -6
- data/spec/kpm/remote/killbill_plugin_artifact_spec.rb +19 -24
- data/spec/kpm/remote/killbill_server_artifact_spec.rb +17 -16
- data/spec/kpm/remote/migrations_spec.rb +12 -11
- data/spec/kpm/remote/nexus_facade_spec.rb +30 -26
- data/spec/kpm/remote/tenant_config_spec.rb +27 -26
- data/spec/kpm/remote/tomcat_manager_spec.rb +2 -1
- data/spec/kpm/unit/actions_spec.rb +52 -0
- data/spec/kpm/unit/base_artifact_spec.rb +17 -16
- data/spec/kpm/unit/cpu_information_spec.rb +67 -0
- data/spec/kpm/unit/disk_space_information_spec.rb +47 -0
- data/spec/kpm/unit/entropy_information_spec.rb +36 -0
- data/spec/kpm/unit/formatter_spec.rb +163 -0
- data/spec/kpm/unit/inspector_spec.rb +34 -42
- data/spec/kpm/unit/installer_spec.rb +5 -4
- data/spec/kpm/unit/memory_information_spec.rb +102 -0
- data/spec/kpm/unit/os_information_spec.rb +38 -0
- data/spec/kpm/unit/plugins_directory_spec.rb +34 -18
- data/spec/kpm/unit/plugins_manager_spec.rb +61 -65
- data/spec/kpm/unit/sha1_checker_spec.rb +107 -60
- data/spec/kpm/unit/uninstaller_spec.rb +107 -61
- data/spec/kpm/unit_mysql/account_spec.rb +120 -135
- data/spec/spec_helper.rb +19 -17
- data/tasks/package.rake +18 -18
- 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'
|
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 =
|
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 = {:
|
38
|
-
|
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 = [
|
41
|
-
|
42
|
-
|
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 = {:
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
-
@
|
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 =
|
85
|
+
@tables_id = {}
|
84
86
|
|
87
|
+
set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
|
85
88
|
|
86
|
-
|
87
|
-
|
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
|
-
|
104
|
-
|
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
|
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
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
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
|
-
|
153
|
-
|
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
|
-
|
178
|
-
|
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
|
-
|
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
|
-
|
175
|
+
row << sanitized_value
|
192
176
|
end
|
193
177
|
|
194
|
-
|
195
|
-
|
196
|
-
if not REMOVE_DATA_FROM[table_name.to_sym].nil?
|
178
|
+
row.join(@delimiter)
|
179
|
+
end
|
197
180
|
|
198
|
-
|
199
|
-
|
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
|
-
|
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
|
-
|
208
|
-
|
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
|
-
|
229
|
-
|
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
|
-
|
232
|
-
|
197
|
+
File.open(source_file, 'r') do |data|
|
198
|
+
rows = nil
|
199
|
+
table_name = nil
|
200
|
+
cols_names = nil
|
233
201
|
|
234
|
-
|
235
|
-
|
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
|
-
|
205
|
+
if /--/.match(words[0])
|
206
|
+
unless table_name.nil?
|
207
|
+
cols_names.shift if @generate_record_id
|
239
208
|
|
240
|
-
rows
|
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
|
-
|
248
|
-
|
249
|
-
cols_names.shift
|
250
|
-
end
|
212
|
+
table_name = words[1]
|
213
|
+
cols_names = words[2].strip.split(@delimiter)
|
251
214
|
|
252
|
-
|
253
|
-
|
215
|
+
rows = []
|
216
|
+
elsif !table_name.nil?
|
217
|
+
row = process_import_data(line, table_name, cols_names, skip_payment_methods, rows)
|
254
218
|
|
255
|
-
|
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
|
-
|
262
|
-
|
263
|
-
|
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
|
-
|
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
|
-
|
239
|
+
import(tables)
|
240
|
+
end
|
278
241
|
|
279
|
-
|
280
|
-
|
281
|
-
|
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
|
-
|
284
|
-
|
285
|
-
|
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
|
-
|
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
|
-
|
298
|
-
|
299
|
-
|
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
|
-
|
309
|
-
|
310
|
-
sanitized_value = fill_empty_column(sanitized_value)
|
260
|
+
row
|
261
|
+
end
|
311
262
|
|
312
|
-
|
313
|
-
|
314
|
-
|
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
|
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
|
-
|
321
|
-
|
322
|
-
|
271
|
+
break unless response
|
272
|
+
end
|
273
|
+
end
|
323
274
|
|
324
|
-
|
325
|
-
|
326
|
-
end
|
275
|
+
def sanitize(table_name, column_name, value, skip_payment_methods)
|
276
|
+
sanitized_value = replace_boolean(value)
|
327
277
|
|
328
|
-
|
329
|
-
sanitized_value = replace_uuid(table_name,column_name,sanitized_value)
|
330
|
-
end
|
278
|
+
sanitized_value = fill_empty_column(sanitized_value)
|
331
279
|
|
332
|
-
|
333
|
-
end
|
280
|
+
sanitized_value = SAFE_PAYMENT_METHOD if table_name == 'payment_methods' && skip_payment_methods && column_name == PLUGIN_NAME_COLUMN
|
334
281
|
|
335
|
-
|
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
|
-
|
284
|
+
sanitized_value = replace_tenant_record_id(table_name, column_name, sanitized_value) unless @tenant_record_id.nil?
|
341
285
|
|
342
|
-
|
286
|
+
sanitized_value = replace_account_record_id(table_name, column_name, sanitized_value) if @generate_record_id
|
343
287
|
|
344
|
-
|
345
|
-
end
|
288
|
+
sanitized_value = replace_uuid(table_name, column_name, sanitized_value) if @round_trip_export_import
|
346
289
|
|
347
|
-
|
348
|
-
return nil
|
349
|
-
end
|
290
|
+
sanitized_value = b64_decode_if_needed(sanitized_value) if column_name == 'billing_events'
|
350
291
|
|
351
|
-
|
292
|
+
sanitized_value
|
293
|
+
end
|
352
294
|
|
353
|
-
|
354
|
-
|
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
|
-
|
359
|
-
|
360
|
-
end
|
298
|
+
value
|
299
|
+
end
|
361
300
|
|
362
|
-
|
363
|
-
|
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
|
-
|
304
|
+
return nil if column_name == 'record_id'
|
367
305
|
|
368
|
-
|
306
|
+
if column_name == 'target_record_id'
|
369
307
|
|
370
|
-
|
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
|
-
|
381
|
-
if !value.equal?(:DEFAULT)
|
311
|
+
return :@account_record_id if column_name == 'search_key1' && table_name == 'bus_ext_events_history'
|
382
312
|
|
383
|
-
|
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
|
-
|
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
|
-
|
392
|
-
|
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
|
-
|
409
|
-
|
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
|
-
|
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
|
-
|
426
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
353
|
+
new_value = if key.equal?(:generate)
|
354
|
+
SecureRandom.uuid
|
355
|
+
else
|
356
|
+
@tables_id[key.to_s]
|
357
|
+
end
|
440
358
|
|
441
|
-
|
442
|
-
|
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
|
-
|
450
|
-
|
451
|
-
|
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
|
-
|
370
|
+
return new_value
|
371
|
+
end
|
454
372
|
|
455
|
-
|
456
|
-
|
373
|
+
value
|
374
|
+
end
|
457
375
|
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
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
|
-
|
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
|
-
|
467
|
-
|
468
|
-
[config_db['username'],config_db['password']],
|
469
|
-
@logger)
|
388
|
+
Blob.new(result, TMP_DIR)
|
389
|
+
end
|
470
390
|
|
471
|
-
|
472
|
-
|
473
|
-
end
|
391
|
+
def sniff_delimiter(file)
|
392
|
+
return nil if File.size?(file).nil?
|
474
393
|
|
475
|
-
|
476
|
-
@config = nil
|
394
|
+
first_line = File.open(file, &:readline)
|
477
395
|
|
478
|
-
|
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
|
-
|
404
|
+
sniff = sniff.sort { |a, b| b[1] <=> a[1] }
|
405
|
+
!sniff.empty? ? sniff[0][0] : nil
|
406
|
+
end
|
487
407
|
|
488
|
-
|
408
|
+
def load_config_from_file(config_file)
|
409
|
+
self.config = config_file
|
489
410
|
|
490
|
-
|
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
|
-
|
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
|
-
|
421
|
+
config_db = @config['database']
|
499
422
|
|
500
|
-
|
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
|
-
|
503
|
-
|
426
|
+
def config=(config_file = nil)
|
427
|
+
@config = nil
|
504
428
|
|
505
|
-
|
429
|
+
return if config_file.nil?
|
506
430
|
|
507
|
-
|
431
|
+
@config = YAML.load_file(config_file) unless Dir[config_file][0].nil?
|
432
|
+
end
|
508
433
|
|
509
|
-
|
510
|
-
|
434
|
+
def set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
|
435
|
+
unless killbill_api_credentials.nil?
|
511
436
|
|
512
|
-
|
437
|
+
@killbill_api_key = killbill_api_credentials[0]
|
438
|
+
@killbill_api_secret = killbill_api_credentials[1]
|
513
439
|
|
514
|
-
|
440
|
+
end
|
515
441
|
|
516
|
-
|
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
|