Class: Sfn::Command::Import

Inherits:
Sfn::Command
  • Object
show all
Includes:
Sfn::CommandModule::Base, Utils::JSON, Utils::ObjectStorage, Utils::PathSelector
Defined in:
lib/sfn/command/import.rb

Overview

Import command

Constant Summary

Constants inherited from Sfn::Command

CONFIG_BASE_NAME, VALID_CONFIG_EXTENSIONS

Instance Method Summary collapse

Methods included from Utils::PathSelector

#humanize_path_basename, #prompt_for_file

Methods included from Utils::ObjectStorage

#file_store

Methods included from Utils::JSON

#_format_json, #_from_json, #_to_json

Methods included from Sfn::CommandModule::Base

included

Methods inherited from Sfn::Command

#config, #initialize

Methods included from Sfn::CommandModule::Callbacks

#api_action!, #callbacks_for, #run_callbacks_for

Constructor Details

This class inherits a constructor from Sfn::Command

Instance Method Details

#bucket_prefixString, NilClass

Generate bucket prefix

Returns:

  • (String, NilClass)


70
71
72
73
74
75
76
77
78
# File 'lib/sfn/command/import.rb', line 70

def bucket_prefix
  if(prefix = config[:bucket_prefix])
    if(prefix.respond_to?(:call))
      prefix.call
    else
      prefix.to_s
    end
  end
end

#execute!Object

Run the import action



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/sfn/command/import.rb', line 15

def execute!
  raise NotImplementedError.new 'Implementation updates required'
  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 config[:bucket]
      _entries.push('fs') if config[: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 = namespace.const_val(:Create).new(
        Smash.new(
          :template => _from_json(export[:stack][:template]),
          :options => _from_json(export[:stack][:options])
        ),
        [stack_name]
      )
      ui.info '  - Starting creation of import'
      creator.execute!
      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")}"
      raise
    end
  else
    ui.fatal "Failed to locate JSON export file (#{json_file})"
    raise
  end
end

#local_discoveryIO

Discover remote file

Returns:

  • (IO)

    stack export IO



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sfn/command/import.rb', line 101

def local_discovery
  _, bucket = config[: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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sfn/command/import.rb', line 83

def remote_discovery
  storage = provider.service_for(:storage)
  directory = storage.directories.get(config[: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