diff --git a/app/models/fileset.rb b/app/models/fileset.rb index ab0500e..e91ce4e 100644 --- a/app/models/fileset.rb +++ b/app/models/fileset.rb @@ -1,59 +1,60 @@ class Fileset < ActiveRecord::Base establish_connection Baas::settings[:local_db] serialize :exclude_directions, Array serialize :include_directions, JSON belongs_to :host + has_many :job_templates 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 + ['}'] end private 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/models/host.rb b/app/models/host.rb index 4227be7..407fb1a 100644 --- a/app/models/host.rb +++ b/app/models/host.rb @@ -1,68 +1,75 @@ class Host < ActiveRecord::Base FILE_RETENTION_DAYS = 60 JOB_RETENTION_DAYS = 180 CATALOG = 'MyCatalog' AUTOPRUNE = 1 establish_connection :local_development enum status: { draft: 0, pending: 1, config: 2, ready: 3 } belongs_to :client, class_name: :Client, foreign_key: :name, primary_key: :name has_many :filesets, dependent: :destroy has_many :job_templates, dependent: :destroy validates :file_retention, :job_retention, :port, :password, presence: true validates :port, numericality: true validates :name, presence: true, uniqueness: true validate :fqdn_format scope :not_baculized, -> { where(baculized: false) } before_validation :set_retention, :unset_baculized, :sanitize_name + def baculize_config + templates = job_templates.enabled.includes(:fileset, :schedule) + + result = [self] + templates.map {|x| [x, x.fileset, x.schedule] }.flatten + result.map(&:to_bacula_config_array) + end + def to_bacula_config_array [ "Client {", " Name = #{name}", " Address = #{fqdn}", " FDPort = #{port}", " Catalog = #{CATALOG}", " Password = \"#{password}\"", " File Retention = #{file_retention} days", " Job Retention = #{job_retention} days", " AutoPrune = yes", "}" ] end def auto_prune_human AUTOPRUNE == 1 ? 'yes' : 'no' end private def sanitize_name self.name = fqdn end def set_retention self.file_retention = FILE_RETENTION_DAYS self.job_retention = JOB_RETENTION_DAYS end def unset_baculized self.baculized = false if new_record? true end def fqdn_format regex = /(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?