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

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



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

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

#create(name, file = nil) ⇒ Object



144
145
146
147
148
# File 'lib/awful/cloudformation.rb', line 144

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



179
180
181
182
183
# File 'lib/awful/cloudformation.rb', line 179

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

#dump(name) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/awful/cloudformation.rb', line 88

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



187
188
189
190
191
192
193
194
195
# File 'lib/awful/cloudformation.rb', line 187

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



78
79
80
81
82
83
84
85
# File 'lib/awful/cloudformation.rb', line 78

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



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/awful/cloudformation.rb', line 238

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



263
264
265
266
267
# File 'lib/awful/cloudformation.rb', line 263

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

#ls(name = /./) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/awful/cloudformation.rb', line 50

def ls(name = /./)
  stacks = stack_summaries

  ## skip deleted stacks unless -a given
  unless options[:all]
    stacks = stacks.select { |stack| stack.stack_status != 'DELETE_COMPLETE' }
  end

  ## match on given arg
  stacks.select do |stack|
    stack.stack_name.match(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



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/awful/cloudformation.rb', line 106

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



97
98
99
100
101
102
103
# File 'lib/awful/cloudformation.rb', line 97

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



253
254
255
256
257
258
259
260
# File 'lib/awful/cloudformation.rb', line 253

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



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
230
231
232
233
234
# File 'lib/awful/cloudformation.rb', line 202

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



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

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

#tag(name, *tags) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/awful/cloudformation.rb', line 163

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



126
127
128
129
130
# File 'lib/awful/cloudformation.rb', line 126

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

#update(name, file = nil) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/awful/cloudformation.rb', line 151

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



133
134
135
136
137
138
139
140
141
# File 'lib/awful/cloudformation.rb', line 133

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