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.

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

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

- Evan
for the XVM team"""

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()

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

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

for vm in vms:
    send_mail(vm)

s.close()