kpm 0.7.1 → 0.9.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 (77) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +138 -0
  4. data/Gemfile +2 -0
  5. data/README.adoc +126 -109
  6. data/Rakefile +2 -1
  7. data/bin/kpm +4 -2
  8. data/kpm.gemspec +10 -8
  9. data/lib/kpm.rb +3 -0
  10. data/lib/kpm/account.rb +269 -337
  11. data/lib/kpm/base_artifact.rb +40 -36
  12. data/lib/kpm/base_installer.rb +71 -84
  13. data/lib/kpm/blob.rb +29 -0
  14. data/lib/kpm/cli.rb +3 -1
  15. data/lib/kpm/coordinates.rb +10 -12
  16. data/lib/kpm/database.rb +93 -103
  17. data/lib/kpm/diagnostic_file.rb +126 -146
  18. data/lib/kpm/formatter.rb +76 -48
  19. data/lib/kpm/inspector.rb +24 -34
  20. data/lib/kpm/installer.rb +53 -46
  21. data/lib/kpm/kaui_artifact.rb +4 -3
  22. data/lib/kpm/killbill_plugin_artifact.rb +10 -7
  23. data/lib/kpm/killbill_server_artifact.rb +24 -10
  24. data/lib/kpm/migrations.rb +26 -11
  25. data/lib/kpm/nexus_helper/actions.rb +45 -9
  26. data/lib/kpm/nexus_helper/github_api_calls.rb +70 -0
  27. data/lib/kpm/nexus_helper/nexus_api_calls_v2.rb +130 -108
  28. data/lib/kpm/nexus_helper/nexus_facade.rb +5 -3
  29. data/lib/kpm/plugins_directory.rb +14 -9
  30. data/lib/kpm/plugins_directory.yml +16 -175
  31. data/lib/kpm/plugins_manager.rb +29 -24
  32. data/lib/kpm/sha1_checker.rb +56 -15
  33. data/lib/kpm/system.rb +104 -135
  34. data/lib/kpm/system_helpers/cpu_information.rb +56 -55
  35. data/lib/kpm/system_helpers/disk_space_information.rb +60 -63
  36. data/lib/kpm/system_helpers/entropy_available.rb +37 -39
  37. data/lib/kpm/system_helpers/memory_information.rb +52 -51
  38. data/lib/kpm/system_helpers/os_information.rb +45 -47
  39. data/lib/kpm/system_helpers/system_proxy.rb +10 -10
  40. data/lib/kpm/tasks.rb +370 -443
  41. data/lib/kpm/tenant_config.rb +68 -83
  42. data/lib/kpm/tomcat_manager.rb +10 -8
  43. data/lib/kpm/trace_logger.rb +18 -16
  44. data/lib/kpm/uninstaller.rb +81 -14
  45. data/lib/kpm/utils.rb +13 -14
  46. data/lib/kpm/version.rb +3 -1
  47. data/packaging/Gemfile +2 -0
  48. data/pom.xml +1 -1
  49. data/spec/kpm/remote/base_artifact_spec.rb +33 -17
  50. data/spec/kpm/remote/base_installer_spec.rb +35 -34
  51. data/spec/kpm/remote/github_api_calls_spec.rb +40 -0
  52. data/spec/kpm/remote/installer_spec.rb +80 -78
  53. data/spec/kpm/remote/kaui_artifact_spec.rb +7 -6
  54. data/spec/kpm/remote/killbill_plugin_artifact_spec.rb +25 -30
  55. data/spec/kpm/remote/killbill_server_artifact_spec.rb +30 -13
  56. data/spec/kpm/remote/migrations_spec.rb +12 -11
  57. data/spec/kpm/remote/nexus_facade_spec.rb +32 -28
  58. data/spec/kpm/remote/tenant_config_spec.rb +30 -29
  59. data/spec/kpm/remote/tomcat_manager_spec.rb +4 -3
  60. data/spec/kpm/unit/actions_spec.rb +52 -0
  61. data/spec/kpm/unit/base_artifact_spec.rb +19 -18
  62. data/spec/kpm/unit/cpu_information_spec.rb +67 -0
  63. data/spec/kpm/unit/disk_space_information_spec.rb +47 -0
  64. data/spec/kpm/unit/entropy_information_spec.rb +36 -0
  65. data/spec/kpm/unit/formatter_spec.rb +163 -0
  66. data/spec/kpm/unit/inspector_spec.rb +34 -42
  67. data/spec/kpm/unit/installer_spec.rb +7 -6
  68. data/spec/kpm/unit/memory_information_spec.rb +102 -0
  69. data/spec/kpm/unit/os_information_spec.rb +38 -0
  70. data/spec/kpm/unit/plugins_directory_spec.rb +38 -22
  71. data/spec/kpm/unit/plugins_manager_spec.rb +62 -66
  72. data/spec/kpm/unit/sha1_checker_spec.rb +107 -60
  73. data/spec/kpm/unit/uninstaller_spec.rb +118 -72
  74. data/spec/kpm/unit_mysql/account_spec.rb +144 -143
  75. data/spec/spec_helper.rb +20 -18
  76. data/tasks/package.rake +18 -18
  77. metadata +26 -22
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env rake
2
+ # frozen_string_literal: true
2
3
 
