Class: TerraformDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/scout/terraform_dsl.rb,
lib/scout/terraform_dsl/util.rb,
lib/scout/terraform_dsl/deployment.rb

Overview

rubocop: disable Style/Documentation

Defined Under Namespace

Classes: Deployment, Module

Constant Summary collapse

MODULES_DIR =
Scout.share.terraform
ANSIBLE_DIR =
Scout.share.ansible
WORK_DIR =
Scout.var.terraform

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modules = MODULES_DIR) ⇒ TerraformDSL

Create a new terraform deployment configuration

Parameters:

  • modules (String) (defaults to: MODULES_DIR)

    directory containing module templates



93
94
95
96
97
98
99
# File 'lib/scout/terraform_dsl.rb', line 93

def initialize(modules = MODULES_DIR)
  @modules = modules
  @elements = []
  @custom_files = []
  @variables = {}
  @element_files = []
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



10
11
12
# File 'lib/scout/terraform_dsl.rb', line 10

def elements
  @elements
end

#modulesObject

Returns the value of attribute modules.



10
11
12
# File 'lib/scout/terraform_dsl.rb', line 10

def modules
  @modules
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/scout/terraform_dsl.rb', line 10

def name
  @name
end

#processed_custom_filesObject

Returns the value of attribute processed_custom_files.



10
11
12
# File 'lib/scout/terraform_dsl.rb', line 10

def processed_custom_files
  @processed_custom_files
end

#processed_elementsObject

Returns the value of attribute processed_elements.



10
11
12
# File 'lib/scout/terraform_dsl.rb', line 10

def processed_elements
  @processed_elements
end

Class Method Details

.log(msg, prefix = nil) ⇒ Object

Log a message, optionally including a prefix between brakets

Parameters:

  • msg (String)

    Message to log

  • prefix (nil, String) (defaults to: nil)

    Optional prefix to prepend



14
15
16
17
18
19
20
# File 'lib/scout/terraform_dsl/util.rb', line 14

def self.log(msg, prefix = nil)
  if prefix
    STDOUT.puts("[#{prefix}] " + msg)
  else
    STDOUT.puts(msg)
  end
end

.module_outputs(module_dir) ⇒ Hash

Gather information about the output variables of a terraform module

Parameters:

  • module_dir (String)

    path to the directory with the module template files

Returns:

  • (Hash)

    for each variable name holds a hash with the description



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/scout/terraform_dsl/util.rb', line 73

def self.module_outputs(module_dir)
  outputs = {}

  module_dir = module_dir.find if Path === module_dir
  file = module_dir['output.tf']
  return outputs unless Open.exist?(file)

  name, description, value = nil
  Open.read(file).split("\n").each do |line|
    if (m = line.match(/^\s*output\s+"([^"]*)"/))
      if name
        outputs[name] = { :description => description }
        name, description, value = nil
      end
      name = m[1].strip
    elsif (m = line.match(/description\s*=\s*"(.*)"/))
      description = m[1].strip
    end
  end

  if name
    outputs[name] = { :description => description }
  end

  outputs
end

.module_variables(module_dir) ⇒ Hash

Gather information about the input variables of a terraform module

Parameters:

  • module_dir (String)

    path to the directory with the module template files

Returns:

  • (Hash)

    for each variable name holds a hash with description, type and default values



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
66
67
# File 'lib/scout/terraform_dsl/util.rb', line 34

def self.module_variables(module_dir)
  variables = {}

  file = module_dir['variables.tf']
  return variables unless Open.exist?(file)

  name, description, type, default = nil
  Open.read(file).split("\n").each do |line|
    if (m = line.match(/^\s*variable\s+"([^"]*)"/))
      if name
        variables[name] =
          { :description => description, :type => type, :default => default }
        name, description, type, default = nil
      end
      name = m[1].strip
    elsif (m = line.match(/description\s*=\s*"(.*)"/))
      description = m[1].strip
    elsif (m = line.match(/type\s*=\s*(.*)/))
      type = m[1].strip
    elsif (m = line.match(/default\s*=\s*(.*)/))
      default = begin
        JSON.parse(m[1].strip)
      rescue StandardError
        m[1].strip
      end
    end
  end

  if name
    variables[name] = { :description => description, :type => type, :default => default }
  end

  variables
end

.obj2digest(obj) ⇒ Object

Returns a md5 digest of an object based on its JSON representation

Parameters:

  • obj (Object)

    object to digest



25
26
27
# File 'lib/scout/terraform_dsl/util.rb', line 25

def self.obj2digest(obj)
  Digest::MD5.hexdigest(obj.to_json)
end

Instance Method Details

#add(provider = nil, module_name = nil, variables = {}) ⇒ Module

Add a new module instance

Parameters:

  • provider (String) (defaults to: nil)

    first level of template organization subdirectory

  • module_name (String) (defaults to: nil)

    subdirectory containing the module to use

  • variables (Hash) (defaults to: {})

    values for variables in the module template, and :name to name the module instance, and :outputs to define the module variables that will become deployment outputs

Returns:

  • (Module)

    A module object used as a reference in Terraform templates



109
110
111
112
113
114
115
# File 'lib/scout/terraform_dsl.rb', line 109

def add(provider = nil, module_name = nil, variables = {})
  variables[:name] ||= variables["name"] ||= [provider, module_name].join('_')
  module_directory = @modules[provider][module_name]
  @elements << [provider, module_name, module_directory, variables]
  @variables.merge!(variables)
  Module.new(variables[:name], module_name, self)
end

#config(dir = nil) ⇒ Object

Populate a directory all the necessary templates: modules, outputs, and custom

Parameters:

  • dir (String) (defaults to: nil)

    directory from which to manage the deployment. If none provided a unique one will be generated based on a md5 digest of the elements defined



