diff options
Diffstat (limited to 'app/models/backends/terraform_aws.rb')
-rw-r--r-- | app/models/backends/terraform_aws.rb | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/app/models/backends/terraform_aws.rb b/app/models/backends/terraform_aws.rb index d79ff51..246af8b 100644 --- a/app/models/backends/terraform_aws.rb +++ b/app/models/backends/terraform_aws.rb @@ -12,6 +12,8 @@ # class Backends::TerraformAws < ApplicationRecord + include Backends::Terraform + has_many :mini_environments, as: :backend self.table_name = "terraform_aws_backends" @@ -30,26 +32,26 @@ class Backends::TerraformAws < ApplicationRecord ) end - def start - logger.info "Setting up #{@mini_environment.name}" + def start(mini_environment) + logger.info "Setting up #{mini_environment.name}" - Dir.chdir("tmp/terraform-working-directories/#{@mini_environment.name}") do # TODO - RubyTerraform.init( - backend: true, - source: "terraform/aws", # TODO - backend_config: { - address: 'http://localhost:3000' + Rails.application.routes.url_helpers.terraform_http_backend_path - }, - plugin_dir: "/gnu/store/x0b54k4i02vi05ghc0np7cqs2p5q6i31-profile/bin" - ) + 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 RubyTerraform.apply( - vars: { - aws_region: 'eu-west-1', - slug: @mini_environment.name.parameterize, - ssh_public_key: ssh_public_key, - start_command: @mini_environment.start_command - }, + vars: terraform_variables(mini_environment), auto_approve: true ) end @@ -59,7 +61,7 @@ class Backends::TerraformAws < ApplicationRecord Dir.chdir('terraform/aws') do RubyTerraform.destroy( vars: { - aws_region: 'eu-west-1', + aws_region: aws_region, slug: @mini_environment.name.parameterize, ssh_public_key: ssh_public_key, start_command: @mini_environment.start_command @@ -69,6 +71,15 @@ class Backends::TerraformAws < ApplicationRecord end end + def terraform_variables(mini_environment) + mini_environment.backend_data.merge( + aws_region: self.aws_region, + slug: mini_environment.name.parameterize, + ssh_public_key: ssh_public_key, + start_command: mini_environment.start_command, + ) + end + def ssh_public_key File.open("#{ENV['HOME']}/.ssh/id_rsa.pub") do |file| file.readline() |