From 051c8bf612126fa79699c8bf45a661dde127f4a0 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 16 Mar 2018 08:51:54 +0000 Subject: Add backend controllers, models and views Also annotate existing models. --- app/models/backends/terraform_aws.rb | 77 ++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 app/models/backends/terraform_aws.rb (limited to 'app/models/backends/terraform_aws.rb') diff --git a/app/models/backends/terraform_aws.rb b/app/models/backends/terraform_aws.rb new file mode 100644 index 0000000..d79ff51 --- /dev/null +++ b/app/models/backends/terraform_aws.rb @@ -0,0 +1,77 @@ +# == Schema Information +# +# Table name: terraform_aws_backends +# +# id :integer not null, primary key +# label :string +# aws_region :string +# aws_access_key_id :string +# aws_secret_access_key :string +# created_at :datetime not null +# updated_at :datetime not null +# + +class Backends::TerraformAws < ApplicationRecord + has_many :mini_environments, as: :backend + + self.table_name = "terraform_aws_backends" + + def self.label + "Amazon Web Services" + end + + def type_and_id + "#{self.class.name}=#{id}" + end + + def build(mini_environment) + GovukGuix::GenerateStartCommandJob.enqueue( + mini_environment.id + ) + end + + def start + 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" + ) + + 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 + }, + auto_approve: true + ) + end + end + + def destroy + Dir.chdir('terraform/aws') do + RubyTerraform.destroy( + vars: { + aws_region: 'eu-west-1', + slug: @mini_environment.name.parameterize, + ssh_public_key: ssh_public_key, + start_command: @mini_environment.start_command + }, + force: true + ) + end + end + + def ssh_public_key + File.open("#{ENV['HOME']}/.ssh/id_rsa.pub") do |file| + file.readline() + end + end +end -- cgit v1.2.3