#!/usr/bin/python

import sipb_xen_database
import subprocess
import os

sipb_xen_database.connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen')

def live_vms():
    p = subprocess.Popen(['xm', 'list'], stdout=subprocess.PIPE)
    p.wait()
    output = p.stdout.read()
    vms = [x.split()[0][2:] for x in output.splitlines() if x.startswith('d_')]
    return vms

def reload_conserver():
    p = subprocess.Popen(['/etc/init.d/conserver-server', 'reload'], stdout=subprocess.PIPE)
    p.wait()

if __name__ == '__main__':
    f = open('/etc/conserver/sipb-xen-consoles.cf', 'w')
    f.write('\n'.join('console %s {}' % vm for vm in live_vms()))
    f.close()
    reload_conserver()
