prima-twig 1.3.9 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prima-twig
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.9
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matteo Giachino
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2021-03-03 00:00:00.000000000 Z
17
+ date: 2021-07-28 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: aws-sdk-autoscaling
@@ -30,20 +30,6 @@ dependencies:
30
30
  - - "~>"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '1'
33
- - !ruby/object:Gem::Dependency
34
- name: aws-sdk-batch
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '1'
40
- type: :runtime
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - "~>"
45
- - !ruby/object:Gem::Version
46
- version: '1'
47
33
  - !ruby/object:Gem::Dependency
48
34
  name: aws-sdk-cloudformation
49
35
  requirement: !ruby/object:Gem::Requirement
@@ -58,20 +44,6 @@ dependencies:
58
44
  - - "~>"
59
45
  - !ruby/object:Gem::Version
60
46
  version: '1'
61
- - !ruby/object:Gem::Dependency
62
- name: aws-sdk-cloudfront
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '1'
68
- type: :runtime
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '1'
75
47
  - !ruby/object:Gem::Dependency
76
48
  name: aws-sdk-core
77
49
  requirement: !ruby/object:Gem::Requirement
@@ -86,62 +58,6 @@ dependencies:
86
58
  - - "~>"
87
59
  - !ruby/object:Gem::Version
88
60
  version: '3'
89
- - !ruby/object:Gem::Dependency
90
- name: aws-sdk-ec2
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "~>"
94
- - !ruby/object:Gem::Version
95
- version: '1'
96
- type: :runtime
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - "~>"
101
- - !ruby/object:Gem::Version
102
- version: '1'
103
- - !ruby/object:Gem::Dependency
104
- name: aws-sdk-ecs
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - "~>"
108
- - !ruby/object:Gem::Version
109
- version: '1'
110
- type: :runtime
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - "~>"
115
- - !ruby/object:Gem::Version
116
- version: '1'
117
- - !ruby/object:Gem::Dependency
118
- name: aws-sdk-elasticloadbalancingv2
119
- requirement: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - "~>"
122
- - !ruby/object:Gem::Version
123
- version: '1'
124
- type: :runtime
125
- prerelease: false
126
- version_requirements: !ruby/object:Gem::Requirement
127
- requirements:
128
- - - "~>"
129
- - !ruby/object:Gem::Version
130
- version: '1'
131
- - !ruby/object:Gem::Dependency
132
- name: aws-sdk-s3
133
- requirement: !ruby/object:Gem::Requirement
134
- requirements:
135
- - - "~>"
136
- - !ruby/object:Gem::Version
137
- version: '1'
138
- type: :runtime
139
- prerelease: false
140
- version_requirements: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - "~>"
143
- - !ruby/object:Gem::Version
144
- version: '1'
145
61
  - !ruby/object:Gem::Dependency
146
62
  name: colorize
147
63
  requirement: !ruby/object:Gem::Requirement
@@ -170,48 +86,6 @@ dependencies:
170
86
  - - "~>"
171
87
  - !ruby/object:Gem::Version
172
88
  version: '1.6'
173
- - !ruby/object:Gem::Dependency
174
- name: launchy
175
- requirement: !ruby/object:Gem::Requirement
176
- requirements:
177
- - - "~>"
178
- - !ruby/object:Gem::Version
179
- version: '2.0'
180
- type: :runtime
181
- prerelease: false
182
- version_requirements: !ruby/object:Gem::Requirement
183
- requirements:
184
- - - "~>"
185
- - !ruby/object:Gem::Version
186
- version: '2.0'
187
- - !ruby/object:Gem::Dependency
188
- name: mail
189
- requirement: !ruby/object:Gem::Requirement
190
- requirements:
191
- - - "~>"
192
- - !ruby/object:Gem::Version
193
- version: '2.6'
194
- type: :runtime
195
- prerelease: false
196
- version_requirements: !ruby/object:Gem::Requirement
197
- requirements:
198
- - - "~>"
199
- - !ruby/object:Gem::Version
200
- version: '2.6'
201
- - !ruby/object:Gem::Dependency
202
- name: octokit
203
- requirement: !ruby/object:Gem::Requirement
204
- requirements:
205
- - - "~>"
206
- - !ruby/object:Gem::Version
207
- version: '3.0'
208
- type: :runtime
209
- prerelease: false
210
- version_requirements: !ruby/object:Gem::Requirement
211
- requirements:
212
- - - "~>"
213
- - !ruby/object:Gem::Version
214
- version: '3.0'
215
89
  - !ruby/object:Gem::Dependency
