Class: Kite::Terraform

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

Instance Method Summary collapse

Constructor Details

#initialize(core, options) ⇒ Terraform

Returns a new instance of Terraform.



4
5
6
7
# File 'lib/kite/terraform.rb', line 4

def initialize(core, options)
  @core = core
  @env_name = options[:env]
end

Instance Method Details

#cloudObject



36
37
38
# File 'lib/kite/terraform.rb', line 36

def cloud
  YAML.load(File.read('config/cloud.yml'))[@env_name]
end

#load_cloudObject



40
41
42
43
44
# File 'lib/kite/terraform.rb', line 40

def load_cloud
  cloud.each do |k, v|
    (v.is_a? Hash) ? @vars.merge!(v) : @vars[k] = v
  end
end

#load_envObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kite/terraform.rb', line 23

def load_env
  load_vars
  load_cloud
  @vars.each do |var, val|
    key = "TF_VAR_#{var}"
    ENV[key] = val.to_s
    STDERR.puts "%-25s: %s" % [key, ENV["TF_VAR_#{var}"]]
  end

  # TODO: Need to be set only in case of GCP
  # ENV['GOOGLE_APPLICATION_CREDENTIALS'] = @vars["credentials"]
end

#load_varsObject



46
47
48
49
50
51
52
53
# File 'lib/kite/terraform.rb', line 46

def load_vars
  @vars = Hash.new
  vars_files = Dir["config/environments/#{@env_name}/vars.*.yml"]
  vars_files.each do |f|
    tf_vars = YAML.load(File.read(f))['terraform']
    @vars.merge!(tf_vars) unless tf_vars.nil?
  end
end

#run(command, *args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kite/terraform.rb', line 9

def run(command, *args)
  exit_status = 0
  STDERR.puts "Loading env"
  load_env
  script = "terraform #{command} #{args.join " "}"
  STDERR.puts script

  Dir.chdir("config/environments/#{@env_name}") do
    system(script)
    exit_status =  $?.exitstatus
  end
  exit_status
end