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,
}
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

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#cost(name) ⇒ Object

this is almost entirely useless in practice



264
# File 'lib/awful/cloudformation.rb', line 264

desc 'cost', 'describe cost for given stack'

#create(name, file = nil) ⇒ Object



138
139
140
141
142
# File 'lib/awful/cloudformation.rb', line 138

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



173
174
175
176
177
# File 'lib/awful/cloudformation.rb', line 173

def delete(name)
  if yes? "Really delete stack #{name}?", :yellow
    cf.delete_stack(stack_name: name)
  end
end

#dump(name) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/awful/cloudformation.rb', line 82

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



181
182
183
184
185
186
187
188
189
# File 'lib/awful/cloudformation.rb', line 181

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



72
73
74
75
76
77
78
79
# File 'lib/awful/cloudformation.rb', line 72

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



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/awful/cloudformation.rb', line 232

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

#limitsObject



257
258
259
260
261
# File 'lib/awful/cloudformation.rb', line 257

def limits
  cf...output do |limits|
    print_table limits.map { |l| [l.name, l.value] }
  end
end

#ls(name = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/awful/cloudformation.rb', line 55

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



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/awful/cloudformation.rb', line 100

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



91
92
93
94
95
96
97
# File 'lib/awful/cloudformation.rb', line 91

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



247
248
249
250
251
252
253
254
# File 'lib/awful/cloudformation.rb', line 247

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



196
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
# File 'lib/awful/cloudformation.rb', line 196

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

#status(name) ⇒ Object



115
116
117
# File 'lib/awful/cloudformation.rb', line 115

def status(name)
  cf.describe_stacks(stack_name: name).stacks.first.stack_status.output(&method(:puts))
end

#tag(name, *tags) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/awful/cloudformation.rb', line 157

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



120
121
122
123
124
# File 'lib/awful/cloudformation.rb', line 120

def template(name)
  cf.get_template(stack_name: name).template_body.output do |template|
    puts template
  end
end

#update(name, file = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/awful/cloudformation.rb', line 145

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



127
128
129
130
131
132
133
134
135
# File 'lib/awful/cloudformation.rb', line 127

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