-import smtplib
-from invirt.database import *
-from email.mime.text import MIMEText
-
-message = """One of the four XVM servers, aperture-science, spontaneously rebooted
-today at around 2:55 today. As of now, we're not sure what the cause of
-this was.
+#!/usr/bin/python
+"""
-Your VM %s was running on aperture-science. We have restarted it.
+VM names on stdin.
+"""
-We realize that this is an inconvenience for you, and we apologize for
-the unexpected downtime.
-
-- Evan
-for the XVM team"""
+import smtplib
+import optparse
+import sys
+from email.mime.text import MIMEText
-vms = "nforrest0 remus ghost-of-golezan tabbott 6470 dh moo mclamb dt-web htns uav-team x lsmb twopi what esg panache gardiner mailman-acl jack-o-tron knowledge ephialtes ocaml happy-go-lucky maridia cochese ocelot ecprice bibix groovy r privoxy thebes intrepid-paravirt-test qren2 quentin woodrow prolix oculus shinnyih gkovacs".split()
+from invirt import database
-def send_mail(vm):
- contact = Machine.query.filter_by(name=vm).first().contact
+def send_mail(smtp, opts, message, vm):
+ contact = database.Machine.query.filter_by(name=vm).first().contact
if '@' not in contact:
contact += '@mit.edu'
msg = MIMEText(message % vm)
msg['To'] = contact
msg['CC'] = 'XVM <xvm@mit.edu>'
msg['Reply-To'] = 'XVM <xvm@mit.edu>'
- msg['From'] = 'Evan Broder <broder@mit.edu>'
+ msg['From'] = opts.from_addr
msg['Subject'] = '[xvm] Unexpected reboot of your VM %s' % vm
- s.sendmail('Evan Broder <broder@mit.edu>',
+ smtp.sendmail(opts.from_addr,
[contact, 'xvm@mit.edu'],
msg.as_string())
-connect()
-s = smtplib.SMTP()
-s.connect()
+def main(argv):
+ parser = optparse.OptionParser(
+ usage = '%prog {-m message, -f from} [options] <vm-list',
+ description = __doc__.strip())
+ parser.add_option('-m', '--message',
+ type = 'string',
+ dest = 'message',
+ help = 'filename with body of message')
+ parser.add_option('-f', '--from',
+ type = 'string',
+ dest = 'from_addr',
+ help = 'for From: and envelope-from; first word is %(sig)s')
+ parser.add_option('--host',
+ type = 'string',
+ dest = 'host',
+ help = 'host that failed; %(host)s in message')
+ parser.add_option('--time',
+ type = 'string',
+ dest = 'time',
+ help = 'time of failure; %(time)s in message')
+ opts, args = parser.parse_args()
+
+ if len(args) or not opts.message or not opts.from_addr:
+ parser.print_help(sys.stderr)
+ return 2
+
+ opts.sig = opts.from_addr.split()[0]
+ message = file(opts.message).read() % opts.__dict__
+
+ vms = sys.stdin.read().split()
+
+ database.connect()
+ s = smtplib.SMTP()
+ s.connect()
+
+ for vm in vms:
+ send_mail(s, opts, message, vm)
-for vm in vms:
- send_mail(vm)
+ s.close()
-s.close()
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))