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
# File 'lib/tflat.rb', line 11

def initialize(args:, directory: '.')
  @args = args.join(' ')
  @directory = directory
  @all_files = Dir.glob("#{directory}/**/*").select{|e| File.file?(e) && !e.match(/^\.tflat\/.+$/)}
  @destination = "#{directory}/.tflat"
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



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

def execute
  exec "cd .tflat && terraform #{args}"
end

#f(file) ⇒ Object



74
75
76
# File 'lib/tflat.rb', line 74

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

#file(file) ⇒ Object



78
79
80
# File 'lib/tflat.rb', line 78

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

#file_sha256(ff) ⇒ Object



82
83
84
85
# File 'lib/tflat.rb', line 82

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

#flatten_directoriesObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tflat.rb', line 46

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 "- [tflat] Skipping: #{entry}"
      next
    end
    FileUtils.cp(entry, ".tflat/#{new_name}")
  end
end

#parse_erbObject



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

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

#read_variablesObject



41
42
43
44
# File 'lib/tflat.rb', line 41

def read_variables
  return unless File.file?('terraform.tfvars.json')
  @variables = JSON.parse(File.read 'terraform.tfvars.json')
end

#render(file) ⇒ Object



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

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

#run!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tflat.rb', line 17

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

#setupObject



33
34
35
36
37
38
39
# File 'lib/tflat.rb', line 33

def setup
  FileUtils.mkdir_p(destination)
  Dir.glob('.tflat/*').each do |entry|
    next unless File.file?(entry)
    FileUtils.rm_f entry
  end
end