diff --git a/app/controllers/filesets_controller.rb b/app/controllers/filesets_controller.rb index 0c4f248..a65b7f9 100644 --- a/app/controllers/filesets_controller.rb +++ b/app/controllers/filesets_controller.rb @@ -1,33 +1,35 @@ class FilesetsController < ApplicationController before_action :fetch_host, only: [:new, :create] def new @fileset = @host.filesets.new end def show end def create @fileset = @host.filesets.new(fetch_params) if @fileset.save redirect_to host_path(@host) else + @fileset.include_files = nil + @fileset.exclude_directions = nil render :new end end def destroy end private def fetch_host @host = Host.find(params[:host_id]) end def fetch_params params.require(:fileset).permit(:name, exclude_directions: [], include_files: []) end end diff --git a/app/models/fileset.rb b/app/models/fileset.rb index 6029796..d4da768 100644 --- a/app/models/fileset.rb +++ b/app/models/fileset.rb @@ -1,75 +1,89 @@ class Fileset < ActiveRecord::Base establish_connection Baas::settings[:local_db] serialize :exclude_directions serialize :include_directions, JSON attr_accessor :include_files belongs_to :host has_many :job_templates + validates :name, presence: true, uniqueness: { scope: :host } + validate :has_included_files, on: :create + before_save :sanitize_exclude_directions, :sanitize_include_directions DEFAULT_EXCLUDED = %w{/var/lib/bacula /proc /tmp /.journal /.fsck /bacula} DEFAULT_INCLUDE_OPTIONS = { signature: :SHA1, compression: :GZIP } DEFAULT_INCLUDE_FILE_LIST = ['/'] def to_bacula_config_array ['FileSet {'] + - [" Name = \"#{name}\""] + + [" Name = \"#{name_for_config}\""] + include_directions_to_config_array + exclude_directions_to_config_array + ['}'] end private + def name_for_config + [host.name, name].join(' ') + end + + def has_included_files + if include_files.blank? || include_files.all?(&:blank?) + errors.add(:include_files, :cant_be_blank) + end + end + def sanitize_include_directions - return false if include_files.blank? + files = include_files.compact.uniq.keep_if(&:present?) + return false if files.blank? - self.include_directions = { options: DEFAULT_INCLUDE_OPTIONS, file: include_files } + self.include_directions = { options: DEFAULT_INCLUDE_OPTIONS, file: files } end def sanitize_exclude_directions - self.exclude_directions = exclude_directions.keep_if(&:present?).uniq rescue [] + self.exclude_directions = exclude_directions.keep_if(&:present?).uniq rescue nil end def exclude_directions_to_config_array return [] if exclude_directions.empty? [' Exclude {'] + exclude_directions.map { |x| " File = \"#{x}\"" } + [' }'] end def include_directions_to_config_array return [] if include_directions.blank? [" Include {"] + included_options + included_files + [' }'] end def included_options formatted = [" Options {"] options = include_directions.deep_symbolize_keys[:options]. reverse_merge(DEFAULT_INCLUDE_OPTIONS) options.each do |k,v| if not [:wildfile].include? k formatted << " #{k} = #{v}" else formatted << v.map { |f| " #{k} = \"#{f}\"" } end end formatted << " }" formatted end def included_files include_directions['file'].map { |f| " File = #{f}" } end def included_wildfile include_directions['wildfile'].map { |f| " wildfile = \"#{f}\"" }.join("\n") end end diff --git a/app/views/filesets/_form.html.erb b/app/views/filesets/_form.html.erb index bd2a384..8309239 100644 --- a/app/views/filesets/_form.html.erb +++ b/app/views/filesets/_form.html.erb @@ -1,29 +1,17 @@ <%= bootstrap_form_for(@fileset, url: host_filesets_path(@host, @fileset), layout: :horizontal, label_col: 'col-xs-3', control_col: 'col-xs-8') do |f| %> - <% if @fileset.errors.any? %> -