Class: CfDeployer::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/cf_deployer/stack.rb

Constant Summary collapse

SUCCESS_STATS =
[:create_complete, :update_complete, :delete_complete]
READY_STATS =
SUCCESS_STATS - [:delete_complete]
FAILED_STATS =
[:create_failed, :update_failed, :delete_failed, :update_rollback_complete]

Instance Method Summary collapse

Constructor Details

#initialize(stack_name, component, context) ⇒ Stack

Returns a new instance of Stack.



11
12
13
14
15
16
# File 'lib/cf_deployer/stack.rb', line 11

def initialize(stack_name, component, context)
  @stack_name = stack_name
  @cf_driver = context[:cf_driver] || CfDeployer::Driver::CloudFormation.new(stack_name)
  @context = context
  @component = component
end

Instance Method Details

#deleteObject



64
65
66
67
68
69
70
71
72
# File 'lib/cf_deployer/stack.rb', line 64

def delete
  if exists?
    CfDeployer::Driver::DryRun.guard "Skipping delete" do
      Log.info "deleting stack #{@stack_name}"
      @cf_driver.delete_stack
      wait_for_stack_to_delete
    end
  end
end

#deployObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cf_deployer/stack.rb', line 18

def deploy
  config_dir = @context[:config_dir]
  template = CfDeployer::ConfigLoader.erb_to_json(@component, @context)
  capabilities = @context[:capabilities] || []
  notify = @context[:notify] || []
  tags = @context[:tags] || {}
  params = to_str(@context[:inputs].select{|key, value| @context[:defined_parameters].keys.include?(key)})
  CfDeployer::Driver::DryRun.guard "Skipping deploy" do
    if exists?
      override_policy_json = nil
      unless @context[:settings][:'override-stack-policy'].nil?
        override_policy_json = CfDeployer::ConfigLoader.erb_to_json(@context[:settings][:'override-stack-policy'], @context)
      end
      update_stack(template, params, capabilities, tags, override_policy_json)
    else
      create_policy_json = nil
      unless @context[:settings][:'create-stack-policy'].nil?
        create_policy_json = CfDeployer::ConfigLoader.erb_to_json(@context[:settings][:'create-stack-policy'], @context)
      end
      create_stack(template, params, capabilities, tags, notify, create_policy_json)
    end
  end
end

#exists?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/cf_deployer/stack.rb', line 74

def exists?
  @cf_driver.stack_exists?
end

#find_output(key) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/cf_deployer/stack.rb', line 56

def find_output key
  begin
    @cf_driver.query_output(key)
  rescue AWS::CloudFormation::Errors::ValidationError => e
    raise ResourceNotInReadyState.new("Resource stack not in ready state yet, perhaps you should provision it first?")
  end
end

#nameObject



107
108
109
# File 'lib/cf_deployer/stack.rb', line 107

def name
  @stack_name
end

#output(key) ⇒ Object



52
53
54
# File 'lib/cf_deployer/stack.rb', line 52

def output key
  find_output(key) || (raise ApplicationError.new("'#{key}' is empty from stack #{name} output"))
end

#outputsObject



42
43
44
45
# File 'lib/cf_deployer/stack.rb', line 42

def outputs
  return {} unless ready?
  @cf_driver.outputs
end

#parametersObject



47
48
49
50
# File 'lib/cf_deployer/stack.rb', line 47

def parameters
  return {} unless ready?
  @cf_driver.parameters
end

#ready?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/cf_deployer/stack.rb', line 78

def ready?
  READY_STATS.include? @cf_driver.stack_status
end

#resource_statusesObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cf_deployer/stack.rb', line 90

def resource_statuses
  AWS.memoize do
    resources = @cf_driver.resource_statuses.merge( { :asg_instances => {}, :instances => {} } )
    if resources['AWS::AutoScaling::AutoScalingGroup']
      resources['AWS::AutoScaling::AutoScalingGroup'].keys.each do |asg_name|
        resources[:asg_instances][asg_name] = CfDeployer::Driver::AutoScalingGroup.new(asg_name).instance_statuses
      end
    end
    if resources['AWS::EC2::Instance']
      resources['AWS::EC2::Instance'].keys.each do |instance_id|
        resources[:instances][instance_id] = CfDeployer::Driver::Instance.new(instance_id).status
      end
    end
    resources
  end
end

#statusObject



82
83
84
85
86
87
88
# File 'lib/cf_deployer/stack.rb', line 82

def status
  if exists?
    ready? ? :ready : :exists
  else
    :does_not_exist
  end
end

#templateObject



111
112
113
# File 'lib/cf_deployer/stack.rb', line 111

def template
  @cf_driver.template
end