3
4
  Dir.glob('tasks/*.rake').each { |r| import r }
4
5
 
@@ -33,4 +34,4 @@ namespace :test do
33
34
  end
34
35
 
35
36
  # Run tests by default
36
- task :default => 'test:spec'
37
+ task default: 'test:spec'
data/bin/kpm CHANGED
@@ -1,10 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
- $:.push File.expand_path("../../lib", __FILE__)
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.push File.expand_path('../lib', __dir__)
3
5
  require 'kpm'
4
6
 
5
7
  begin
6
8
  KPM::Cli.start
7
- rescue => e
9
+ rescue StandardError => e
8
10
  KPM.ui.say "#{e.message}\n#{e.backtrace.join("\n")}", :red
9
11
  exit 1
10
12
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Copyright 2014 The Billing Project, LLC
3
5
  #
@@ -14,7 +16,7 @@
14
16
  # under the License.
15
17
  #
16
18
 
17
- $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
19
+ $LOAD_PATH.unshift File.expand_path('lib', __dir__)
18
20
  require 'kpm/version'
19
21
 
20
22
  Gem::Specification.new do |s|
@@ -34,17 +36,17 @@ Gem::Specification.new do |s|
34
36
  s.files = `git ls-files`.split("\n")
35
37
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
36
38
  s.bindir = 'bin'
37
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
38
- s.require_paths = ["lib"]
39
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
40
+ s.require_paths = ['lib']
39
41
 
40
42
  s.rdoc_options << '--exclude' << '.'
41
43
 
42
44
  s.add_dependency 'highline', '~> 1.6.21'
43
- s.add_dependency 'thor', '~> 0.19.1'
45
+ s.add_dependency 'killbill-client', '~> 3.2'
44
46
  s.add_dependency 'rubyzip', '~>1.2.0'
45
- s.add_dependency 'killbill-client', '~> 2.5.0'
47
+ s.add_dependency 'thor', '~> 0.19.1'
46
48
 
47
- s.add_development_dependency 'rake', '>= 10.0.0', '< 11.0.0'
48
- s.add_development_dependency 'rspec', '~> 2.12.0'
49
+ s.add_development_dependency 'rake', '~> 13.0'
50
+ s.add_development_dependency 'rspec', '~> 3.9'
51
+ s.add_development_dependency 'rubocop', '~> 0.88.0' if RUBY_VERSION >= '2.4'
49
52
  end
50
-
data/lib/kpm.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module KPM
2
4
  autoload :Utils, 'kpm/utils'
3
5
  autoload :BaseArtifact, 'kpm/base_artifact'
@@ -19,6 +21,7 @@ module KPM
19
21
  autoload :Migrations, 'kpm/migrations'
20
22
  autoload :System, 'kpm/system'
21
23
  autoload :Account, 'kpm/account'
24
+ autoload :Blob, 'kpm/blob'
22
25
  autoload :Database, 'kpm/database'
23
26
  autoload :TenantConfig, 'kpm/tenant_config'
24
27
  autoload :DiagnosticFile, 'kpm/diagnostic_file'
@@ -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,331 @@ 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
+ case value.to_s
320
+ when 'true'
321
+ 1
322
+ when 'false'
323
+ 0
324
+ else
386
325
  value
387
326
  end
327
+ end
388
328
 
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]
329
+ def fix_dates(value)
330
+ unless value.equal?(:DEFAULT)
405
331
 
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
332
+ dt = DateTime.parse(value)
333
+ return dt.strftime('%F %T').to_s
418
334
 
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]
335
+ end
422
336
 
423
- return new_value
424
- end
337
+ value
338
+ end
425
339
 
340
+ def fill_empty_column(value)
341
+ if value.to_s.strip.empty?
342
+ :DEFAULT
343
+ else
426
344
  value
427
345
  end
346
+ end
428
347
 
429
- def sniff_delimiter(file)
430
-
431
- return nil if File.size?(file).nil?
432
-
433
- first_line = File.open(file) {|f| f.readline}
348
+ def replace_uuid(table_name, column_name, value)
349
+ @tables_id["#{table_name}_id"] = SecureRandom.uuid if column_name == 'id'
434
350
 
435
- return nil if first_line.nil?
351
+ if ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym] && ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym][column_name.to_sym]
352
+ key = ROUND_TRIP_EXPORT_IMPORT_MAP[table_name.to_sym][column_name.to_sym]
436
353
 
