Class: Berta::NotificationManager
- Inherits:
-
Object
- Object
- Berta::NotificationManager
- Defined in:
- lib/berta/notification_manager.rb
Overview
Class for managing notifications, setting and sending them
Instance Attribute Summary collapse
-
#email_template ⇒ Object
readonly
Returns the value of attribute email_template.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
-
#initialize(service) ⇒ NotificationManager
constructor
Creates NotificationManager object with given service.
-
#notify_user(user, user_vms) ⇒ Object
Notifies given user about given vms.
-
#notify_users(vms) ⇒ Object
Notifies users.
-
#send_notification(user, vms) ⇒ Object
Sends email to given user about given vms using sendmail.
-
#uids_to_notify(vms) ⇒ Hash<String, Array<VirtualMachineHandler>>
Finds and return uids of users from vms that should be notified.
Constructor Details
#initialize(service) ⇒ NotificationManager
Creates NotificationManager object with given service. Notification manager needs service for fetching user data from opennebula database. Also initializes email template object.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/berta/notification_manager.rb', line 17 def initialize(service) @service = service email_file = 'email.erb'.freeze email_template_path = "#{File.dirname(__FILE__)}/../../config/#{email_file}" email_template_path = "/etc/berta/#{email_file}" \ if File.exist?("/etc/berta/#{email_file}") email_template_path = "#{ENV['HOME']}/.berta/#{email_file}" \ if File.exist?("#{ENV['HOME']}/.berta/#{email_file}") @email_template = Tilt.new(email_template_path) end |
Instance Attribute Details
#email_template ⇒ Object (readonly)
Returns the value of attribute email_template.
8 9 10 |
# File 'lib/berta/notification_manager.rb', line 8 def email_template @email_template end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
8 9 10 |
# File 'lib/berta/notification_manager.rb', line 8 def service @service end |
Instance Method Details
#notify_user(user, user_vms) ⇒ Object
Notifies given user about given vms.
51 52 53 54 55 56 57 |
# File 'lib/berta/notification_manager.rb', line 51 def notify_user(user, user_vms) send_notification(user, user_vms) rescue ArgumentError, Berta::Errors::Entities::NoUserEmailError => e logger.error e. else user_vms.each(&:update_notified) end |
#notify_users(vms) ⇒ Object
Notifies users. Finds all users that should be notified and sends email to each of them. Email is generated from template.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/berta/notification_manager.rb', line 34 def notify_users(vms) begin users = service.users rescue Berta::Errors::BackendError => e logger.error e. return end uids_to_notify(vms).each do |uid, uvms| user = users.find { |usr| usr['ID'] == uid } notify_user(user, uvms) if user end end |
#send_notification(user, vms) ⇒ Object
Sends email to given user about given vms using sendmail. Email is generated from email template.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/berta/notification_manager.rb', line 77 def send_notification(user, vms) user_email = user['TEMPLATE/EMAIL'] user_name = user['NAME'] raise Berta::Errors::Entities::NoUserEmailError, "User: #{user_name} with id: #{user['ID']} has no email set" \ unless user_email email_text = email_template.render(Hash, user_email: user_email, user_name: user_name, vms: vms_data(vms)) logger.debug "Sending mail to user: #{user_name} with email: #{user_email}:\n#{email_text}" return if Berta::Settings['dry-run'] mail = Mail.new(email_text) mail.delivery_method :sendmail mail.deliver end |
#uids_to_notify(vms) ⇒ Hash<String, Array<VirtualMachineHandler>>
Finds and return uids of users from vms that should be notified.
64 65 66 67 68 69 |
# File 'lib/berta/notification_manager.rb', line 64 def uids_to_notify(vms) notif = vms.keep_if(&:should_notify?) uidsvm = Hash.new([]) notif.each { |vm| uidsvm[vm.handle['UID']] += [vm] } uidsvm end |