Class: Chef::Knife::CloudformationUpdate

Inherits:
Chef::Knife show all
Includes:
KnifeCloudformation::Knife::Base, KnifeCloudformation::Knife::Stack, KnifeCloudformation::Knife::Template
Defined in:
lib/chef/knife/cloudformation_update.rb

Constant Summary

Constants included from KnifeCloudformation::Knife::Template

KnifeCloudformation::Knife::Template::TEMPLATE_IGNORE_DIRECTORIES

Instance Method Summary collapse

Methods included from KnifeCloudformation::Knife::Stack

included

Methods included from KnifeCloudformation::Knife::Template

included

Methods included from KnifeCloudformation::Knife::Base

included

Instance Method Details

#_runObject

Run the stack creation command



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef/knife/cloudformation_update.rb', line 33

def _run
  name = name_args.first
  unless(name)
    ui.fatal "Formation name must be specified!"
    exit 1
  end

  stack = provider.connection.stacks.get(name)

  if(stack)
    ui.info "#{ui.color('Cloud Formation:', :bold)} #{ui.color('update', :green)}"
    stack_info = "#{ui.color('Name:', :bold)} #{name}"

    if(Chef::Config[:knife][:cloudformation][:file])
      file = load_template_file
      stack_info << " #{ui.color('Path:', :bold)} #{Chef::Config[:knife][:cloudformation][:file]}"
    else
      stack_info << " #{ui.color('(no temlate update)', :yellow)}"
    end
    ui.info "  -> #{stack_info}"

    apply_stacks!(stack)

    if(file)
      stack_parameters_update!(stack)
      stack.template = translate_template(file)
      stack.parameters = Chef::Config[:knife][:cloudformation][:parameters]
    else
      stack_parameters_update!(stack)
    end

    stack.save

    if(Chef::Config[:knife][:cloudformation][:poll])
      poll_stack(stack.name)
      if(stack.success?)
        ui.info "Stack update complete: #{ui.color('SUCCESS', :green)}"
        knife_output = Chef::Knife::CloudformationDescribe.new
        knife_output.name_args.push(name)
        knife_output.config[:outputs] = true
        knife_output.run
      else
        ui.fatal "Update of stack #{ui.color(name, :bold)}: #{ui.color('FAILED', :red, :bold)}"
        ui.info ""
        knife_inspect = Chef::Knife::CloudformationInspect.new
        knife_inspect.name_args.push(name)
        knife_inspect.config[:instance_failure] = true
        knife_inspect.run
        exit 1
      end
    else
      ui.warn 'Stack state polling has been disabled.'
      ui.info "Stack update initialized for #{ui.color(name, :green)}"
    end
  else
    ui.fatal "Failed to locate requested stack: #{ui.color(name, :red, :bold)}"
    exit -1
  end
end

#apply_stacks!(stack) ⇒ Miasma::Models::Orchestration::Stack

Apply any defined remote stacks

Parameters:

  • stack (Miasma::Models::Orchestration::Stack)

Returns:

  • (Miasma::Models::Orchestration::Stack)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/chef/knife/cloudformation_update.rb', line 112

def apply_stacks!(stack)
  remote_stacks = Chef::Config[:knife][:cloudformation].
    fetch(:update, {}).fetch(:apply_stacks, [])
  remote_stacks.each do |stack_name|
    remote_stack = provider.connection.stacks.get(stack_name)
    if(remote_stack)
      remote_stack.parameters.each do |key, value|
        next if Chef::Config[:knife][:cloudformation][:stacks][:ignore_parameters].include?(key)
        if(stack.parameters.has_key?(key))
          stack.parameters[key] = value
        end
      end
    else
      ui.error "Failed to apply requested stack. Unable to locate. (#{stack_name})"
      exit 1
    end
  end
  stack
end

#redefault_stack_parameters(template, stack) ⇒ Hash

Update default values for parameters in template with currently used parameters on the existing stack

Parameters:

  • template (Hash)

    stack template

  • stack (Fog::Orchestration::Stack)

Returns:

  • (Hash)


99
100
101
102
103
104
105
106
# File 'lib/chef/knife/cloudformation_update.rb', line 99

def redefault_stack_parameters(template, stack)
  stack.parameters.each do |key, value|
    if(template['Parameters'][key])
      template['Parameters'][key]['Default'] = value
    end
  end
  template
end

#stack_parameters_update!(stack) ⇒ Miasma::Models::Orchestration::Stack

Update parameters within existing stack

Parameters:

  • stack (Miasma::Models::Orchestration::Stack)

Returns:

  • (Miasma::Models::Orchestration::Stack)


136
137
138
139
140
141
# File 'lib/chef/knife/cloudformation_update.rb', line 136

def stack_parameters_update!(stack)
  stack.parameters.each do |key, value|
    answer = ui.ask_question("#{key.split(/([A-Z]+[^A-Z]*)/).find_all{|s|!s.empty?}.join(' ')}: ", :default => value)
    stack.parameters[key] = answer
  end
end