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



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

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

#create(name, file = nil) ⇒ Object



140
141
142
143
144
# File 'lib/awful/cloudformation.rb', line 140

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



175
176
177
178
179
# File 'lib/awful/cloudformation.rb', line 175

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

#dump(name) ⇒ Object



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

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



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

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



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

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



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

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



259
260
261
262
263
# File 'lib/awful/cloudformation.rb', line 259

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

#ls(name = /./) ⇒ Object



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

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



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

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



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

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



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

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



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

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



117
118
119
# File 'lib/awful/cloudformation.rb', line 117

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

#tag(name, *tags) ⇒ Object



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

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



122
123
124
125
126
# File 'lib/awful/cloudformation.rb', line 122

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

#update(name, file = nil) ⇒ Object



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

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



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

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