+def _setupRTenvironment():
+ os.environ['PATH'] = os.environ['PATH'] + ":/home/soltesz/local/bin/"
+ os.environ['RTSERVER'] = "https://rt.planet-lab.org/"
+ os.environ['RTUSER'] = "monitor"
+ os.environ['RTPASSWD'] = "ssorcmor"
+ os.environ['RTDEBUG'] = "0"
+ return
+
+def setAdminCCViaRT(ticket_id, to):
+ # Set ENV Variables/PATH
+ _setupRTenvironment()
+ if ticket_id == None or ticket_id == "":
+ raise Exception("ERROR: ticket_id must be set to some integer value")
+
+ # This will raise an exception if it is not a valid id.
+ i_ticket_id = int(ticket_id)
+
+ # create a comma-separated list
+ s_to = ",".join(to)
+ cmd = "rt edit ticket/%s set admincc='%s'" % (ticket_id, s_to)
+ (f_in, f_out, f_err) = os.popen3(cmd)
+ value = f_out.read()
+ l_values = value.split()
+ f_in.close() ; f_out.close() ; f_err.close()
+ if len(l_values) > 3 and "updated" in l_values[3]:
+ # Success
+ pass
+ else:
+ print "VALUE:", value
+ print "ERROR: RT failed to update AdminCC for ticket %s" % ticket_id
+
+ return
+
+def setSubjectViaRT(ticket_id, subject):
+ # Set ENV Variables/PATH
+ _setupRTenvironment()
+ if ticket_id == None or ticket_id == "":
+ raise Exception("ERROR: ticket_id must be set to some integer value")
+
+ # This will raise an exception if it is not a valid id.
+ i_ticket_id = int(ticket_id)
+
+ cmd = "rt edit ticket/%s set subject='%s'" % (ticket_id, subject)
+ (f_in, f_out, f_err) = os.popen3(cmd)
+ value = f_out.read()
+ l_values = value.split()
+ f_in.close() ; f_out.close() ; f_err.close()
+ if len(l_values) > 3 and "updated" in l_values[3]:
+ # Success
+ pass
+ else:
+ print "VALUE:", value
+ print "ERROR: RT failed to update subject for ticket %s" % ticket_id
+
+ return
+
+
+def addCommentViaRT(ticket_id, comment):
+ # Set ENV Variables/PATH
+ _setupRTenvironment()
+ if ticket_id == None or ticket_id == "":
+ raise Exception("ERROR: ticket_id must be set to some integer value")
+
+ # This will raise an exception if it is not a valid id.
+ i_ticket_id = int(ticket_id)
+
+ cmd = "rt comment -m '%s' ticket/%s" % (comment, i_ticket_id)
+ (f_in, f_out, f_err) = os.popen3(cmd)
+ value = f_out.read()
+ l_values = value.split()
+ f_in.close() ; f_out.close() ; f_err.close()
+ if len(l_values) > 1 and "recorded" in l_values[1]:
+ # Success
+ pass
+ else:
+ # Error
+ f_in.close() ; f_out.close() ; f_err.close()
+ print "ERROR: RT failed to add comment to id %s" % ticket_id
+
+ return
+
+def closeTicketViaRT(ticket_id, comment):
+ # Set ENV Variables/PATH
+ _setupRTenvironment()
+ if ticket_id == None or ticket_id == "":
+ raise Exception("ERROR: ticket_id must be set to some integer value")
+
+ # This will raise an exception if it is not a valid id.
+ i_ticket_id = int(ticket_id)
+
+ # Append comment to RT ticket
+ addCommentViaRT(ticket_id, comment)
+
+ if not config.debug:
+ cmd = "rt edit ticket/%s set status=resolved" % i_ticket_id
+ (f_in, f_out, f_err) = os.popen3(cmd)
+ f_in.close()
+ value = f_out.read()
+ f_out.close()
+ f_err.close()
+ l_values = value.split()
+ if len(l_values) >= 4 and "updated" in l_values[3]:
+ # Success!!
+ pass
+ else:
+ print "VALUE: ", value
+ # Failed!!
+ print "FAILED to resolve Ticket %s" % ticket_id
+ print "FAILED to resolve Ticket %s" % i_ticket_id
+
+ return
+
+def emailViaRT(subject, text, to, ticket_id=None):
+ if ticket_id == None or ticket_id == "":
+ return emailViaRT_NoTicket(subject, text, to)
+
+
+ # Set ENV Variables/PATH
+ _setupRTenvironment()
+
+ if config.mail and not config.debug:
+ setSubjectViaRT(ticket_id, subject)
+ setAdminCCViaRT(ticket_id, to)
+
+ cmd = "rt correspond -m - %s" % ticket_id
+ (f_in, f_out, f_err) = os.popen3(cmd)
+ f_in.write(text)
+ f_in.flush()
+ f_in.close()
+ value = f_out.read()
+
+ # TODO: rt doesn't write to stderr on error!!!
+ if value == "":
+ raise Exception, f_err.read()
+
+ del f_in
+ f_out.close(); del f_out
+ f_err.close(); del f_err
+ os.wait()
+
+ return ticket_id
+
+
+def emailViaRT_NoTicket(subject, text, to):