bora 1.6.0 → 1.7.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.
@@ -8,14 +8,13 @@ class Bora
8
8
  NotFoundError = Class.new(StandardError)
9
9
  MultipleMatchesError = Class.new(StandardError)
10
10
 
11
- def initialize(stack)
12
- end
11
+ def initialize(stack); end
13
12
 
14
13
  def resolve(uri)
15
14
  zone_name = uri.host
16
15
  zone_type = uri.path[1..-1]
17
- raise InvalidParameterError, "Invalid hostedzone parameter #{uri}" if !zone_name
18
- zone_name += "."
16
+ raise InvalidParameterError, "Invalid hostedzone parameter #{uri}" unless zone_name
17
+ zone_name += '.'
19
18
  route53 = Aws::Route53::Client.new
20
19
  res = route53.list_hosted_zones
21
20
  zones = res.hosted_zones.select do |hz|
@@ -23,16 +22,15 @@ class Bora
23
22
  end
24
23
  raise NotFoundError, "Could not find hosted zone #{uri}" if !zones || zones.empty?
25
24
  raise MultipleMatchesError, "Multiple candidates for hosted zone #{uri}. Use public/private discrimiator." if zones.size > 1
26
- zones[0].id.split("/")[-1]
25
+ zones[0].id.split('/')[-1]
27
26
  end
28
27
 
29
28
  private
30
29
 
31
30
  def zone_type_matches(required_zone_type, is_private_zone)
32
31
  return true if !required_zone_type || required_zone_type.empty?
33
- (required_zone_type == "private" && is_private_zone) || (required_zone_type == "public" && !is_private_zone)
32
+ (required_zone_type == 'private' && is_private_zone) || (required_zone_type == 'public' && !is_private_zone)
34
33
  end
35
-
36
34
  end
37
35
  end
38
36
  end
@@ -9,18 +9,18 @@ require 'bora/parameter_resolver'
9
9
 
10
10
  class Bora
11
11
  class Stack
12
- STACK_ACTION_SUCCESS_MESSAGE = "%s stack '%s' completed successfully"
13
- STACK_ACTION_FAILURE_MESSAGE = "%s stack '%s' failed"
14
- STACK_ACTION_NOT_CHANGED_MESSAGE = "%s stack '%s' skipped as template has not changed"
15
- STACK_DOES_NOT_EXIST_MESSAGE = "Stack '%s' does not exist"
16
- STACK_EVENTS_DO_NOT_EXIST_MESSAGE = "Stack '%s' has no events"
17
- STACK_EVENTS_MESSAGE = "Events for stack '%s'"
18
- STACK_OUTPUTS_DO_NOT_EXIST_MESSAGE = "Stack '%s' has no outputs"
19
- STACK_PARAMETERS_DO_NOT_EXIST_MESSAGE = "Stack '%s' has no parameters"
20
- STACK_VALIDATE_SUCCESS_MESSAGE = "Template for stack '%s' is valid"
21
- STACK_DIFF_TEMPLATE_UNCHANGED_MESSAGE = "Template has not changed"
22
- STACK_DIFF_PARAMETERS_UNCHANGED_MESSAGE = "Parameters have not changed"
23
- STACK_DIFF_NO_CHANGES_MESSAGE = "No changes will be applied"
12
+ STACK_ACTION_SUCCESS_MESSAGE = "%s stack '%s' completed successfully".freeze
13
+ STACK_ACTION_FAILURE_MESSAGE = "%s stack '%s' failed".freeze
14
+ STACK_ACTION_NOT_CHANGED_MESSAGE = "%s stack '%s' skipped as template has not changed".freeze
15
+ STACK_DOES_NOT_EXIST_MESSAGE = "Stack '%s' does not exist".freeze
16
+ STACK_EVENTS_DO_NOT_EXIST_MESSAGE = "Stack '%s' has no events".freeze
17
+ STACK_EVENTS_MESSAGE = "Events for stack '%s'".freeze
18
+ STACK_OUTPUTS_DO_NOT_EXIST_MESSAGE = "Stack '%s' has no outputs".freeze
19
+ STACK_PARAMETERS_DO_NOT_EXIST_MESSAGE = "Stack '%s' has no parameters".freeze
20
+ STACK_VALIDATE_SUCCESS_MESSAGE = "Template for stack '%s' is valid".freeze
21
+ STACK_DIFF_TEMPLATE_UNCHANGED_MESSAGE = 'Template has not changed'.freeze
22
+ STACK_DIFF_PARAMETERS_UNCHANGED_MESSAGE = 'Parameters have not changed'.freeze
23
+ STACK_DIFF_NO_CHANGES_MESSAGE = 'No changes will be applied'.freeze
24
24
 
