Class: Sfn::Command::Export

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

Overview

Export command

Constant Summary

Constants inherited from Sfn::Command

CONFIG_BASE_NAME, VALID_CONFIG_EXTENSIONS

Instance Method Summary collapse

Methods included from Utils::ObjectStorage

#file_store

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

#execute!Object

Run export action



11
12
13
14
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
# File 'lib/sfn/command/export.rb', line 11

def execute!
  raise NotImplementedError.new "Implementation updates required"
  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 = Smash.new.tap do |opts|
      [:chef_popsicle, :chef_environment_parameter, :ignore_parameters].each do |key|
        opts[key] = config[key] unless config[key].nil?
      end
    end
    exporter = Sfn::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



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sfn/command/export.rb', line 49

def export_file_name(stack)
  name = config[: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)


87
88
89
90
91
92
93
94
95
96
# File 'lib/sfn/command/export.rb', line 87

def write_to_bucket(payload, stack)
  raise NotImplementedError
  if bucket = config[: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)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sfn/command/export.rb', line 67

def write_to_file(payload, stack)
  raise NotImplementedError
  if config[:path]
    full_path = File.join(
      config[: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