diff --git a/app/controllers/filesets_controller.rb b/app/controllers/filesets_controller.rb index 8fbac37..77c6c0c 100644 --- a/app/controllers/filesets_controller.rb +++ b/app/controllers/filesets_controller.rb @@ -1,51 +1,82 @@ class FilesetsController < ApplicationController before_action :require_logged_in - before_action :fetch_host, only: [:new, :create, :show] - before_action :fetch_job_id, only: [:new, :create] + before_action :fetch_host, only: [:new, :create, :show, :edit, :update] + before_action :fetch_job_id, only: [:new, :create, :edit, :update] + before_action :fetch_fileset, only: [:show, :edit, :update] def new @fileset = @host.filesets.new + @fileset.include_directions = { 'file' => [nil] } end def show @fileset = @host.filesets.find(params[:id]) respond_to do |format| format.js {} end end + # GET /hosts/:host_id/filesets/:id/edit + def edit + @fileset.include_files = @fileset.include_directions['file'] + end + + # PATCH /hosts/:host_id/filesets/:id/ + def update + if @fileset.update(fetch_params) + flash[:success] = 'Fileset updated successfully' + participating_hosts = @fileset.participating_hosts + if participating_hosts.size.nonzero? + participating_hosts.each(&:recalculate) + flash[:alert] = 'You will need to redeploy the afffected clients: ' + + participating_hosts.map(&:name).join(', ') + end + if @job_id + redirect_to edit_host_job_path(@host, @job_id, fileset_id: @fileset.id) + else + redirect_to new_host_job_path(@host, fileset_id: @fileset.id) + end + else + render :edit + end + end + def create @fileset = @host.filesets.new(fetch_params) if @fileset.save flash[:success] = 'Fileset created' if @job_id.present? redirect_to edit_host_job_path(@host, @job_id, fileset_id: @fileset.id) else redirect_to new_host_job_path(@host, fileset_id: @fileset.id) end else @fileset.include_files = nil @fileset.exclude_directions = nil render :new end end def destroy end private def fetch_host @host = current_user.hosts.find(params[:host_id]) end def fetch_job_id @job_id = @host.job_templates.find(params[:job_id]).id if params[:job_id].present? end + def fetch_fileset + @fileset = @host.filesets.find(params[: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 686754a..7056f3a 100644 --- a/app/models/fileset.rb +++ b/app/models/fileset.rb @@ -1,98 +1,105 @@ # Fileset model is the application representation of Bacula's Fileset. # It has references to a host and job templates. class Fileset < ActiveRecord::Base 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 validates_with NameValidator 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 = ['/'] # Constructs an array where each element is a line for the Fileset's bacula config # # @return [Array] def to_bacula_config_array ['FileSet {'] + [" Name = \"#{name_for_config}\""] + include_directions_to_config_array + exclude_directions_to_config_array + ['}'] end # Generates a name that will be used for the configuration file. # It is the name that will be sent to Bacula through the configuration # files. # # @return [String] def name_for_config [host.name, name].join(' ') end + # Returns the hosts that have enabled jobs that use this fileset + # + # @return [ActiveRecord::Relation] the participating hosts + def participating_hosts + Host.joins(:job_templates).where(job_templates: { enabled: true, fileset_id: id }).uniq + end + private 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 files = include_files.compact.uniq.keep_if(&:present?) return false if files.blank? self.include_directions = { options: DEFAULT_INCLUDE_OPTIONS, file: files } end def sanitize_exclude_directions 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 3bc01df..8d0a7df 100644 --- a/app/views/filesets/_form.html.erb +++ b/app/views/filesets/_form.html.erb @@ -1,16 +1,19 @@ -<%= bootstrap_form_for(@fileset, url: host_filesets_path(@host, @fileset), layout: :horizontal, +<%= bootstrap_form_for(@fileset, url: url, method: method, layout: :horizontal, label_col: 'col-xs-3', control_col: 'col-xs-8') do |f| %>
<%= fileset.to_bacula_config_array.join("\n") %>