25
25
  def initialize(stack_name, template_file, stack_config)
26
26
  @stack_name = stack_name
@@ -43,12 +43,12 @@ class Bora
43
43
 
44
44
  def apply(override_params = {}, pretty_json = false)
45
45
  cfn_options = generate(override_params, pretty_json)
46
- action = @cfn_stack.exists? ? "update" : "create"
46
+ action = @cfn_stack.exists? ? 'update' : 'create'
47
47
  success = invoke_action(action.capitalize, action, cfn_options)
48
48
  if success
49
49
  outputs = @cfn_stack.outputs
50
- if outputs && outputs.length > 0
51
- puts "Stack outputs"
50
+ if outputs && !outputs.empty?
51
+ puts 'Stack outputs'
52
52
  outputs.each { |output| puts output }
53
53
  end
54
54
  end
@@ -56,7 +56,7 @@ class Bora
56
56
  end
57
57
 
58
58
  def delete
59
- invoke_action("Delete", "delete")
59
+ invoke_action('Delete', 'delete')
60
60
  end
61
61
 
62
62
  def diff(override_params = {}, context_lines = 3)
@@ -69,7 +69,7 @@ class Bora
69
69
  def events
70
70
  events = @cfn_stack.events
71
71
  if events
72
- if events.length > 0
72
+ if !events.empty?
73
73
  puts STACK_EVENTS_MESSAGE % @cfn_stack_name
74
74
  events.each { |e| puts e }
75
75
  else
@@ -84,7 +84,7 @@ class Bora
84
84
  def outputs
85
85
  outputs = @cfn_stack.outputs
86
86
  if outputs
87
- if outputs.length > 0
87
+ if !outputs.empty?
88
88
  puts "Outputs for stack '#{@cfn_stack_name}'"
89
89
  outputs.each { |output| puts output }
90
90
  else
@@ -99,7 +99,7 @@ class Bora
99
99
  def parameters
100
100
  parameters = @cfn_stack.parameters
101
101
  if parameters
102
- if parameters.length > 0
102
+ if !parameters.empty?
103
103
  puts "Parameters for stack '#{@cfn_stack_name}'"
104
104
  parameters.each { |parameter| puts parameter }
105
105
  else
@@ -113,7 +113,7 @@ class Bora
113
113
 
114
114
  def recreate(override_params = {})
115
115
  cfn_options = generate(override_params)
116
- invoke_action("Recreate", "recreate", cfn_options)
116
+ invoke_action('Recreate', 'recreate', cfn_options)
117
117
  end
118
118
 
119
119
  def show(override_params = {})
@@ -122,7 +122,7 @@ class Bora
122
122
  end
123
123
 
124
124
  def show_current
125
- template = get_current_template
125
+ template = current_template
126
126
  puts template ? template : (STACK_DOES_NOT_EXIST_MESSAGE % @cfn_stack_name)
127
127
  end
128
128
 
@@ -160,7 +160,7 @@ class Bora
160
160
  end
161
161
 
162
162
  def execute_change_set(change_set_name)
