diff --git a/web/Gemfile b/web/Gemfile index 4218ee1..86fb28c 100644 --- a/web/Gemfile +++ b/web/Gemfile @@ -36,6 +36,10 @@ gem 'puma' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development +source 'https://rails-assets.org' do + gem 'rails-assets-tether', '>= 1.1.0' +end + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' diff --git a/web/Gemfile.lock b/web/Gemfile.lock index ffa7970..da41155 100644 --- a/web/Gemfile.lock +++ b/web/Gemfile.lock @@ -1,5 +1,6 @@ GEM remote: https://rubygems.org/ + remote: https://rails-assets.org/ specs: ace-rails-ap (4.0.1) actionmailer (4.2.5.1) @@ -101,6 +102,7 @@ GEM bundler (>= 1.3.0, < 2.0) railties (= 4.2.5.1) sprockets-rails + rails-assets-tether (1.2.0) rails-deprecated_sanitizer (1.0.3) activesupport (>= 4.2.0.alpha) rails-dom-testing (1.0.7) @@ -174,6 +176,7 @@ DEPENDENCIES jquery-rails puma rails (= 4.2.5.1) + rails-assets-tether (>= 1.1.0)! rubocop sass-rails (~> 5.0) sdoc (~> 0.4.0) diff --git a/web/app/assets/javascripts/application.js b/web/app/assets/javascripts/application.js index d13bc15..dcfd7c9 100644 --- a/web/app/assets/javascripts/application.js +++ b/web/app/assets/javascripts/application.js @@ -12,6 +12,11 @@ // //= require jquery //= require jquery_ujs +//= require tether //= require turbolinks //= require active_scaffold //= require bootstrap-sprockets +//= require ace-rails-ap +//= require ace/mode-apache_conf +//= require ace/mode-ini +//= require ace/mode-dockerfile diff --git a/web/app/assets/javascripts/edit_configuration_file_form.js b/web/app/assets/javascripts/edit_configuration_file_form.js new file mode 100644 index 0000000..3f90d3a --- /dev/null +++ b/web/app/assets/javascripts/edit_configuration_file_form.js @@ -0,0 +1,17 @@ +$(document).ready(function() { + var editor = ace.edit("editor"); + //editor.setTheme("ace/theme/monokai"); + //editor.getSession().setMode("ace/mode/javascript"); + editor.setOptions({ + maxLines: 40 + }); + editor.resize(); + + $("#save_button").click(function() { + var content = editor.getValue(); + $("#content").val(content); + var filePath = $("#visible_file_path").val() + $("#file_path").val(filePath) + $("#update_form").submit(); + }); +}); diff --git a/web/app/assets/stylesheets/application.css.scss b/web/app/assets/stylesheets/application.css.scss index c597c83..4735f27 100644 --- a/web/app/assets/stylesheets/application.css.scss +++ b/web/app/assets/stylesheets/application.css.scss @@ -59,6 +59,14 @@ border-color: gray; } +.unclickable { + pointer-events: none; +} + +.editor { + padding-bottom: 30px; +} + body { margin-right: 5px; margin-left: 5px; diff --git a/web/app/controllers/configuration_files_controller.rb b/web/app/controllers/configuration_files_controller.rb index 5b052b1..f1c7edf 100644 --- a/web/app/controllers/configuration_files_controller.rb +++ b/web/app/controllers/configuration_files_controller.rb @@ -1,5 +1,6 @@ class ConfigurationFilesController < ApplicationController - before_filter :selected_server, only: [:new, :edit] + before_filter :selected_server, only: [:new] + skip_before_action :verify_authenticity_token, only: [:update_by_api] active_scaffold :"configuration_file" do |conf| conf.columns[:file_type].form_ui = :select @@ -11,6 +12,33 @@ class ConfigurationFilesController < ApplicationController end def create_from_template - template = ConfigurationTemplate.find(params[:template_id]) + template = ConfigurationTemplate.find(params[:template_id].to_i) + file = ConfigurationFile.create_from_template!(template, params[:server_id].to_i) + redirect_to edit_configuration_file_path(file) + end + + def update_by_form + file = ConfigurationFile.find(params[:id].to_i) + file.update_by_form(params[:content], params[:file_path]) + redirect_to edit_configuration_file_path(file) + end + + def update_by_api + file = ConfigurationFile.find(params[:id].to_i) + uploaded_file = params[:content] + file.update_by_api(uploaded_file) + render text: "OK" + end + + def content + file = ConfigurationFile.find(params[:id].to_i) + render text: file.last_version.content + end + + def destroy + file = ConfigurationFile.find(params[:id]) + server = file.server + file.destroy! + redirect_to server_path(server) end end diff --git a/web/app/controllers/configuration_templates_controller.rb b/web/app/controllers/configuration_templates_controller.rb index 70d1be2..1f430e6 100644 --- a/web/app/controllers/configuration_templates_controller.rb +++ b/web/app/controllers/configuration_templates_controller.rb @@ -1,4 +1,6 @@ class ConfigurationTemplatesController < ApplicationController active_scaffold :"configuration_template" do |conf| + conf.columns[:file_type].form_ui = :select + conf.columns[:operating_system].form_ui = :select end end diff --git a/web/app/helpers/configuration_files_helper.rb b/web/app/helpers/configuration_files_helper.rb index 352f74f..9d899b2 100644 --- a/web/app/helpers/configuration_files_helper.rb +++ b/web/app/helpers/configuration_files_helper.rb @@ -1,2 +1,11 @@ module ConfigurationFilesHelper -end \ No newline at end of file + def pull_command(file) + url = pull_file_url(file) + "curl -o #{file.file_path} #{url}" + end + + def push_command(file) + url = push_file_url(file) + %Q{ curl -F "content=@#{file.file_path}" #{url} } + end +end diff --git a/web/app/models/configuration_file.rb b/web/app/models/configuration_file.rb index dc0804d..26df2f4 100644 --- a/web/app/models/configuration_file.rb +++ b/web/app/models/configuration_file.rb @@ -1,5 +1,33 @@ class ConfigurationFile < ActiveRecord::Base belongs_to :server belongs_to :file_type - has_many :file_versions + has_many :file_versions, -> { order('number DESC') } + + def self.create_from_template!(template, server_id) + file = ConfigurationFile.new + file.server_id = server_id + file.file_path = template.file_path + file.file_type = template.file_type + file.namehash = SecureRandom.hex + transaction do + file.save! + file.file_versions.create!(content: template.content) + end + file + end + + def update_by_form(content, path) + transaction do + update_attribute(:file_path, path) + file_versions.create!(content: content, number: last_version.number + 1) + end + end + + def update_by_api(file) + file_versions.create!(content: file.read, number: last_version.number + 1) + end + + def last_version + file_versions.first + end end diff --git a/web/app/models/file_type.rb b/web/app/models/file_type.rb index 2366c29..45f0964 100644 --- a/web/app/models/file_type.rb +++ b/web/app/models/file_type.rb @@ -1,3 +1,7 @@ class FileType < ActiveRecord::Base has_many :configuration_files + + def to_s + name + end end diff --git a/web/app/views/configuration_files/create.html.erb b/web/app/views/configuration_files/create.html.erb index 56604f0..07d276d 100644 --- a/web/app/views/configuration_files/create.html.erb +++ b/web/app/views/configuration_files/create.html.erb @@ -13,16 +13,16 @@ <% @templates.each do |template| %>
<%= text_field_tag 'command', command, readonly: true, class: 'form-control-sm', size: command.length %>
<%= button_to(new_configuration_file_path, method: :get, params: { server_id: @record.id}, class: "btn btn-success pull-center") do %>
+ Create New Configuration File
+ <% end %>
+
| File Path | +Version | +Last Modified | ++ | |
|---|---|---|---|---|
| <%= file.file_path %> | +v<%= file.last_version.number %> | +<%= distance_of_time_in_words(file.last_version.updated_at, Time.now) %> | +<%= button_to(edit_configuration_file_path(file), method: :get, class: "btn btn-default inline") do %> + Edit + <% end %> + | ++ <%= button_to(configuration_file_path(file), method: :delete, class: "btn btn-danger inline") do %> + Delete + <% end %> + | +