Class: CfDeployer::Stack

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

Constant Summary collapse

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

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



47
48
49
50
51
52
53
54
55
# File 'lib/cf_deployer/stack.rb', line 47

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
# File 'lib/cf_deployer/stack.rb', line 18

def deploy
  config_dir = @context[:config_dir]
  template = CfDeployer::ConfigLoader.component_json(@component, @context)
  capabilities = @context[:capabilities] || []
  tags = @context[:tags] || []
  params = to_str(@context[:inputs].select{|key, value| @context[:defined_parameters].keys.include?(key)})
  CfDeployer::Driver::DryRun.guard "Skipping deploy" do
    exists? ? update_stack(template, params, capabilities, tags) : create_stack(template, params, capabilities, tags)
  end
end

#exists?Boolean

Returns:



57
58
59
# File 'lib/cf_deployer/stack.rb', line 57

def exists?
  @cf_driver.stack_exists?
end

#nameObject



90
91
92
# File 'lib/cf_deployer/stack.rb', line 90

def name
  @stack_name
end

#output(key) ⇒ Object



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

def output key
  begin
    @cf_driver.query_output(key) || (raise ApplicationError.new("'#{key}' is empty from stack #{name} output"))
  rescue AWS::CloudFormation::Errors::ValidationError => e
    raise ResourceNotInReadyState.new("Resource stack not in ready state yet, perhaps you should provision it first?")
  end
end

#outputsObject



29
30
31
32
# File 'lib/cf_deployer/stack.rb', line 29

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

#parametersObject



34
35
36
37
# File 'lib/cf_deployer/stack.rb', line 34

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

#ready?Boolean

Returns:



61
62
63
# File 'lib/cf_deployer/stack.rb', line 61

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

#resource_statusesObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cf_deployer/stack.rb', line 73

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



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

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