163
- invoke_action("Execute change set '#{change_set_name}'", "execute_change_set", change_set_name)
163
+ invoke_action("Execute change set '#{change_set_name}'", 'execute_change_set', change_set_name)
164
164
  end
165
165
 
166
166
  def resolved_params(override_params = {})
@@ -169,7 +169,6 @@ class Bora
169
169
  @resolver.resolve(params)
170
170
  end
171
171
 
172
-
173
172
  protected
174
173
 
175
174
  def diff_parameters(cfn_options)
@@ -180,26 +179,26 @@ class Bora
180
179
 
181
180
  current_params_str = params_as_string(current_params)
182
181
  new_params_str = params_as_string(new_params)
183
- if current_params_str || new_params_str
184
- puts "Parameters".colorize(mode: :bold)
185
- puts "----------"
186
- diff = Diffy::Diff.new(current_params_str, new_params_str).to_s(String.disable_colorization ? :text : :color).chomp
187
- puts diff && !diff.empty? ? diff : STACK_DIFF_PARAMETERS_UNCHANGED_MESSAGE
188
- puts
189
- end
182
+ return unless current_params_str || new_params_str
183
+ puts 'Parameters'.colorize(mode: :bold)
184
+ puts '----------'
185
+ diff = Diffy::Diff.new(current_params_str, new_params_str).to_s(String.disable_colorization ? :text : :color).chomp
186
+ unchanged = diff.nil? || diff.empty?
187
+ puts unchanged ? STACK_DIFF_PARAMETERS_UNCHANGED_MESSAGE : diff
188
+ puts
190
189
  end
191
190
 
192
191
  def params_as_string(params)
193
- params ? params.sort.map {|k, v| "#{k} - #{v}" }.join("\n") + "\n" : nil
192
+ params ? params.sort.map { |k, v| "#{k} - #{v}" }.join("\n") + "\n" : nil
194
193
  end
195
194
 
196
195
  def template_default_parameters(cfn_options)
197
196
  params = nil
198
197
  template = JSON.parse(cfn_options[:template_body])
199
- if template["Parameters"]
200
- params_with_defaults = template["Parameters"].select { |_, v| v["Default"] }
201
- if !params_with_defaults.empty?
202
- params = params_with_defaults.map { |k, v| [k, v["Default"]] }.to_h
198
+ if template['Parameters']
199
+ params_with_defaults = template['Parameters'].select { |_, v| v['Default'] }
200
+ unless params_with_defaults.empty?
201
+ params = params_with_defaults.map { |k, v| [k, v['Default']] }.to_h
203
202
  end
204
203
  end
205
204
  params
@@ -223,9 +222,9 @@ class Bora
223
222
  end
224
223
 
225
224
  def diff_template(context_lines, cfn_options)
226
- diff = Diffy::Diff.new(get_current_template, get_new_template(cfn_options),
227
- context: context_lines,
228
- include_diff_info: true)
225
+ diff = Diffy::Diff.new(current_template, get_new_template(cfn_options),
226
+ context: context_lines,
227
+ include_diff_info: true)
229
228
  diff = diff.reject { |line| line =~ /^(---|\+\+\+|\\\\)/ }
230
229
  diff = diff.map do |line|
231
230
  case line
@@ -241,53 +240,50 @@ class Bora
241
240
  end
242
241
  diff = diff.join("\n")
243
242
 
244
- puts "Template".colorize(mode: :bold)
245
- puts "--------"
246
- puts diff && !diff.empty? ? diff : STACK_DIFF_TEMPLATE_UNCHANGED_MESSAGE
243
+ puts 'Template'.colorize(mode: :bold)
244
+ puts '--------'
245
+ unchanged = diff.nil? || diff.empty?
246
+ puts unchanged ? STACK_DIFF_TEMPLATE_UNCHANGED_MESSAGE : diff
247
247
  puts
248
248
  end
249
249
 
