Class: Awful::CloudFormation

Inherits:
Cli
  • Object
show all
Defined in:
lib/awful/cloudformation.rb

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

#create(name, file = nil) ⇒ Object



104
105
106
107
108
# File 'lib/awful/cloudformation.rb', line 104

def create(name, file = nil)
  cf.create_stack(stack_name: name, template_body: file_or_stdin(file)).tap do |response|
    puts response.stack_id
  end
end

#delete(name) ⇒ Object



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

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

#dump(name) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/awful/cloudformation.rb', line 64

def dump(name)
  cf.describe_stacks(stack_name: name).stacks.tap do |stacks|
    stacks.each do |stack|
      puts YAML.dump(stringify_keys(stack.to_hash))
    end
  end
end

#events(name) ⇒ Object



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

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



54
55
56
57
58
59
60
61
# File 'lib/awful/cloudformation.rb', line 54

def exists(name)
  begin
    cf.describe_stacks(stack_name: name)
    true
  rescue Aws::CloudFormation::Errors::ValidationError
    false
  end.tap(&method(:puts))
end

#id(name, resource) ⇒ Object



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

def id(name, resource)
  detail = cf.describe_stack_resource(stack_name: name, logical_resource_id: resource).stack_resource_detail
  if options[:all]
    detail.tap do |d|
      puts YAML.dump(stringify_keys(d.to_hash))
    end
  else
    detail.physical_resource_id.tap do |id|
      puts id
    end
  end
end

#limitsObject



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

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

#ls(name = /./) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/awful/cloudformation.rb', line 34

def ls(name = /./)
  stacks = cf.list_stacks.stack_summaries.select do |stack|
    stack.stack_name.match(name)
  end

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

  stacks.tap do |stacks|
    if options[:long]
      print_table stacks.map { |s| [s.stack_name, s.creation_time, color(s.stack_status), s.template_description] }
    else
      puts stacks.map(&:stack_name)
    end
  end
end

#outputs(name) ⇒ Object



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

def outputs(name)
  cf.describe_stacks(stack_name: name).stacks.map do |stack|
    stack.outputs.each_with_object({}) do |output, hash|
      hash[output.output_key] = output.output_value
    end
  end.tap do |stacks|
    stacks.each do |output|
      print_table output
    end
  end
end

#policy(name, file = nil) ⇒ Object



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

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.tap(&method(:puts))
  end
end

#resources(name) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/awful/cloudformation.rb', line 139

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.tap 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

#template(name) ⇒ Object



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

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

#update(name, file = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/awful/cloudformation.rb', line 111

def update(name, file = nil)
  begin
    cf.update_stack(stack_name: name, template_body: file_or_stdin(file)).tap do |response|
      puts response.stack_id
    end
  rescue Aws::CloudFormation::Errors::ValidationError => e
    e.tap { |err| puts err.message }
  end
end

#validate(file = nil) ⇒ Object



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

def validate(file = nil)
  begin
    cf.validate_template(template_body: file_or_stdin(file)).tap do |response|
      puts YAML.dump(stringify_keys(response.to_hash))
    end
  rescue Aws::CloudFormation::Errors::ValidationError => e
    e.tap { |err| puts err.message }
  end
end