Module: Enscalator::Helpers::Stack

Included in:
Enscalator::Helpers
Defined in:
lib/enscalator/helpers/stack.rb

Overview

Helpers for operations requiring stack instance or stack_name

Instance Method Summary collapse

Instance Method Details

#cfn_create_stack(region, dependent_stack_name, template, stack_name, keys: [], extra_parameters: []) ⇒ Aws::CloudFormation::Resource

Deprecated.

this method is no longer used

Create stack using cloudformation interface

Parameters:

  • region (String)

    AWS region identifier

  • dependent_stack_name (String)

    name of the stack current stack depends on

  • template (String)

    name

  • stack_name (String)

    stack name

  • keys (Array) (defaults to: [])

    keys

  • extra_parameters (Array) (defaults to: [])

    additional parameters

Returns:

  • (Aws::CloudFormation::Resource)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/enscalator/helpers/stack.rb', line 36

def cfn_create_stack(region, dependent_stack_name, template, stack_name, keys: [], extra_parameters: [])
  cfn = cfn_resource(cfn_client(region))
  stack = wait_stack(cfn, dependent_stack_name)
  extra_parameters_cleaned = extra_parameters.map do |x|
    if x.key? 'ParameterKey'
      {
        parameter_key: x['ParameterKey'],
        parameter_value: x['ParameterValue']
      }
    else
      x
    end
  end
  options = {
    stack_name: stack_name,
    template_body: template,
    parameters: generate_parameters(stack, keys) + extra_parameters_cleaned
  }
  cfn.create_stack(options)
end

#generate_parameters(stack, keys) ⇒ Object

Generate parameters list

Parameters:

  • stack (Aws::CloudFormation::Stack)

    cloudformation stack instance

  • keys (Array)

    list of keys



102
103
104
# File 'lib/enscalator/helpers/stack.rb', line 102

def generate_parameters(stack, keys)
  keys.map { |k| { parameter_key: k, parameter_value: get_resource(stack, k) } }
end

#get_resource(stack, key) ⇒ String

Get resource for given key from given stack

Parameters:

  • stack (Aws::CloudFormation::Stack)

    cloudformation stack instance

  • key (String)

    resource identifier (key)

Returns:

  • (String)

    AWS resource identifier

Raises:

  • (ArgumentError)

    when stack is nil

  • (ArgumentError)

    when key is nil or empty



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/enscalator/helpers/stack.rb', line 64

def get_resource(stack, key)
  fail ArgumentError, 'stack must not be nil' if stack.nil?
  fail ArgumentError, 'key must not be nil nor empty' if key.nil? || key.empty?
  # query with physical_resource_id
  resource = begin
    stack.resource(key).physical_resource_id
  rescue RuntimeError
    nil
  end
  if resource.nil?
    # fallback to values from stack.outputs
    output = stack.outputs.select { |s| s.output_key == key }
    resource = begin
      output.first.output_value
    rescue RuntimeError
      nil
    end
  end
  resource
end

#get_resources(stack, keys) ⇒ String

Get list of resources for given keys

Parameters:

  • stack (Aws::CloudFormation::Stack)

    cloudformation stack instance

  • keys (Array)

    list of resource identifiers (keys)

Returns:

  • (String)

    list of AWS resource identifiers

Raises:

  • (ArgumentError)

    when stack is nil

  • (ArgumentError)

    when keys are nil or empty list



92
93
94
95
96
# File 'lib/enscalator/helpers/stack.rb', line 92

def get_resources(stack, keys)
  fail ArgumentError, 'stack must not be nil' if stack.nil?
  fail ArgumentError, 'key must not be nil nor empty' if keys.nil? || keys.empty?
  keys.map { |k| get_resource(stack, k) }.compact
end

#wait_stack(cfn, stack_name) ⇒ Aws::CloudFormation::Stack

Wait until stack gets created

Parameters:

  • cfn (Aws::CloudFormation::Resource)

    accessor for cloudformation resource

  • stack_name (String)

    name of the stack

Returns:

  • (Aws::CloudFormation::Stack)


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/enscalator/helpers/stack.rb', line 12

def wait_stack(cfn, stack_name)
  stack = cfn.stack(stack_name)
  title = 'Waiting for stack to be created'
  progress = ProgressBar.create(title: title, starting_at: 10, total: nil)
  loop do
    break unless stack.stack_status =~ /(CREATE|UPDATE)_IN_PROGRESS$/
    progress.title = title + " [#{stack.stack_status}]"
    progress.increment
    sleep 5
    stack = cfn.stack(stack_name)
  end
  stack
end