250
250
  def diff_change_set(cfn_options)
251
251
  change_set_name = "cs-#{SecureRandom.uuid}"
252
- if @cfn_stack.exists?
253
- change_set = @cfn_stack.create_change_set(change_set_name, cfn_options)
254
- @cfn_stack.delete_change_set(change_set_name)
255
- if change_set.has_changes?
256
- puts "Changes".colorize(mode: :bold)
257
- puts "-------"
258
- puts change_set.to_s(changes_only: true)
259
- puts
260
- else
261
- puts "Changes".colorize(mode: :bold)
262
- puts "-------"
263
- puts STACK_DIFF_NO_CHANGES_MESSAGE
264
- end
252
+ return unless @cfn_stack.exists?
253
+ change_set = @cfn_stack.create_change_set(change_set_name, cfn_options)
254
+ @cfn_stack.delete_change_set(change_set_name)
255
+ puts 'Changes'.colorize(mode: :bold)
256
+ puts '-------'
257
+ if change_set.changes?
258
+ puts change_set.to_s(changes_only: true)
259
+ else
260
+ puts STACK_DIFF_NO_CHANGES_MESSAGE
265
261
  end
262
+ puts
266
263
  end
267
264
 
268
265
  def generate(override_params = {}, pretty_json = false)
269
266
  cfn_options = cfn_options_from_stack_config
270
267
  params = resolved_params(override_params)
271
- if File.extname(@template_file) == ".rb"
268
+ if File.extname(@template_file) == '.rb'
272
269
  template_body = run_cfndsl(@template_file, params, pretty_json)
273
270
  template_json = JSON.parse(template_body)
274
- if template_json["Parameters"]
275
- cfn_param_keys = template_json["Parameters"].keys
276
- cfn_params = params.select { |k, v| cfn_param_keys.include?(k) }.map do |k, v|
271
+ if template_json['Parameters']
272
+ cfn_param_keys = template_json['Parameters'].keys
273
+ cfn_params = params.select { |k, _v| cfn_param_keys.include?(k) }.map do |k, v|
277
274
  { parameter_key: k, parameter_value: v }
278
275
  end
279
- cfn_options[:parameters] = cfn_params if !cfn_params.empty?
276
+ cfn_options[:parameters] = cfn_params unless cfn_params.empty?
280
277
  end
281
278
  cfn_options[:template_body] = template_body
282
279
  else
283
280
  cfn_options[:template_body] = File.read(@template_file)
284
- if !params.empty?
281
+ unless params.empty?
285
282
  cfn_options[:parameters] = params.map do |k, v|
286
283
  { parameter_key: k, parameter_value: v }
287
284
  end
288
285
  end
289
286
  end
290
- #binding.pry
291
287
  cfn_options
292
288
  end
293
289
 
@@ -295,19 +291,17 @@ class Bora
295
291
  puts "#{action_desc} stack '#{@cfn_stack_name}' in region #{@region}"
296
292
  success = @cfn_stack.send(action, *args) { |event| puts event }
297
293
  if success
298
- puts STACK_ACTION_SUCCESS_MESSAGE % [action_desc, @cfn_stack_name]
294
+ puts format(STACK_ACTION_SUCCESS_MESSAGE, action_desc, @cfn_stack_name)
295
+ elsif success.nil?
296
+ puts format(STACK_ACTION_NOT_CHANGED_MESSAGE, action_desc, @cfn_stack_name)
299
297
  else
300
- if success == nil
301
- puts STACK_ACTION_NOT_CHANGED_MESSAGE % [action_desc, @cfn_stack_name]
302
- else
303
- raise(STACK_ACTION_FAILURE_MESSAGE % [action_desc, @cfn_stack_name])
304
- end
298
+ raise format(STACK_ACTION_FAILURE_MESSAGE, action_desc, @cfn_stack_name)
305
299
  end
306
300
  success
307
301
  end
308
302
 
