sct 1.1.2 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87ea56635397f45831d78759ad65fa074bbde63827fc8c8fba75903f71bf45a6
4
- data.tar.gz: 0c72ba5441f383c3dea4f5912c33734822774c93054fdd332a7f57042d46ec66
3
+ metadata.gz: aa71b183edc2b021f2c3399d5ca7dffe713e3b7b1a26ceada2e49bb224ae50b8
4
+ data.tar.gz: 32f4f9e7d27f5f9a77fdb34138150c5b336c21504695dfa174448e5fd6152f88
5
5
  SHA512:
6
- metadata.gz: 508892aaef3fffe214203682a28f8976abb681c01243154df5c4ef061a3ebd84c97a510858ae91ead399ab56e68329db6b318ff434afa9b48ca966aad62d89cb
7
- data.tar.gz: 6b7868aaf906825de525197e174766edca7de50e90775cd9a130a48c6a4c2cd305250e455c964da5088cead2799250d4083a973101a1288bfd5ab1a093c80b5c
6
+ metadata.gz: 77cdf1a0d171c64d0e90e54e218a4bb904bfba9d806f4f0b56f8bae7b2f8494c88e74d5496890f188acf3058835ea149154582077af5174977809e75f152bab8
7
+ data.tar.gz: d3b0dcccad17ecd873a9f2288b66c81d82f0e8786e94365b7aa7790a970a794146a9701e9dc510c50dfbf28d6fb03f4fdc50d748b003584d72d4be06676cad41
@@ -11,11 +11,16 @@ module Cluster
11
11
  end
12
12
  end
13
13
 
14
+ def dc_system
15
+ return executer = system("which docker-compose") ? 'docker-compose' : 'docker compose'
16
+ end
17
+
14
18
  def run_dc command, options = {}
15
- run "docker-compose -f ~/development/spend-cloud/docker-compose.yml #{command}", options
19
+ run "#{dc_system} -f ~/development/spend-cloud/docker-compose.yml #{command}", options
16
20
  end
17
21
 
18
22
  def start args, options
23
+ print "DC_SYSTEM: #{dc_system}"
19
24
  if options.pull
20
25
  run_dc "pull"
21
26
  end
@@ -26,7 +31,7 @@ module Cluster
26
31
 
27
32
  run "docker container prune -f"
28
33
 
29
- run_dc "up --detach"
34
+ run_dc "up --detach --remove-orphans --force-recreate --always-recreate-deps"
30
35
 
31
36
  if options.pull or options.build
32
37
  system "docker image prune -f"
@@ -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,48 @@ 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'
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
+ begin
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
+ rescue Interrupt
136
+ print "\nStopping import"
137
+ exit
138
+ end
139
+ when 1
140
+ dump_url = args.first
141
+ else
142
+ UI.error "Expected 0 or 1 arguments, received #{args.length}: #{args}"
143
+ exit 1
144
+ end
145
+
146
+ Henk::Runner.new.import args, options, dump_url
147
+ end
148
+ end
149
+
108
150
  run!
109
151
  end
110
152
 
@@ -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
@@ -10,12 +10,17 @@ module Sct
10
10
  exit 1
11
11
  end
12
12
 
13
+ def dc_system
14
+ return executer = system("which docker-compose") ? 'docker-compose' : 'docker compose'
15
+ end
16
+
13
17
  def dc command, env = {}
14
- system env, "docker-compose -f ~/development/spend-cloud/docker-compose.yml #{command}"
18
+ system env, "#{dc_system} -f ~/development/spend-cloud/docker-compose.yml #{command}"
15
19
  end
16
20
 
17
21
  def dc_dev command, env = {}
18
- system env, "docker-compose -f #{@@file} #{command}"
22
+
23
+ system env, "#{dc_system} -f #{@@file} #{command}"
19
24
  end
20
25
 
21
26
  def manifest
@@ -27,6 +32,9 @@ module Sct
27
32
  end
28
33
 
29
34
  def execute args, options
35
+
36
+ UI.message('Trying to start development environment')
37
+
30
38
  services = manifest["services"].to_a
31
39
 
32
40
  if Sct::Helper.is_windows?
@@ -46,7 +54,7 @@ module Sct
46
54
  }
47
55
 
48
56
  if options.pull
49
- return unless dc_dev "pull"
57
+ return unless dc_dev "pull", env
50
58
  end
51
59
 
52
60
  if options.build
@@ -57,18 +65,28 @@ module Sct
57
65
  system "docker image prune -f"
58
66
  end
59
67
 
60
- for service, settings in services
68
+ for service, service_spec in services
61
69
  return unless dc "rm --stop --force #{service}"
62
70
  end
63
71
 
64
- begin
65
- dc_dev "up", env
66
- rescue SystemExit, Interrupt
67
- end
72
+ if services.length == 1
73
+ service, service_spec = services.first
74
+
75
+ container = service_spec["container_name"]
76
+ command = service_spec["command"] || ""
68
77
 
69
- dc_dev "rm --force --stop"
78
+ dc_dev "run --rm --service-ports --name #{container} #{service} #{command}", env
79
+ else
80
+ begin
81
+ dc_dev "up --remove-orphans --force-recreate --always-recreate-deps", env
82
+ rescue Interrupt
83
+ # user pressed Ctrl+C, do nothing
84
+ end
85
+
86
+ dc_dev "down --remove-orphans", env
87
+ end
70
88
 
71
- for service, settings in services
89
+ for service, service_spec in services
72
90
  dc "up --detach #{service}"
73
91
  end
74
92
  end
@@ -39,6 +39,8 @@ module Sct
39
39
 
40
40
  user = options.root ? "root:root" : "$(id -u):$(id -g)"
41
41
 
42
+ puts "Attaching to shell in container #{container.bold}".green
43
+
42
44
  system "docker exec -it --user #{user} #{container} #{command}"
43
45
  end
44
46
 
@@ -1,3 +1,3 @@
1
1
  module Sct
2
- VERSION = "1.1.2"
2
+ VERSION = "1.4.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.1.2
4
+ version: 1.4.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: 2021-12-21 00:00:00.000000000 Z
11
+ date: 2022-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colored