import os
from config import *
from xml.dom import minidom
import subprocess
from functions.fpool import *


def get_volume_disk(disk_path):
   refresh_pool()
   vol_disk = conn.storageVolLookupByPath(disk_path)
   vol_size = vol_disk.info()[1]
   vol_size_G = vol_size / (1024*1024*1024)
   return vol_size_G

def get_disks_info(vm_name):
    disks = []
    dom = conn.lookupByName(vm_name)
    raw_xml = dom.XMLDesc(0)
    xml = minidom.parseString(raw_xml)
    diskTypes = xml.getElementsByTagName('disk')
    for diskType in diskTypes:
        if diskType.getAttribute('device') == 'disk':
            disk_unit = []
            disk_id = 'Unknown'
            diskNodes = diskType.childNodes
            for diskNode in diskNodes:
                if diskNode.nodeName == 'target':
                     for attr in diskNode.attributes.keys():
                       if diskNode.attributes[attr].name == 'dev':
                          disk_id = diskNode.attributes[attr].value
                if diskNode.nodeName == 'source':
                    for attr in diskNode.attributes.keys():
                        if diskNode.attributes[attr].name == 'file':
                            blkinf = dom.blockInfo(diskNode.attributes[attr].value)
                            vol_size = os.path.getsize(diskNode.attributes[attr].value)
                            diskname = os.path.basename(diskNode.attributes[attr].value)
                            volsize = get_volume_disk(diskNode.attributes[attr].value)
                            disksize = round(blkinf[0] / 1024**3)
                            diskused = round(blkinf[1] / 1024**3)
                            if disksize > 0:
                              diskpercent = (diskused*100 / disksize)
                            else:
                              diskpercent = 0
                            disk_unit = [diskNode.attributes[attr].value,disksize,diskused,diskpercent,diskname,int(volsize)]
                if diskNode.nodeName == 'target':
                     for attr in diskNode.attributes.keys():
                       if diskNode.attributes[attr].name == 'dev':
                          disk_id = diskNode.attributes[attr].value
                          disk_unit.append(disk_id)
            disks.append(disk_unit)
    return disks

def create_attached_disk(vm_name,disk_name,disk_size,disk_id):
  cmd = "qemu-img create -f qcow2 "+str(disk_path)+str(disk_name)+".qcow2 "+str(disk_size)+"G"
  subprocess.call(cmd, shell=True)
  dom = conn.lookupByName(vm_name)
  if not dom.isActive():
    dom.create()
  disk_full = disk_path+disk_name+".qcow2"
  cmd = "virsh attach-disk --domain "+vm_name+" "+disk_full+" --target "+disk_id+" --persistent --config --live"
  subprocess.call(cmd, shell=True)
  refresh_pool()

def resize_disk(disk_file,actual_size,new_size,vm_name,disk_id):
  cmd = "virsh detach-disk '"+vm_name+"' "+disk_file+" --persistent --config --live"
  subprocess.call(cmd, shell=True)
  if int(actual_size) < int(new_size):
         cmd = "qemu-img resize -f qcow2 "+str(disk_file)+" "+str(new_size)+"G"
         subprocess.call(cmd, shell=True)
  else:
         cmd = "qemu-img resize -f qcow2 --shrink "+str(disk_file)+" "+str(new_size)+"G"
         subprocess.call(cmd, shell=True)
  cmd = "virsh attach-disk '"+str(vm_name)+"' "+str(disk_file)+" "+str(disk_id)+" --cache none --persistent --config --live"
  subprocess.call(cmd, shell=True)
  refresh_pool()

def detach_disk(vm_name,diskfile):
  cmd = "virsh detach-disk '"+str(vm_name)+"' "+str(diskfile)+" --persistent --config --live"
  subprocess.call(cmd, shell=True)
  refresh_pool()

