From ac099e3210ceb649194ea2418dd0f461e4e7b39e Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 21 Feb 2016 19:17:04 +0100 Subject: [PATCH] created file sync --- web/Gemfile | 4 ++ web/Gemfile.lock | 3 + web/app/assets/javascripts/application.js | 5 ++ .../edit_configuration_file_form.js | 17 ++++++ .../assets/stylesheets/application.css.scss | 8 +++ .../configuration_files_controller.rb | 32 ++++++++++- .../configuration_templates_controller.rb | 2 + web/app/helpers/configuration_files_helper.rb | 11 +++- web/app/models/configuration_file.rb | 30 +++++++++- web/app/models/file_type.rb | 4 ++ .../views/configuration_files/create.html.erb | 11 ++-- .../views/configuration_files/edit.html.erb | 12 ---- .../views/configuration_files/update.html.erb | 57 +++++++++++++++++++ web/app/views/servers/show.html.erb | 35 ++++++++++++ web/config/initializers/assets.rb | 2 +- web/config/routes.rb | 7 +++ ...525_add_file_path_to_configuration_file.rb | 5 ++ web/db/schema.rb | 13 +++-- 18 files changed, 230 insertions(+), 28 deletions(-) create mode 100644 web/app/assets/javascripts/edit_configuration_file_form.js delete mode 100644 web/app/views/configuration_files/edit.html.erb create mode 100644 web/app/views/configuration_files/update.html.erb create mode 100644 web/db/migrate/20160221110525_add_file_path_to_configuration_file.rb 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| %>
-
-
+
+
<%= image_tag(template.safe_icon, size: '128', class:"img-responsive" ) %> -
-

<%= template.name %>

+
+

<%= template.name %>

-
+
<%= template.description %>
@@ -30,4 +30,5 @@ <%= form_tag(create_from_template_configuration_files_url, method: :post, id: "create_from_template_form") do %> <%= hidden_field_tag('template_id','', id: 'template_id') %> + <%= hidden_field_tag('server_id', @server.id, id: 'server_id') %> <% end %> diff --git a/web/app/views/configuration_files/edit.html.erb b/web/app/views/configuration_files/edit.html.erb deleted file mode 100644 index 15f23ad..0000000 --- a/web/app/views/configuration_files/edit.html.erb +++ /dev/null @@ -1,12 +0,0 @@ -
- -
- - -
-
-
- <%= @record.content %> -
-
-
diff --git a/web/app/views/configuration_files/update.html.erb b/web/app/views/configuration_files/update.html.erb new file mode 100644 index 0000000..b6a0365 --- /dev/null +++ b/web/app/views/configuration_files/update.html.erb @@ -0,0 +1,57 @@ +<% content_for :javascript_includes do %> +<%= javascript_include_tag "edit_configuration_file_form.js" %> +<% end %> + +
+

<%= @record.file_type %> + file on + <%= @record.server.name %> +

+
<%= @record.namehash %>
+
Version: v<%= @record.last_version.number %> + (modified + <%= distance_of_time_in_words(@record.last_version.updated_at, Time.now) %> + ago) +
+
+
+
+ <% command = pull_command(@record) %> + Type this into your server's console to pull latest saved version
+ <%= text_field_tag 'command_pull', command, readonly: true, class: 'form-control-sm', size: command.length %> +
+
+ +
+
+ <% command = push_command(@record) %> + Type this into your server's console to push servers version into CHUB
+ <%= text_field_tag 'command_push', command, readonly: true, class: 'form-control-sm', size: command.length %> +
+
+ +
+
+
+ <%= text_field_tag 'file_path', @record.file_path, class: 'form-control-sm', id: 'visible_file_path' , size: "100%" %> +
+
+
+
+ <%= @record.last_version.content %> +
+
+

+
+
+
+ <%= button_tag(type: "button", class: "btn btn-success", id: "save_button") do %>Save as v<%= @record.last_version.number + 1 %> + <% end %> +
+ +
+ +<%= form_tag(update_by_form_configuration_file_url(@record), method: :put, id: "update_form") do %> +<%= hidden_field_tag('content','', id: 'content') %> +<%= hidden_field_tag('file_path', @record.file_path, id: 'file_path') %> +<% end %> diff --git a/web/app/views/servers/show.html.erb b/web/app/views/servers/show.html.erb index 7a9124c..b122cbe 100644 --- a/web/app/views/servers/show.html.erb +++ b/web/app/views/servers/show.html.erb @@ -25,6 +25,41 @@

<%= text_field_tag 'command', command, readonly: true, class: 'form-control-sm', size: command.length %>

+<% else %> +

<%= 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 %>

+

+ + + + + + + + + + + + <% @record.configuration_files.each do |file| %> + + + + + + + + <% end %> + +
File PathVersionLast 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 %> +
+ <% end %>
diff --git a/web/config/initializers/assets.rb b/web/config/initializers/assets.rb index ac54a58..9e6baa8 100644 --- a/web/config/initializers/assets.rb +++ b/web/config/initializers/assets.rb @@ -10,4 +10,4 @@ Rails.application.config.assets.version = '1.0' # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. # Rails.application.config.assets.precompile += %w( search.js ) Rails.application.config.assets.precompile += %w( active_scaffold/indicator.gif ) -Rails.application.config.assets.precompile += %w( template_select_form.js ) +Rails.application.config.assets.precompile += %w( template_select_form.js edit_configuration_file_form.js ) diff --git a/web/config/routes.rb b/web/config/routes.rb index b62b431..2f7da03 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -1,4 +1,8 @@ Rails.application.routes.draw do + + get '/file/:id', to: 'configuration_files#content', as: 'pull_file' + post '/file/:id', to: 'configuration_files#update_by_api', as: 'push_file' + resources :configuration_templates do as_routes end resources :file_versions do as_routes end resources :file_types do as_routes end @@ -7,6 +11,9 @@ Rails.application.routes.draw do collection do post 'create_from_template' end + member do + put 'update_by_form' + end end resources :operating_systems do as_routes end resources :servers do as_routes end diff --git a/web/db/migrate/20160221110525_add_file_path_to_configuration_file.rb b/web/db/migrate/20160221110525_add_file_path_to_configuration_file.rb new file mode 100644 index 0000000..e3f7600 --- /dev/null +++ b/web/db/migrate/20160221110525_add_file_path_to_configuration_file.rb @@ -0,0 +1,5 @@ +class AddFilePathToConfigurationFile < ActiveRecord::Migration + def change + add_column :configuration_files, :file_path, :string, null: false, default: "/tmp/configuration.txt" + end +end diff --git a/web/db/schema.rb b/web/db/schema.rb index 79e84c2..13145b6 100644 --- a/web/db/schema.rb +++ b/web/db/schema.rb @@ -11,15 +11,16 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160221101719) do +ActiveRecord::Schema.define(version: 20160221110525) do create_table "configuration_files", force: :cascade do |t| - t.string "namehash", null: false - t.integer "server_id", null: false - t.integer "file_type_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "namehash", null: false + t.integer "server_id", null: false + t.integer "file_type_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.integer "configuration_template_id" + t.string "file_path", default: "/tmp/configuration.txt", null: false end create_table "configuration_templates", force: :cascade do |t|