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