diff options
author | Christopher Baines <mail@cbaines.net> | 2018-06-23 09:21:30 +0100 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2018-06-23 12:58:05 +0100 |
commit | 7fb0c23b891b8734cb04a9690cd030a134f5cd4d (patch) | |
tree | eaac05016a75763698f821bb19816ca63b9acab3 | |
parent | 7302c52a0011556527e5cbb6b0e996bd5bf49508 (diff) | |
download | govuk-mini-environment-admin-7fb0c23b891b8734cb04a9690cd030a134f5cd4d.tar govuk-mini-environment-admin-7fb0c23b891b8734cb04a9690cd030a134f5cd4d.tar.gz |
Add initial support for fetching govuk-guix revisions on remote hosts
This begins to remote the restriction of having to have Guix installed
locally.
-rw-r--r-- | app/controllers/govuk_guix/revisions_controller.rb | 14 | ||||
-rw-r--r-- | app/jobs/govuk_guix/fetch_revision_job.rb | 61 | ||||
-rw-r--r-- | test/controllers/govuk_guix/revisions_controller_test.rb | 3 |
3 files changed, 64 insertions, 14 deletions
diff --git a/app/controllers/govuk_guix/revisions_controller.rb b/app/controllers/govuk_guix/revisions_controller.rb index 1e311eb..f7c7f56 100644 --- a/app/controllers/govuk_guix/revisions_controller.rb +++ b/app/controllers/govuk_guix/revisions_controller.rb @@ -26,7 +26,19 @@ class GovukGuix::RevisionsController < ApplicationController def enqueue_fetch_revision revision = params.require('revision') - GovukGuix::FetchRevisionJob.enqueue(revision) + # Attempt to check if this can be performed locally + if File.exist? '/var/guix/daemon-socket/socket' + options = {} + else + # Assume that the AWS backend is in use + backend = Backends::TerraformAws.first + + options = { + run_remotely_on_host: "ubuntu@guix-daemon.#{backend.domain}" + } + end + + GovukGuix::FetchRevisionJob.enqueue(revision, options) flash[:info] = "Fetching govuk-guix revision (#{revision})..." redirect_to setup_path diff --git a/app/jobs/govuk_guix/fetch_revision_job.rb b/app/jobs/govuk_guix/fetch_revision_job.rb index 2787156..d3f11bf 100644 --- a/app/jobs/govuk_guix/fetch_revision_job.rb +++ b/app/jobs/govuk_guix/fetch_revision_job.rb @@ -24,26 +24,22 @@ class GovukGuix::FetchRevisionJob < Que::Job extend EnqueuedJobs include ::ShellUtils - REPOSITORY_DIRECTORY = 'tmp/cache/govuk-guix'.freeze - @retry_interval = 30 - def run(commit_hash) - repository.fetch - - sha = repository.object(commit_hash).sha + def run(commit_hash, options = {}) + remote_host = options[:run_remotely_on_host] - repository.checkout(sha) + fetch_and_checkout(commit_hash, remote_host) command = [ - "#{REPOSITORY_DIRECTORY}/guix-pre-inst-env", + "#{@repository_directory}/guix-pre-inst-env", 'guix', 'build', '-e', '(begin (use-modules (gds packages govuk)) (current-govuk-guix))' ] - output = run_command(*command) + output = run_command(*command, run_remotely_on_host: remote_host) store_path = output.last.strip logger.debug(self.class) { "store_path: #{store_path}" } @@ -55,12 +51,53 @@ class GovukGuix::FetchRevisionJob < Que::Job ) end + def fetch_and_checkout(commit_hash, remote_host) + if remote_host + @repository_directory = '/tmp/govuk-guix' + + run_command( + "/bin/sh", + "-c", + "if cd '#{@repository_directory}';"\ + "then git fetch;"\ + "else "\ + "git clone #{self.class.repository_remote_location} '#{@repository_directory}';"\ + "fi", + run_remotely_on_host: remote_host + ) + + sha = run_command( + "/bin/sh", + "-c", + "cd '#{@repository_directory}';"\ + "git rev-list -n 1 '#{commit_hash}'", + run_remotely_on_host: remote_host + ).last.strip + + run_command( + "/bin/sh", + "-c", + "cd '#{@repository_directory}';"\ + "git checkout #{sha}", + run_remotely_on_host: remote_host + ) + else + @repository_directory = 'tmp/cache/govuk-guix' + + repository.fetch + + sha = repository.object(commit_hash).sha + + repository.checkout(sha) + end + end + def repository @_repository ||= begin - if File.exist? REPOSITORY_DIRECTORY - Git.open(REPOSITORY_DIRECTORY, log: Rails.logger) + if File.exist? @repository_directory + Git.open(@repository_directory, log: Rails.logger) else - Git.clone(self.class.repository_remote_location, REPOSITORY_DIRECTORY) + Git.clone(self.class.repository_remote_location, @repository_directory) end end end diff --git a/test/controllers/govuk_guix/revisions_controller_test.rb b/test/controllers/govuk_guix/revisions_controller_test.rb index 51ca048..6f08489 100644 --- a/test/controllers/govuk_guix/revisions_controller_test.rb +++ b/test/controllers/govuk_guix/revisions_controller_test.rb @@ -19,7 +19,8 @@ class GovukGuix::RevisionsControllerTest < ActionDispatch::IntegrationTest test 'enqueue_fetch_revision' do revision = 'test-revision' - GovukGuix::FetchRevisionJob.expects(:enqueue).with(revision) + Guix.stubs(:available_locally?).returns(true) + GovukGuix::FetchRevisionJob.expects(:enqueue).with(revision, {}) post( enqueue_fetch_govuk_guix_revision_path, |