diff options
-rw-r--r-- | app/controllers/backends/terraform_aws_controller.rb | 34 | ||||
-rw-r--r-- | app/models/backends/terraform.rb | 22 | ||||
-rw-r--r-- | app/models/backends/terraform_aws.rb | 47 | ||||
-rw-r--r-- | app/models/backends/terraform_libvirt.rb | 21 | ||||
-rw-r--r-- | app/views/backends/terraform_aws/show.html.erb | 124 |
5 files changed, 207 insertions, 41 deletions
diff --git a/app/controllers/backends/terraform_aws_controller.rb b/app/controllers/backends/terraform_aws_controller.rb index b7f10ef..72c7e3a 100644 --- a/app/controllers/backends/terraform_aws_controller.rb +++ b/app/controllers/backends/terraform_aws_controller.rb @@ -1,7 +1,41 @@ class Backends::TerraformAwsController < ApplicationController def new + @backend = Backends::TerraformAws.new + end + + def create + backend = Backends::TerraformAws.create(backend_params) + + flash[:success] = "#{backend.label} created" + + redirect_to setup_path + end + + def update + backend = Backends::TerraformAws.update(params[:id], backend_params) + + flash[:success] = "#{backend.label} updated" end def show + @backend = Backends::TerraformAws.find(params[:id]) + end + + def destroy + backend = Backends::TerraformAws.find(params[:id]) + + backend.delete + end + + private + + def backend_params + params + .permit( + :label, + :aws_region, + :aws_access_key_id, + :aws_secret_access_key, + ) end end diff --git a/app/models/backends/terraform.rb b/app/models/backends/terraform.rb new file mode 100644 index 0000000..e1f937f --- /dev/null +++ b/app/models/backends/terraform.rb @@ -0,0 +1,22 @@ +module Backends::Terraform + def working_directory(mini_environment) + "tmp/terraform-working-directories/#{mini_environment.name}" + end + + def terraform_backend_address(mini_environment) + ( + 'http://localhost:3000' + + Rails + .application + .routes + .url_helpers + .mini_environment_terraform_http_backend_path( + mini_environment + ) + ) + end + + def terraform_plugin_dir + ENV['PATH'].split(':').first + end +end diff --git a/app/models/backends/terraform_aws.rb b/app/models/backends/terraform_aws.rb index d79ff51..246af8b 100644 --- a/app/models/backends/terraform_aws.rb +++ b/app/models/backends/terraform_aws.rb @@ -12,6 +12,8 @@ # class Backends::TerraformAws < ApplicationRecord + include Backends::Terraform + has_many :mini_environments, as: :backend self.table_name = "terraform_aws_backends" @@ -30,26 +32,26 @@ class Backends::TerraformAws < ApplicationRecord ) end - def start - logger.info "Setting up #{@mini_environment.name}" + def start(mini_environment) + logger.info "Setting up #{mini_environment.name}" - Dir.chdir("tmp/terraform-working-directories/#{@mini_environment.name}") do # TODO - RubyTerraform.init( - backend: true, - source: "terraform/aws", # TODO - backend_config: { - address: 'http://localhost:3000' + Rails.application.routes.url_helpers.terraform_http_backend_path - }, - plugin_dir: "/gnu/store/x0b54k4i02vi05ghc0np7cqs2p5q6i31-profile/bin" - ) + source = File.expand_path("terraform/libvirt") + FileUtils.mkdir_p working_directory(mini_environment) + + Dir.chdir(working_directory(mini_environment)) do + if Dir.empty?(".") + RubyTerraform.init( + backend: true, + from_module: source, + backend_config: { + address: terraform_backend_address(mini_environment) + }, + plugin_dir: plugin_dir + ) + end 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 - }, + vars: terraform_variables(mini_environment), auto_approve: true ) end @@ -59,7 +61,7 @@ class Backends::TerraformAws < ApplicationRecord Dir.chdir('terraform/aws') do RubyTerraform.destroy( vars: { - aws_region: 'eu-west-1', + aws_region: aws_region, slug: @mini_environment.name.parameterize, ssh_public_key: ssh_public_key, start_command: @mini_environment.start_command @@ -69,6 +71,15 @@ class Backends::TerraformAws < ApplicationRecord end end + def terraform_variables(mini_environment) + mini_environment.backend_data.merge( + aws_region: self.aws_region, + slug: mini_environment.name.parameterize, + ssh_public_key: ssh_public_key, + start_command: mini_environment.start_command, + ) + end + def ssh_public_key File.open("#{ENV['HOME']}/.ssh/id_rsa.pub") do |file| file.readline() diff --git a/app/models/backends/terraform_libvirt.rb b/app/models/backends/terraform_libvirt.rb index 4cb63b7..11e04ad 100644 --- a/app/models/backends/terraform_libvirt.rb +++ b/app/models/backends/terraform_libvirt.rb @@ -72,27 +72,6 @@ class Backends::TerraformLibvirt < ApplicationRecord ) end - def terraform_backend_address(mini_environment) - ( - 'http://localhost:3000' + - Rails - .application - .routes - .url_helpers - .mini_environment_terraform_http_backend_path( - mini_environment - ) - ) - end - - def working_directory(mini_environment) - "tmp/terraform-working-directories/#{mini_environment.name}" - end - - def plugin_dir - ENV['PATH'].split(':').first - end - def ssh_public_key File.open("#{ENV['HOME']}/.ssh/id_rsa.pub") do |file| file.readline() diff --git a/app/views/backends/terraform_aws/show.html.erb b/app/views/backends/terraform_aws/show.html.erb index e112d7d..a9324fc 100644 --- a/app/views/backends/terraform_aws/show.html.erb +++ b/app/views/backends/terraform_aws/show.html.erb @@ -1,2 +1,122 @@ -<h1>Backends::TerraformAws#show</h1> -<p>Find me in app/views/backends/terraform_aws/show.html.erb</p> +<a href="<%= setup_path %>" class="btn btn-lg btn-primary pull-right"> + Back to Setup +</a> + +<h1>Backend: <%= @backend.label %></h1> + +<div class="row"> + <div class="col-md-8"> + <h3>Update details</h3> + <br> + + <%= form_with(model: @backend, + url: { action: "update" }, + html: { class: "form-horizontal" }) do |f| + %> + + <div class="form-group form-group-lg"> + <%= f.label :label, class: 'col-sm-4 control-label' %> + <div class="col-sm-8"> + <%= f.text_field( + :label, + class: 'form-control', + placeholder: 'Label for this backend' + ) %> + </div> + </div> + + <div class="form-group form-group-lg"> + <%= f.label :aws_region, 'AWS Region', class: 'col-sm-4 control-label' %> + <div class="col-sm-8"> + <%= f.text_field( + :aws_region, + class: 'form-control', + placeholder: 'What region to use' + ) %> + </div> + </div> + + <div class="form-group form-group-lg"> + <%= f.label :aws_access_key_id, 'AWS Access Key ID', class: 'col-sm-4 control-label' %> + <div class="col-sm-8"> + <%= f.text_field( + :aws_access_key_id, + class: 'form-control', + ) %> + </div> + </div> + + <div class="form-group form-group-lg"> + <%= f.label :aws_secret_access_key, 'AWS Secret Access Key', class: 'col-sm-4 control-label' %> + <div class="col-sm-8"> + <%= f.password_field( + :aws_secret_access_key, + class: 'form-control', + placeholder: 'Secret key hidden', + ) %> + <span id="helpBlock" class="help-block"> + The AWS Secret Access Key is not accessible once entered. + </span> + </div> + </div> + + + <div class="form-group form-group-lg"> + <div class="col-sm-offset-2 col-sm-10"> + <%= f.submit "Save", class: 'btn btn-lg btn-success' %> + </div> + </div> + <% end %> + </div> + <div class="col-md-4"> + <h3>Delete backend</h3> + <br> + + <% unless @backend.mini_environments.empty? %> + <p> + Unable to delete backend, as mini environments using this + backend still exist. + </p> + + <p> + To delete this backend, first delete all the mini environments + using it. + </p> + <% end %> + + <%= form_with(model: @backend, + url: { action: "destroy" }, + html: { class: "form-horizontal", method: :delete }) do |f| + %> + <%= f.submit( + "Delete", + class: ( + 'btn btn-lg btn-danger' + + (@backend.mini_environments.empty? ? '' : ' disabled') + ) + ) %> + <% end %> + + </div> +</div> + +<h3>Mini environments</h3> + +<table class="table table-striped"> + <tr> + <th>Name</th> + <th></th> + </tr> + <% @backend.mini_environments.each do |mini_environment| %> + <tr> + <td><%= mini_environment.name %></td> + <td> + <a class="btn btn-default btn-lg pull-right" + role="button" + href="<%= mini_environment_path(mini_environment) %>"> + Show details + </a> + </td> + </tr> + <% end %> +</table> |