aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/mini_environments_controller.rb
blob: 50818017f24f70f0a57c7762d48e8c920d22dc4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# GOV.UK Mini Environment Admin
# Copyright © 2018  Christopher Baines <mail@cbaines.net>
#
# 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
# <http://www.gnu.org/licenses/>.

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 'Destroy'
      MiniEnvironmentJob.enqueue(@mini_environment.id, :destroy)

      flash[:notice] = 'Destroying mini environment'
    when 'Start the mini environment'
      MiniEnvironmentJob.enqueue(@mini_environment.id, :start)

      flash[:notice] = 'Starting mini environment'
    else
      flash[:error] = "Unknown action #{action}"
    end

    render 'show'
  end
end