302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/scout/terraform_dsl.rb', line 302

def config(dir = nil)
  dir = WORK_DIR[TerraformDSL.obj2digest(@elements)] if dir.nil?
  Open.mkdir dir
  main(dir)
  outputs(dir)
  custom_files(dir)
  @processed_elements ||= []
  @processed_elements.concat(@elements)
  @processed_custom_files ||= []
  @processed_custom_files.concat(@custom_files)
  @elements = []
  @custom_files = []
  dir
end

#custom(file, text) ⇒ Object

Add a terraform file with custom content. Used only to support defining non Hashicorp provider configuration

Parameters:

  • file (String)

    name of the file

  • text (String)

    content of the file



178
179
180
181
# File 'lib/scout/terraform_dsl.rb', line 178

def custom(file, text)
  @custom_files << [file, text]
  nil
end

#custom_files(dir) ⇒ Object

Populate a directory with the terraform templates corresponding to the custom defined elements

Parameters:

  • dir (String)

    directory from which to manage the deployment



249
250
251
252
253
# File 'lib/scout/terraform_dsl.rb', line 249

def custom_files(dir)
  @custom_files.each do |element_file, text|
    Open.write(dir[element_file + '.tf'], text)
  end
end

#main(dir) ⇒ Object

Populate a directory with the terraform templates corresponding to the defined elements

Parameters:

  • dir (String)

    directory from which to manage the deployment



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/scout/terraform_dsl.rb', line 142

def main(dir)
  @elements.each do |info|
    _provider, _module_name, template, variables = info

    template = template.find
    # Add an additional / to mark the base_path of the module directory and
    # allow modules to reference other modules relatively
    template = template.split('/').tap {|l| l[-2] = '/'+l[-2] } * '/'

    name = variables[:name]

    text =<<~EOF
        module "#{name}" {
          source = "#{template}"
        #{variable_block(variables)}
        }
    EOF

    element_file = [_module_name, name.to_s.sub(/_#{_module_name}$/,'')] * "."

    # rubocop: disable Layout/LineLength
    raise Deployment::TerraformException,
      "Warning: element file '#{element_file}' already exists, consider renaming it by using the parameter ':name'" if @element_files.include?(element_file)
    # rubocop: enable Layout/LineLength

    @element_files << element_file

    Open.write(dir[element_file + '.tf'], text)
  end
end

#outputs(dir) ⇒ Object

Populate a directory with the terraform templates corresponding to the defined element outputs variables

Parameters:

  • dir (String)

    directory from which to manage the deployment



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/scout/terraform_dsl.rb', line 259

def outputs(dir)
  @elements.each do |info|
    _provider, module_name, template, variables = info
    outputs = variables[:outputs]
    module_outputs = TerraformDSL.module_outputs(template)
    outputs = module_outputs.keys if outputs.to_s == 'all'
    next unless outputs && outputs.any?

    name = variables[:name]

    outputs = outputs.collect do |o|
      (o.is_a?(String) || o.is_a?(Symbol)) && o.to_s == 'all' ? module_outputs.keys : o
    end.flatten.uniq if outputs.is_a?(Array)

    text = ''
    outputs.each do |output, output_rename = nil|
      output, output_rename = output.collect.first if output.is_a?(Hash)

      output_rename = output if output_rename.nil?

      description = module_outputs[output.to_s][:description]
      description ||= "Value of #{output} from module #{name} (type #{module_name})"

      text +=<<~EOF
          output "#{name}_#{output_rename}"{
            description = "#{description}"
            value = module.#{name}.#{output}
          }
      EOF
    end

    element_file = [module_name, name.to_s] * "."

    Open.write(dir[element_file + '.outputs.tf'], text)
  end
end

#provider(name, variables = {}) ⇒ nil, Module

Add a provider template file without using modules. Defining providers in modules is problematic when providers are not managed by Hashicorp. Hopefully we can find a fix for this soon.

Parameters:

  • name (String)

    name of the provider

  • variables (Hash) (defaults to: {})

    variables for the provider: :source & :version

Returns:

  • (nil, Module)

    If a Module is found at <modules_dir>/<name>/provider it returns it



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/scout/terraform_dsl.rb', line 193

def provider(name, variables = {})
  variables = variables.dup

  if Open.exist?(@modules[name].provider)
    provider = add name, :provider
  else
    provider = nil
  end

  source = variables.delete :source
  version = variables.delete :version

  text = ''

  if source
    if version
      text +=<<~EOF
          terraform {
            required_providers {
              #{name} = {
              source = "#{source}"
              version = "#{version}"
              }
            }
          }
      EOF
    else
      text +=<<~EOF
          terraform {
            required_providers {
              #{name} = {
              source = "#{source}"
              }
            }
          }
      EOF
    end
  end

  text +=<<~EOF
      provider "#{name}" {
      #{variable_block(variables)}
      }
  EOF

  element_file = ['provider_config', name.to_s].join('.')

  custom(element_file, text)

  provider
end

#variable_block(variables) ⇒ String

Terraform text that describes variables passed to a given module instance

Parameters:

  • variables (Hash)

    module variables and their values, :name and :outputs are ignored as they are not module variables themselves

Returns:

  • (String)

    text to include inside the terraform module definition containing the variable assignments



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/scout/terraform_dsl.rb', line 124

def variable_block(variables)
  variables.each_with_object([]) do |p, acc|
    name, value = p
    next acc if name.to_s == 'name'
    next acc if name.to_s == 'outputs'

    if value.is_a?(String) && (m = value.match(/^module\.(.*)\.(.*)/))
      value = Module::Output.new m[1], m[2]
    end

    acc << "  #{name} = #{value.to_json}"
  end * "\n"
end