437
- sniff = {}
354
+ new_value = if key.equal?(:generate)
355
+ SecureRandom.uuid
356
+ else
357
+ @tables_id[key.to_s]
358
+ end
438
359
 
439
- DELIMITERS.each do |delimiter|
440
- sniff[delimiter] = first_line.count(delimiter)
360
+ if new_value.nil?
361
+ new_value = SecureRandom.uuid
362
+ @tables_id[key.to_s] = new_value
441
363
  end
442
-
443
- sniff = sniff.sort {|a,b| b[1]<=>a[1]}
444
- sniff.size > 0 ? sniff[0][0] : nil
364
+ return new_value
445
365
  end
446
366
 
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)
367
+ unless ROUND_TRIP_EXPORT_IMPORT_MAP[:all][column_name.to_sym].nil?
368
+ key = ROUND_TRIP_EXPORT_IMPORT_MAP[:all][column_name.to_sym]
369
+ new_value = @tables_id[key.to_s]
450
370
 
451
- set_config(config_file)
371
+ return new_value
372
+ end
452
373
 
453
- if not @config.nil?
454
- config_killbill = @config['killbill']
374
+ value
375
+ end
455
376
 
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
377
+ def b64_decode_if_needed(input)
378
+ # Exclude nil or non string
379
+ return input if input.nil? || !input.is_a?(String)
380
+ # Apply regex to check that string is built as a B64 string: the character set is [A-Z, a-z, 0-9, and + /]
381
+ # and if the rest length is less than 4, the string is padded with '=' characters.
382
+ return input if input.match(B64_REGEX).nil?
461
383
 
462
- config_db = @config['database']
384
+ # Decode
385
+ result = Base64.decode64(input)
386
+ # Verify encoded of the decoded value == input prior return result
387
+ return input if Base64.strict_encode64(result) != input
463
388
 
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)
389
+ Blob.new(result, TMP_DIR)
390
+ end
468
391
 
469
- end
470
- end
471
- end
392
+ def sniff_delimiter(file)
393
+ return nil if File.size?(file).nil?
472
394
 
473
- def set_config(config_file = nil)
474
- @config = nil
395
+ first_line = File.open(file, &:readline)
475
396
 
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
397
+ return nil if first_line.nil?
481
398
 
399
+ sniff = {}
400
+
401
+ DELIMITERS.each do |delimiter|
402
+ sniff[delimiter] = first_line.count(delimiter)
482
403
  end
483
404
 
484
- def set_database_options(database_host = nil, database_port = nil, database_name = nil, database_credentials = nil, logger)
405
+ sniff = sniff.sort { |a, b| b[1] <=> a[1] }
406
+ !sniff.empty? ? sniff[0][0] : nil
407
+ end
485
408
 
486
- Database.set_logger(logger)
409
+ def load_config_from_file(config_file)
410
+ self.config = config_file
487
411
 
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?
412
+ return if @config.nil?
492
413
 
493
- Database.set_mysql_command_line
414
+ config_killbill = @config['killbill']
415
+
416
+ unless config_killbill.nil?
417
+ set_killbill_options([config_killbill['api_key'], config_killbill['api_secret']],
418
+ [config_killbill['user'], config_killbill['password']],
419
+ "http://#{config_killbill['host']}:#{config_killbill['port']}")
494
420
  end
495
421
 
496
- def set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
422
+ config_db = @config['database']
497
423
 
498
- if not killbill_api_credentials.nil?
424
+ @database = Database.new(config_db['name'], config_db['host'], config_db['port'], config_db['username'], config_db['password'], @logger) unless config_db.nil?
425
+ end
499
426
 
500
- @killbill_api_key = killbill_api_credentials[0]
501
- @killbill_api_secrets = killbill_api_credentials[1]
427
+ def config=(config_file = nil)
428
+ @config = nil
502
429
 
503
- end
430
+ return if config_file.nil?
504
431
 
505
- if not killbill_credentials.nil?
432
+ @config = YAML.load_file(config_file) unless Dir[config_file][0].nil?
433
+ end
506
434
 
507
- @killbill_user = killbill_credentials[0]
508
- @killbill_password = killbill_credentials[1]
435
+ def set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)
436
+ unless killbill_api_credentials.nil?
509
437
 
510
- end
438
+ @killbill_api_key = killbill_api_credentials[0]
439
+ @killbill_api_secret = killbill_api_credentials[1]
511
440
 
512
- if not killbill_url.nil?
441
+ end
513
442
 
514
- @killbill_url = killbill_url
443
+ unless killbill_credentials.nil?
444
+
445
+ @killbill_user = killbill_credentials[0]
446
+ @killbill_password = killbill_credentials[1]
515
447
 
516
- end
517
448
  end
518
449
 
450
+ @killbill_url = killbill_url unless killbill_url.nil?
451
+ end
519
452
  end
520
-
521
- end
453
+ end