diff options
author | Christopher Baines <mail@cbaines.net> | 2018-03-16 08:51:54 +0000 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2018-03-29 07:55:01 +0100 |
commit | 051c8bf612126fa79699c8bf45a661dde127f4a0 (patch) | |
tree | 849a3b05b4ab4b59a426761d7cf1da3cc7036349 /app/models/backends/terraform_aws.rb | |
parent | a8c8f68971dd9e20dee01d9f65c64283e41fe4a3 (diff) | |
download | govuk-mini-environment-admin-051c8bf612126fa79699c8bf45a661dde127f4a0.tar govuk-mini-environment-admin-051c8bf612126fa79699c8bf45a661dde127f4a0.tar.gz |
Add backend controllers, models and views
Also annotate existing models.
Diffstat (limited to 'app/models/backends/terraform_aws.rb')
-rw-r--r-- | app/models/backends/terraform_aws.rb | 77 |
1 files changed, 77 insertions, 0 deletions
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 |