Module: Stacker
Instance Attribute Summary collapse
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#region ⇒ Object
Returns the value of attribute region.
Instance Method Summary collapse
- #all_stack_names ⇒ Object
- #all_stacks ⇒ Object
-
#cloud_formation ⇒ Object
lazy CloudFormation client.
- #create_or_update_stack(stack_name, template, parameters, parent_stack_name = nil, tags = nil) ⇒ Object
- #create_stack(stack_name, template, parameters, parent_stack_name = nil, tags = nil) ⇒ Object
- #delete_stack(stack_name) ⇒ Object
- #estimate_template_cost(template, parameters, parent_stack_name = nil) ⇒ Object
- #find_stack(stack_name) ⇒ Object
- #get_stack_events(stack_name_or_id) ⇒ Object
- #get_stack_output(stack_name) ⇒ Object
- #get_stack_outputs(stack_name) ⇒ Object
- #get_stack_resources(stack_name) ⇒ Object
- #get_template(stack_name) ⇒ Object
- #template_body(template) ⇒ Object
- #transform_input(input) ⇒ Object
- #transform_output(output) ⇒ Object
- #update_stack(stack_name, template, parameters, parent_stack_name = nil, tags = nil) ⇒ Object
- #validate_template(template) ⇒ Object
- #wait_for_stack(stack_name, operation, seen_events = Set.new) ⇒ Object
Instance Attribute Details
#credentials ⇒ Object
Returns the value of attribute credentials.
12 13 14 |
# File 'lib/autostacker24/stacker.rb', line 12 def credentials @credentials end |
#region ⇒ Object
Returns the value of attribute region.
12 13 14 |
# File 'lib/autostacker24/stacker.rb', line 12 def region @region end |
Instance Method Details
#all_stack_names ⇒ Object
132 133 134 |
# File 'lib/autostacker24/stacker.rb', line 132 def all_stack_names all_stacks.map{|s| s.stack_name} end |
#all_stacks ⇒ Object
136 137 138 |
# File 'lib/autostacker24/stacker.rb', line 136 def all_stacks cloud_formation.describe_stacks.stacks end |
#cloud_formation ⇒ Object
lazy CloudFormation client
173 174 175 176 177 178 179 180 181 |
# File 'lib/autostacker24/stacker.rb', line 173 def cloud_formation # lazy CloudFormation client unless @lazy_cloud_formation params = {} params[:credentials] = @credentials if @credentials params[:region] = @region if @region @lazy_cloud_formation = Aws::CloudFormation::Client.new(params) end @lazy_cloud_formation end |
#create_or_update_stack(stack_name, template, parameters, parent_stack_name = nil, tags = nil) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/autostacker24/stacker.rb', line 29 def create_or_update_stack(stack_name, template, parameters, parent_stack_name = nil, = nil) if find_stack(stack_name).nil? create_stack(stack_name, template, parameters, parent_stack_name, ) else update_stack(stack_name, template, parameters, parent_stack_name, ) end end |
#create_stack(stack_name, template, parameters, parent_stack_name = nil, tags = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/autostacker24/stacker.rb', line 37 def create_stack(stack_name, template, parameters, parent_stack_name = nil, = nil) merge_and_validate(template, parameters, parent_stack_name) cloud_formation.create_stack(stack_name: stack_name, template_body: template_body(template), on_failure: 'DELETE', parameters: transform_input(parameters), capabilities: ['CAPABILITY_IAM'], tags: ) wait_for_stack(stack_name, :create) end |
#delete_stack(stack_name) ⇒ Object
88 89 90 91 92 |
# File 'lib/autostacker24/stacker.rb', line 88 def delete_stack(stack_name) seen_events = get_stack_events(stack_name).map {|e| e[:event_id]} cloud_formation.delete_stack(stack_name: stack_name) wait_for_stack(stack_name, :delete, seen_events) end |
#estimate_template_cost(template, parameters, parent_stack_name = nil) ⇒ Object
140 141 142 143 |
# File 'lib/autostacker24/stacker.rb', line 140 def estimate_template_cost(template, parameters, parent_stack_name = nil) merge_and_validate(template, parameters, parent_stack_name) cloud_formation.estimate_template_cost(:template_body => template_body(template), :parameters => transform_input(parameters)) end |
#find_stack(stack_name) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/autostacker24/stacker.rb', line 125 def find_stack(stack_name) cloud_formation.describe_stacks(stack_name: stack_name).stacks.first rescue Aws::CloudFormation::Errors::ValidationError => error raise error unless error. =~ /does not exist/i # may be flaky, do more research in API nil end |
#get_stack_events(stack_name_or_id) ⇒ Object
169 170 171 |
# File 'lib/autostacker24/stacker.rb', line 169 def get_stack_events(stack_name_or_id) cloud_formation.describe_stack_events(stack_name: stack_name_or_id).data.stack_events end |
#get_stack_output(stack_name) ⇒ Object
149 150 151 152 153 |
# File 'lib/autostacker24/stacker.rb', line 149 def get_stack_output(stack_name) stack = find_stack(stack_name) raise "stack #{stack_name} not found" unless stack transform_output(stack.outputs).freeze end |
#get_stack_outputs(stack_name) ⇒ Object
145 146 147 |
# File 'lib/autostacker24/stacker.rb', line 145 def get_stack_outputs(stack_name) get_stack_output(stack_name) end |
#get_stack_resources(stack_name) ⇒ Object
164 165 166 167 |
# File 'lib/autostacker24/stacker.rb', line 164 def get_stack_resources(stack_name) resources = cloud_formation.describe_stack_resources(stack_name: stack_name).data.stack_resources resources.inject({}){|map, resource| map.merge(resource.logical_resource_id.to_sym => resource)}.freeze end |
#get_template(stack_name) ⇒ Object
121 122 123 |
# File 'lib/autostacker24/stacker.rb', line 121 def get_template(stack_name) cloud_formation.get_template(stack_name: stack_name).template_body end |
#template_body(template) ⇒ Object
183 184 185 186 |
# File 'lib/autostacker24/stacker.rb', line 183 def template_body(template) template = File.read(template) if File.exists?(template) AutoStacker24::Preprocessor.preprocess(template) end |
#transform_input(input) ⇒ Object
159 160 161 162 |
# File 'lib/autostacker24/stacker.rb', line 159 def transform_input(input) input.each{|k,v| raise "#{k} must not be nil" if v.nil? } input.inject([]) { |m, kv| m << {parameter_key: kv[0].to_s, parameter_value: kv[1].to_s} } end |
#transform_output(output) ⇒ Object
155 156 157 |
# File 'lib/autostacker24/stacker.rb', line 155 def transform_output(output) output.inject({}) { |m, o| m.merge(o.output_key.to_sym => o.output_value) } end |
#update_stack(stack_name, template, parameters, parent_stack_name = nil, tags = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/autostacker24/stacker.rb', line 48 def update_stack(stack_name, template, parameters, parent_stack_name = nil, = nil) seen_events = get_stack_events(stack_name).map {|e| e[:event_id]} begin merge_and_validate(template, parameters, parent_stack_name) cloud_formation.update_stack(stack_name: stack_name, template_body: template_body(template), parameters: transform_input(parameters), capabilities: ['CAPABILITY_IAM'], tags: ) rescue Aws::CloudFormation::Errors::ValidationError => error raise error unless error. =~ /No updates are to be performed/i # may be flaky, do more research in API puts "stack #{stack_name} is already up to date" find_stack(stack_name) else wait_for_stack(stack_name, :update, seen_events) end end |
#validate_template(template) ⇒ Object
84 85 86 |
# File 'lib/autostacker24/stacker.rb', line 84 def validate_template(template) cloud_formation.validate_template(template_body: template_body(template)) end |
#wait_for_stack(stack_name, operation, seen_events = Set.new) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/autostacker24/stacker.rb', line 94 def wait_for_stack(stack_name, operation, seen_events = Set.new) timeout_in_minutes = 60 # for now stop_time = Time.now + timeout_in_minutes * 60 finished = /(CREATE_COMPLETE|UPDATE_COMPLETE|DELETE_COMPLETE|ROLLBACK_COMPLETE|ROLLBACK_FAILED|CREATE_FAILED|DELETE_FAILED)$/ puts "waiting for #{operation} stack #{stack_name}" stack_id = find_stack(stack_name)[:stack_id] while Time.now < stop_time sleep(5) stack = find_stack(stack_name) status = stack ? stack.stack_status : 'DELETE_COMPLETE' expected_status = case operation when :create then /CREATE_COMPLETE$/ when :update then /UPDATE_COMPLETE$/ when :delete then /DELETE_COMPLETE$/ end new_events = get_stack_events(stack_id).select{|e| !seen_events.include?(e[:event_id])}.sort_by{|e| e[:timestamp]} new_events.each do |e| seen_events << e[:event_id] puts "#{e[:timestamp]}\t#{e[:resource_status].ljust(20)}\t#{e[:resource_type].ljust(40)}\t#{e[:logical_resource_id].ljust(30)}\t#{e[:resource_status_reason]}" end return true if status =~ expected_status raise "#{operation} #{stack_name} failed, current status #{status}" if status =~ finished end raise "waiting for #{operation} stack #{stack_name} timed out after #{timeout_in_minutes} minutes" end |