216
90
  name: redcarpet
217
91
  requirement: !ruby/object:Gem::Requirement
@@ -226,34 +100,6 @@ dependencies:
226
100
  - - "~>"
227
101
  - !ruby/object:Gem::Version
228
102
  version: '3.3'
229
- - !ruby/object:Gem::Dependency
230
- name: redis
231
- requirement: !ruby/object:Gem::Requirement
232
- requirements:
233
- - - "~>"
234
- - !ruby/object:Gem::Version
235
- version: '4.0'
236
- type: :runtime
237
- prerelease: false
238
- version_requirements: !ruby/object:Gem::Requirement
239
- requirements:
240
- - - "~>"
241
- - !ruby/object:Gem::Version
242
- version: '4.0'
243
- - !ruby/object:Gem::Dependency
244
- name: rubyflare
245
- requirement: !ruby/object:Gem::Requirement
246
- requirements:
247
- - - "~>"
248
- - !ruby/object:Gem::Version
249
- version: '0'
250
- type: :runtime
251
- prerelease: false
252
- version_requirements: !ruby/object:Gem::Requirement
253
- requirements:
254
- - - "~>"
255
- - !ruby/object:Gem::Version
256
- version: '0'
257
103
  - !ruby/object:Gem::Dependency
258
104
  name: rugged
259
105
  requirement: !ruby/object:Gem::Requirement
@@ -285,13 +131,11 @@ dependencies:
285
131
  description: Our tools to manage git and github
286
132
  email: matteo.giachino@prima.it
287
133
  executables:
288
- - twig-feature
289
134
  - twig-packer-builder
290
135
  - twig-update-ami
291
136
  extensions: []
292
137
  extra_rdoc_files: []
293
138
  files:
294
- - bin/twig-feature
295
139
  - bin/twig-packer-builder
296
140
  - bin/twig-update-ami
297
141
  - lib/command.rb
