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



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

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



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



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

def events(name)
  cf.describe_stack_events(stack_name: name).stack_events.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



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/awful/cloudformation.rb', line 217

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



242
243
244
245
246
# File 'lib/awful/cloudformation.rb', line 242

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



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

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/awful/cloudformation.rb', line 191

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.physical_resource_id, r.resource_type, color(r.resource_status), r.resource_status_reason] }
    else
      puts resources.map(&:logical_resource_id)
    end
  end
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