diff options
author | Christopher Baines <mail@cbaines.net> | 2018-03-26 21:14:18 +0100 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2018-04-03 21:57:39 +0100 |
commit | 037054d45200fc03576fc86be6dff39235c3d9d0 (patch) | |
tree | 4d0e080fc3ee716dec3966ddc7cf153a859dca34 /app | |
parent | 0867e8beef3f964cfd42d4e86c0ddf468bc980c5 (diff) | |
download | govuk-mini-environment-admin-037054d45200fc03576fc86be6dff39235c3d9d0.tar govuk-mini-environment-admin-037054d45200fc03576fc86be6dff39235c3d9d0.tar.gz |
Refactor the backends
Add a with_terraform helper, which simplifies both backends.
Diffstat (limited to 'app')
-rw-r--r-- | app/models/backends/terraform.rb | 22 | ||||
-rw-r--r-- | app/models/backends/terraform_aws.rb | 27 | ||||
-rw-r--r-- | app/models/backends/terraform_libvirt.rb | 20 |
3 files changed, 30 insertions, 39 deletions
diff --git a/app/models/backends/terraform.rb b/app/models/backends/terraform.rb index 896876d..06408e0 100644 --- a/app/models/backends/terraform.rb +++ b/app/models/backends/terraform.rb @@ -1,8 +1,30 @@ +require 'fileutils' + module Backends::Terraform def working_directory(mini_environment) "tmp/terraform-working-directories/#{mini_environment.name}" end + def with_terraform(mini_environment, source) + source = File.expand_path(source) + FileUtils.mkdir_p working_directory(mini_environment) + + Dir.chdir(working_directory(mini_environment)) do + if Dir.empty?('.') + RubyTerraform.init( + backend: true, + from_module: source, + backend_config: { + address: terraform_backend_address(mini_environment) + }, + plugin_dir: plugin_dir + ) + end + + yield + end + end + def terraform_backend_address(mini_environment) ( 'http://localhost:3000' + diff --git a/app/models/backends/terraform_aws.rb b/app/models/backends/terraform_aws.rb index 7f908e8..ec4f7ab 100644 --- a/app/models/backends/terraform_aws.rb +++ b/app/models/backends/terraform_aws.rb @@ -35,21 +35,7 @@ class Backends::TerraformAws < ApplicationRecord def start(mini_environment) logger.info "Setting up #{mini_environment.name}" - source = File.expand_path("terraform/libvirt") - FileUtils.mkdir_p working_directory(mini_environment) - - Dir.chdir(working_directory(mini_environment)) do - if Dir.empty?(".") - RubyTerraform.init( - backend: true, - from_module: source, - backend_config: { - address: terraform_backend_address(mini_environment) - }, - plugin_dir: plugin_dir - ) - end - + with_terraform(mini_environment, 'terraform/aws') do RubyTerraform.apply( vars: terraform_variables(mini_environment), auto_approve: true @@ -57,15 +43,10 @@ class Backends::TerraformAws < ApplicationRecord end end - def destroy - Dir.chdir('terraform/aws') do + def destroy(mini_environment) + with_terraform(mini_environment, 'terraform/aws') do RubyTerraform.destroy( - vars: { - aws_region: aws_region, - slug: @mini_environment.name.parameterize, - ssh_public_key: ssh_public_key, - start_command: @mini_environment.start_command - }, + vars: terraform_variables(mini_environment), force: true ) end diff --git a/app/models/backends/terraform_libvirt.rb b/app/models/backends/terraform_libvirt.rb index 3e5c138..e9982e0 100644 --- a/app/models/backends/terraform_libvirt.rb +++ b/app/models/backends/terraform_libvirt.rb @@ -10,6 +10,8 @@ # class Backends::TerraformLibvirt < ApplicationRecord + include Backends::Terraform + has_many :mini_environments, as: :backend self.table_name = 'terraform_libvirt_backends' @@ -31,21 +33,7 @@ class Backends::TerraformLibvirt < ApplicationRecord def start(mini_environment) logger.info "Setting up #{mini_environment.name}" - source = File.expand_path("terraform/libvirt") - FileUtils.mkdir_p working_directory(mini_environment) - - Dir.chdir(working_directory(mini_environment)) do - if Dir.empty?(".") - RubyTerraform.init( - backend: true, - from_module: source, - backend_config: { - address: terraform_backend_address(mini_environment) - }, - plugin_dir: plugin_dir - ) - end - + with_terraform(mini_environment, 'terraform/libvirt') do RubyTerraform.apply( vars: terraform_variables(mini_environment), auto_approve: true @@ -54,7 +42,7 @@ class Backends::TerraformLibvirt < ApplicationRecord end def destroy(mini_environment) - Dir.chdir(working_directory(mini_environment)) do + with_terraform(mini_environment, 'terraform/libvirt') do RubyTerraform.destroy( vars: terraform_variables(mini_environment), force: true |