diff options
author | Christopher Baines <mail@cbaines.net> | 2018-06-23 11:16:26 +0100 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2018-06-23 12:58:05 +0100 |
commit | 05df9f91e9bdf39b190710ef5d80162d03dd52c5 (patch) | |
tree | 4a092a555a120131f8b3f096d274d2e3afc5bbae /app | |
parent | 837e1ecec9798381f78b838947f8028403cb0bef (diff) | |
download | govuk-mini-environment-admin-05df9f91e9bdf39b190710ef5d80162d03dd52c5.tar govuk-mini-environment-admin-05df9f91e9bdf39b190710ef5d80162d03dd52c5.tar.gz |
Support managing the libvirt backend from the show page
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/backends/terraform_libvirt_controller.rb | 23 | ||||
-rw-r--r-- | app/jobs/backends/terraform_libvirt_job.rb (renamed from app/jobs/backends/deploy_terraform_libvirt_job.rb) | 18 | ||||
-rw-r--r-- | app/models/backends/terraform_libvirt/backend_methods.rb | 35 | ||||
-rw-r--r-- | app/views/backends/terraform_libvirt/show.html.erb | 58 |
4 files changed, 119 insertions, 15 deletions
diff --git a/app/controllers/backends/terraform_libvirt_controller.rb b/app/controllers/backends/terraform_libvirt_controller.rb index bbc5e08..1feae41 100644 --- a/app/controllers/backends/terraform_libvirt_controller.rb +++ b/app/controllers/backends/terraform_libvirt_controller.rb @@ -26,7 +26,7 @@ class Backends::TerraformLibvirtController < ApplicationController def create backend = Backends::TerraformLibvirt.create(create_params) - Backends::DeployTerraformLibvirtJob.enqueue(backend.id) + Backends::TerraformLibvirtJob.enqueue(backend.id, :deploy_backend) flash[:success] = "Backend #{backend.label} created" @@ -57,6 +57,27 @@ class Backends::TerraformLibvirtController < ApplicationController redirect_to setup_path end + def perform_action + @backend = Backends::TerraformLibvirt.find(params['id']) + + action = params.require(:commit) + + case action + when 'Destroy' + Backends::TerraformLibvirtJob.enqueue(@backend.id, :destroy_backend) + + flash[:notice] = 'Destroying the backend' + when 'Deploy' + Backends::TerraformLibvirtJob.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/deploy_terraform_libvirt_job.rb b/app/jobs/backends/terraform_libvirt_job.rb index 4565189..af8eba3 100644 --- a/app/jobs/backends/deploy_terraform_libvirt_job.rb +++ b/app/jobs/backends/terraform_libvirt_job.rb @@ -18,13 +18,17 @@ # License along with the GOV.UK Mini Environment Admin. If not, see # <http://www.gnu.org/licenses/>. -class Backends::DeployTerraformLibvirtJob < Que::Job - def run(terraform_libvirt_backend_id) - @terraform_libvirt_backend_id = terraform_libvirt_backend_id +class Backends::TerraformLibvirtJob < Que::Job + def run(terraform_libvirt_backend_id, action) + ActiveRecord::Base.transaction do + @backend = Backends::TerraformLibvirt.find( + terraform_libvirt_backend_id + ) - Backends::TerraformLibvirt - .find(terraform_libvirt_backend_id) - .deploy_backend + @backend.send(action) + + finish + end end def self.jobs(terraform_libvirt_backend_id) @@ -33,6 +37,6 @@ class Backends::DeployTerraformLibvirtJob < Que::Job job_class: name ).where( "args->>0 = '#{terraform_libvirt_backend_id}'" - ).to_a + ) end end diff --git a/app/models/backends/terraform_libvirt/backend_methods.rb b/app/models/backends/terraform_libvirt/backend_methods.rb index 7c3c47d..7595611 100644 --- a/app/models/backends/terraform_libvirt/backend_methods.rb +++ b/app/models/backends/terraform_libvirt/backend_methods.rb @@ -33,4 +33,39 @@ module Backends::TerraformLibvirt::BackendMethods ) end end + + def destroy_backend + TerraformWorkingDirectory.new( + terraform_state_id, + 'terraform/libvirt/backend' + ).within_working_directory do + RubyTerraform.destroy( + vars: { + uri: uri, + domain: domain + }, + force: true + ) + end + 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_libvirt/show.html.erb b/app/views/backends/terraform_libvirt/show.html.erb index d7a1641..5b2213b 100644 --- a/app/views/backends/terraform_libvirt/show.html.erb +++ b/app/views/backends/terraform_libvirt/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_libvirt_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_libvirt_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::TerraformLibvirtJob.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"> @@ -118,10 +169,3 @@ License along with the GOV.UK Mini Environment Admin. If not, see </tr> <% end %> </table> - -<h3>Jobs</h3> - -<%= render( - partial: 'shared/jobs', - locals: { jobs: Backends::DeployTerraformLibvirtJob.jobs(@backend.id) } -) %> |