aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/backends/terraform_aws.rb13
-rw-r--r--app/models/backends/terraform_libvirt.rb13
-rw-r--r--config/application.rb2
-rw-r--r--config/routes.rb8
-rw-r--r--lib/terraform_working_directory.rb (renamed from app/models/backends/terraform.rb)27
5 files changed, 39 insertions, 24 deletions
diff --git a/app/models/backends/terraform_aws.rb b/app/models/backends/terraform_aws.rb
index 98b1f41..45c766b 100644
--- a/app/models/backends/terraform_aws.rb
+++ b/app/models/backends/terraform_aws.rb
@@ -35,8 +35,6 @@
require 'ruby_terraform'
class Backends::TerraformAws < ApplicationRecord
- include Backends::Terraform
-
has_many :mini_environments, as: :backend
self.table_name = 'terraform_aws_backends'
@@ -65,7 +63,7 @@ class Backends::TerraformAws < ApplicationRecord
def start(mini_environment)
logger.info "Setting up #{mini_environment.name}"
- with_terraform(mini_environment, 'terraform/aws') do
+ within_terraform_working_directory(mini_environment) do
RubyTerraform.apply(
vars: terraform_variables(mini_environment),
auto_approve: true
@@ -74,7 +72,7 @@ class Backends::TerraformAws < ApplicationRecord
end
def destroy(mini_environment)
- with_terraform(mini_environment, 'terraform/aws') do
+ within_terraform_working_directory(mini_environment) do
RubyTerraform.destroy(
vars: terraform_variables(mini_environment),
force: true
@@ -82,6 +80,13 @@ class Backends::TerraformAws < ApplicationRecord
end
end
+ def within_terraform_working_directory(mini_environment, &block)
+ TerraformWorkingDirectory.new(
+ "mini_environment/#{mini_environment.id}",
+ 'terraform/aws'
+ ).within_working_directory(&block)
+ end
+
def signon_url(mini_environment)
"https://signon.#{mini_environment.name.parameterize}.#{domain}"
end
diff --git a/app/models/backends/terraform_libvirt.rb b/app/models/backends/terraform_libvirt.rb
index 147c27d..f108560 100644
--- a/app/models/backends/terraform_libvirt.rb
+++ b/app/models/backends/terraform_libvirt.rb
@@ -33,8 +33,6 @@
require 'ruby_terraform'
class Backends::TerraformLibvirt < ApplicationRecord
- include Backends::Terraform
-
has_many :mini_environments, as: :backend
self.table_name = 'terraform_libvirt_backends'
@@ -65,7 +63,7 @@ class Backends::TerraformLibvirt < ApplicationRecord
def start(mini_environment)
logger.info "Setting up #{mini_environment.name}"
- with_terraform(mini_environment, 'terraform/libvirt') do
+ within_terraform_working_directory(mini_environment) do
RubyTerraform.apply(
vars: terraform_variables(mini_environment),
auto_approve: true
@@ -74,7 +72,7 @@ class Backends::TerraformLibvirt < ApplicationRecord
end
def destroy(mini_environment)
- with_terraform(mini_environment, 'terraform/libvirt') do
+ within_terraform_working_directory(mini_environment) do
RubyTerraform.destroy(
vars: terraform_variables(mini_environment),
force: true
@@ -82,6 +80,13 @@ class Backends::TerraformLibvirt < ApplicationRecord
end
end
+ def within_terraform_working_directory(mini_environment, &block)
+ TerraformWorkingDirectory.new(
+ "mini_environment/#{mini_environment.id}",
+ 'terraform/libvirt'
+ ).within_working_directory(&block)
+ end
+
def signon_url(mini_environment)
"https://signon.#{mini_environment.name.parameterize}.#{domain}"
end
diff --git a/config/application.rb b/config/application.rb
index a7c23fb..ab4fa46 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -14,5 +14,7 @@ module GovukMiniEnvironmentAdmin
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
+
+ config.autoload_paths << "#{Rails.root}/lib"
end
end
diff --git a/config/routes.rb b/config/routes.rb
index 51e5add..e706194 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -32,11 +32,11 @@ Rails.application.routes.draw do
end
end
+ resources :terraform_http_backend,
+ controller: :terraform_http_backend,
+ except: %i(new edit update)
+
resources :mini_environments, path: '/' do
post '/', to: 'mini_environments#perform_action', as: 'perform_action'
-
- resource :terraform_http_backend,
- controller: :terraform_http_backend,
- except: %i[new edit update]
end
end
diff --git a/app/models/backends/terraform.rb b/lib/terraform_working_directory.rb
index b3c6ee7..b561edf 100644
--- a/app/models/backends/terraform.rb
+++ b/lib/terraform_working_directory.rb
@@ -20,22 +20,27 @@
require 'fileutils'
-module Backends::Terraform
- def working_directory(mini_environment)
- "tmp/terraform-working-directories/#{mini_environment.name}"
+class TerraformWorkingDirectory
+ def initialize(id, source)
+ @id = id
+ @source = source
end
- def with_terraform(mini_environment, source)
- source = File.expand_path(source)
- FileUtils.mkdir_p working_directory(mini_environment)
+ def working_directory
+ "tmp/terraform-working-directories/#{@id}"
+ end
+
+ def within_working_directory
+ source = File.expand_path(@source)
+ FileUtils.mkdir_p working_directory
- Dir.chdir(working_directory(mini_environment)) do
+ Dir.chdir(working_directory) do
if Dir.empty?('.')
RubyTerraform.init(
backend: true,
from_module: source,
backend_config: {
- address: terraform_backend_address(mini_environment)
+ address: terraform_backend_address
},
plugin_dir: terraform_plugin_dir
)
@@ -45,16 +50,14 @@ module Backends::Terraform
end
end
- def terraform_backend_address(mini_environment)
+ def terraform_backend_address
(
'http://localhost:3000' +
Rails
.application
.routes
.url_helpers
- .mini_environment_terraform_http_backend_path(
- mini_environment
- )
+ .terraform_http_backend_path(@id)
)
end