diff options
author | Christopher Baines <mail@cbaines.net> | 2018-03-16 08:51:54 +0000 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2018-03-29 07:55:01 +0100 |
commit | 051c8bf612126fa79699c8bf45a661dde127f4a0 (patch) | |
tree | 849a3b05b4ab4b59a426761d7cf1da3cc7036349 /app/jobs | |
parent | a8c8f68971dd9e20dee01d9f65c64283e41fe4a3 (diff) | |
download | govuk-mini-environment-admin-051c8bf612126fa79699c8bf45a661dde127f4a0.tar govuk-mini-environment-admin-051c8bf612126fa79699c8bf45a661dde127f4a0.tar.gz |
Add backend controllers, models and views
Also annotate existing models.
Diffstat (limited to 'app/jobs')
-rw-r--r-- | app/jobs/destroy_job.rb | 2 | ||||
-rw-r--r-- | app/jobs/enqueued_jobs.rb | 12 | ||||
-rw-r--r-- | app/jobs/govuk_guix/fetch_revision_job.rb | 8 | ||||
-rw-r--r-- | app/jobs/govuk_guix/generate_start_command_job.rb (renamed from app/jobs/govuk_guix/build_job.rb) | 12 | ||||
-rw-r--r-- | app/jobs/govuk_guix/generate_vm_image_and_system_job.rb | 39 | ||||
-rw-r--r-- | app/jobs/govuk_guix/job.rb | 2 | ||||
-rw-r--r-- | app/jobs/mini_environment_job.rb (renamed from app/jobs/terraform_job.rb) | 8 | ||||
-rw-r--r-- | app/jobs/start_job.rb | 31 |
8 files changed, 74 insertions, 40 deletions
diff --git a/app/jobs/destroy_job.rb b/app/jobs/destroy_job.rb index 2bc72c1..45cd845 100644 --- a/app/jobs/destroy_job.rb +++ b/app/jobs/destroy_job.rb @@ -1,6 +1,6 @@ require 'ruby_terraform' -class DestroyJob < TerraformJob +class DestroyJob < MiniEnvironmentJob @retry_interval = 30 def run_terraform diff --git a/app/jobs/enqueued_jobs.rb b/app/jobs/enqueued_jobs.rb new file mode 100644 index 0000000..c394611 --- /dev/null +++ b/app/jobs/enqueued_jobs.rb @@ -0,0 +1,12 @@ +module EnqueuedJobs + def enqueued_jobs(argument = nil) + if argument + condition = " AND args->>0 = '#{argument}'" + else + condition = "" + end + + Que.execute("SELECT * FROM que_jobs WHERE job_class = '#{name}'#{condition}") + end +end + diff --git a/app/jobs/govuk_guix/fetch_revision_job.rb b/app/jobs/govuk_guix/fetch_revision_job.rb index b1cb171..f1391cc 100644 --- a/app/jobs/govuk_guix/fetch_revision_job.rb +++ b/app/jobs/govuk_guix/fetch_revision_job.rb @@ -3,12 +3,16 @@ require 'git' require 'open3' class GovukGuix::FetchRevisionJob < GovukGuix::Job + extend EnqueuedJobs + @retry_interval = 30 def run(commit_hash) repository.fetch - repository.checkout(commit_hash) + sha = repository.object(commit_hash).sha + + repository.checkout(sha) command = [ "#{repository_directory}/guix-pre-inst-env", @@ -23,7 +27,7 @@ class GovukGuix::FetchRevisionJob < GovukGuix::Job logger.debug("FetchRevisionJob: store_path: #{store_path}") GovukGuix::Revision.create( - commit_hash: commit_hash, + commit_hash: sha, store_path: store_path ) end diff --git a/app/jobs/govuk_guix/build_job.rb b/app/jobs/govuk_guix/generate_start_command_job.rb index 381a717..489e35e 100644 --- a/app/jobs/govuk_guix/build_job.rb +++ b/app/jobs/govuk_guix/generate_start_command_job.rb @@ -1,11 +1,11 @@ require 'ruby_terraform' require 'open3' -class GovukGuix::BuildJob < GovukGuix::Job +class GovukGuix::GenerateStartCommandJob < GovukGuix::Job @retry_interval = 30 def run(mini_environment_id) - logger.info "GovukGuix::BuildJob: Building mini environment #{mini_environment_id}" + logger.info "GovukGuix::GenerateStartCommandJob: Building mini environment #{mini_environment_id}" mini_environment = MiniEnvironment.find(mini_environment_id) @@ -25,9 +25,13 @@ class GovukGuix::BuildJob < GovukGuix::Job run_command(command) do |output| store_path = output.last.strip - logger.debug("GovukGuix::BuildJob: start_command: #{store_path}") + logger.debug("GovukGuix::GenerateStartCommandJob: start_command: #{store_path}") - mini_environment.update(start_command: store_path) + mini_environment.update( + backend_data: { + start_command: store_path + } + ) end end end diff --git a/app/jobs/govuk_guix/generate_vm_image_and_system_job.rb b/app/jobs/govuk_guix/generate_vm_image_and_system_job.rb new file mode 100644 index 0000000..21455f9 --- /dev/null +++ b/app/jobs/govuk_guix/generate_vm_image_and_system_job.rb @@ -0,0 +1,39 @@ +require 'ruby_terraform' +require 'open3' + +class GovukGuix::GenerateVmImageAndSystemJob < GovukGuix::Job + @retry_interval = 30 + + def run(mini_environment_id) + logger.info "#{self.class}: Building mini environment #{mini_environment_id}" + + mini_environment = MiniEnvironment.find(mini_environment_id) + + slug = mini_environment.name.parameterize + + command = [ + "#{mini_environment.govuk_guix_revision.store_path}/bin/govuk", + "system", + "build", + "--output=vm-image-and-system", + "--rails-environment=production", + "--app-domain=#{slug}.aws.cbaines.net", + "--web-domain=www.#{slug}.aws.cbaines.net", + "--use-high-ports=false", + "--use-https=certbot", + "--fallback", + "whitehall", + ] + + run_command(command) do |output| + store_path = output.last.strip + logger.debug "#{self.class}: vm_image_and_system: #{store_path}" + + mini_environment.update( + backend_data: { + vm_image_and_system: store_path + } + ) + end + end +end diff --git a/app/jobs/govuk_guix/job.rb b/app/jobs/govuk_guix/job.rb index 0161cb1..aebf2f9 100644 --- a/app/jobs/govuk_guix/job.rb +++ b/app/jobs/govuk_guix/job.rb @@ -1,6 +1,6 @@ class GovukGuix::Job < Que::Job def run_command(command) - logger.debug("#{self.class}: Running command #{command}") + logger.debug("#{self.class}: Running command #{command.join(' ')}") Open3.popen2e(*command) do |stdin, stdout_and_stderr, wait_thr| logger.info("#{self.class}: commmand running, pid #{wait_thr.pid}") diff --git a/app/jobs/terraform_job.rb b/app/jobs/mini_environment_job.rb index 739a5b5..cda28b4 100644 --- a/app/jobs/terraform_job.rb +++ b/app/jobs/mini_environment_job.rb @@ -1,9 +1,9 @@ -class TerraformJob < Que::Job - def run(mini_environment_id) +class MiniEnvironmentJob < Que::Job + def run(mini_environment_id, action) ActiveRecord::Base.transaction do - @mini_environment = MiniEnvironment.find(mini_environment_id) + mini_environment = MiniEnvironment.find(mini_environment_id) - run_terraform + mini_environment.backend.send(action, mini_environment) end end diff --git a/app/jobs/start_job.rb b/app/jobs/start_job.rb index ec17732..e7acaa4 100644 --- a/app/jobs/start_job.rb +++ b/app/jobs/start_job.rb @@ -1,34 +1,9 @@ require 'ruby_terraform' -class StartJob < TerraformJob +class StartJob < MiniEnvironmentJob @retry_interval = 30 - def run_terraform - logger.info "Setting up #{@mini_environment.name}" - - Dir.chdir('terraform/aws') do - RubyTerraform.init( - backend: true, - backend_config: { - address: 'http://localhost:3000' + Rails.application.routes.url_helpers.terraform_http_backend_path - } - ) - - RubyTerraform.apply( - vars: { - aws_region: 'eu-west-1', - slug: @mini_environment.name.parameterize, - ssh_public_key: ssh_public_key, - start_command: @mini_environment.start_command - }, - auto_approve: true - ) - end - end - - def ssh_public_key - File.open("#{ENV['HOME']}/.ssh/id_rsa.pub") do |file| - file.readline() - end + def perform_mini_environment_action + @mini_environment.backend.start(@mini_environment) end end |