data/bin/twig-feature DELETED
@@ -1,725 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- require_relative '../lib/prima_twig'
5
- require_relative '../lib/prima_aws_client'
6
- require 'digest'
7
- require 'json'
8
- require 'launchy'
9
- require 'pp'
10
- require 'redis'
11
- require 'octokit'
12
-
13
- class Release
14
- include Command
15
- include PrimaAwsClient
16
-
17
- def initialize(update_gem = true)
18
- @prima = Prima.new
19
- if update_gem
20
- output 'Controllo se ci sono aggiornamenti da fare (potrebbe richiedere qualche minuto)'
21
- unless `gem update prima-twig` == "Updating installed gems\nNothing to update\n"
22
- output 'Gemma prima-twig aggiornata'
23
- exec "twig feature #{ARGV.join ' '}"
24
- end
25
- end
26
- @dns_record_identifier = nil
27
- @ecs_cluster_name = nil
28
- @projects = {
29
- 'prima' => {},
30
- 'urania' => {},
31
- 'ermes' => {},
32
- 'bburago' => {},
33
- 'hal9000' => {},
34
- 'fidaty' => {},
35
- 'peano' => {},
36
- 'assange' => {},
37
- 'borat' => {},
38
- 'crash' => {},
39
- 'activia' => {},
40
- 'skynet' => {},
41
- 'roger' => {},
42
- 'rachele' => {},
43
- 'leftorium' => {},
44
- 'starsky' => {},
45
- 'hutch' => {},
46
- 'maia' => {},
47
- 'legion' => {},
48
- 'vianello' => {},
49
- 'domus' => {},
50
- 'toretto' => {},
51
- 'lira' => {},
52
- 'frontale' => {},
53
- 'baggio' => {},
54
- 'mario' => {},
55
- 'zuhause' => {},
56
- 'pyxis' => {}
57
- }
58
- @base_stack_name_alb = 'ecs-alb-http-public-qa-'
59
- @base_stack_name_alb_ws = 'ecs-alb-ws-public-qa-'
60
- @cloudflare = Rubyflare.connect_with(ENV['CLOUDFLARE_EMAIL'], ENV['CLOUDFLARE_APIKEY'])
61
- @config = YAML.load_file 'twig.yml'
62
- @gh = Octokit::Client.new(access_token: @config['github'])
63
- end
64
-
65
- def execute!(args)
66
- case args[0]
67
- when 'qainit'
68
- abort('Non sei nella cartella di qainit') unless Dir.pwd.match('qainit$') || Dir.pwd.match('/drone/src')
69
- if %w[terminate stop shutdown halt destroy].include? args[1]
70
- qainit_deploy_shutdown!
71
- elsif 'update' == args[1]
72
- qainit_deploy_update!
73
- else
74
- if args[1]
75
- select_branches(args[1..-1])
76
- else
77
- select_branches
78
- end
79
- qainit_deploy!
80
- end
81
- when 'suite'
82
- abort('Non sei nella cartella di qainit') unless Dir.pwd.match 'qainit$'
83
- if 'deploy' == args[1]
84
- suite_py_branches
85
- qainit_deploy!(true)
86
- end
87
- when 'deploy'
88
- deploy_lock! if 'lock' == args[1]
89
- when 'aggregator'
90
- if 'enable' == args[1]
91
- aggregator_enable!
92
- elsif 'disable' == args[1]
93
- aggregator_disable!
94
- else
95
- stop_for_wrong_args
96
- end
97
- else
98
- stop_for_wrong_args
99
- end
100
- end
101
-
102
- def stop_for_wrong_args
103
- puts help_content
104
- stop_if true, [:wrong_args, ['start', 'finish', 'deploy', 'deploy project_name', 'deploy stop', 'deploy update', 'aggregator enable', 'aggregator disable']]
105
- end
106
-
107
- def aggregator_disable!
108
- output 'Disable aggregator'
109
-
110
- output 'Recupero le informazioni relative al puntamento dei record DNS...'
111
- output 'Recupero le informazioni sui QA attivi...'
112
- stack_list, envs = get_stacks
113
-
114
- env_hash = nil
115
- if envs.empty?
116
- output 'Nessun QA trovato'.red
117
- exit
118
- else
119
- env_hash = envs.detect do |_key, tags|
120
- aggregator_enabled = tags.detect do |tag|
121
- tag.key === 'hostname_pattern_priority' and tag.value === '1'
122
- end.is_a?(Aws::CloudFormation::Types::Tag)
123
- aggregator_enabled
124
- end[0]
125
- dns_records = @cloudflare.get('zones/1fb634f19c43dfb0162cc4cb91915da2/dns_records', { per_page: 100, type: 'CNAME', content: get_alb_host(@base_stack_name_alb + env_hash[3..8]) })
126
- stop_if dns_records.body[:result].empty?, 'I record DNS degli aggregatori non stanno puntando ad un QA'.red
127
- change_hostname_priority(env_hash, hostname_pattern_priority)
128
- dns_to_staging(env_hash)
129
- end
130
-
131
- output 'Finito!'.green
132
- end
133
-
134
- def aggregator_enable!
135
- output 'Enable aggregator'
136
-
137
- output 'Recupero le informazioni relative al puntamento dei record DNS...'
138
- dns_records = @cloudflare.get('zones/1fb634f19c43dfb0162cc4cb91915da2/dns_records', { per_page: 100, type: 'CNAME', content: 'staging.prima.it' })
139
- stop_if dns_records.body[:result].empty?, "I record DNS degli aggregatori stanno gia' puntando ad un QA".red
140
-
141
- output 'Recupero le informazioni sui QA attivi...'
142
- stack_list, envs = get_stacks
143
-
144
- env_hash = nil
145
- if envs.empty?
146
- output 'Nessun QA trovato'.red
147
- exit
148
- else
149
- env_hash = choose do |menu|
150
- menu.prompt = 'Scegli il QA al quale vuoi far puntare gli ambienti di staging dei comparatori: '.cyan
151
- menu.shell = true
152
- envs.each do |key, env|
153
- title = ''
154
- env.each do |e|
155
- title << "\n#{e.key.upcase}: #{e.value}"
156
- end
157
- msg = @prima.reduce_size(title, 1000).to_s.light_blue
158
- menu.choice(msg) { key }
159
- end
160
- end
161
- end
162
-
163
- change_hostname_priority(env_hash, '1')
164
-
165
- dns_records.body[:result].each do |dns|
166
- if dns[:name] =~ /^\w+-\w+-staging\.prima\.it$/
167
- output "Changing #{dns[:name]} DNS record"
168
- @cloudflare.put("zones/1fb634f19c43dfb0162cc4cb91915da2/dns_records/#{dns[:id]}", { type: 'CNAME', name: dns[:name], content: get_alb_host(@base_stack_name_alb + env_hash[3..8]), proxied: true, ttl: 1 })
169
- end
170
- end
171
-
172
- output 'Finito!'.green
173
- end
174
-
175
- def change_hostname_priority(env_hash, hostname_pattern_priority)
176
- cluster_stack_name = "ecs-cluster-#{env_hash}"
177
- tags = get_stack_tags(cluster_stack_name).map do |tag|
178
- if tag.key === 'hostname_pattern_priority'
179
- {
180
- key: 'hostname_pattern_priority',
181
- value: hostname_pattern_priority
182
- }
183
- else
184
- tag
185
- end
186
- end
187
-
188
- stack_list = stack_list()
189
-
190
- stack_list.each do |stack|
191
- if stack.stack_name.match(/#{env_hash}$/)
192
- stack_name = stack.stack_name
193
- update_stack(stack_name, get_stack_template(stack_name), get_stack_parameters(stack_name), tags)
194
- end
195
- end
196
-
197
- stack_list.each do |stack|
198
- if stack.stack_name.match(/#{env_hash}$/)
199
- wait_for_stack_ready(stack.stack_name) unless stack_ready?(stack.stack_name)
200
- end
201
- end
202
-
203
- stack_name_web = "ecs-task-web-#{env_hash}"
204
- parameters = get_stack_parameters(stack_name_web).map do |param|
205
- if param.parameter_key === 'HostnamePatternPriority'
206
- {
207
- parameter_key: 'HostnamePatternPriority',
208
- parameter_value: hostname_pattern_priority
209
- }
210
- elsif param.parameter_key === 'HostnamePatternAggregatorPriority'
211
- {
212
- parameter_key: 'HostnamePatternAggregatorPriority',
213
- parameter_value: (hostname_pattern_priority.to_i + 1).to_s
214
- }
215
- else
216
- param
217
- end
218
- end
219
-
220
- update_stack(stack_name_web, get_stack_template(stack_name_web), parameters)
221
-
222
- wait_for_stack_ready(stack_name_web) unless stack_ready?(stack_name_web)
223
- end
224
-
225
- def dns_to_staging(env_hash)
226
- output 'Recupero le informazioni relative al puntamento dei record DNS...'
227
- dns_records = @cloudflare.get('zones/1fb634f19c43dfb0162cc4cb91915da2/dns_records', { per_page: 100, type: 'CNAME', content: get_alb_host(@base_stack_name_alb + env_hash[3..8]) })
228
- dns_records.body[:result].each do |dns|
229
- if dns[:name] =~ /^\w+-\w+-staging\.prima\.it$/
230
- output "Changing #{dns[:name]} DNS record"
231
- @cloudflare.put("zones/1fb634f19c43dfb0162cc4cb91915da2/dns_records/#{dns[:id]}", { type: 'CNAME', name: dns[:name], content: 'staging.prima.it', proxied: true, ttl: 1 })
232
- end
233
- end
234
- end
235
-
236
- def deploy_shutdown!
237
- output 'Recupero le informazioni sui QA attivi...'
238
- stack_list, envs = get_stacks
239
-
240
- env_hash = nil
241
- if envs.empty?
242
- output 'Nessun environment trovato'.red
243
- exit
244
- else
245
- env_hash = choose do |menu|
246
- menu.prompt = "Scegli l'environment che vuoi spegnere: ".cyan
247
- menu.shell = true
248
-
249
- envs.each do |key, env|
250
- title = ''
251
- env.each do |e|
252
- title << "\n#{e.key}: #{e.value} "
253
- end
254
- msg = @prima.reduce_size(title, 1000).to_s.light_blue
255
- menu.choice(msg) { key }
256
- end
257
- end
258
- end
259
-
260
- cluster_stack_name = nil
261
- stacks_to_delete = []
262
- stack_list.each do |stack|
263
- if stack.stack_name.match(/#{env_hash}$/)
264
- if stack.stack_name.match(/ecs-cluster/)
265
- cluster_stack_name = stack.stack_name
266
- else
267
- break unless stack.stack_name.match(/#{env_hash}$/)
268
-
269
- stacks_to_delete.push(stack.stack_name)
270
- delete_stack(stack.stack_name)
271
- end
272
- end
273
- end
274
-
275
- cluster_stack_name = "ecs-cluster-#{env_hash}"
276
- aggregator_enabled = get_stack_tags(cluster_stack_name).detect do |tag|
277
- tag.key === 'hostname_pattern_priority' and tag.value === '1'
278
- end.is_a?(Aws::CloudFormation::Types::Tag)
279
-
280
- dns_to_staging(env_hash) if aggregator_enabled
281
-
282
- # Se non ha finito di cancellare le altre non si puo' cancellare il cluster
283
- output 'Attendo 10 secondi per poter eliminare il cluster ECS'
284
-
285
- while stacks_to_delete.length > 0
286
- sleep 13
287
- stacks_to_delete.each do |stack_name|
288
- stacks_to_delete -= [stack_name] unless stack_exists?(stack_name)
289
- end
290
- output "Stack ancora attivi: #{stacks_to_delete.length}. Attendo altri 10 secondi per eliminare il cluster ECS"
291
- end
292
-
293
- delete_stack(cluster_stack_name)
294
- delete_stack(@base_stack_name_alb + env_hash[3..8])
295
- delete_stack(@base_stack_name_alb_ws + env_hash[3..8])
296
- output 'Finito!'.green
297
- end
298
-
299
- def deploy_lock!
300
- output 'Deploy update menu'
301
- `git pull`
302
-
303
- output 'Recupero le informazioni sui QA attivi...'
304
- stack_list, envs = get_clusters
305
-
306
- env_hash = nil
307
- if envs.empty?
308
- output 'Nessun QA trovato'.red
309
- exit
310
- else
311
- env_hash = choose do |menu|
312
- menu.prompt = 'Scegli il QA che vuoi proteggere dallo spegnimento automatico: '.cyan
313
- menu.shell = true
314
- envs.each do |key, env|
315
- title = ''
316
- env.each do |e|
317
- title << e.value.to_s if e.key == 'qainit'
318
- end
319
- msg = @prima.reduce_size(title, 1000).to_s.light_blue
320
- menu.choice(msg) { key }
321
- end
322
- end
323
- end
324
-
325
- cluster_stack_name = "ecs-cluster-#{env_hash}"
326
- if stack_exists?(cluster_stack_name)
327
- tags = get_stack_tags(cluster_stack_name)
328
- tag_keep_data = Aws::CloudFormation::Types::Tag.new({ key: 'AUTOMATIC_DELETION_PROTECTION', value: 'true' })
329
- tags.push tag_keep_data
330
- end
331
-
332
- update_cluster_stack(cluster_stack_name, tags, get_stack_parameters(cluster_stack_name))
333
-
334
- output 'Finito!'.green
335
- end
336
-
337
- def deploy_update!
338
- output 'Deploy update menu'
339
- `git pull`
340
-
341
- output 'Recupero le informazioni sui QA attivi...'
342
- stack_list, envs = get_stacks
343
-
344
- env_hash = nil
345
- if envs.empty?
346
- output 'Nessun QA trovato'.red
347
- exit
348
- else
349
- env_hash = choose do |menu|
350
- menu.prompt = 'Scegli il QA che vuoi aggiornare: '.cyan
351
- menu.shell = true
352
- envs.each do |key, env|
353
- title = ''
354
- env.each do |e|
355
- title << "\n#{e.key.upcase}: #{e.value}"
356
- end
357
- msg = @prima.reduce_size(title, 1000).to_s.light_blue
358
- menu.choice(msg) { key }
359
- end
360
- end
361
- end
362
-
363
- envs[env_hash].each do |env|
364
- unless %w[hostname_pattern_priority AUTOMATIC_DELETION_PROTECTION].include? env.key
365
- @projects[env.key] = select_branch_to_deploy(env.key, env.value)
366
- end
367
- end
368
- deploy_feature!
369
-
370
- output 'Finito!'.green
371
- end
372
-
373
- def get_default_branch_name(projects)
374
- projects.each_key do |project|
375
- return projects[project]['name'] unless projects[project]['default_branch']
376
- end
377
- end
378
-
379
- def suite_py_branches
380
- if File.exist?('suitepy-projects.yml')
381
- arg_projects = YAML.load(File.read('suitepy-projects.yml'))
382
-
383
- @projects.merge!(arg_projects)
384
-
385
- `rm suitepy-projects.yml`
386
-
387
- @projects.each_key do |project|
388
- @projects[project] = choose_branch_to_deploy(project, true) if @projects[project].empty?
389
- end
390
- end
391
- end
392
-
393
- def get_git_user
394
- `git config user.name`.gsub(/[^A-Za-z]/, '').gsub("\n", '')
395
- end
396
-
397
- def get_git_mail
398
- `git config user.email`.gsub("\n", '')
399
- end
400
-
401
- def qainit_deploy!(quiet = false)
402
- `git checkout master && git fetch && git pull && git remote prune origin`
403
-
404
- `git fetch -p && for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do git branch -D $branch; done`
405
-
406
- default_name = get_default_branch_name @projects
407
- feature_number = ''
408
- unless quiet
409
- output "Inserisci la feature a cui si riferisce il QA: [#{default_name}]".cyan
410
- feature_number = String(STDIN.gets.chomp)
411
- end
412
- feature_number = default_name if feature_number.empty?
413
- branch_name = get_git_user + '_' + feature_number
414
-
415
- if `git branch -l | grep #{branch_name}`.size > 0
416
- `git checkout #{branch_name} && git pull`
417
- else
418
- `git checkout -b #{branch_name}`
419
- end
420
-
421
- File.open('projects.yml', 'w') { |file| file.write(@projects.to_yaml) }
422
-
423
- update_drone_yml!
424
-
425
- `git add projects.yml .drone.yml && \
426
- git commit -m '#{branch_name}' && \
427
- git push -f --set-upstream origin #{branch_name} && \
428
- git checkout master`
429
- end
430
-
431
- def qainit_deploy_update!
432
- `git checkout master && git pull`
433
- # cancelliamo tutti i branch che non sono più sul repo remoto
434
- `git fetch -p && for branch in \`git branch -vv | grep ': gone]' | awk '{print $1}'\`; do git branch -D $branch; done`
435
- # leggiamo i nomi dei branch superstiti
436
- former_branches = `git branch -a | grep remotes/ | grep -v HEAD | sed 's/ remotes\\/origin\\///g'`.split "\n"
437
- git_user = get_git_user
438
- # stampiamo la lista
439
- chosen_branch = choose do |menu|
440
- menu.prompt = 'Scegli il QA che vuoi aggiornare: '.cyan
441
- menu.shell = true
442
- former_branches.delete('master')
443
- former_branches.each_with_index do |branch, index|
444
- msg = index.odd? ? branch.white : branch.light_yellow # uno bianco e uno giallo alternati
445
- msg = branch.start_with?(git_user) ? msg.on_blue : msg.on_black # i branch creati da chi lancia l'update sono su sfondo più chiaro
446
- menu.choice(msg) { branch }
447
- end
448
- end
449
- # checkout master, checkout branch, pull branch
450
- `git checkout master && git checkout #{chosen_branch} && git pull`
451
-
452
- # aggiornare il commit (revision a cui fa riferimento)
453
-
454
- # leggo il file projects.yml / recupero i nomi dei branch / riscrivo tutto
455
- projects = YAML.load(File.read('projects.yml'))
456
-
457
- projects.each do |key, project|
458
- @projects[key] = select_branch_to_deploy(key, project['name'])
459
- @projects[key]['default_branch'] = project['default_branch']
460
- end
461
-
462
- File.open('projects.yml', 'w') { |file| file.write(@projects.to_yaml) }
463
-
464
- update_drone_yml!
465
-
466
- `git add projects.yml .drone.yml`
467
- `git commit -m 'update'`
468
- `git push && git checkout master`
469
- end
470
-
471
- def qainit_deploy_shutdown!(selection = nil)
472
- `git checkout master && git pull && git remote prune origin`
473
- # leggiamo i nomi dei branch
474
- former_branches = `git branch -a | grep remotes/ | grep -v HEAD | sed 's/ remotes\\/origin\\///g'`.split "\n"
475
- if selection.nil?
476
- # stampiamo la lista
477
- chosen_branch = choose do |menu|
478
- menu.prompt = 'Scegli il QA che vuoi spegnere: '.cyan
479
- menu.shell = true
480
- git_user = get_git_user
481
- former_branches.delete('master')
482
- former_branches.each_with_index do |branch, index|
483
- msg = index.odd? ? branch.white : branch.light_yellow # uno bianco e uno giallo alternati
484
- msg = branch.start_with?(git_user) ? msg.on_blue : msg.on_black # i branch creati da chi lancia l'update sono su sfondo blu
485
- menu.choice(msg) { branch }
486
- end
487
- end
488
- else
489
- chosen_branch = selection
490
- end
491
- # checkout master, checkout branch, pull branch, push sul branch con commit vuoto
492
- `git checkout master && git checkout #{chosen_branch} && git pull`
493
- `git commit --allow-empty -m 'shutdown' && git push && git checkout master`
494
- end
495
-
496
- def qainit_drone_shutdown!
497
- output 'Recupero le informazioni sui QA attivi...'
498
- stack_list, envs = get_stacks
499
-
500
- env_hash = 'qa-' + get_deploy_id
501
-
502
- cluster_stack_name = nil
503
- stacks_to_delete = []
504
- stack_list.each do |stack|
505
- if stack.stack_name.match(/#{env_hash}$/)
506
- if stack.stack_name.match(/ecs-cluster/)
507
- cluster_stack_name = stack.stack_name
508
- else
509
- break unless stack.stack_name.match(/#{env_hash}$/)
510
-
511
- stacks_to_delete.push(stack.stack_name)
512
- delete_stack(stack.stack_name)
513
- end
514
- end
515
- end
516
-
517
- cluster_stack_name = "ecs-cluster-#{env_hash}"
518
- if stack_exists?(cluster_stack_name)
519
- aggregator_enabled = get_stack_tags(cluster_stack_name).detect do |tag|
520
- tag.key === 'hostname_pattern_priority' and tag.value === '1'
521
- end.is_a?(Aws::CloudFormation::Types::Tag)
522
-
523
- dns_to_staging(env_hash) if aggregator_enabled
524
- end
525
-
526
- # Se non ha finito di cancellare le altre non si puo' cancellare il cluster
527
- output 'Attendo 10 secondi per poter eliminare il cluster ECS'
528
-
529
- while stacks_to_delete.length > 0
530
- sleep 13
531
- stacks_to_delete.each do |stack_name|
532
- stacks_to_delete -= [stack_name] unless stack_exists?(stack_name)
533
- end
534
- output "Stack ancora attivi: #{stacks_to_delete.length}. Attendo altri 10 secondi per eliminare il cluster ECS"
535
- end
536
-
537
- delete_stack(cluster_stack_name) if stack_exists?(cluster_stack_name)
538
- delete_stack(@base_stack_name_alb + env_hash[3..8]) if stack_exists?(@base_stack_name_alb + env_hash[3..8])
539
- delete_stack(@base_stack_name_alb_ws + env_hash[3..8]) if stack_exists?(@base_stack_name_alb_ws + env_hash[3..8])
540
- `git checkout master && git push origin --delete ${DRONE_BRANCH}`
541
- output 'Cancello il record DNS utilizzato da Lighthouse'
542
- delete_lighthouse_dns
543
- output 'Finito!'.green
544
- end
545
-
546
- def qainit_write_output(file_message, output_message)
547
- `mkdir -p /etc/qainit-output`
548
- qa_file_name = '/etc/qainit-output/url_qa'
549
- File.open(qa_file_name + '.txt', 'w') { |file| file.write(file_message) }
550
- output "#{output_message} #{qa_file_name}".green
551
- end
552
-
553
- def update_drone_yml!
554
- drone_yml = File.read('.drone.yml')
555
- @projects.each do |key, project|
556
- drone_yml = drone_yml.gsub(/#{key}@.+\n/, "#{key}@#{project['revision']}\n")
557
- end
558
- File.open('.drone.yml', 'w') do |f|
559
- f.write(drone_yml)
560
- end
561
- end
562
-
563
- def get_deploy_id
564
- if @deploy_id
565
- @deploy_id
566
- else
567
- @deploy_id = Digest::MD5.hexdigest(ENV['DRONE_BRANCH'])
568
- @deploy_id
569
- end
570
- end
571
-
572
- def get_alb_host(stack_name)
573
- if stack_name.include?('alb-http-public')
574
- logical_resource_id = 'EcsApplicationLoadBalancerPublic'
575
- elsif stack_name.include?('alb-ws-public')
576
- logical_resource_id = 'EcsApplicationLoadBalancerPublic'
577
- end
578
- resp = describe_stack_resource(stack_name, logical_resource_id)
579
- resp = describe_load_balancers([resp.stack_resource_detail.physical_resource_id])
580
- resp.load_balancers[0].dns_name
581
- end
582
-
583
- def update_cluster_stack(stack_name, tags = [], parameters = [])
584
- stack_body = get_stack_template(stack_name)
585
- update_stack(stack_name, stack_body, parameters, tags)
586
- end
587
-
588
- def choose_branch_to_deploy(project_name, select_master = false)
589
- # chiamare api octokit per recuperare i branch/commit
590
- output "Recupero la lista dei branch del progetto #{project_name}..."
591
- branches = @gh.branches("primait/#{project_name}", { per_page: 100 })
592
- master_branch = branches.select { |branch| branch[:name] == 'master' }[0]
593
-
594
- if select_master || branches.length == 1
595
- branch_name = 'master'
596
- else
597
- branches.insert(0, branches.delete(master_branch))
598
- branch_name = choose do |menu|
599
- menu.prompt = "Scegli il branch di #{project_name} da deployare: ".cyan
600
- menu.shell = true
601
-
602
- git_mail = get_git_mail
603
-
604
- branches.each_with_index do |branch, index|
605
- title = @prima.reduce_size(branch[:name], 100)
606
- msg = index.odd? ? title.white : title.light_yellow # uno bianco e uno giallo alternati
607
- # msg = branch.include?(git_mail) ? msg.on_blue : msg.on_black # i branch aggiornati da chi lancia la creazione sono su sfondo blu
608
- menu.choice(msg) { branch[:name] }
609
- menu.default = branch[:name] if branch == master_branch
610
- end
611
- end
612
- end
613
-
614
- chosen_branch = branches.select { |branch| branch[:name] == branch_name }[0]
615
- name = branch_name
616
- revision = chosen_branch[:commit][:sha]
617
- { 'name' => name, 'revision' => revision[0..14], 'default_branch' => select_master }
618
- end
619
-
620
- def select_branch_to_deploy(project_name, branch_name)
621
- output "Recupero il branch #{project_name}:#{branch_name} ..."
622
- branch = @gh.branch("primait/#{project_name}", branch_name)
623
- name = branch[:name]
624
- revision = branch[:commit][:sha]
625
- committer_email = branch[:commit][:commit][:author][:email]
626
- { 'name' => name, 'revision' => revision[0..14], 'committer' => committer_email }
627
- end
628
-
629
- def get_stacks
630
- envs = {}
631
- stack_list = stack_list()
632
- stack_list.each do |stack|
633
- unless stack.stack_name.match(/spotfleet-allinone-qa-(\w+)$/)
634
- env_hash = stack.stack_name.match(/qa-(\w+)$/)[0]
635
- envs[env_hash] = stack.tags unless envs.has_key?(env_hash) || stack.tags.empty?
636
- end
637
- end
638
- [stack_list, envs]
639
- end
640
-
641
- def get_clusters
642
- envs = {}
643
- cluster_list = cluster_list()
644
- cluster_list.each do |stack|
645
- unless stack.stack_name.match(/spotfleet-allinone-qa-(\w+)$/)
646
- env_hash = stack.stack_name.match(/qa-(\w+)$/)[0]
647
- envs[env_hash] = stack.tags unless envs.has_key?(env_hash) || stack.tags.empty?
648
- end
649
- end
650
- [cluster_list, envs]
651
- end
652
-
653
- def hostname_pattern_priority
654
- (Time.now.to_i.to_s[-4..-1].to_i + Random.rand(40_000)).to_s
655
- end
656
-
657
- def select_branches(project_names = nil)
658
- output 'Deploy feature menu'
659
- if project_names.nil?
660
- @projects.each { |key, _value| @projects[key] = choose_branch_to_deploy(key) }
661
- else
662
- project_names.each do |project|
663
- @projects[project] = choose_branch_to_deploy(project)
664
- end
665
- @projects.each_key do |branch_project|
666
- unless project_names.include? branch_project
667
- @projects[branch_project] = choose_branch_to_deploy(branch_project, true)
668
- end
669
- end
670
- end
671
- end
672
- end
673
-
674
- def help_content
675
- <<~HELP
676
-
677
- twig-feature
678
- ===========
679
-
680
- Manage feature branches
681
-
682
- Synopsis
683
- --------
684
-
685
- twig release start
686
- twig release finish
687
- twig release deploy
688
- twig release aggregator
689
-
690
- Description
691
- -----------
692
-
693
- start creates a new feature branch
694
- finish finishes the feature by merging to dev and master
695
- qainit deploys a new environment with selected branches from every project
696
- qainit $PROJECT_NAME deploys a new environment allowing to selected a branch from the input project (everything else is master)
697
- qainit shutdown deletes a specific qa environment
698
-
699
- Available only to devops (from artemide)
700
- -----------
701
- deploy deploys the feature branch to a temporary AWS Elastic Beanstalk env
702
- deploy stop destroys the AWS Elastic Beanstalk env
703
- deploy update updates a feature branch with current branches
704
- deploy lock protects a qa environment from automatic deletion
705
- aggregator enable/disable directs comparator's staging environments to a qa/staging
706
-
707
- Subcommand for Twig: <http://rondevera.github.io/twig/>
708
- Author: Andrea Usuelli <https://github.com/andreausu>
709
-
710
- HELP
711
- end
712
-
713
- args = ARGV.dup
714
-
715
- if args.include?('--help')
716
- puts help_content
717
- exit
718
- end
719
-
720
- gem_update = true
721
- gem_update = false if args.include?('no-gem-update')
722
-
723
- args.delete('no-gem-update')
724
-
725
- Release.new(gem_update).execute!(args)