#!/usr/bin/python
import smtplib
from invirt.database import *
from email.mime.text import MIMEText

sender = 'Greg Price <price@mit.edu>'
sendername = sender.split()[0]
host = 'arklay-mansion'

message = """\
One of the four XVM host servers, %s, failed tonight at
about 23:17 and was rebooted.  We're working to understand the cause.

Your VM %%s was running on %s.  We have restarted it.

We realize that this is an inconvenience for you, and we apologize for
the unexpected downtime.

- %s
for the XVM team
""" % (host, host, sendername)

vms = "amd64-test brain-trust cluster-test david-vista debathena-livecd-build ecprice gradbook greg-ns1 greg-ns2 groovy htns iodine jsoltren latex liftm medkaz mhd mksvn niska polarix price-lenny price-test8 qren2 shawn-of-awesome staxowax tdc tdc-secure thesecrete tor transistor watmap".split()

def send_mail(vm):
    contact = 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'] = sender
    msg['Subject'] = '[xvm] Unexpected reboot of your VM %s' % vm
    s.sendmail(sender,
        [contact, 'xvm@mit.edu'],
        msg.as_string())

connect()
s = smtplib.SMTP()
s.connect()

for vm in vms:
    send_mail(vm)

s.close()
