module TFW

TFW is a Terraform Wrapper which uses terraform DSL for Ruby

Constants

LIB_DIR
VERSION
WORKSPACE

Public Instance Methods

as_json?() click to toggle source
# File lib/tfw.rb, line 23
def as_json?
  !ENV['TFW_AS_JSON'].nil?
end
build_config() click to toggle source
# File lib/tfw.rb, line 43
def build_config
  FileUtils.mkdir_p WORKSPACE
  stack = TFW.get_stack_for_dir '.'
  stack_file = "#{WORKSPACE}/stack.tf"
  write_stack_file stack_file, stack
end
cli(args) click to toggle source
# File lib/tfw.rb, line 37
def cli(args)
  build_config
  State.instance.reset
  run_terraform args
end
configure_input_method(input) click to toggle source
# File lib/tfw.rb, line 114
def configure_input_method(input)
  silent_block do
    # This will trigger warnings as we are redefining methods
    TOPLEVEL_BINDING.eval('self').define_singleton_method('tfw_module_input') { input }
  end
end
configure_methods_using_stack(stack) click to toggle source
# File lib/tfw.rb, line 103
def configure_methods_using_stack(stack)
  silent_block do
    # This will trigger warnings as we are redefining methods
    %w[provider variable locals tfmodule datasource resource output terraform].each do |name|
      TOPLEVEL_BINDING.eval('self').define_singleton_method name do |*args, &block|
        stack.method(name).call(*args, &block)
      end
    end
  end
end
get_stack_for_dir(dir, input = nil, stack = State.instance.stack) click to toggle source
# File lib/tfw.rb, line 27
def get_stack_for_dir(dir, input = nil, stack = State.instance.stack)
  configure_methods_using_stack stack
  configure_input_method input

  files = Dir.glob "#{dir}/*.rb"
  files.sort.each { |f| load f }
  configure_methods_using_stack State.instance.stack
  stack
end
load_module(stack, &block) click to toggle source
# File lib/tfw.rb, line 73
def load_module(stack, &block)
  t = TFW::Module.new do
    instance_eval(&block) if block_given?
  end

  mstack = t.instance_variable_get '@stack'
  mname = t.instance_variable_get '@name'
  mpath = "./modules/#{mname}"

  FileUtils.mkdir_p "#{WORKSPACE}/#{mpath}"

  stack_file = "#{WORKSPACE}/#{mpath}/stack.tf"
  write_stack_file stack_file, mstack

  stack.tfmodule mname do
    source mpath
  end
end
pretty_json(json) click to toggle source
# File lib/tfw.rb, line 121
def pretty_json(json)
  JSON.pretty_generate JSON.parse(json)
end
run_terraform(args) click to toggle source
# File lib/tfw.rb, line 50
def run_terraform(args)
  old_dir = Dir.pwd
  Dir.chdir WORKSPACE

  cmd = "terraform #{args.join ' '}"
  pid = fork { exec cmd }
  Dir.chdir old_dir
  trap_pids [pid]
  Process.wait pid
  $?.exitstatus
end
trap_pids(pids) click to toggle source
# File lib/tfw.rb, line 62
def trap_pids(pids)
  %w[SIGINT SIGTERM].each do |sig|
    Signal.trap sig do
      pids.each { |pid| Process.kill sig, pid }
      Process.waitall
      puts "ERROR: TFW received and forwarded #{sig} to terraform"
      exit 2
    end
  end
end
write_stack_file(stack_file, stack) click to toggle source
# File lib/tfw.rb, line 92
def write_stack_file(stack_file, stack)
  [stack_file, "#{stack_file}.json"].each { |f| FileUtils.rm_f f }

  if as_json?
    stack_file = "#{stack_file}.json"
    File.write stack_file, pretty_json(AwsSgWorkaround.fix(stack.to_json))
  else
    File.write stack_file, stack
  end
end