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
12
# File 'lib/dome/terraform.rb', line 7

def initialize
  @environment = Dome::Environment.new
  @secrets     = Dome::Secrets.new(@environment)
  @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



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

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

#create_planObject



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

def create_plan
  @secrets.secret_env_vars
  @secrets.extract_certs
  command         = "terraform plan -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



70
71
72
73
74
# File 'lib/dome/terraform.rb', line 70

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



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

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



76
77
78
79
80
# File 'lib/dome/terraform.rb', line 76

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



82
83
84
85
86
# File 'lib/dome/terraform.rb', line 82

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

#planObject



40
41
42
43
44
45
46
# File 'lib/dome/terraform.rb', line 40

def plan
  delete_terraform_directory
  delete_plan_file
  install_terraform_modules
  @state.s3_state
  create_plan
end

#validate_environmentObject

TODO: this method is a bit of a mess and needs looking at rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dome/terraform.rb', line 17

def validate_environment
  puts 'Initialising domed-city...'
  puts "* Your 'account' and 'environment' are assigned based on your current directory. "\
      "The expected directory structure is 'terraform/<account>/<environment>'".colorize(:yellow)
  puts "* Your 'project' is defined using the 'project' key in your 'itv.yaml'.".colorize(:yellow)
  puts "* Valid environments are defined using the 'environments' key in your 'itv.yaml'. "\
    "You have defined: #{@environment.environments}".colorize(:yellow)
  puts '* Valid accounts are of the format <project>-dev and <project>-prd and are calculated '\
    "automatically using your 'project' variable.".colorize(:yellow)

  puts "\nDebug output:\n------------"
  environment = @environment.environment
       = @environment.
  @environment. unless @environment.valid_account? 
  @environment.invalid_environment_message unless @environment.valid_environment? environment
  puts "Project: #{@environment.project.colorize(:green)}"
  puts "State S3 bucket name: #{@state.state_bucket_name.colorize(:green)}"
  puts "State file name: #{@state.state_file_name.colorize(:green)}"
  @environment.unset_aws_keys
  @environment.aws_credentials
  puts '----------------------------------------------------------------'
end