10 from email.mime.text import MIMEText
12 from invirt import database
14 def send_mail(smtp, opts, message, vm):
15 contact = database.Machine.query.filter_by(name=vm).first().contact
16 if '@' not in contact:
18 msg = MIMEText(message % vm)
20 msg['From'] = opts.from_addr
21 msg['Reply-To'] = 'XVM <xvm@mit.edu>'
22 msg['Subject'] = opts.subject % vm
23 smtp.sendmail(opts.from_addr,
26 return msg.as_string()
28 def send_summary(smtp, opts, messages):
29 msg = MIMEText('\n\n\n'.join(messages))
30 msg['To'] = 'xvm@mit.edu'
31 msg['From'] = opts.from_addr
32 msg['Subject'] = ("xvm outage-mail summary (%d): %s"
33 % (len(messages), opts.subject))
34 smtp.sendmail(opts.from_addr,
39 parser = optparse.OptionParser(
40 usage = '%prog {-m message, -f from} [options] <vm-list',
41 description = __doc__.strip())
42 parser.add_option('-m', '--message',
45 help = 'filename with body of message')
46 parser.add_option('-s', '--subject',
49 default = '[xvm] Unexpected reboot of your VM %s',
50 help = 'subject line of message; %s for VM name')
51 parser.add_option('-f', '--from',
54 help = 'for From: and envelope-from; first word is %(sig)s')
55 parser.add_option('--host',
58 help = 'host that failed; %(host)s in message')
59 parser.add_option('--time',
62 help = 'time of failure; %(time)s in message')
63 opts, args = parser.parse_args()
65 if len(args) or not opts.message or not opts.from_addr:
66 parser.print_help(sys.stderr)
69 opts.sig = opts.from_addr.split()[0]
70 message = file(opts.message).read() % opts.__dict__
72 vms = sys.stdin.read().split()
80 messages.append(send_mail(s, opts, message, vm))
81 send_summary(s, opts, messages)
85 if __name__ == '__main__':
86 sys.exit(main(sys.argv))