From 3b135d15e48eafb99395b3f1a62e15e5de1c5a4f Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 28 May 2018 20:36:00 +0100 Subject: Add a new model to track services Associated with a mini environment. This can also be used to store customisations over the chosen revision of govuk-guix. --- app/models/mini_environment.rb | 2 + app/models/mini_environment_service.rb | 57 +++++++++++++++++++ ...80527183740_create_mini_environment_services.rb | 11 ++++ db/structure.sql | 66 +++++++++++++++++++++- 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 app/models/mini_environment_service.rb create mode 100644 db/migrate/20180527183740_create_mini_environment_services.rb diff --git a/app/models/mini_environment.rb b/app/models/mini_environment.rb index 712e3a0..77e2f1b 100644 --- a/app/models/mini_environment.rb +++ b/app/models/mini_environment.rb @@ -49,6 +49,8 @@ class MiniEnvironment < ApplicationRecord belongs_to :govuk_guix_revision, class_name: 'GovukGuix::Revision' belongs_to :backend, polymorphic: true + has_many :services, class_name: 'MiniEnvironmentService', dependent: :destroy + def enqueued_terraform_jobs Que.execute("SELECT * FROM que_jobs WHERE args->>0 = '#{id}'") end diff --git a/app/models/mini_environment_service.rb b/app/models/mini_environment_service.rb new file mode 100644 index 0000000..73aa8aa --- /dev/null +++ b/app/models/mini_environment_service.rb @@ -0,0 +1,57 @@ +# GOV.UK Mini Environment Admin +# Copyright © 2018 Christopher Baines +# +# This file is part of the GOV.UK Mini Environment Admin. +# +# The GOV.UK Mini Environment Admin is free software: you can +# redistribute it and/or modify it under the terms of the GNU Affero +# General Public License as published by the Free Software Foundation, +# either version 3 of the License, or (at your option) any later +# version. +# +# The GOV.UK Mini Environment Admin is distributed in the hope that it +# will be useful, but WITHOUT ANY WARRANTY; without even the implied +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with the GOV.UK Mini Environment Admin. If not, see +# . + +# == Schema Information +# +# Table name: mini_environment_services +# +# id :integer not null, primary key +# name :string +# revision :string +# mini_environment_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + +class MiniEnvironmentService < ApplicationRecord + belongs_to :mini_environment + + def customised? + service = mini_environment + .govuk_guix_revision + .all_available_services + .find do |s| + + s['name'] == name + end + + Rails.logger.info service + + service['package']['version'] != revision + end + + def build_argument_string + if customised? + "#{name}@#{revision}" + else + name + end + end +end diff --git a/db/migrate/20180527183740_create_mini_environment_services.rb b/db/migrate/20180527183740_create_mini_environment_services.rb new file mode 100644 index 0000000..135889c --- /dev/null +++ b/db/migrate/20180527183740_create_mini_environment_services.rb @@ -0,0 +1,11 @@ +class CreateMiniEnvironmentServices < ActiveRecord::Migration[5.1] + def change + create_table :mini_environment_services do |t| + t.string :name + t.string :revision + t.references :mini_environment, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/structure.sql b/db/structure.sql index 12c19cf..cd71ee3 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -84,6 +84,39 @@ CREATE TABLE public.govuk_guix_revisions ( ); +-- +-- Name: mini_environment_services; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.mini_environment_services ( + id bigint NOT NULL, + name character varying, + revision character varying, + mini_environment_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +-- +-- Name: mini_environment_services_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.mini_environment_services_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: mini_environment_services_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.mini_environment_services_id_seq OWNED BY public.mini_environment_services.id; + + -- -- Name: mini_environments; Type: TABLE; Schema: public; Owner: - -- @@ -318,6 +351,13 @@ ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id; ALTER TABLE ONLY public.finished_terraform_jobs ALTER COLUMN id SET DEFAULT nextval('public.finished_terraform_jobs_id_seq'::regclass); +-- +-- Name: mini_environment_services id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.mini_environment_services ALTER COLUMN id SET DEFAULT nextval('public.mini_environment_services_id_seq'::regclass); + + -- -- Name: mini_environments id; Type: DEFAULT; Schema: public; Owner: - -- @@ -384,6 +424,14 @@ ALTER TABLE ONLY public.govuk_guix_revisions ADD CONSTRAINT govuk_guix_revisions_pkey PRIMARY KEY (commit_hash); +-- +-- Name: mini_environment_services mini_environment_services_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.mini_environment_services + ADD CONSTRAINT mini_environment_services_pkey PRIMARY KEY (id); + + -- -- Name: mini_environments mini_environments_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -447,6 +495,13 @@ ALTER TABLE ONLY public.users CREATE UNIQUE INDEX index_govuk_guix_revisions_on_commit_hash ON public.govuk_guix_revisions USING btree (commit_hash); +-- +-- Name: index_mini_environment_services_on_mini_environment_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_mini_environment_services_on_mini_environment_id ON public.mini_environment_services USING btree (mini_environment_id); + + -- -- Name: index_mini_environments_on_backend_type_and_backend_id; Type: INDEX; Schema: public; Owner: - -- @@ -469,6 +524,14 @@ ALTER TABLE ONLY public.mini_environments ADD CONSTRAINT fk_rails_12ab275069 FOREIGN KEY (govuk_guix_revision_id) REFERENCES public.govuk_guix_revisions(commit_hash); +-- +-- Name: mini_environment_services fk_rails_36052da616; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.mini_environment_services + ADD CONSTRAINT fk_rails_36052da616 FOREIGN KEY (mini_environment_id) REFERENCES public.mini_environments(id); + + -- -- PostgreSQL database dump complete -- @@ -503,6 +566,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20180410192329'), ('20180410192412'), ('20180417195307'), -('20180523062426'); +('20180523062426'), +('20180527183740'); -- cgit v1.2.3