Class: Sfn::Utils::StackExporter

Inherits:
Object
  • Object
show all
Includes:
Bogo::AnimalStrings, JSON
Defined in:
lib/sfn/utils/stack_exporter.rb

Overview

Stack serialization helper

Constant Summary collapse

DEFAULT_CHEF_ENVIRONMENT =

default chef environment name

"_default"
DEFAULT_OPTIONS =

default instance options

Mash.new(
  :chef_popsicle => true,
  :ignored_parameters => ["Environment", "StackCreator", "Creator"],
  :chef_environment_parameter => "Environment",
)
DEFAULT_EXPORT_STRUCTURE =

default structure of export payload

{
  :stack => Mash.new(
    :template => nil,
    :options => {
      :parameters => Mash.new,
      :capabilities => [],
      :notification_topics => [],
    },
  ),
  :generator => {
    :timestamp => Time.now.to_i,
    :name => "SparkleFormation",
    :version => Sfn::VERSION.version,
    :provider => nil,
  },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JSON

#_format_json, #_from_json, #_to_json

Constructor Details

#initialize(stack, options = {}) ⇒ StackExporter

Create new instance

Parameters:

  • stack (Miasma::Models::Orchestration::Stack)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :provider (KnifeCloudformation::Provider)
  • :chef_popsicle (TrueClass, FalseClass)

    freeze run list

  • :ignored_parameters (Array<String>)
  • :chef_environment_parameter (String)


57
58
59
60
61
# File 'lib/sfn/utils/stack_exporter.rb', line 57

def initialize(stack, options = {})
  @stack = stack
  @options = DEFAULT_OPTIONS.merge(options)
  @stack_export = Smash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object

Provide query methods on options hash

Parameters:

  • args (Object)

    argument list

Returns:

  • (Object)


90
91
92
93
94
95
96
97
# File 'lib/sfn/utils/stack_exporter.rb', line 90

def method_missing(*args)
  m = args.first.to_s
  if m.end_with?("?") && options.has_key?(k = m.sub("?", "").to_sym)
    !!options[k]
  else
    super
  end
end

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)


45
46
47
# File 'lib/sfn/utils/stack_exporter.rb', line 45

def options
  @options
end

#stackMiasma::Models::Orchestration::Stack (readonly)

Returns:

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


43
44
45
# File 'lib/sfn/utils/stack_exporter.rb', line 43

def stack
  @stack
end

#stack_exportHash (readonly)

Returns:

  • (Hash)


47
48
49
# File 'lib/sfn/utils/stack_exporter.rb', line 47

def stack_export
  @stack_export
end

Instance Method Details

#exportHash

Export stack

Returns:

  • (Hash)

    exported stack



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sfn/utils/stack_exporter.rb', line 66

def export
  @stack_export = Smash.new(DEFAULT_EXPORT_STRUCTURE).tap do |stack_export|
    [:parameters, :capabilities, :notification_topics].each do |key|
      if val = stack.send(key)
        stack_export[:stack][key] = val
      end
    end
    stack_export[:stack][:template] = stack.template
    stack_export[:generator][:timestamp] = Time.now.to_i
    stack_export[:generator][:provider] = stack.provider.connection.provider
    if chef_popsicle? && defined?(Chef)
      freeze_runlists(stack_export)
    end
    remove_ignored_parameters(stack_export)
    stack_export[:stack][:template] = _to_json(
      stack_export[:stack][:template]
    )
  end
end