Class: Awful::CloudFormation
  
  
  
  
  
    - Inherits:
 
    - 
      Cli
      
        
        show all
      
    
 
  
  
  
  
  
  
  
  
  
  
    - Defined in:
 
    - lib/awful/changesets.rb,
  lib/awful/cloudformation.rb
 
  
  
 
Overview
  
    
add as a subcommand of `cf`
   
 
  
  
    
      Constant Summary
      collapse
    
    
      
        - COLORS =
          
        
 
        {
  CREATE_IN_PROGRESS:                  :yellow,
  DELETE_IN_PROGRESS:                  :yellow,
  UPDATE_IN_PROGRESS:                  :yellow,
  UPDATE_COMPLETE_CLEANUP_IN_PROGRESS: :yellow,
  CREATE_FAILED:                       :red,
  DELETE_FAILED:                       :red,
  UPDATE_FAILED:                       :red,
  CREATE_COMPLETE:                     :green,
  DELETE_COMPLETE:                     :green,
  UPDATE_COMPLETE:                     :green,
  DELETE_SKIPPED:                      :yellow,
  ROLLBACK_IN_PROGRESS:                :red,
  ROLLBACK_COMPLETE:                   :red,
  ROLLBACK_FAILED:                     :red,
  ACTIVE:                              :green,
  DELETED:                             :red,
} 
      
        - STATUSES =
          
  
    
stack statuses that are not DELETE_COMPLETE
   
 
  
         
        %i[
  CREATE_IN_PROGRESS CREATE_FAILED CREATE_COMPLETE
  ROLLBACK_IN_PROGRESS ROLLBACK_FAILED ROLLBACK_COMPLETE
  DELETE_IN_PROGRESS DELETE_FAILED
  UPDATE_IN_PROGRESS UPDATE_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_COMPLETE
  UPDATE_ROLLBACK_IN_PROGRESS UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_ROLLBACK_COMPLETE
  REVIEW_IN_PROGRESS
]
 
      
    
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods inherited from Cli
  #initialize, #ll, #version
  
  Constructor Details
  
    This class inherits a constructor from Awful::Cli
  
 
  
    Instance Method Details
    
      
  
  
    #cost(name)  ⇒ Object 
  
  
  
  
    
this is almost entirely useless in practice
   
 
  
  
    
      
265 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 265
desc 'cost', 'describe cost for given stack' 
     | 
  
 
    
      
  
  
    #create(name, file = nil)  ⇒ Object 
  
  
  
  
    
      
139
140
141
142
143 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 139
def create(name, file = nil)
  cf.create_stack(stack_name: name, template_body: file_or_stdin(file)).output do |response|
    puts response.stack_id
  end
end
     | 
  
 
    
      
  
  
    #delete(name)  ⇒ Object 
  
  
  
  
    
      
174
175
176
177
178 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 174
def delete(name)
  if yes? "Really delete stack #{name}?", :yellow
    cf.delete_stack(stack_name: name)
  end
end
     | 
  
 
    
      
  
  
    #dump(name)  ⇒ Object 
  
  
  
  
    
      
83
84
85
86
87
88
89 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 83
def dump(name)
  cf.describe_stacks(stack_name: name).stacks.output do |stacks|
    stacks.each do |stack|
      puts YAML.dump(stringify_keys(stack.to_hash))
    end
  end
end
     | 
  
 
    
      
  
  
    #events(name)  ⇒ Object 
  
  
  
  
    
      
182
183
184
185
186
187
188
189
190 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 182
def events(name)
  events = cf.describe_stack_events(stack_name: name).stack_events
  events = events.first(options[:number]) if options[:number]
  events.reverse.output do |events|
    print_table events.map { |e|
      [e.timestamp, color(e.resource_status), e.resource_type, e.logical_resource_id, e.resource_status_reason]
    }
  end
end
     | 
  
 
    
      
  
  
    #exists(name)  ⇒ Object 
  
  
  
  
    
      
73
74
75
76
77
78
79
80 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 73
def exists(name)
  begin
    cf.describe_stacks(stack_name: name)
    true
  rescue Aws::CloudFormation::Errors::ValidationError
    false
  end.output(&method(:puts))
end
     | 
  
 
    
      
  
  
    #id(name, resource)  ⇒ Object 
  
  
  
  
    
      
233
234
235
236
237
238
239
240
241
242
243
244 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 233
def id(name, resource)
  detail = cf.describe_stack_resource(stack_name: name, logical_resource_id: resource).stack_resource_detail
  if options[:all]
    detail.output do |d|
      puts YAML.dump(stringify_keys(d.to_hash))
    end
  else
    detail.physical_resource_id.output do |id|
      puts id
    end
  end
end
     | 
  
 
    
      
  
  
    
      
258
259
260
261
262 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 258
def limits
  cf.describe_account_limits.account_limits.output do |limits|
    print_table limits.map { |l| [l.name, l.value] }
  end
