Class: Tflat::Terraform

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args:, directory: '.') ⇒ Terraform

Returns a new instance of Terraform.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tflat.rb', line 11

def initialize(args:, directory: '.')
  @workspace = ENV['TFLAT_WORKSPACE']
  @args = args.join(' ')
  @directory = directory
  @all_files = Dir.glob("#{directory}/**/*").select{|e| File.file?(e) && !e.match(/^\.tflat.*\/.+$/)}
  if @workspace
    @destination = "#{directory}/.tflat.#{@workspace}"
    @tag = "[tflat | #{@workspace}]"
    @tflat_folder = ".tflat.#{@workspace}"
  else
    @destination = "#{directory}/.tflat"
    @tag = "[tflat]"
    @tflat_folder = ".tflat"
  end

end

Instance Attribute Details

#all_filesObject

Returns the value of attribute all_files.



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

def all_files
  @all_files
end

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

#destinationObject

Returns the value of attribute destination.



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

def destination
  @destination
end

#directoryObject

Returns the value of attribute directory.



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

def directory
  @directory
end

Instance Method Details

#executeObject



107
108
109
# File 'lib/tflat.rb', line 107

def execute
  exec "cd #{@tflat_folder} && terraform #{args}"
end

#f(file) ⇒ Object



89
90
91
# File 'lib/tflat.rb', line 89

def f(file)
  file.sub(/^\.\//, "").gsub('/', '#')
end

#file(file) ⇒ Object



93
94
95
# File 'lib/tflat.rb', line 93

def file(file)
  render(file).inspect[1...-1]
end

#file_sha256(ff) ⇒ Object



97
98
99
100
# File 'lib/tflat.rb', line 97

def file_sha256(ff)
  f = file(ff)
  Digest::SHA256.hexdigest(f)
end

#flatten_directoriesObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tflat.rb', line 61

def flatten_directories
  all_files.each do |entry|
    next if entry =~ /#/
    new_name = entry.sub(/^#{directory}\//,'').gsub('/', '#')
    if new_name =~ /^#/ # Skip files/directories that start with a hash sign
      puts "- #{@tag} Skipping: #{entry}"
      next
    end
    FileUtils.cp(entry, "#{@tflat_folder}/#{new_name}")
  end
end

#parse_erbObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tflat.rb', line 73

def parse_erb
  Dir.glob("#{@tflat_folder}/*").sort.each do |entry|
    next unless File.file?(entry)
    next if File.binary?(entry)
    puts "- #{@tag} -> #{entry}"
    begin
      rendered = render(entry)
    rescue Exception => e
      puts "- #{@tag} ERROR: Could not parse ERB on file #{entry}"
      puts e.message
      exit 1
    end
    File.write(entry, rendered)
  end
end

#read_variablesObject



51
52
53
54
55
56
57
58
59
# File 'lib/tflat.rb', line 51

def read_variables
  return unless File.file?('terraform.tfvars.json')
  @variables = JSON.parse(File.read 'terraform.tfvars.json')
  # Env vars TF_VAR_* will overwrite anything set on the JSON file.
  ENV.select{|k,v| k =~ /^TF_VAR_.+$/}.each do |k,v|
    name = k.sub('TF_VAR_', '')
    @variables[name] = v
  end
end

#render(file) ⇒ Object



102
103
104
105
# File 'lib/tflat.rb', line 102

def render(file)
  template = File.read(file)
  ERB.new(template).result(binding)
end

#run!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tflat.rb', line 27

def run!
  if args.empty?
    puts `terraform`
    puts "\n\n- #{@tag} Notice: You must run tflat with terraform arguments.\n\n"
    return
  end
  setup
  read_variables
  puts "- #{@tag} Generating files"
  flatten_directories
  parse_erb
  puts " done!"
  puts "- #{@tag} Running: terraform #{args}"
  execute
end

#setupObject



43
44
45
46
47
48
49
# File 'lib/tflat.rb', line 43

def setup
  FileUtils.mkdir_p(destination)
  Dir.glob("#{@tflat_folder}/*").each do |entry|
    next unless File.file?(entry)
    FileUtils.rm_f entry
  end
end