Class: Chef::Knife::CloudformationExport
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::CloudformationExport
- Defined in:
- lib/chef/knife/cloudformation_export.rb
Overview
Cloudformation export command
Instance Method Summary collapse
-
#_run ⇒ Object
Run export action.
-
#export_file_name(stack) ⇒ String
Generate file name for stack export JSON contents.
-
#write_to_bucket(payload, stack) ⇒ String, NilClass
Write stack export to remote bucket.
-
#write_to_file(payload, stack) ⇒ String, NilClass
Write stack export to local file.
Methods included from KnifeCloudformation::Utils::ObjectStorage
Methods included from KnifeCloudformation::Knife::Base
Instance Method Details
#_run ⇒ Object
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) = 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, ) 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
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
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
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.(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 |