diff options
-rw-r--r-- | app/controllers/govuk_guix/revisions_controller.rb | 6 | ||||
-rw-r--r-- | app/models/backends/terraform_aws/backend_methods.rb | 10 | ||||
-rw-r--r-- | app/models/backends/terraform_aws/mini_environment_methods.rb | 22 | ||||
-rw-r--r-- | terraform/aws/backend/main.tf | 4 | ||||
-rw-r--r-- | test/models/backends/terraform_aws_test.rb | 11 |
5 files changed, 49 insertions, 4 deletions
diff --git a/app/controllers/govuk_guix/revisions_controller.rb b/app/controllers/govuk_guix/revisions_controller.rb index 838bdae..6e75e82 100644 --- a/app/controllers/govuk_guix/revisions_controller.rb +++ b/app/controllers/govuk_guix/revisions_controller.rb @@ -33,8 +33,12 @@ class GovukGuix::RevisionsController < ApplicationController # Assume that the AWS backend is in use backend = Backends::TerraformAws.first + remote_host = backend.backend_latest_terraform_state.output_value( + 'guix_daemon_public_dns' + ) + options = { - run_remotely_on_host: "ubuntu@guix-daemon.#{backend.domain}" + run_remotely_on_host: "ubuntu@#{remote_host}" } end diff --git a/app/models/backends/terraform_aws/backend_methods.rb b/app/models/backends/terraform_aws/backend_methods.rb index 89509bd..b21c83e 100644 --- a/app/models/backends/terraform_aws/backend_methods.rb +++ b/app/models/backends/terraform_aws/backend_methods.rb @@ -20,8 +20,12 @@ module Backends::TerraformAws::BackendMethods def create_data_snapshot + remote_host = backend_latest_terraform_state.output_value( + 'guix_daemon_public_dns' + ) + GovukGuix::CreateDataSnapshotJob.enqueue( - run_remotely_on_host: "ubuntu@guix-daemon.#{domain}", + run_remotely_on_host: "ubuntu@#{remote_host}", backend_type: self.class.name, backend_id: id ) @@ -101,6 +105,10 @@ module Backends::TerraformAws::BackendMethods ) end + def backend_latest_terraform_state + backend_terraform_states.order(:id).last + end + def status latest_terraform_state = backend_terraform_states.order(:id).last diff --git a/app/models/backends/terraform_aws/mini_environment_methods.rb b/app/models/backends/terraform_aws/mini_environment_methods.rb index e0fe03b..6049829 100644 --- a/app/models/backends/terraform_aws/mini_environment_methods.rb +++ b/app/models/backends/terraform_aws/mini_environment_methods.rb @@ -22,6 +22,14 @@ module Backends::TerraformAws::MiniEnvironmentMethods def build(mini_environment) slug = mini_environment.name.parameterize + remote_build_host = + mini_environment + .backend + .backend_latest_terraform_state + .output_value( + 'guix_daemon_public_dns' + ) + GovukGuix::BuildMiniEnvironment.build( mini_environment.id, services: mini_environment.services.map(&:build_argument_string), @@ -42,7 +50,7 @@ module Backends::TerraformAws::MiniEnvironmentMethods '/var/log/govuk-mini-environment-admin=/var/log' ] }, - run_remotely_on_host: "ubuntu@guix-daemon.#{domain}" + run_remotely_on_host: "ubuntu@#{remote_build_host}" ) end @@ -74,13 +82,23 @@ module Backends::TerraformAws::MiniEnvironmentMethods end end + def terraform_states(mini_environment) + TerraformState.where( + state_id: mini_environment_state_id(mini_environment) + ) + end + def within_terraform_working_directory(mini_environment, &block) TerraformWorkingDirectory.new( - "mini_environment/#{mini_environment.id}", + mini_environment_state_id(mini_environment), 'terraform/aws/mini_environment' ).within_working_directory(&block) end + def mini_environment_state_id(mini_environment) + "mini_environment/#{mini_environment.id}" + end + def signon_url(mini_environment) "https://signon.#{mini_environment.name.parameterize}.#{domain}" end diff --git a/terraform/aws/backend/main.tf b/terraform/aws/backend/main.tf index 24dd3d8..c3fcf60 100644 --- a/terraform/aws/backend/main.tf +++ b/terraform/aws/backend/main.tf @@ -352,6 +352,10 @@ output "guix_daemon_private_dns" { value = "${aws_spot_instance_request.main.private_dns}" } +output "guix_daemon_public_dns" { + value = "${aws_spot_instance_request.main.public_dns}" +} + output "efs_file_system_dns_name" { value = "${aws_efs_file_system.main.dns_name}" } diff --git a/test/models/backends/terraform_aws_test.rb b/test/models/backends/terraform_aws_test.rb index 1a98440..f9a34f3 100644 --- a/test/models/backends/terraform_aws_test.rb +++ b/test/models/backends/terraform_aws_test.rb @@ -3,9 +3,20 @@ require 'test_helper' class Backends::TerraformAwsTest < ActiveSupport::TestCase setup do @backend = Backends::TerraformAws.new + + backend_latest_terraform_state = mock + backend_latest_terraform_state + .stubs(:output_value) + .returns('example.com') + + @backend.stubs(:backend_latest_terraform_state).returns( + backend_latest_terraform_state + ) + @mini_environment = MiniEnvironment.new( id: 1, name: 'Test Mini Environment', + backend: @backend, backend_data: { 'build_output' => '/gnu/store/testbuildoutput' } |