3 # Copyright (c) 2004 The Trustees of Princeton University (Trustees).
5 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
7 # $Id: mailer.py,v 1.10 2007/08/08 13:28:06 soltesz Exp $
16 logger = logging.getLogger("monitor")
19 FROM="monitor@planet-lab.org"
21 def reformat_for_rt(text):
22 lines = text.split("\n")
25 spaced_text += " %s\n" %line
29 def _setupRTenvironment():
30 os.environ['PATH'] = os.environ['PATH'] + ":" + config.RT_WEB_TOOLS_PATH
31 os.environ['RTSERVER'] = config.RT_WEB_SERVER
32 os.environ['RTUSER'] = config.RT_WEB_USER
33 os.environ['RTPASSWD'] = config.RT_WEB_PASSWORD
34 os.environ['RTDEBUG'] = config.RT_WEB_DEBUG
37 def setTicketStatus(ticket_id, status):
39 if ticket_id == None or ticket_id == "":
42 cmd = "rt edit ticket/%s set status=%s" % (ticket_id, status)
43 (f_in, f_out, f_err) = os.popen3(cmd)
45 l_values = value.split('\n')
46 return "".join(l_values).strip()
48 def getTicketStatus(ticket_id):
50 if ticket_id == None or ticket_id == "":
53 cmd = "rt show -t ticket -f id,subject,status,queue,created %s" % (ticket_id)
54 (f_in, f_out, f_err) = os.popen3(cmd)
56 l_values = value.split('\n')
59 if len(line) == 0: continue
60 vals = line.split(':')
62 r_values[key] = ":".join(vals[1:])
63 r_values[key] = r_values[key].strip()
65 r_values['Created'] = calendar.timegm(time.strptime(r_values['Created']))
68 def setAdminCCViaRT(ticket_id, to):
69 # Set ENV Variables/PATH
71 if ticket_id == None or ticket_id == "":
72 raise Exception("ERROR: ticket_id must be set to some integer value")
74 # This will raise an exception if it is not a valid id.
75 i_ticket_id = int(ticket_id)
77 # create a comma-separated list
79 cmd = "rt edit ticket/%s set admincc='%s'" % (ticket_id, s_to)
80 (f_in, f_out, f_err) = os.popen3(cmd)
82 l_values = value.split()
83 f_in.close() ; f_out.close() ; f_err.close()
84 if len(l_values) > 3 and "updated" in l_values[3]:
89 print "ERROR: RT failed to update AdminCC for ticket %s" % ticket_id
93 def setSubjectViaRT(ticket_id, subject):
94 # Set ENV Variables/PATH
96 if ticket_id == None or ticket_id == "":
97 raise Exception("ERROR: ticket_id must be set to some integer value")
99 # This will raise an exception if it is not a valid id.
100 i_ticket_id = int(ticket_id)
102 cmd = "rt edit ticket/%s set subject='%s'" % (ticket_id, subject)
103 (f_in, f_out, f_err) = os.popen3(cmd)
105 l_values = value.split()
106 f_in.close() ; f_out.close() ; f_err.close()
107 if len(l_values) > 3 and "updated" in l_values[3]:
111 print "VALUE:", value
112 print "ERROR: RT failed to update subject for ticket %s" % ticket_id
117 def addCommentViaRT(ticket_id, comment):
118 # Set ENV Variables/PATH
119 _setupRTenvironment()
120 if ticket_id == None or ticket_id == "":
121 raise Exception("ERROR: ticket_id must be set to some integer value")
123 # This will raise an exception if it is not a valid id.
124 i_ticket_id = int(ticket_id)
126 cmd = "rt comment -m '%s' ticket/%s" % (comment, i_ticket_id)
127 (f_in, f_out, f_err) = os.popen3(cmd)
129 l_values = value.split()
130 f_in.close() ; f_out.close() ; f_err.close()
131 if len(l_values) > 1 and "recorded" in l_values[1]:
136 f_in.close() ; f_out.close() ; f_err.close()
137 print "ERROR: RT failed to add comment to id %s" % ticket_id
141 def closeTicketViaRT(ticket_id, comment):
142 # Set ENV Variables/PATH
143 _setupRTenvironment()
144 if ticket_id == None or ticket_id == "":
145 raise Exception("ERROR: ticket_id must be set to some integer value")
147 # This will raise an exception if it is not a valid id.
148 i_ticket_id = int(ticket_id)
150 # Append comment to RT ticket
151 addCommentViaRT(ticket_id, comment)
154 cmd = "rt edit ticket/%s set status=resolved" % i_ticket_id
155 (f_in, f_out, f_err) = os.popen3(cmd)
160 l_values = value.split()
161 if len(l_values) >= 4 and "updated" in l_values[3]:
165 print "VALUE: ", value
167 print "FAILED to resolve Ticket %s" % ticket_id
168 print "FAILED to resolve Ticket %s" % i_ticket_id
172 def emailViaRT(subject, text, to, ticket_id=None):
173 if ticket_id == None or ticket_id == "" or ticket_id == 0:
175 return emailViaRT_NoTicket(subject, text, to)
178 # Set ENV Variables/PATH
179 _setupRTenvironment()
181 if config.mail and not config.debug:
182 setSubjectViaRT(ticket_id, subject)
183 setAdminCCViaRT(ticket_id, to)
185 cmd = "rt correspond -m - %s" % ticket_id
186 (f_in, f_out, f_err) = os.popen3(cmd)
192 # TODO: rt doesn't write to stderr on error!!!
194 raise Exception, f_err.read()
197 f_out.close(); del f_out
198 f_err.close(); del f_err
204 def emailViaRT_NoTicket(subject, text, to):
205 """Use RT command line tools to send email.
206 return the generated RT ticket ID number.
210 if config.mail and config.debug:
213 # Set ENV Variables/PATH
214 _setupRTenvironment()
216 # NOTE: AdminCc: (in PLC's RT configuration) gets an email sent.
217 # This is not the case (surprisingly) for Cc:
218 input_text = "Subject: %s\n"
219 input_text += "Requestor: monitor@planet-lab.org\n"
220 input_text += "id: ticket/new\n"
221 input_text += "Queue: Monitor\n"
223 input_text += "AdminCc: %s\n" % recipient
224 input_text += "Text: %s"
226 # Add a space for each new line to get RT to accept the file.
227 spaced_text = reformat_for_rt(text)
229 if config.mail and not config.debug:
230 cmd = "rt create -i -t ticket"
231 (f_in, f_out, f_err) = os.popen3(cmd)
232 f_in.write(input_text % (subject, spaced_text))
237 # TODO: rt doesn't write to stderr on error!!!
239 raise Exception, f_err.read()
241 print "MAILER: ticket value == %s" % value.split()[2]
242 i_ticket = int(value.split()[2])
243 # clean up the child process.
244 f_in.close(); del f_in
245 f_out.close(); del f_out
246 f_err.close(); del f_err
248 elif config.mail and config.debug:
249 email(subject, spaced_text, to)
256 def email(subject, text, to):
257 """Create a mime-message that will render HTML in popular
258 MUAs, text in better ones"""
263 if (config.mail and config.debug) or (not config.mail and not config.debug and config.bcc):
266 out = cStringIO.StringIO() # output buffer for our message
267 txtin = cStringIO.StringIO(text)
269 writer = MimeWriter.MimeWriter(out)
271 # set up some basic headers... we put subject here
272 # because smtplib.sendmail expects it to be in the
275 writer.addheader("Subject", subject)
276 if to.__class__ == [].__class__ :
277 writer.addheader("To", to[0])
279 for dest in to[1:len(to)]:
282 writer.addheader("Cc", cc)
284 writer.addheader("To", to)
286 if config.bcc and not config.debug:
287 writer.addheader("Bcc", config.email)
289 writer.addheader("Reply-To", 'monitor@planet-lab.org')
291 writer.addheader("MIME-Version", "1.0")
293 # start the multipart section of the message
294 # multipart/alternative seems to work better
295 # on some MUAs than multipart/mixed
297 writer.startmultipartbody("alternative")
298 writer.flushheaders()
300 # the plain text section
302 subpart = writer.nextpart()
303 subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
304 pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
305 mimetools.encode(txtin, pout, 'quoted-printable')
308 # Now that we're done, close our writer and
309 # return the message body
317 # mail and debug, 'to' changed at the beginning'
318 # nomail, but report who I'd send to.
320 for mta in [MTA, 'golf.cs.princeton.edu']:
322 # This is normal operation
327 server = smtplib.SMTP(mta)
328 #server = smtplib.SMTP('golf.cs.princeton.edu')
329 server.sendmail(FROM, to, msg)
330 if config.bcc and not config.debug:
331 server.sendmail(FROM, config.email, msg)
333 except Exception, err:
334 print "Mailer error1: failed using MTA(%s) with: %s" % (mta, err)
336 elif not config.debug and not config.mail and config.bcc:
337 for mta in [MTA, 'golf.cs.princeton.edu']:
339 server = smtplib.SMTP(mta)
340 server.sendmail(FROM, to, msg)
342 except Exception, err:
343 print "Mailer error2: failed using MTA(%s) with: %s" % (mta, err)
345 #print "Would mail %s" %to
346 logger.debug("Would send mail to %s" % to)
348 if __name__=="__main__":
352 #email("[spam] bcc test from golf.cs.princeton.edu",
353 # "It gets to both recipients",
354 # "soltesz@cs.utk.edu")
355 #emailViaRT("rt via golf",
356 # "It gets to both recipients",
357 # "soltesz@cs.utk.edu")
358 email("Re: [PL #21323] TEST 7",
359 mailtxt.newbootcd_one[1] % {'hostname_list':"hostname list..."},
360 ['monitor@planet-lab.org'])
361 #print "ticketid: %d" % id
362 #id = plc.siteId(["alice.cs.princeton.edu"])
365 #email('TEST', emailTxt.mailtxt.ssh % {'hostname': "ALICE.cs.princeton.edu"}, "tech-" + id + "@sites.planet-lab.org")
368 #email("TEST111", "I'd like to see if this works anywhere", ["soltesz@cs.princeton.edu", "soltesz@cs.utk.edu"])
369 #print "mailer does nothing in main()"