Class: Dome::Terraform
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #apply ⇒ Object
- #create_plan ⇒ Object
- #delete_plan_file ⇒ Object
- #delete_terraform_directory ⇒ Object
-
#initialize ⇒ Terraform
constructor
A new instance of Terraform.
- #install_terraform_modules ⇒ Object
- #output ⇒ Object
- #plan ⇒ Object
-
#validate_environment ⇒ Object
TODO: this method is a bit of a mess and needs looking at rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize.
Methods included from Shell
Constructor Details
#initialize ⇒ Terraform
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.account}-#{@environment.environment}-plan.tf" end |
Instance Attribute Details
#state ⇒ Object (readonly)
Returns the value of attribute state.
5 6 7 |
# File 'lib/dome/terraform.rb', line 5 def state @state end |
Instance Method Details
#apply ⇒ Object
48 49 50 51 52 53 |
# File 'lib/dome/terraform.rb', line 48 def apply @secrets.secret_env_vars command = "terraform apply #{@plan_file}" = 'something went wrong when applying the TF plan' execute_command(command, ) end |
#create_plan ⇒ Object
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" = 'something went wrong when creating the TF plan' execute_command(command, ) end |
#delete_plan_file ⇒ Object
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_directory ⇒ Object
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_modules ⇒ Object
76 77 78 79 80 |
# File 'lib/dome/terraform.rb', line 76 def install_terraform_modules command = 'terraform get -update=true' = 'something went wrong when pulling remote TF modules' execute_command(command, ) end |
#output ⇒ Object
82 83 84 85 86 |
# File 'lib/dome/terraform.rb', line 82 def output command = 'terraform output' = 'something went wrong when printing TF output variables' execute_command(command, ) end |
#plan ⇒ Object
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_environment ⇒ Object
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 account = @environment.account @environment. unless @environment.valid_account? account @environment. 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 |