created file sync

This commit is contained in:
Senad Uka
2016-02-21 19:17:04 +01:00
parent 709ce3e38c
commit ac099e3210
18 changed files with 230 additions and 28 deletions

View File

@@ -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'

View File

@@ -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)

View File

@@ -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

View File

@@ -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();
});
});

View File

@@ -59,6 +59,14 @@
border-color: gray;
}
.unclickable {
pointer-events: none;
}
.editor {
padding-bottom: 30px;
}
body {
margin-right: 5px;
margin-left: 5px;

View File

@@ -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

View File

@@ -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

View File

@@ -1,2 +1,11 @@
module ConfigurationFilesHelper
end
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

View File

@@ -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

View File

@@ -1,3 +1,7 @@
class FileType < ActiveRecord::Base
has_many :configuration_files
def to_s
name
end
end

View File

@@ -13,16 +13,16 @@
<% @templates.each do |template| %>
<div class="row template-selection" id="template_<%= template.id %>">
<div class="col-md-2 text-md-center">
<div class="thumbnail">
<div class="col-md-2 text-md-center unclickable">
<div class="thumbnail unclickable">
<%= image_tag(template.safe_icon, size: '128', class:"img-responsive" ) %>
<div class="caption text-md-center">
<h3 class="server-name"><%= template.name %></h3>
<div class="caption text-md-center unclickable">
<h3 class="server-name unclickable"><%= template.name %></h3>
</div>
</div>
</div>
<div class="col-md-10 template-description">
<div class="col-md-10 template-description unclickable">
<%= template.description %>
</div>
</div>
@@ -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 %>

View File

@@ -1,12 +0,0 @@
<div class="row">
</div>
<div class="row">
<div class="col-md-10">
<div id="editor">
<%= @record.content %>
</div>
</div>
</div>

View File

@@ -0,0 +1,57 @@
<% content_for :javascript_includes do %>
<%= javascript_include_tag "edit_configuration_file_form.js" %>
<% end %>
<div class="row">
<h3><%= @record.file_type %>
file on
<%= @record.server.name %>
</h3>
<div><%= @record.namehash %></div>
<div>Version: v<%= @record.last_version.number %>
(modified
<%= distance_of_time_in_words(@record.last_version.updated_at, Time.now) %>
ago)
</div>
</div>
<div class="row">
<div class="col-md-10">
<% command = pull_command(@record) %>
Type this into your server's console to pull latest saved version<br />
<%= text_field_tag 'command_pull', command, readonly: true, class: 'form-control-sm', size: command.length %>
</div>
</div>
<div class="row">
<div class="col-md-10">
<% command = push_command(@record) %>
Type this into your server's console to push servers version into CHUB<br />
<%= text_field_tag 'command_push', command, readonly: true, class: 'form-control-sm', size: command.length %>
</div>
</div>
<div class="row editor">
<div class="col-md-5">
<div>
<%= text_field_tag 'file_path', @record.file_path, class: 'form-control-sm', id: 'visible_file_path' , size: "100%" %>
<br/>
<br/>
</div>
<div id="editor">
<%= @record.last_version.content %>
</div>
</div>
<br/><br/>
</div>
<div class="row">
<div class="col-md-2">
<%= button_tag(type: "button", class: "btn btn-success", id: "save_button") do %>Save as v<%= @record.last_version.number + 1 %>
<% end %>
</div>
</div>
<%= 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 %>

View File

@@ -25,6 +25,41 @@
<p><%= text_field_tag 'command', command, readonly: true, class: 'form-control-sm', size: command.length %></p>
</div>
<% else %>
<p><%= 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 %><br/><br/>
</p>
<table class="table">
<thead>
<tr>
<th>File Path</th>
<th>Version</th>
<th>Last Modified</th>
<th></th>
</tr>
</thead>
<tbody>
<% @record.configuration_files.each do |file| %>
<tr>
<td><%= file.file_path %></td>
<td>v<%= file.last_version.number %></td>
<td><%= distance_of_time_in_words(file.last_version.updated_at, Time.now) %></td>
<td><%= button_to(edit_configuration_file_path(file), method: :get, class: "btn btn-default inline") do %>
Edit
<% end %>
</td>
<td>
<%= button_to(configuration_file_path(file), method: :delete, class: "btn btn-danger inline") do %>
Delete
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
</div>

View File

@@ -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 )

View File

@@ -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

View File

@@ -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

View File

@@ -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|