sct 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d0986700d21895e4341fb5dfccf2ae8ea24dcda3fd62677733fcfa082396f410
4
- data.tar.gz: 06d47615615d788cc402f099451b4efc833093f9030b624bfdf849b893c8b98b
3
+ metadata.gz: 1ca92e3c8b95fa282f479795b17cd3b0c349afee8f07c50ee53626396e1473fd
4
+ data.tar.gz: 61313bc641c95704bf4c4591914f3a9a8b33b29b4962ec5c22f4cfc6a84d990b
5
5
  SHA512:
6
- metadata.gz: f778fc09644bd3f8a0ce76f891c9b744216643e61cc1592e654db260b84afd660fbcf6991484e2d2097f577a64b55a0f1fa832fe4a3226c2454e209be870b787
7
- data.tar.gz: 3344106a0b57eb94b5cf82cc7e3e6872697fc95eb567e00a1a63149986d695db54e0a84fd8db3ad477394ec0a4620592dbf26381633d5dee218750b2ee94d597
6
+ metadata.gz: f6453c0b80246ab00dbcb6ea69c36801259f3666867c0a78ec4bb56827647740a245d1df47d3e4e207a068c969689264cd0668373e036a3420e3c603b45ec3db
7
+ data.tar.gz: ce6bced4dce7f734ef8b724c1400974ee1a8a51136b0ca6d3e506108ee62ce571dd3375289caef18f8474ebd5b7ed974b4869d118b9f8e13778633ae0ca92ae6
@@ -40,7 +40,7 @@ module Henk
40
40
  Henk::Runner.new.encrypt_config_data options.domain
41
41
  end
42
42
  end
43
-
43
+
44
44
  command :copy do |c|
45
45
  c.syntax = 'sct henk copy'
46
46
  c.description = 'Copy a client from another environment to your local environment'
@@ -81,7 +81,7 @@ module Henk
81
81
  Henk::Runner.new.synchronize_employees options.target_domain
82
82
  end
83
83
  end
84
-
84
+
85
85
  command :remove do |c|
86
86
  c.syntax = 'sct henk remove'
87
87
  c.description = 'Remove a client from your local environment.'
@@ -105,6 +105,44 @@ module Henk
105
105
  end
106
106
  end
107
107
 
108
+ command :"list-dumps" do |c|
109
+ c.syntax = 'sct henk list-dumps'
110
+ c.description = 'List the customer dumps currently available for import.'
111
+
112
+ c.action do |args, options|
113
+ Henk::Runner.new.list_dumps args, options
114
+ end
115
+ end
116
+
117
+ command :"import" do |c|
118
+ c.syntax = 'sct henk import <URL>'
119
+ c.description = 'Import a customer dump from GCP.'
120
+
121
+ c.action do |args, options|
122
+ case args.length
123
+ when 0
124
+ puts "Loading available customer dumps...".blue
125
+
126
+ available_dumps = `sct henk list-dumps`.lines chomp: true
127
+
128
+ dump_url = choose do |menu|
129
+ menu.prompt = "Please choose a dump to import:"
130
+
131
+ for dump in available_dumps
132
+ menu.choice dump
133
+ end
134
+ end
135
+ when 1
136
+ dump_url = args.first
137
+ else
138
+ UI.error "Expected 0 or 1 arguments, received #{args.length}: #{args}"
139
+ exit 1
140
+ end
141
+
142
+ Henk::Runner.new.import args, options, dump_url
143
+ end
144
+ end
145
+
108
146
  run!
109
147
  end
110
148
 
@@ -3,7 +3,7 @@ require 'pp'
3
3
  module Henk
4
4
  class Runner
5
5
  attr_accessor :options_mapping
6
-
6
+
7
7
  def initialize
