# 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 # . class MiniEnvironmentsController < ApplicationController DEFAULT_SERVICES = %w( collections-publisher contacts-admin content-tagger local-links-manager manuals-publisher maslow policy-publisher publisher service-manual-publisher short-url-manager specialist-publisher travel-advice-publisher whitehall calculators calendars collections email-alert-frontend feedback finder-frontend frontend government-frontend licencefinder manuals-frontend service-manual-frontend smartanswers draft-collections draft-email-alert-frontend draft-frontend draft-government-frontend draft-finder-frontend draft-manuals-frontend draft-service-manual-frontend ).freeze def show @mini_environment = MiniEnvironment.find(params[:id]) end def new @mini_environment = MiniEnvironment.new @mini_environment.signon_users = [ { name: current_user.name, email: current_user.email } ] end def create parameters = params.require( :mini_environment ).permit( :name, :govuk_guix_revision_id, :backend, signon_users_attributes: [ :name, :email, :role ] ) @mini_environment = MiniEnvironment.new( parameters .except(:backend) ) @mini_environment.backend = Backends.find_by_type_and_id( *parameters[:backend].split('=') ) @mini_environment.signon_users[0]['passphrase'] = SecureRandom.hex @mini_environment.save! redirect_to services_mini_environment_path(@mini_environment) end def services @mini_environment = MiniEnvironment.find(params[:id]) if request.put? @mini_environment.transaction do @mini_environment.govuk_guix_revision.all_available_services .each do |service| name = service['name'] next if params["#{name}-selected"] != 'on' revision = params["#{name}-revision"] @mini_environment.services.where( name: name ).delete_all @mini_environment.services.create( name: name, revision: revision ) end data_snapshot_id = params.fetch(:data_snapshot, nil) if data_snapshot_id # TODO: Validate that this is on the list of approved snapshots @mini_environment.update!( data_snapshot_id: data_snapshot_id ) end end MiniEnvironmentJob.enqueue(@mini_environment.id, :build) redirect_to @mini_environment end end def perform_action @mini_environment = MiniEnvironment.find(params[:mini_environment_id]) action = params.require(:commit) case action when 'Stop' MiniEnvironmentJob.enqueue(@mini_environment.id, :destroy) flash[:notice] = 'Destroying mini environment' when 'Start' MiniEnvironmentJob.enqueue(@mini_environment.id, :start) flash[:notice] = 'Starting mini environment' when 'Refresh' MiniEnvironmentJob.enqueue(@mini_environment.id, :refresh) flash[:notice] = 'Refreshing the mini environment state information' when 'Archive' @mini_environment.update(archived: true) flash[:notice] = 'Archiving mini environment' else flash[:error] = "Unknown action #{action}" end render 'show' end end