diff options
author | Christopher Baines <mail@cbaines.net> | 2018-06-23 11:40:52 +0100 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2018-06-23 21:16:39 +0100 |
commit | 7bc196dabd3186793880c6d833392e8d8da3d191 (patch) | |
tree | 027f3faffdfeda646cb4535b0df8c59e338c68a3 | |
parent | 76f7351c19777379d4d42a348a7f8da735a30649 (diff) | |
download | govuk-mini-environment-admin-7bc196dabd3186793880c6d833392e8d8da3d191.tar govuk-mini-environment-admin-7bc196dabd3186793880c6d833392e8d8da3d191.tar.gz |
Add support for managing Terraform AWS backends from the show page
-rw-r--r-- | app/controllers/backends/terraform_aws_controller.rb | 21 | ||||
-rw-r--r-- | app/jobs/backends/terraform_aws_job.rb | 46 | ||||
-rw-r--r-- | app/models/backends/terraform_aws.rb | 3 | ||||
-rw-r--r-- | app/models/backends/terraform_aws/backend_methods.rb | 20 | ||||
-rw-r--r-- | app/views/backends/terraform_aws/show.html.erb | 51 | ||||
-rw-r--r-- | config/routes.rb | 6 | ||||
-rw-r--r-- | terraform/aws/backend/main.tf | 4 |
7 files changed, 150 insertions, 1 deletions
diff --git a/app/controllers/backends/terraform_aws_controller.rb b/app/controllers/backends/terraform_aws_controller.rb index bffac9d..f11403f 100644 --- a/app/controllers/backends/terraform_aws_controller.rb +++ b/app/controllers/backends/terraform_aws_controller.rb @@ -55,6 +55,27 @@ class Backends::TerraformAwsController < ApplicationController redirect_to setup_path end + def perform_action + @backend = Backends::TerraformAws.find(params['id']) + + action = params.require(:commit) + + case action + when 'Destroy' + Backends::TerraformAwsJob.enqueue(@backend.id, :destroy_backend) + + flash[:notice] = 'Destroying the backend' + when 'Deploy' + Backends::TerraformAwsJob.enqueue(@backend.id, :deploy_backend) + + flash[:notice] = 'Deploying the backend' + else + flash[:error] = "Unknown action #{action}" + end + + render 'show' + end + private def create_params diff --git a/app/jobs/backends/terraform_aws_job.rb b/app/jobs/backends/terraform_aws_job.rb new file mode 100644 index 0000000..8a542fc --- /dev/null +++ b/app/jobs/backends/terraform_aws_job.rb @@ -0,0 +1,46 @@ +# GOV.UK Mini Environment Admin +# Copyright © 2018 Christopher Baines <mail@cbaines.net> +# +# This file is part of the GOV.UK Mini Environment Admin. +# +# The GOV.UK Mini Environment Admin is free software: you can +# redistribute it and/or modify it under the terms of the GNU Affero +# General Public License as published by the Free Software Foundation, +# either version 3 of the License, or (at your option) any later +# version. +# +# The GOV.UK Mini Environment Admin is distributed in the hope that it +# will be useful, but WITHOUT ANY WARRANTY; without even the implied +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with the GOV.UK Mini Environment Admin. If not, see +# <http://www.gnu.org/licenses/>. + +class Backends::TerraformAwsJob < Que::Job + def run(terraform_aws_backend_id, action) + ActiveRecord::Base.transaction do + @backend = Backends::TerraformAws.find( + terraform_aws_backend_id + ) + + @backend.send(action) + + finish + end + end + + def self.job_title(que_job) + que_job.args.last.titleize + end + + def self.jobs(terraform_aws_backend_id) + QueJob + .where( + job_class: name + ).where( + "args->>0 = '#{terraform_aws_backend_id}'" + ) + end +end diff --git a/app/models/backends/terraform_aws.rb b/app/models/backends/terraform_aws.rb index 4db7c51..d0655ee 100644 --- a/app/models/backends/terraform_aws.rb +++ b/app/models/backends/terraform_aws.rb @@ -39,6 +39,9 @@ require 'ruby_terraform' class Backends::TerraformAws < ApplicationRecord + include MiniEnvironmentMethods + include BackendMethods + has_many :mini_environments, as: :backend self.table_name = 'terraform_aws_backends' diff --git a/app/models/backends/terraform_aws/backend_methods.rb b/app/models/backends/terraform_aws/backend_methods.rb index 726494c..8c867d9 100644 --- a/app/models/backends/terraform_aws/backend_methods.rb +++ b/app/models/backends/terraform_aws/backend_methods.rb @@ -70,4 +70,24 @@ module Backends::TerraformAws::BackendMethods def available_data_snapshots GovukGuix::DataSnapshot.where(backend: self) end + + def backend_terraform_states + TerraformState.where( + state_id: terraform_state_id + ) + end + + def status + latest_terraform_state = backend_terraform_states.order(:id).last + + running = ( + latest_terraform_state && + (latest_terraform_state.output_value('backend_up') == 'true') + ) + + { + running: running, + updated_at: latest_terraform_state.try(:created_at) + } + end end diff --git a/app/views/backends/terraform_aws/show.html.erb b/app/views/backends/terraform_aws/show.html.erb index 3a1b329..eca5d3f 100644 --- a/app/views/backends/terraform_aws/show.html.erb +++ b/app/views/backends/terraform_aws/show.html.erb @@ -27,6 +27,57 @@ License along with the GOV.UK Mini Environment Admin. If not, see </a> <h1>Backend: <%= @backend.label %></h1> +<% status = @backend.status %> + +<div class="row"> + <div class="col-md-2"> + <%= form_with( + url: perform_action_terraform_aws_backend_path(@backend), + local: true, + method: "post" + ) do %> + <%= submit_tag('Deploy', + role: 'button', + class: 'btn btn-lg btn-success') + %> + <% end %> + + <%= form_with( + url: perform_action_terraform_aws_backend_path(@backend), + local: true, + method: "post" + ) do %> + <%= submit_tag("Destroy", + role: 'button', + class: 'btn btn-lg btn-danger') + %> + <% end %> + </div> + <div class="col-md-6"> + <%= render( + partial: 'shared/jobs', + locals: { + jobs: Backends::TerraformAwsJob.jobs(@backend.id).order(id: :desc) + } + ) %> + </div> + <div class="col-md-4"> + + <% if status[:running] %> + <div class="alert alert-success text-center" + role="alert" + style="margin-bottom: 0px;"> + Backend available + </div> + <% else %> + <div class="alert alert-warning text-center" + role="alert" + style="margin-bottom: 0px;"> + Backend down + </div> + <% end %> + </div> +</div> <div class="row"> <div class="col-md-8"> diff --git a/config/routes.rb b/config/routes.rb index 70d967a..55aeab5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,7 +16,11 @@ Rails.application.routes.draw do resources :terraform_aws, as: 'terraform_aws_backends', controller: 'backends/terraform_aws', - only: %i[create new show update destroy] + only: %i[create new show update destroy] do + member do + post 'perform_action' + end + end resources :terraform_libvirt, as: 'terraform_libvirt_backends', diff --git a/terraform/aws/backend/main.tf b/terraform/aws/backend/main.tf index 346ab0d..24dd3d8 100644 --- a/terraform/aws/backend/main.tf +++ b/terraform/aws/backend/main.tf @@ -328,6 +328,10 @@ resource "aws_route53_record" "main" { # Outputs +output "backend_up" { + value = "${length(aws_spot_instance_request.main.public_ip) != 0}" +} + output "deployer_key_pair_name" { value = "${aws_key_pair.deployer.key_name}" } |