#!/usr/bin/python

from sys import exit
from os import rename
from os.path import exists
from time import sleep
from vm import sshtestmachine

mvover = False

def main():
    ouch = False
    t = None
    try:
        image = 'install.img'
        t = sshtestmachine(0,
                           sshmaster=None,
                           mkimage='qemu-img create -f qcow2 %s 2G' % image,
                           image=image,
                           qemuopts='-boot n') # -nographic
        t.wait()
        t=sshtestmachine(0, mkimage='true', image=image)

        while not t.ready():
            sleep(5)

        t.putfile('files/dhcp.hostname', '/etc/dhcp3/dhclient-exit-hooks.d/hostname')
        t.script("sed -ie '/^127\.0\.1\.1/d' /etc/hosts") # HATE
    except:
        ouch = True
        raise
    finally:
        try:
            if t is not None:
                t.shutdown()
        except:
            pass
        if t is not None:
            t.wait()

    if not ouch and mvover:
        rename('install.img', 'master.img')
    else:
        exit(1)
    

if __name__ == '__main__':
    main()
    exit(0)