end
     | 
  
 
    
      
  
  
    #ls(name = nil)  ⇒ Object 
  
  
  
  
    
      
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 56
def ls(name = nil)
  paginate(:stack_summaries) do |next_token|
    cf.list_stacks(stack_status_filter: STATUSES, next_token: next_token)
  end.tap do |stacks|
    stacks.select! { |s| s.stack_name.match(name) } if name
  end.output do |list|
    if options[:long]
      print_table list.map { |s|
        [s.stack_name, s.creation_time, color(s.stack_status), s.template_description]
      }.sort
    else
      puts list.map(&:stack_name).sort
    end
  end
end
     | 
  
 
    
      
  
  
    #outputs(name, key = nil)  ⇒ Object 
  
  
  
  
    
      
101
102
103
104
105
106
107
108
109
110
111
112
113 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 101
def outputs(name, key = nil)
  output_hash = cf.describe_stacks(stack_name: name).stacks.first.outputs.each_with_object({}) do |o, hash|
    hash[o.output_key] = o.output_value
  end
  if key
    output_hash[key.to_s].output(&method(:puts))
  else
    output_hash.output do |hash|
      print_table hash.sort
    end
  end
end
     | 
  
 
    
      
  
  
    #parameters(name)  ⇒ Object 
  
  
  
  
    
      
92
93
94
95
96
97
98 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 92
def parameters(name)
  cf.describe_stacks(stack_name: name).stacks.first.parameters.each_with_object({}) do |p, h|
    h[p.parameter_key] = p.parameter_value
  end.output do |hash|
    print_table hash.sort
  end
end
     | 
  
 
    
      
  
  
    #policy(name, file = nil)  ⇒ Object 
  
  
  
  
    
      
248
249
250
251
252
253
254
255 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 248
def policy(name, file = nil)
  policy = options[:json].nil? ? file_or_stdin(file) : options[:json]
  if policy
    cf.set_stack_policy(stack_name: name, stack_policy_body: policy)
  else
    cf.get_stack_policy(stack_name: name).stack_policy_body.output(&method(:puts))
  end
end
     | 
  
 
    
      
  
  
    #resources(name)  ⇒ Object 
  
  
  
  
    
      
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 197
def resources(name)
  resources = cf.list_stack_resources(stack_name: name).stack_resource_summaries
  if options[:type]
    resources.select! do |resource|
      options[:type].include?(resource.resource_type)
    end
  end
  if options[:match]
    resources.select! do |resource|
      Regexp.new(options[:match], Regexp::IGNORECASE).match(resource.resource_type)
    end
  end
  resources.output do |resources|
    if options[:long]
      print_table(
        resources.map { |r|
          [
            r.logical_resource_id,
            r.resource_type,
            color(r.resource_status),
            r.physical_resource_id,
          ]
        },
        truncate: options[:truncate]
      )
    else
      puts resources.map(&:logical_resource_id)
    end
  end
end
     | 
  
 
    
      
  
  
    
      
281
282
283
284
285
286
287
288
289
290
291
292
293 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 281
def sets
  paginate(:summaries) do |next_token|
    cf.list_stack_sets(status: options[:status].upcase, next_token: next_token)
  end.output do |sets|
    if options[:long]
      print_table sets.map { |s|
        [s.stack_set_name, s.stack_set_id, color(s.status), s.description]
      }
    else
      puts sets.map(&:stack_set_name)
    end
  end
end
     | 
  
 
    
      
  
  
    #status(name)  ⇒ Object 
  
  
  
  
    
      
116
117
118 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 116
def status(name)
  cf.describe_stacks(stack_name: name).stacks.first.stack_status.output(&method(:puts))
end 
     | 
  
 
    
      
  
  
    #tag(name, *tags)  ⇒ Object 
  
  
  
  
    
      
158
159
160
161
162
163
164
165
166
167
168
169
170
171 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 158
def tag(name, *tags)
  params = {
    stack_name:            name,
    use_previous_template: true,
    capabilities:          ['CAPABILITY_IAM'],
    tags: tags.map do |t|
      key, value = t.split(/[:=]/)
      {key: key, value: value}
    end
  }
  cf.update_stack(params).output do |response|
    puts response.stack_id
  end
end
     | 
  
 
    
      
  
  
    #template(name)  ⇒ Object 
  
  
  
  
    
      
121
122
123
124
125 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 121
def template(name)
  cf.get_template(stack_name: name).template_body.output do |template|
    puts template
  end
end
     | 
  
 
    
      
  
  
    #update(name, file = nil)  ⇒ Object 
  
  
  
  
    
      
146
147
148
149
150
151
152
153
154 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 146
def update(name, file = nil)
  begin
    cf.update_stack(stack_name: name, template_body: file_or_stdin(file)).output do |response|
      puts response.stack_id
    end
  rescue Aws::CloudFormation::Errors::ValidationError => e
    e.output { |err| puts err.message }
  end
end
     | 
  
 
    
      
  
  
    #validate(file = nil)  ⇒ Object 
  
  
  
  
    
      
128
129
130
131
132
133
134
135
136 
     | 
    
      # File 'lib/awful/cloudformation.rb', line 128
def validate(file = nil)
  begin
    cf.validate_template(template_body: file_or_stdin(file)).output do |response|
      puts YAML.dump(stringify_keys(response.to_hash))
    end
  rescue Aws::CloudFormation::Errors::ValidationError => e
    e.output { |err| puts err.message }
  end
end
     |