Class: Autobuild::MailReporter

Inherits:
Reporter
  • Object
show all
Defined in:
lib/autobuild/mail_reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ MailReporter

Returns a new instance of MailReporter.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/autobuild/mail_reporter.rb', line 27

def initialize(config)
    super()

    @from_email = (config[:from] || default_mail)
    @to_email   = (config[:to]   || default_mail)
    @subject =
        config[:subject] ||
        "Build %result% on #{Socket.gethostname} at %time%"
    @only_errors = config[:only_errors]
    @smtp_hostname = (config[:smtp] || "localhost")
    @smtp_port = Integer(config[:port] || Socket.getservbyname('smtp'))
end

Instance Attribute Details

#from_emailObject (readonly)

Returns the value of attribute from_email.



24
25
26
# File 'lib/autobuild/mail_reporter.rb', line 24

def from_email
  @from_email
end

#only_errorsObject (readonly)

Returns the value of attribute only_errors.



24
25
26
# File 'lib/autobuild/mail_reporter.rb', line 24

def only_errors
  @only_errors
end

#smtp_hostnameObject (readonly)

Returns the value of attribute smtp_hostname.



24
25
26
# File 'lib/autobuild/mail_reporter.rb', line 24

def smtp_hostname
  @smtp_hostname
end

#smtp_portObject (readonly)

Returns the value of attribute smtp_port.



24
25
26
# File 'lib/autobuild/mail_reporter.rb', line 24

def smtp_port
  @smtp_port
end

#subjectObject (readonly)

Returns the value of attribute subject.



24
25
26
# File 'lib/autobuild/mail_reporter.rb', line 24

def subject
  @subject
end

#to_emailObject (readonly)

Returns the value of attribute to_email.



24
25
26
# File 'lib/autobuild/mail_reporter.rb', line 24

def to_email
  @to_email
end

Instance Method Details

#default_mailObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/autobuild/mail_reporter.rb', line 13

def default_mail
    Etc.endpwent
    uname = while (pwent = Etc.getpwent)
                break pwent.name if pwent.uid == Process.uid
            end

    raise "FATAL: cannot find a user with uid=#{Process.uid}" unless uname

    "#{pwent.name}@#{Socket.gethostname}"
end

#error(error) ⇒ Object



40
41
42
# File 'lib/autobuild/mail_reporter.rb', line 40

def error(error)
    send_mail("failed", error.to_s) if error.mail?
end

#send_mail(result, body = "") ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/autobuild/mail_reporter.rb', line 50

def send_mail(result, body = "")
    mail = RMail::Message.new
    mail.header.date = Time.now
    mail.header.from = from_email
    mail.header.subject = subject.
        gsub('%result%', result).
        gsub('%time%', Time.now.to_s).
        gsub('%hostname%', Socket.gethostname)

    part = RMail::Message.new
    part.header.set('Content-Type', 'text/plain')
    part.body = body
    mail.add_part(part)

    # Attach log files
    Reporting.each_log do |file|
        name = file[Autobuild.logdir.size..-1]
        mail.add_file(name, file)
    end

    # Send the mails
    if smtp_hostname =~ %r{/} && File.directory?(File.dirname(smtp_hostname))
        File.open(smtp_hostname, 'w') do |io|
            io.puts "From: #{from_email}"
            io.puts "To: #{to_email.join(' ')}"
            io.write RMail::Serialize.write('', mail)
        end
        puts "saved notification email in #{smtp_hostname}"
    else
        smtp = Net::SMTP.new(smtp_hostname, smtp_port)
        smtp.start do
            to_email.each do |email|
                mail.header.to = email
                smtp.send_mail(RMail::Serialize.write('', mail),
                               from_email, email)
            end
        end

        # Notify the sending
        puts "sent notification mail to #{to_email} with source #{from_email}"
    end
end

#successObject



44
45
46
47
48
# File 'lib/autobuild/mail_reporter.rb', line 44

def success
    unless only_errors
        send_mail("success", Autobuild.post_success_message || "")
    end
end