Class: Dome::Terraform

Inherits:
Object
  • Object
show all
Includes:
Shell
Defined in:
lib/dome/terraform.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shell

#execute_command

Constructor Details

#initializeTerraform

Returns a new instance of Terraform.



7
8
9
10
11
# File 'lib/dome/terraform.rb', line 7

def initialize
  @environment = Dome::Environment.new
  @state       = Dome::State.new(@environment)
  @plan_file   = "plans/#{@environment.}-#{@environment.environment}-plan.tf"
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#applyObject



31
32
33
34
35
# File 'lib/dome/terraform.rb', line 31

def apply
  command         = "terraform apply #{@plan_file}"
  failure_message = 'something went wrong when applying the TF plan'
  execute_command(command, failure_message)
end

#create_planObject



37
38
39
40
41
# File 'lib/dome/terraform.rb', line 37

def create_plan
  command         = "terraform plan -module-depth=1 -refresh=true -out=#{@plan_file} -var-file=params/env.tfvars"
  failure_message = 'something went wrong when creating the TF plan'
  execute_command(command, failure_message)
end

#delete_plan_fileObject



50
51
52
53
54
# File 'lib/dome/terraform.rb', line 50

def delete_plan_file
  puts 'Deleting older terraform plan ...'.colorize(:green)
  puts "About to delete: #{@plan_file}"
  FileUtils.rm_f @plan_file
end

#delete_terraform_directoryObject



43
44
45
46
47
48
# File 'lib/dome/terraform.rb', line 43

def delete_terraform_directory
  puts 'Deleting older terraform module cache dir ...'.colorize(:green)
  terraform_directory = '.terraform'
  puts "About to delete directory: #{terraform_directory}"
  FileUtils.rm_rf '.terraform/'
end

#install_terraform_modulesObject



56
57
58
59
60
# File 'lib/dome/terraform.rb', line 56

def install_terraform_modules
  command         = 'terraform get -update=true'
  failure_message = 'something went wrong when pulling remote TF modules'
  execute_command(command, failure_message)
end

#outputObject



62
63
64
65
66
# File 'lib/dome/terraform.rb', line 62

def output
  command         = 'terraform output'
  failure_message = 'something went wrong when printing TF output variables'
  execute_command(command, failure_message)
end

#planObject



23
24
25
26
27
28
29
# File 'lib/dome/terraform.rb', line 23

def plan
  delete_terraform_directory
  delete_plan_file
  install_terraform_modules
  @state.synchronise_s3_state
  create_plan
end

#validate_environmentObject



13
14
15
16
17
18
19
20
21
# File 'lib/dome/terraform.rb', line 13

def validate_environment
  environment = @environment.environment
       = @environment.
  @environment. unless @environment.valid_account? 
  @environment.invalid_environment_message unless @environment.valid_environment?(, environment)
  puts "Team: #{@environment.team.colorize(:green)}"
  puts '----------------------------------------------------------------'
  @environment.populate_aws_access_keys
end