309
303
  def run_cfndsl(template_file, params, pretty_json)
310
- temp_extras = Tempfile.new(["bora", ".yaml"])
304
+ temp_extras = Tempfile.new(['bora', '.yaml'])
311
305
  temp_extras.write(params.to_yaml)
312
306
  temp_extras.close
313
307
  cfndsl_model = CfnDsl.eval_file_with_extras(template_file, [[:yaml, temp_extras.path]])
@@ -334,10 +328,9 @@ class Bora
334
328
  JSON.pretty_generate(JSON.parse(template))
335
329
  end
336
330
 
337
- def get_current_template
331
+ def current_template
338
332
  template = @cfn_stack.template
339
333
  template ? JSON.pretty_generate(JSON.parse(template)) : nil
340
334
  end
341
-
342
335
  end
343
336
  end
@@ -7,7 +7,6 @@ class Bora
7
7
  define_tasks
8
8
  end
9
9
 
10
-
11
10
  protected
12
11
 
13
12
  def define_tasks
@@ -91,7 +90,7 @@ class Bora
91
90
  within_namespace do
92
91
  desc "Shows the new template for '#{@stack.stack_name}' stack"
93
92
  task :show do |_, args|
94
- @stack.show(self.extract_params_from_args(args.extras))
93
+ @stack.show(extract_params_from_args(args.extras))
95
94
  end
96
95
  end
97
96
  end
@@ -129,12 +128,8 @@ class Bora
129
128
  end
130
129
  end
131
130
 
132
-
133
- protected
134
-
135
131
  def extract_params_from_args(args)
136
- args ? Hash[args.map { |arg| arg.split("=", 2) }] : {}
132
+ args ? Hash[args.map { |arg| arg.split('=', 2) }] : {}
137
133
  end
138
-
139
134
  end
140
135
  end
@@ -5,10 +5,10 @@ require 'bora/cfn/stack'
5
5
  class Bora
6
6
  class Tasks < Rake::TaskLib
7
7
  def initialize(stack_name, template_uri = nil)
8
- puts "--------------------------------------------------------------------------------"
9
- puts "Bora::Tasks is deprecated. Please use YAML based configuration instead."
10
- puts "See http://ampedandwired.com/bora for more information."
11
- puts "--------------------------------------------------------------------------------"
8
+ puts '--------------------------------------------------------------------------------'
9
+ puts 'Bora::Tasks is deprecated. Please use YAML based configuration instead.'
10
+ puts 'See http://ampedandwired.com/bora for more information.'
11
+ puts '--------------------------------------------------------------------------------'
12
12
 
13
13
  @stack_name = stack_name
14
14
  @stack = Cfn::Stack.new(stack_name)
@@ -17,11 +17,8 @@ class Bora
17
17
  within_namespace { yield self } if block_given?
18
18
 
19
19
  if template_uri
20
- if @stack_options[:template_body] || @stack_options[:template_url]
21
- raise "You cannot specify a template in the constructor as well as in the stack_options"
22
- else
23
- @stack_options[:template_body] = File.read(template_uri)
24
- end
20
+ raise 'You cannot specify a template in the constructor as well as in the stack_options' if @stack_options[:template_body] || @stack_options[:template_url]
21
+ @stack_options[:template_body] = File.read(template_uri)
25
22
  elsif @stack_options[:template_url]
26
23
  @stack_options[:template_body] = File.read(@stack_options[:template_url])
27
24
  @stack_options.delete(:template_url)
@@ -37,7 +34,6 @@ class Bora
37
34
  String.disable_colorization = !@colorize
38
35
  end
39
36
 
40
-
41
37
  private
42
38
 
43
39
  def define_tasks
@@ -57,12 +53,12 @@ class Bora
57
53
  def define_apply_task
58
54
  within_namespace do
59
55
  desc "Creates (or updates) the '#{@stack_name}' stack"
