1 | #!/usr/bin/python |
---|
2 | """ |
---|
3 | |
---|
4 | VM names on stdin. |
---|
5 | """ |
---|
6 | |
---|
7 | import smtplib |
---|
8 | import optparse |
---|
9 | import sys |
---|
10 | from email.mime.text import MIMEText |
---|
11 | |
---|
12 | from invirt import database |
---|
13 | |
---|
14 | def send_mail(smtp, opts, message, vm): |
---|
15 | contact = database.Machine.query.filter_by(name=vm).first().contact |
---|
16 | if '@' not in contact: |
---|
17 | contact += '@mit.edu' |
---|
18 | msg = MIMEText(message % vm) |
---|
19 | msg['To'] = contact |
---|
20 | msg['CC'] = 'XVM <xvm@mit.edu>' |
---|
21 | msg['Reply-To'] = 'XVM <xvm@mit.edu>' |
---|
22 | msg['From'] = opts.from_addr |
---|
23 | msg['Subject'] = '[xvm] Unexpected reboot of your VM %s' % vm |
---|
24 | smtp.sendmail(opts.from_addr, |
---|
25 | [contact, 'xvm@mit.edu'], |
---|
26 | msg.as_string()) |
---|
27 | |
---|
28 | def main(argv): |
---|
29 | parser = optparse.OptionParser( |
---|
30 | usage = '%prog {-m message, -f from} [options] <vm-list', |
---|
31 | description = __doc__.strip()) |
---|
32 | parser.add_option('-m', '--message', |
---|
33 | type = 'string', |
---|
34 | dest = 'message', |
---|
35 | help = 'filename with body of message') |
---|
36 | parser.add_option('-f', '--from', |
---|
37 | type = 'string', |
---|
38 | dest = 'from_addr', |
---|
39 | help = 'for From: and envelope-from; first word is %(sig)s') |
---|
40 | parser.add_option('--host', |
---|
41 | type = 'string', |
---|
42 | dest = 'host', |
---|
43 | help = 'host that failed; %(host)s in message') |
---|
44 | parser.add_option('--time', |
---|
45 | type = 'string', |
---|
46 | dest = 'time', |
---|
47 | help = 'time of failure; %(time)s in message') |
---|
48 | opts, args = parser.parse_args() |
---|
49 | |
---|
50 | if len(args) or not opts.message or not opts.from_addr: |
---|
51 | parser.print_help(sys.stderr) |
---|
52 | return 2 |
---|
53 | |
---|
54 | opts.sig = opts.from_addr.split()[0] |
---|
55 | message = file(opts.message).read() % opts.__dict__ |
---|
56 | |
---|
57 | vms = sys.stdin.read().split() |
---|
58 | |
---|
59 | database.connect() |
---|
60 | s = smtplib.SMTP() |
---|
61 | s.connect() |
---|
62 | |
---|
63 | for vm in vms: |
---|
64 | send_mail(s, opts, message, vm) |
---|
65 | |
---|
66 | s.close() |
---|
67 | |
---|
68 | if __name__ == '__main__': |
---|
69 | sys.exit(main(sys.argv)) |
---|