8
8
  @options_mapping = {
9
9
  "organization_name" => {
@@ -56,8 +56,8 @@ module Henk
56
56
 
57
57
  connected_to_network = `docker network inspect spend-cloud | jq '.[0] | .Containers | map(select((.Name | test("^#{required_containers.join('|^')}")))) | length'`
58
58
 
59
- if connected_to_network.to_s.strip! != "3"
60
- puts "Error: ".red + "Not all required services are running or connected to the cluster (#{connected_to_network}/3)"
59
+ if connected_to_network.to_s.strip! != "5"
60
+ puts "Error: ".red + "Not all required services are running or connected to the cluster (#{connected_to_network}/5)"
61
61
  exit(1)
62
62
  end
63
63
 
@@ -88,7 +88,7 @@ module Henk
88
88
  def encrypt_config_data domain
89
89
  puts "Manually encrypting config data for #{domain}".green
90
90
  php_command = "php /var/www/scripts/encryptConfigData.php -c='#{domain}' --execute"
91
- command = 'docker exec -it proactive-config bash -c "#{php_command}"'
91
+ command = 'docker exec -it proactive-config-php bash -c "#{php_command}"'
92
92
  self.run command
93
93
  end
94
94
 
@@ -171,5 +171,127 @@ module Henk
171
171
  options_str += parsed_options.join(' ')
172
172
  return options_str
173
173
  end
174
+
175
+ def list_dumps args, options
176
+ run "gsutil ls gs://henk-db-dumps/"
177
+ end
178
+
179
+ def import args, options, dump_url
180
+ puts "Going to import #{dump_url}".blue
181
+
182
+ dir = Dir.mktmpdir
183
+
184
+ begin
185
+ zip = download_and_unzip dump_url, dir
186
+
187
+ customer = zip.slice 0, zip.rindex("_")
188
+
189
+ customer_database = customer.gsub "-", "_"
190
+
191
+ remove_customer customer
192
+
193
+ copy_db_encryption_key customer, dir
194
+
195
+ run_sql_files dir
196
+
197
+ add_customer_account customer, customer_database
198
+
199
+ update_and_encrypt_customer_config customer, customer_database
200
+
201
+ puts "Finished importing #{customer}".green
202
+ ensure
203
+ run "rm -r #{dir}"
204
+ end
205
+ end
206
+
207
+ def download_and_unzip dump_url, dir
208
+ run "gsutil cp #{dump_url} #{dir}"
209
+
210
+ zip_path = Dir["#{dir}/*.zip"].first
211
+
212
+ run "unzip #{zip_path} -d #{dir}"
213
+
214
+ zip = File.basename zip_path
215
+ end
216
+
217
+ def remove_customer customer
218
+ local_customers = `sct henk list-clients`.lines chomp: true
219
+
220
+ if local_customers.include? customer
221
+ puts "Customer already exists. Removing customer before importing...".yellow
222
+
223
+ run "sct henk remove --domain #{customer}"
224
+
225
+ puts "Removed existing customer".green
226
+ end
227
+ end
228
+
229
+ def copy_db_encryption_key customer, dir
230
+ run "docker exec proactive-config-php mkdir /data/#{customer}"
231
+ run "docker cp #{dir}/db_encryption.key proactive-config-php:/data/#{customer}/"
232
+
233
+ puts "Copied db_encryption.key to proactive-config-php:/data/#{customer}/".green
234
+ end
235
+
236
+ def run_sql_files dir
237
+ sql_paths = Dir["#{dir}/*.sql"]
238
+
239
+ sql_paths.each_with_index do |path, index|
240
+ file = File.basename path
241
+ database = File.basename path, ".*"
242
+
243
+ puts "Running #{file} (#{index + 1}/#{sql_paths.length})".blue
244
+
245
+ run_query "CREATE DATABASE IF NOT EXISTS `#{database}`"
246
+
247
+ run_query "source /sql/#{file}", database: database, volume: "#{dir}:/sql"
248
+ end
249
+
250
+ puts "Finished running SQL files".green
251
+ end
252
+
253
+ def add_customer_account customer, customer_database
254
+ account_data = "\n[#{customer}]\ndatabase = #{customer_database}\ndomain = NULL\nrewrite = #{customer}\nimport_planning = 1\njobs = 1\nscan_upload = 0\nfiatteren_mailbox = 1\ncontracten_mailbox = 1"
255
+
256
+ run "docker exec proactive-config-php sh -c 'echo \"#{account_data}\" >> /data/proactive_accounts.ini'"
257
+
258
+ puts "Added account to proactive_accounts.ini".green
259
+ end
260
+
261
+ def update_and_encrypt_customer_config customer, customer_database
262
+ dsn = "mysql:host=mysql-service;user=root;dbname=#{customer_database};config=proactive-php;config-path=cfg/db;"
263
+
264
+ website_application = "https://#{customer}.dev.spend.cloud"
265
+
266
+ email_administrator = "noreply@dev.spend.cloud"
267
+
268
+ debug_email_address = "noreply@dev.spend.cloud"
269
+
270
+ query = "UPDATE `00_settings` SET `045` = \"#{dsn}\", `029` = \"#{website_application}\", `002` = \"#{email_administrator}\", `031` = \"#{debug_email_address}\" WHERE `043` = \"#{customer}\""
271
+
272
+ run_query query, database: "spend-cloud-config"
273
+
274
+ puts "Updated config values".green
275
+
276
+ run "docker exec proactive-config-php php /var/www/scripts/encryptConfigData.php -c='#{customer}' --execute"
277
+
278
+ puts "Encrypted config values".green
279
+ end
280
+
281
+ def run_query query, database: nil, volume: nil
282
+ command = "docker run --rm --network container:mysql-service"
283
+
284
+ if volume
285
+ command = "#{command} -v #{volume}"
286
+ end
287
+
288
+ command = "#{command} arey/mysql-client -h mysql-service"
289
+
290
+ if database
291
+ command = "#{command} -D #{database}"
292
+ end
293
+
294
+ run "#{command} -e '#{query}'"
295
+ end
174
296
  end
175
297
  end
@@ -1,3 +1,3 @@
1
1
  module Sct
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sct
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reshad Farid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-17 00:00:00.000000000 Z
11
+ date: 2022-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colored