60
- task :apply => :generate do
61
- success = invoke_action(@stack.exists? ? "update" : "create", @stack_options)
56
+ task apply: :generate do
57
+ success = invoke_action(@stack.exists? ? 'update' : 'create', @stack_options)
62
58
  if success
63
59
  outputs = @stack.outputs
64
- if outputs && outputs.length > 0
65
- puts "Stack outputs"
60
+ if outputs && !outputs.empty?
61
+ puts 'Stack outputs'
66
62
  outputs.each { |output| puts output }
67
63
  end
68
64
  end
@@ -84,7 +80,7 @@ class Bora
84
80
  within_namespace do
85
81
  desc "Deletes the '#{@stack_name}' stack"
86
82
  task :delete do
87
- invoke_action("delete")
83
+ invoke_action('delete')
88
84
  end
89
85
  end
90
86
  end
@@ -92,7 +88,7 @@ class Bora
92
88
  def define_diff_task
93
89
  within_namespace do
94
90
  desc "Diffs the new template with the '#{@stack_name}' stack's current template"
95
- task :diff => :generate do
91
+ task diff: :generate do
96
92
  puts @stack.diff(@stack_options).to_s(@colorize ? :color : :text)
97
93
  end
98
94
  end
@@ -104,7 +100,7 @@ class Bora
104
100
  task :events do
105
101
  events = @stack.events
106
102
  if events
107
- if events.length > 0
103
+ if !events.empty?
108
104
  puts "Events for stack '#{@stack_name}'"
109
105
  @stack.events.each { |e| puts e }
110
106
  else
@@ -126,7 +122,7 @@ class Bora
126
122
  def define_new_template_task
127
123
  within_namespace do
128
124
  desc "Shows the new template for '#{@stack_name}' stack"
129
- task :new_template => :generate do
125
+ task new_template: :generate do
130
126
  puts @stack.new_template(@stack_options)
131
127
  end
132
128
  end
@@ -138,7 +134,7 @@ class Bora
138
134
  task :outputs do
139
135
  outputs = @stack.outputs
140
136
  if outputs
141
- if outputs.length > 0
137
+ if !outputs.empty?
142
138
  puts "Outputs for stack '#{@stack_name}'"
143
139
  outputs.each { |output| puts output }
144
140
  else
@@ -154,8 +150,8 @@ class Bora
154
150
  def define_recreate_task
155
151
  within_namespace do
156
152
  desc "Recreates (deletes then creates) the '#{@stack_name}' stack"
157
- task :recreate => :generate do
158
- invoke_action("recreate", @stack_options)
153
+ task recreate: :generate do
154
+ invoke_action('recreate', @stack_options)
159
155
  end
160
156
  end
161
157
  end
@@ -172,7 +168,7 @@ class Bora
172
168
  def define_validate_task
173
169
  within_namespace do
174
170
  desc "Checks the '#{@stack_name}' stack's template for validity"
175
- task :validate => :generate do
171
+ task validate: :generate do
176
172
  puts "Template for stack '#{@stack_name}' is valid" if @stack.validate(@stack_options)
177
173
  end
178
174
  end
@@ -184,11 +180,8 @@ class Bora
184
180
  if success
185
181
  puts "#{action.capitalize} stack '#{@stack_name}' completed successfully"
186
182
  else
187
- if success == nil
188
- puts "#{action.capitalize} stack '#{@stack_name}' skipped as template has not changed"
189
- else
190
- fail("#{action.capitalize} stack '#{@stack_name}' failed")
191
- end
183
+ raise "#{action.capitalize} stack '#{@stack_name}' failed" unless success.nil?
184
+ puts "#{action.capitalize} stack '#{@stack_name}' skipped as template has not changed"
192
185
  end
193
186
  success
194
187
  end
@@ -200,6 +193,5 @@ class Bora
200
193
  end
201
194
  end
202
195
  end
203
-
204
196
  end
205
197
  end