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.



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

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

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



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

def capabilities
  @capabilities
end

#notification_arnsObject (readonly)

Returns the value of attribute notification_arns.



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

def notification_arns
  @notification_arns
end

#parametersObject (readonly)

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#regionObject

Returns the value of attribute region.



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

def region
  @region
end

#templateObject

Returns the value of attribute template.



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

def template
  @template
end

Class Method Details

.define_task(args, &block) ⇒ Object



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

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



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
79
# File 'lib/rake/cloud_formation.rb', line 47

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.to_a 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)


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

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

#prepareObject



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

def prepare
  prerequisites << file(@template)
end