Class: Rake::CloudFormation::Stack

Inherits:
Task
  • Object
show all
Includes:
Rake::CloudFormation, DSL
Defined in:
lib/rake/cloud_formation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rake::CloudFormation

#cf

Constructor Details

#initialize(*args) ⇒ Stack

Returns a new instance of Stack.



14
15
16
17
18
19
20
# File 'lib/rake/cloud_formation.rb', line 14

def initialize(*args)
  super
  @region = 'us-east-1'
  @parameters = {}
  @capabilities = []
  @notification_arns = []
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



32
33
34
# File 'lib/rake/cloud_formation.rb', line 32

def capabilities
  @capabilities
end

#notification_arnsObject (readonly)

Returns the value of attribute notification_arns.



32
33
34
# File 'lib/rake/cloud_formation.rb', line 32

def notification_arns
  @notification_arns
end

#parametersObject (readonly)

Returns the value of attribute parameters.



32
33
34
# File 'lib/rake/cloud_formation.rb', line 32

def parameters
  @parameters
end

#regionObject

Returns the value of attribute region.



31
32
33
# File 'lib/rake/cloud_formation.rb', line 31

def region
  @region
end

#templateObject

Returns the value of attribute template.



31
32
33
# File 'lib/rake/cloud_formation.rb', line 31

def template
  @template
end

Class Method Details

.define_task(args, &block) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rake/cloud_formation.rb', line 22

def self.define_task(args, &block)
  t = super
  if (t.is_a?(Stack))
    yield t if block_given?
    t.prepare
  end
  t
end

Instance Method Details

#execute(*args) ⇒ 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
72
73
74
75
76
77
78
# File 'lib/rake/cloud_formation.rb', line 46

def execute(*args)
  super
  options = {
    stack_name: name,
    template_body: IO.read(template),
  }
  unless parameters.empty?
    options[:parameters] = parameters.inject([]) do |params, (key, value)|
      param = {
        :parameter_key => key
      }
      if value.is_a?(String)
        param[:parameter_value] = value
      elsif value.is_a?(Proc)
        param[:parameter_value] = value.call
      else
        raise "Parameter value of unknown type: key=#{key.inspect} value=#{value.inspect}"
      end
      params << param
      params
    end
  end
  options[:capabilities] = capabilities.to_a unless capabilities.empty?
  options[:notification_arns] = notification_arns unless notification_arns.empty?
  puts "Creating CloudFormation stack: #{name}"
  cf(region).create_stack(options)
  while cf(region).describe_stacks({stack_name: name}).stacks.first.stack_status === 'CREATE_IN_PROGRESS'
    sleep 20
  end
  unless cf(region).describe_stacks({stack_name: name}).stacks.first.stack_status === 'CREATE_COMPLETE'
    raise "Stack creation failed"
  end
end

#needed?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/rake/cloud_formation.rb', line 38

def needed?
  begin
    cf(region).describe_stacks({stack_name: name}).stacks.first.stack_status != 'CREATE_COMPLETE'
  rescue
    true
  end
end

#prepareObject



34
35
36
# File 'lib/rake/cloud_formation.rb', line 34

def prepare
  prerequisites << file(@template)
end