Class: Chef::Knife::CloudformationExport

Inherits:
Chef::Knife show all
Includes:
KnifeCloudformation::Knife::Base, KnifeCloudformation::Utils::ObjectStorage
Defined in:
lib/chef/knife/cloudformation_export.rb

Overview

Cloudformation export command

Instance Method Summary collapse

Methods included from KnifeCloudformation::Utils::ObjectStorage

#file_store

Methods included from KnifeCloudformation::Knife::Base

included

Instance Method Details

#_runObject

Run export action



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
# File 'lib/chef/knife/cloudformation_export.rb', line 70

def _run
  stack_name = name_args.first
  ui.info "#{ui.color('Stack Export:', :bold)} #{stack_name}"
  ui.confirm 'Perform export'
  stack = provider.stacks.get(stack_name)
  if(stack)
    export_options = Mash.new.tap do |opts|
      [:chef_popsicle, :chef_environment_parameter, :ignore_parameters].each do |key|
        unless(Chef::Config[:knife][:cloudformation][:export][key].nil?)
          opts[key] = Chef::Config[:knife][:cloudformation][:export][key]
        end
      end
    end
    exporter = KnifeCloudformation::Utils::StackExporter.new(stack, export_options)
    result = exporter.export
    outputs = [
      write_to_file(result, stack),
      write_to_bucket(result, stack)
    ].compact
    if(outputs.empty?)
      ui.warn 'No persistent output location defined. Printing export:'
      ui.info _format_json(result)
    end
    ui.info "#{ui.color('Stack export', :bold)} (#{name_args.first}): #{ui.color('complete', :green)}"
    unless(outputs.empty?)
      outputs.each do |output|
        ui.info ui.color("  -> #{output}", :blue)
      end
    end
  else
    ui.fatal "Failed to discover requested stack: #{ui.color(stack_name, :red, :bold)}"
    exit -1
  end
end

#export_file_name(stack) ⇒ String

Generate file name for stack export JSON contents

Parameters:

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

Returns:

  • (String)

    file name



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef/knife/cloudformation_export.rb', line 109

def export_file_name(stack)
  name = Chef::Config[:knife][:cloudformation][:export][:file]
  if(name)
    if(name.respond_to?(:call))
      name.call(stack)
    else
      name.to_s
    end
  else
    "#{stack.stack_name}-#{Time.now.to_i}.json"
  end
end

#write_to_bucket(payload, stack) ⇒ String, NilClass

Write stack export to remote bucket

Parameters:

  • payload (Hash)

    stack export payload

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

Returns:

  • (String, NilClass)

    remote bucket key

Raises:

  • (NotImplementedError)


148
149
150
151
152
153
154
155
156
157
158
# File 'lib/chef/knife/cloudformation_export.rb', line 148

def write_to_bucket(payload, stack)
  raise NotImplementedError
  if(bucket = Chef::Config[:knife][:cloudformation][:export][:bucket])
    key_path = File.join(*[
        bucket_prefix(stack),
        export_file_name(stack)
      ].compact
    )
    file_store(payload, key_path, provider.service_for(:storage).directories.get(bucket))
  end
end

#write_to_file(payload, stack) ⇒ String, NilClass

Write stack export to local file

Parameters:

  • payload (Hash)

    stack export payload

  • stack (Misama::Stack::Orchestration::Stack)

Returns:

  • (String, NilClass)

    path to file

Raises:

  • (NotImplementedError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/knife/cloudformation_export.rb', line 127

def write_to_file(payload, stack)
  raise NotImplementedError
  if(Chef::Config[:knife][:cloudformation][:export][:path])
    full_path = File.join(
      File.expand_path(Chef::Config[:knife][:cloudformation][:export][:path]),
      export_file_name(stack)
    )
    _, bucket, path = full_path.split('/', 3)
    directory = provider.service_for(:storage,
      :provider => :local,
      :local_root => '/'
    ).directories.get(bucket)
    file_store(payload, path, directory)
  end
end