Class: Stacco::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/stacco/template.rb

Direct Known Subclasses

Base

Defined Under Namespace

Classes: Base, ColonPath, ConcatenatedPath, Mapping, Old, Path, Resource, StackVar, Type, Var

Constant Summary collapse

AttachmentTypes =
{
  Set[Type[:ec2, :vpc], Type[:ec2, :internet_gateway]] => Type[:ec2, :vpc_gateway_attachment],
}

Instance Method Summary collapse

Constructor Details

#initializeTemplate

Returns a new instance of Template.



28
29
30
31
# File 'lib/stacco/template.rb', line 28

def initialize
  @parameters = []
  @resources = []
end

Instance Method Details

#attach(resource_a, resource_b, opts = {}) ⇒ Object



122
123
# File 'lib/stacco/template.rb', line 122

def attach(resource_a, resource_b, opts = {})
end

#export_for_json(o, is_key = false) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/stacco/template.rb', line 163

def export_for_json(o, is_key = false)
  case o
  when Numeric
    o.to_s
  when TrueClass, FalseClass
    o.to_s
  when NilClass
    raise ArgumentError, o
  when Var
    if is_key
      o.to_s
    else
      {"Ref" => o.to_s}
    end
  when Symbol
    ConcatenatedPath[o].to_s
  when Path
    o.to_s
  when Array
    o.map{ |el| export_for_json(el) }
  when Hash
    nh = {}
    o.each{ |k, v| nh[ export_for_json(k, true) ] = export_for_json(v) }
    nh
  when Regexp
    o.source
  else
    o
  end
end

#mapping(name, hsh) ⇒ Object



118
119
120
# File 'lib/stacco/template.rb', line 118

def mapping(name, hsh)
  @mappings.push [name, hsh]
end

#parameter(name, type, opts = {}) ⇒ Object



108
109
110
# File 'lib/stacco/template.rb', line 108

def parameter(name, type, opts = {})
  @parameters.push [name, type, opts]
end

#renderObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/stacco/template.rb', line 125

def render
  tpl = {AWSTemplateFormatVersion: "2010-09-09"}
  tpl[:description] = "Stacco stack"

  tpl[:Mappings] = {}

  @mappings.each do |(name, hsh)|
    name = [name] if name.kind_of?(String) or name.kind_of?(Symbol)
    tpl[:Mappings][Mapping[*name]] = hsh
  end

  tpl[:Parameters] = {}

  @parameters.each do |(name, type, args)|
    name = [name] if name.kind_of?(String) or name.kind_of?(Symbol)
    constraints = {}
    args.merge(type: type.camelize).each do |k,v|
      constraints[k.camelize] = v
    end

    tpl[:Parameters][name] = constraints
  end

  tpl[:Resources] = {}

  @resources.each do |(name, type, args)|
    name = [name] if name.kind_of?(String) or name.kind_of?(Symbol)
    properties = {}
    args.each do |k,v|
      properties[k.camelize] = v
    end

    tpl[:Resources][name] = {"Type" => type, "DependsOn" => [], "Properties" => properties}
  end

  tpl
end

#resource(type, opts = {}) ⇒ Object



112
113
114
115
116
# File 'lib/stacco/template.rb', line 112

def resource(type, opts = {})
  name = type.resource(opts.delete(:name))
  @resources.push [name, type, opts]
  name
end

#to_json(opts = {}) ⇒ Object



194
195
196
# File 'lib/stacco/template.rb', line 194

def to_json(opts = {})
  self.export_for_json(self.render).to_json
end

#udscript(roles, vars) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/stacco/template.rb', line 33

def udscript(roles, vars)
  roles = roles.map{ |role| role.intern }

  unless roles.include? :backend
    vars = vars.dup
    vars.delete 'wallet_data'
  end

  lns = []
  lns.concat(vars.map do |k, v|
    var_val = v.include?("\0") ? Base64.encode64(v) : v
    "export #{k.to_s.upcase}=#{Shellwords.escape(var_val)}\n"
  end)
  lns.concat Stacco::Resources::CloudInit::CommonScript
  roles.each{ |role| lns.concat Stacco::Resources::CloudInit::RoleScripts[role] }
  lns
end