Class: Yutani::Stack

Inherits:
Object
  • Object
show all
Includes:
Hiera
Defined in:
lib/yutani/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hiera

#hiera, init_hiera, lookup, pop, push, scope

Constructor Details

#initialize(*namespace, &block) ⇒ Stack

Returns a new instance of Stack.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/yutani/stack.rb', line 10

def initialize(*namespace, &block)
  @resources     = []
  @data          = []
  @providers     = []
  @remote_config = nil
  @outputs       = {}
  @variables     = {}
  @namespace     = namespace

  Docile.dsl_eval(self, &block) if block_given?
end

Instance Attribute Details

#outputsObject

Returns the value of attribute outputs.



8
9
10
# File 'lib/yutani/stack.rb', line 8

def outputs
  @outputs
end

#providersObject

Returns the value of attribute providers.



8
9
10
# File 'lib/yutani/stack.rb', line 8

def providers
  @providers
end

#resourcesObject

Returns the value of attribute resources.



8
9
10
# File 'lib/yutani/stack.rb', line 8

def resources
  @resources
end

#variablesObject

Returns the value of attribute variables.



8
9
10
# File 'lib/yutani/stack.rb', line 8

def variables
  @variables
end

Instance Method Details

#add_provider(provider) ⇒ Object



50
51
52
# File 'lib/yutani/stack.rb', line 50

def add_provider(provider)
  @providers << provider
end

#add_resource(resource) ⇒ Object

troposphere-like methods



46
47
48
# File 'lib/yutani/stack.rb', line 46

def add_resource(resource)
  @resources << resource
end

#data(data_type, *namespace, &block) ⇒ Object



31
32
33
34
# File 'lib/yutani/stack.rb', line 31

def data(data_type, *namespace, &block)
  @data <<
    Data.new(data_type, *namespace, &block)
end

#dir_pathObject



90
91
92
# File 'lib/yutani/stack.rb', line 90

def dir_path
  File.join(Yutani.config['terraform_dir'], name)
end

#inc(&block) ⇒ Object



54
55
56
57
58
# File 'lib/yutani/stack.rb', line 54

def inc(&block)
  path = File.join(Yutani.config['includes_dir'], yield)

  eval File.read(path), block.binding, path
end

#nameObject



22
23
24
# File 'lib/yutani/stack.rb', line 22

def name
  @namespace.to_underscored_string
end

#pretty_jsonObject



86
87
88
# File 'lib/yutani/stack.rb', line 86

def pretty_json
  JSON.pretty_generate(to_h)
end

#provider(name, &block) ⇒ Object



36
37
38
39
# File 'lib/yutani/stack.rb', line 36

def provider(name, &block)
  @providers <<
    Provider.new(name, &block)
end

#remote_config(&block) ⇒ Object



41
42
43
# File 'lib/yutani/stack.rb', line 41

def remote_config(&block)
  @remote_config = RemoteConfig.new(&block)
end

#resource(resource_type, *namespace, &block) ⇒ Object



26
27
28
29
# File 'lib/yutani/stack.rb', line 26

def resource(resource_type, *namespace, &block)
  @resources <<
    Resource.new(resource_type, *namespace, &block)
end

#to_fsObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/yutani/stack.rb', line 94

def to_fs
  FileUtils.mkdir_p(dir_path)
  FileUtils.cd(dir_path) do
    File.open('main.tf.json', 'w+', 0644) do |f|
      f.write pretty_json
    end

    @remote_config.execute! unless @remote_config.nil?
  end
end

#to_hObject

this generates the contents of *.tf.main



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/yutani/stack.rb', line 61

def to_h
  h = {
    resource: @resources.inject(DeepMergeHash.new){|resources,r|
      resources.deep_merge!(r.to_h)
    },
    data: @data.inject(DeepMergeHash.new){|data,d|
      data.deep_merge!(d.to_h)
    },
    provider: @providers.inject(DeepMergeHash.new){|providers,r|
      providers.deep_merge(r.to_h)
    },
    output: @outputs.inject({}){|outputs,(k,v)|
      outputs[k] = { value: v }
      outputs
    },
    variable: @variables.inject({}){|variables,(k,v)|
      variables[k] = {}
      variables
    }
  }

  # terraform doesn't like empty output and variable collections
  h.delete_if {|_,v| v.empty? }
end