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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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_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]}"
    nested_stacks = file.delete('sfn_nested_stack')
  end

  if(nested_stacks)

    unpack_nesting(name, file, :update)

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

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

      unless(file)
        if(Chef::Config[:knife][:cloudformation][:template])
          file = Chef::Config[:knife][:cloudformation][:template]
          stack_info << " #{ui.color('(template provided)', :green)}"
        else
          stack_info << " #{ui.color('(no template update)', :yellow)}"
        end
      end
      ui.info "  -> #{stack_info}"

      apply_stacks!(stack)

      if(file)
        populate_parameters!(file, stack.parameters)
        stack.template = translate_template(file)
        stack.parameters = Chef::Config[:knife][:cloudformation][:options][:parameters]
        stack.template = KnifeCloudformation::Utils::StackParameterScrubber.scrub!(stack.template)
      else
        populate_parameters!(stack.template, stack.parameters)
        stack.parameters = Chef::Config[:knife][:cloudformation][:options][:parameters]
      end

      begin
        stack.save
      rescue Miasma::Error::ApiError::RequestError => e
        if(e.message.downcase.include?('no updates')) # :'(
          ui.warn "No updates detected for stack (#{stack.name})"
        else
          raise
        end
      end

      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
end

#redefault_stack_parameters(template, stack) ⇒ Hash

TODO:

to be scrubbed

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)


126
127
128
129
130
131
132
133
# File 'lib/chef/knife/cloudformation_update.rb', line 126

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