diff --git a/app/models/job_template.rb b/app/models/job_template.rb index d971e3c..7b41e99 100644 --- a/app/models/job_template.rb +++ b/app/models/job_template.rb @@ -1,111 +1,112 @@ # JobTemplate class is a helper class that enables us to configure Bacula job # configurations, without messing with Bacula's native models (Job). # It has a unique: # # * host # * fileset # * schedule class JobTemplate < ActiveRecord::Base enum job_type: { backup: 0, restore: 1, verify: 2, admin: 3 } belongs_to :host belongs_to :fileset belongs_to :schedule validates :name, :fileset_id, presence: true validates :schedule_id, presence: true, unless: :restore? validates :name, uniqueness: { scope: :host } validates_with NameValidator before_save :set_job_type after_save :notify_host scope :enabled, -> { where(enabled: true) } # Constructs an array where each element is a line for the Job's bacula config # # @return [Array] def to_bacula_config_array ['Job {'] + options_array.map { |x| " #{x}" } + job_settings.map { |k,v| " #{k.capitalize} = #{v}" } + ['}'] end # Fetches the Job's priority def priority job_settings[:priority] end # Helper method for the job template's enabled status def enabled_human enabled? ? 'yes' : 'no' end # Helper method for the job template's schedule name # # @return [String] The schedule's name or nothing def schedule_human schedule.present? ? schedule.name : '-' 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}" end # Sends a hot backup request to Bacula via BaculaHandler def backup_now return false if not (enabled? && baculized? && backup?) host.backup_now(name) end private def name_format unless name =~ /^[a-zA-Z0-1\.-_ ]+$/ self.errors.add(:name, :format) end end def notify_host host.recalculate end # Sets the default job_type as backup def set_job_type self.job_type = :backup if job_type.nil? end def options_array result = [ "Name = \"#{name_for_config}\"", "Enabled = #{enabled_human}", "FileSet = \"#{fileset.name_for_config}\"", "Client = \"#{host.name}\"", "Type = \"#{job_type.capitalize}\"", "Schedule = \"#{schedule.name_for_config}\"" ] if client_before_run_file.present? result += ["Client Run Before Job = \"#{client_before_run_file}\""] end if client_after_run_file.present? result += ["Client Run After Job = \"#{client_after_run_file}\""] end result end # Fetches and memoizes the general configuration settings for Jobs # # @see ConfigurationSetting.current_job_settings # @return [Hash] containing the settings def job_settings - @job_settings ||= ConfigurationSetting.current_job_settings + messages = host.email_recipients.any? ? "message_#{host.name}" : :Standard + @job_settings ||= ConfigurationSetting.current_job_settings.merge(messages: messages) end end diff --git a/config/initializers/00_settings.rb b/config/initializers/00_settings.rb index 259ddfc..3cdcb48 100644 --- a/config/initializers/00_settings.rb +++ b/config/initializers/00_settings.rb @@ -1,13 +1,14 @@ Archiving.settings director_name: YAML.load_file(Rails.root.join('config', 'bacula.yml'))[Rails.env]. symbolize_keys[:director] Archiving.settings vima_oauth_enabled: true Archiving.settings institutional_authentication_enabled: true Archiving.settings okeanos_authentication_enabled: false Archiving.settings default_sender: 'admin@archiving.grnet.gr' Archiving.settings admin_email: 'admin@archiving.grnet.gr' Archiving.settings temp_db_retention: 3.days Archiving.settings skip_host_fetch_time_period: 1.month +Archiving.settings mail_settings: YAML::load(File.open("#{Rails.root}/config/mailer.yml"))[Rails.env].symbolize_keys diff --git a/config/mailer.yml.sample b/config/mailer.yml.sample index 1acb12a..f29392c 100644 --- a/config/mailer.yml.sample +++ b/config/mailer.yml.sample @@ -1,8 +1,9 @@ development: address: smtp.grnet.gr port: 587 domain: example.com user_name: sender@archiving.grnet.gr password: a_pass authentication: plain enable_starttls_auto: true + default_sender: sender@archiving.grnet.gr diff --git a/lib/configuration/host.rb b/lib/configuration/host.rb index 0ac8b0b..f01362e 100644 --- a/lib/configuration/host.rb +++ b/lib/configuration/host.rb @@ -1,81 +1,121 @@ module Configuration # Helper module to add configuration getters for Host module Host # Constructs the final Bacula configuration for the host by appending configs for # # * Client # * Jobs # * Schedules # * Filesets # # by calling their `to_bacula_config_array` methods. # # @return [Array] containing each element's configuration line by line def baculize_config templates = job_templates.includes(:fileset, :schedule) result = [self] + templates.map {|x| [x, x.fileset, x.schedule] }.flatten.compact.uniq result.map(&:to_bacula_config_array) end # Constructs the final Bacula configuration for the host by appending configs for # # * Client # * Jobs # * Schedules # * Filesets # # by calling their `to_bacula_config_array` methods. # # It hides the password. # # @return [Array] containing each element's configuration line by line def baculize_config_no_pass baculize_config.join("\n").gsub(/Password = ".*"$/, 'Password = "*************"') end # Constructs an array where each element is a line for the Client's bacula config # # @return [Array] def to_bacula_config_array [ "Client {", " Name = #{name}", " Address = #{fqdn}", " FDPort = #{port}", " Catalog = #{client_settings[:catalog]}", " Password = \"#{password}\"", - " File Retention = #{file_retention} #{file_retention_period_type}", + " File Retention = #{file_retention} #{file_retention_period_type}", " Job Retention = #{job_retention} #{job_retention_period_type}", " AutoPrune = #{auto_prune_human}", "}" + ] + message_config + end + + # Constructs the messages bacula resource + # + # @return [Array] + def message_config + return [] if email_recipients.empty? + [ + "Messages {", + " Name = message_#{name}", + " mailcommand = \"#{mail_command}\"", + " operatorcommand = \"#{operator_command}\"", + " mail = root = all, !skipped", + " operator = root = mount", + " console = all, !skipped, !saved", + " append = \"/var/log/bacula/bacula.log\" = all, !skipped", + " catalog = all", + "}" ] end # Fetches the Director resource for the file-deamon configuration # file def bacula_fd_director_config [ 'Director {', " Name = \"#{Archiving.settings[:director_name]}\"", " Password = \"#{password}\"", '}' ].join("\n") end # Fetches the FileDeamon resource for the file-deamon configuration def bacula_fd_filedeamon_config [ 'FileDeamon {', " Name = #{name}", " FDport = #{port}", ' WorkingDirectory = /var/lib/bacula', ' Pid Directory = /var/run/bacula', ' Maximum Concurrent Jobs = 10', ' FDAddress = 0.0.0.0', '}' ].join("\n") end + + private + + def mail_command + "#{mail_general} -u \\\"\[Bacula\]: %t %e of %c %l\\\" -m \\\"Bacula Report %r\\\"" + end + + def operator_command + "#{mail_general} -u \\\"\[Bacula\]: Intervention needed for %j\\\" -m \\\"Intervention needed %r\\\"" + end + + def mail_general + "/usr/bin/sendEmail -f #{settings[:default_sender]}" << + " -t #{email_recipients.join(' ')}" << + " -s #{settings[:address]}:#{settings[:port]}" << + " -o tls=yes -xu #{settings[:user_name]} -xp #{settings[:password]}" + end + + def settings + Archiving.settings[:mail_settings] + end end end