Class: Kitchen::Driver::Sparkleformation

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/driver/sparkleformation.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Sparkleformation

Returns a new instance of Sparkleformation.



19
20
21
22
# File 'lib/kitchen/driver/sparkleformation.rb', line 19

def initialize(config)
  init_config config
  @cf = Aws::CloudFormation::Client.new
end

Instance Method Details

#create(state) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
# File 'lib/kitchen/driver/sparkleformation.rb', line 24

def create(state)
  unless state[:stack_id].nil?
    # will fail if the stack does not exist:
    @stack_desc = @cf.describe_stacks(stack_name: state[:stack_id]).stacks.first

    case @stack_desc.stack_status
    when 'CREATE_IN_PROGRESS'
      @cf.wait_until :stack_create_complete, stack_name: state[:stack_id]
    when 'CREATE_COMPLETE'
    else
      raise "Invalid stack state: #{@stack_desc.stack_status}"
    end

    if state[:hostname].nil?
      state[:hostname] = hostname(state[:stack_id])
    end

    return
  end

  # generate stack name
  @stack_name = config[:stack_name]
  if config[:stack_name_random_suffix]
    @stack_name << "-#{SecureRandom.hex(6)}"
  end

  json = generate_cloudformation_template

  if config[:upload_template]
    url = upload_template @stack_name, json
  end

  # build options for call to create_stack
  stack_options = {
    stack_name:   @stack_name,
    parameters:   config[:cf_params].map { |k, v| { parameter_key: SparkleFormation.camel(k), parameter_value: v } }
  }
  if config[:upload_template]
    stack_options[:template_url] = url
  else
    stack_options[:template_body] = json.to_json
  end
  stack_options.merge! config[:cf_options]

  info 'Triggering CloudFormation stack creation'
  state[:stack_id] = @cf.create_stack(stack_options).stack_id

  info "Waiting for stack #{state[:stack_id]} to reach state CREATE_COMPLETE..."
  @cf.wait_until :stack_create_complete, stack_name: state[:stack_id]
  info 'Stack creation finished'

  state[:hostname] = hostname(state[:stack_id])
end

#destroy(state) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kitchen/driver/sparkleformation.rb', line 78

def destroy(state)
  return if state[:stack_id].nil?

  info 'Triggering CloudFormation stack deletion'
  @cf.delete_stack stack_name: state[:stack_id]

  info 'Waiting for stack to reach state DELETE_COMPLETE...'
  @cf.wait_until :stack_delete_complete, stack_name: state[:stack_id]

  info 'Stack deletion finished'
end