#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on Mar 23, 2016

@author: karli
'''

import time

from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read("vpscreate.conf")

from ovirtsdk.api import API

URL             = parser.get('vpscreate', 'url')
CACERT          = parser.get('vpscreate', 'cacert')
USERNAME        = parser.get('vpscreate', 'username')
PASSWORD        = parser.get('vpscreate', 'password')

api = API(url=URL, ca_file=CACERT, username=USERNAME, password=PASSWORD)

def StopVM(VM):
    try:
        if api.vms.get(VM).status.state != "down":
            print "Stopping VM \"%s\"" % (VM)
            api.vms.get(VM).stop()
            print "Waiting for VM \"%s\" to reach Down status" % (VM)
            status = api.vms.get(VM).status.state
            while status != "down":
                time.sleep(1)
                status = api.vms.get(VM).status.state
            print "VM \"%s\" is now Down" % (VM)
        else:
            print "VM \"%s\" already Down" % (VM)
    except Exception as e:
        print "Exception in stopping VM \"%s\":\n%s" % (VM, e)
        exit(1)

def StartVM(VM):
    try:
        if api.vms.get(VM).status.state != "up":
            print "Starting VM \"%s\"" % (VM)
            api.vms.get(VM).start()
            print "Waiting for VM \"%s\" to reach Up status" % (VM)
            status = api.vms.get(VM).status.state
            while status != "up":
                time.sleep(1)
                status = api.vms.get(VM).status.state
            print "VM \"%s\" is now Up" % (VM)
        else:
            print "VM \"%s\" already Up" % (VM)
    except Exception as e:
        print "Exception in starting VM \"%s\":\n%s" % (VM, e)
        exit(1)

def ChangeClusterOnVM(VM_NAME):
    try:
        print "Changing to Cluster Nimbus for VM \"%s\"" % (VM_NAME)
        CLUSTER = api.clusters.get(name='Nimbus')
        VM = api.vms.get(VM_NAME)
        VM.set_cluster(CLUSTER)
        VM.update()
        print "Cluster changed to Nimbus for VM \"%s\"" % (VM_NAME)
    except Exception as e:
        print e

for VM in api.vms.list():
    VM_NAME = VM.name
    if not (VM_NAME == 'ckan-fbsd' or VM_NAME == 'nagios2-2' or VM_NAME == 'pkgmaster' or VM_NAME == 'puppetmaster' or VM_NAME == 'searchit' or VM_NAME == 'testbake' or VM_NAME == 'VPSTemplateFreeBSD'):
        VM_OBJECT = api.vms.get(VM_NAME)
        VM_CLUSTER_OBJECT = VM_OBJECT.get_cluster()
        VM_CLUSTER_ID = VM_CLUSTER_OBJECT.get_id()
        CLUSTER = api.clusters.get(id=VM_CLUSTER_ID).get_name()
        if CLUSTER == 'Cirrus':
            print 'VM "%s" must change cluster' % (VM_NAME)
            VM_STATUS_STATE = VM_OBJECT.status.state
            StopVM(VM_NAME)
            ChangeClusterOnVM(VM_NAME)
            if VM_STATUS_STATE == 'down':
                print 'VM "%s" was %s before so will not be restarted' % (VM_NAME,VM_STATUS_STATE)
            else:
                print 'VM "%s" was %s before and will be restarted' % (VM_NAME,VM_STATUS_STATE)
                StartVM(VM_NAME)

api.disconnect()
