diff --git a/app/models/fileset.rb b/app/models/fileset.rb index e91ce4e..6029796 100644 --- a/app/models/fileset.rb +++ b/app/models/fileset.rb @@ -1,60 +1,75 @@ class Fileset < ActiveRecord::Base establish_connection Baas::settings[:local_db] - serialize :exclude_directions, Array + serialize :exclude_directions serialize :include_directions, JSON + attr_accessor :include_files + belongs_to :host has_many :job_templates + 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}\""] + - exclude_directions_to_config_array + include_directions_to_config_array + + exclude_directions_to_config_array + ['}'] end private + def sanitize_include_directions + return false if include_files.blank? + + self.include_directions = { options: DEFAULT_INCLUDE_OPTIONS, file: include_files } + end + + def sanitize_exclude_directions + self.exclude_directions = exclude_directions.keep_if(&:present?).uniq rescue [] + 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 = 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/spec/factories/fileset.rb b/spec/factories/fileset.rb index cf63981..c417bcf 100644 --- a/spec/factories/fileset.rb +++ b/spec/factories/fileset.rb @@ -1,8 +1,8 @@ FactoryGirl.define do factory :fileset do sequence(:name) { |n| "Fileset #{n}" } exclude_directions [] - include_directions {} + include_files [:foo] host end end diff --git a/spec/models/fileset_spec.rb b/spec/models/fileset_spec.rb index 5fb2fac..ab56e17 100644 --- a/spec/models/fileset_spec.rb +++ b/spec/models/fileset_spec.rb @@ -1,24 +1,40 @@ require 'spec_helper' describe Fileset do describe '#to_bacula_config_array' do let(:fileset) do FactoryGirl.create(:fileset, name: 'Test Fileset', exclude_directions: Fileset::DEFAULT_EXCLUDED, include_directions: { options: Fileset::DEFAULT_INCLUDE_OPTIONS, file: Fileset::DEFAULT_INCLUDE_FILE_LIST } ) end subject { fileset.to_bacula_config_array } it 'is a Fileset type resource' do expect(subject.first).to eq('FileSet {') expect(subject.last).to eq('}') end it 'contains the name' do expect(subject).to include(" Name = \"#{fileset.name}\"") end end + + context 'when duplicate exclude_directions are given' do + let(:fileset) { FactoryGirl.create(:fileset, exclude_directions: [:foo, :foo, :bar]) } + + it 'keeps its exclude_directions uniq' do + expect(fileset.exclude_directions).to eq([:foo, :bar]) + end + end + + context 'when exclude_directions are given' do + let(:fileset) { FactoryGirl.create(:fileset, exclude_directions: [:foo, :foo, :bar, '']) } + + it 'rejects them' do + expect(fileset.exclude_directions).to eq([:foo, :bar]) + end + end end