From 7fb0c23b891b8734cb04a9690cd030a134f5cd4d Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sat, 23 Jun 2018 09:21:30 +0100 Subject: Add initial support for fetching govuk-guix revisions on remote hosts This begins to remote the restriction of having to have Guix installed locally. --- app/jobs/govuk_guix/fetch_revision_job.rb | 61 +++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 12 deletions(-) (limited to 'app/jobs') 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 -- cgit v1.2.3