Class: Chef::Knife::CloudformationImport

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

Overview

Cloudformation import command

Instance Method Summary collapse

Methods included from KnifeCloudformation::Utils::PathSelector

#humanize_path_basename, #prompt_for_file

Methods included from KnifeCloudformation::Utils::ObjectStorage

#file_store

Methods included from KnifeCloudformation::Utils::JSON

#_format_json, #_from_json, #_to_json, #try_json_compat

Methods included from KnifeCloudformation::Knife::Base

included

Instance Method Details

#_runObject

Run the import action



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

def _run
  stack_name, json_file = name_args
  ui.info "#{ui.color('Stack Import:', :bold)} #{stack_name}"
  unless(json_file)
    entries = [].tap do |_entries|
      _entries.push('s3') if Chef::Config[:knife][:cloudformation][:import][:bucket]
      _entries.push('fs') if Chef::Config[:knife][:cloudformation][:import][:path]
    end
    if(entries.size > 1)
      valid = false
      until(valid)
        answer = ui.ask_question('Import via file system (fs) or remote bucket (remote)?', :default => 'remote')
        valid = true if %w(remote fs).include?(answer)
        entries = [answer]
      end
    elsif(entries.size < 1)
      ui.fatal 'No path or bucket set. Unable to perform dynamic lookup!'
      exit 1
    end
    case entries.first
    when 'remote'
      json_file = remote_discovery
    else
      json_file = local_discovery
    end
  end
  if(File.exists?(json_file) || json_file.is_a?(IO))
    content = json_file.is_a?(IO) ? json_file.read : File.read(json_file)
    export = Mash.new(_from_json(content))
    begin
      creator = Chef::Knife::CloudformationCreate.new
      creator.name_args = [stack_name]
      Chef::Config[:knife][:cloudformation][:template] = _from_json(export[:stack][:template])
      Chef::Config[:knife][:cloudformation][:options] = export[:stack][:options]
      ui.info '  - Starting creation of import'
      creator.run
      ui.info "#{ui.color('Stack Import', :bold)} (#{json_file}): #{ui.color('complete', :green)}"
    rescue => e
      ui.fatal "Failed to import stack: #{e}"
      debug "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
      exit -1
    end
  else
    ui.fatal "Failed to locate JSON export file (#{json_file})"
    exit 1
  end
end

#bucket_prefixString, NilClass

Generate bucket prefix

Returns:

  • (String, NilClass)


92
93
94
95
96
97
98
99
100
# File 'lib/chef/knife/cloudformation_import.rb', line 92

def bucket_prefix
  if(prefix = Chef::Config[:knife][:cloudformation][:import][:bucket_prefix])
    if(prefix.respond_to?(:cal))
      prefix.call
    else
      prefix.to_s
    end
  end
end

#local_discoveryIO

Discover remote file

Returns:

  • (IO)

    stack export IO



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/chef/knife/cloudformation_import.rb', line 125

def local_discovery
  _, bucket = Chef::Config[:knife][:cloudformation][:import][:path].split('/', 2)
  storage = provider.service_for(:storage,
    :provider => :local,
    :local_root => '/'
  )
  directory = storage.directories.get(bucket)
  prompt_for_file(
    directory,
    :directories_name => 'Collections',
    :files_names => 'Exports'
  )
end

#remote_discoveryIO

Discover remote file

Returns:

  • (IO)

    stack export IO



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef/knife/cloudformation_import.rb', line 105

def remote_discovery
  storage = provider.service_for(:storage)
  directory = storage.directories.get(
    Chef::Config[:knife][:cloudformation][:import][:bucket]
  )
  file = prompt_for_file(
    directory,
    :directories_name => 'Collections',
    :files_names => 'Exports',
    :filter_prefix => bucket_prefix
  )
  if(file)
    remote_file = storage.files.get(file)
    StringIO.new(remote_file.body)
  end
end