diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | app/controllers/terraform_http_backend_controller.rb | 25 | ||||
-rw-r--r-- | app/jobs/setup_job.rb | 19 | ||||
-rw-r--r-- | app/models/mini_environment.rb | 4 | ||||
-rw-r--r-- | app/models/terraform_state.rb | 2 | ||||
-rw-r--r-- | app/views/mini_environments/show.html.erb | 12 | ||||
-rw-r--r-- | config/initializers/que.rb | 1 | ||||
-rw-r--r-- | config/routes.rb | 6 | ||||
-rw-r--r-- | db/migrate/20180128125638_create_terraform_states.rb | 9 | ||||
-rw-r--r-- | db/schema.rb | 8 | ||||
-rw-r--r-- | guix.scm | 38 | ||||
-rw-r--r-- | terraform/aws/mini_environment.tf (renamed from terraform/example.tf) | 17 | ||||
-rw-r--r-- | terraform/aws/mini_environment/govuk.service.tpl (renamed from terraform/example/govuk.service.tpl) | 0 |
13 files changed, 137 insertions, 6 deletions
@@ -17,3 +17,5 @@ /yarn-error.log .byebug_history + +.terraform diff --git a/app/controllers/terraform_http_backend_controller.rb b/app/controllers/terraform_http_backend_controller.rb new file mode 100644 index 0000000..6b94d8d --- /dev/null +++ b/app/controllers/terraform_http_backend_controller.rb @@ -0,0 +1,25 @@ +class TerraformHttpBackendController < ApplicationController + skip_before_action :verify_authenticity_token + + def create + TerraformState.create(data: params) + + render json: '{ "success": true }', status: 200 + end + + def show + state = TerraformState.last + + if state.nil? + render json: '{}', status: 404 + else + render json: state.data, status: 200 + end + end + + def destroy + TerraformState.delete_all + + render json: '{ "success": true }', status: 200 + end +end diff --git a/app/jobs/setup_job.rb b/app/jobs/setup_job.rb index c7b1b80..6b4f9fe 100644 --- a/app/jobs/setup_job.rb +++ b/app/jobs/setup_job.rb @@ -1,5 +1,24 @@ +require 'ruby_terraform' + class SetupJob < TerraformJob def run_terraform puts "Setting up #{@mini_environment.name}" + + RubyTerraform.init( + source: 'terraform/aws', + path: 'terraform/aws', + backend: true, + backend_config: { + address: 'http://localhost:3000' + Rails.application.routes.url_helpers.terraform_http_backend_path + } + ) + + RubyTerraform.apply( + directory: 'terraform/aws', + vars: { + slug: @mini_environment.name.parameterize + }, + auto_approve: true + ) end end diff --git a/app/models/mini_environment.rb b/app/models/mini_environment.rb index d53a8b1..2d6d8ae 100644 --- a/app/models/mini_environment.rb +++ b/app/models/mini_environment.rb @@ -1,3 +1,7 @@ class MiniEnvironment < ApplicationRecord has_many :finished_terraform_jobs, dependent: :destroy + + def enqueued_terraform_jobs + Que.execute("SELECT * FROM que_jobs WHERE args->>0 = '#{id}'") + end end diff --git a/app/models/terraform_state.rb b/app/models/terraform_state.rb new file mode 100644 index 0000000..fe1d364 --- /dev/null +++ b/app/models/terraform_state.rb @@ -0,0 +1,2 @@ +class TerraformState < ApplicationRecord +end diff --git a/app/views/mini_environments/show.html.erb b/app/views/mini_environments/show.html.erb index 2d8988c..5b2199e 100644 --- a/app/views/mini_environments/show.html.erb +++ b/app/views/mini_environments/show.html.erb @@ -1,6 +1,18 @@ <h1>Name: <%= @mini_environment.name %></h1> +<h2>Finished jobs</h2> + <% @mini_environment.finished_terraform_jobs.each do |job| %> <%= job.job_class %> <%= job.created_at %> <% end %> + +<h2>Enqueued jobs</h2> + +<% @mini_environment.enqueued_terraform_jobs.each do |job| %> + <%= job['job_class'] %> + + <pre> + <%= job['last_error'] %> + </pre> +<% end %> diff --git a/config/initializers/que.rb b/config/initializers/que.rb new file mode 100644 index 0000000..b9e5a68 --- /dev/null +++ b/config/initializers/que.rb @@ -0,0 +1 @@ +Que.mode = :off diff --git a/config/routes.rb b/config/routes.rb index 2296683..0dcd800 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,9 @@ Rails.application.routes.draw do root :to => 'mini_environments#index' - resources :mini_environments, :path => '/' + resource :terraform_http_backend, + controller: :terraform_http_backend, + except: %i[new edit update] + + resources :mini_environments, path: '/' end diff --git a/db/migrate/20180128125638_create_terraform_states.rb b/db/migrate/20180128125638_create_terraform_states.rb new file mode 100644 index 0000000..1882af9 --- /dev/null +++ b/db/migrate/20180128125638_create_terraform_states.rb @@ -0,0 +1,9 @@ +class CreateTerraformStates < ActiveRecord::Migration[5.1] + def change + create_table :terraform_states do |t| + t.json :data + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 70325bb..a20083e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180127222948) do +ActiveRecord::Schema.define(version: 20180128125638) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -45,6 +45,12 @@ ActiveRecord::Schema.define(version: 20180127222948) do t.text "queue", default: "", null: false end + create_table "terraform_states", force: :cascade do |t| + t.json "data" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "users", force: :cascade do |t| t.string "name" t.string "email" diff --git a/guix.scm b/guix.scm new file mode 100644 index 0000000..332ed1e --- /dev/null +++ b/guix.scm @@ -0,0 +1,38 @@ +(use-modules (guix packages) + (guix build-system ruby) + (gnu packages ruby) + (gnu packages databases) + (gnu packages rails) + (gnu packages rails-wip) + (gds packages govuk ruby)) + +(define govuk-mini-environment-admin + (package + (name "govuk-mini-environment-admin") + (version "0") + (source #f) + (build-system ruby-build-system) + (inputs + `(("ruby-rails" ,ruby-rails) + ("ruby-pg" ,ruby-pg) + ("ruby-gds-sso" ,ruby-gds-sso) + ("ruby-govuk-admin-template" ,ruby-govuk-admin-template) + ("ruby-plek" ,ruby-plek) + ("ruby-terraform" ,ruby-terraform) + ("ruby-que" ,ruby-que))) + (synopsis "") + (description "") + (home-page "") + (license ""))) + +(define govuk-mini-environment-admin-development-environment + (package + (inherit govuk-mini-environment-admin) + (inputs + `(,@(package-inputs govuk-mini-environment-admin) + ("ruby" ,ruby) + ("postgresql" ,postgresql))))) + +govuk-mini-environment-admin-development-environment + + diff --git a/terraform/example.tf b/terraform/aws/mini_environment.tf index 0efeed8..56c8a3c 100644 --- a/terraform/example.tf +++ b/terraform/aws/mini_environment.tf @@ -1,3 +1,12 @@ +terraform { + backend "http" {} +} + +variable "slug" { + type = "string" +} + + provider "aws" { region = "eu-west-1" } @@ -24,8 +33,8 @@ data "template_file" "govuk_service" { vars { guix_daemon_socket = "guix://${data.aws_instance.guix-daemon.private_dns}", - app_domain = "banana.aws.cbaines.net", - web_domain = "www.banana.aws.cbaines.net" + app_domain = "${var.slug}.aws.cbaines.net", + web_domain = "www.${var.slug}.aws.cbaines.net" } } @@ -80,7 +89,7 @@ resource "aws_spot_instance_request" "example" { resource "aws_route53_record" "example" { zone_id = "${data.aws_route53_zone.main.zone_id}" - name = "banana" + name = "${var.slug}" type = "A" ttl = "60" records = ["${aws_spot_instance_request.example.public_ip}"] @@ -88,7 +97,7 @@ resource "aws_route53_record" "example" { resource "aws_route53_record" "example_wildcard" { zone_id = "${data.aws_route53_zone.main.zone_id}" - name = "*.banana" + name = "*.${var.slug}" type = "A" ttl = "60" records = ["${aws_spot_instance_request.example.public_ip}"] diff --git a/terraform/example/govuk.service.tpl b/terraform/aws/mini_environment/govuk.service.tpl index 52b4b56..52b4b56 100644 --- a/terraform/example/govuk.service.tpl +++ b/terraform/aws/mini_environment/govuk.service.tpl |