Exemple #1
0
 def get_name_vm_dest(self):
   self.P = physical_host(self.ip_physique)
   self.ssh = Ssh(self.ip_physique)
   
   self.ssh.del_keyfile()
   self.ssh.del_keyfile_client(self.ip_physique)
   
   if self.no_pxe == True:
     print "no_pxe : %s" % self.no_pxe
     self.ssh.copy_id_client(self.ip_physique)
   
   name_vm_dest = self.ssh.exec_cmd("hostname")
   try :
     self.name_vm_dest = name_vm_dest[0].strip()
   except:
     if self.no_pxe == True:
       print ""
       print "!!! ATTENTION L'OPTION 'SERVEUR PXE' A ETE DESACTIVE AFIN DE PASSER PAR UN LIVECD DANS LE CEDEROM !!!"
       print ""
       print "Assurez vous que : "
       print "   - Le port du switch soit bien UnTaggé avec le vlan %s" % self.vlan
       print "   - Votre LiveCD soit bien inséré et configuré avec l'ip correspondante :"
       print "       # ip addr add %s/24 dev eth0" % self.ip_physique
       print "   - Le paquet 'dcfldd' soit installé sur le livecd"
     else:
       print "connexion impossible avec le serveur physique, le CFp2v est bien descendu sur le serveur physique ?"
     sys.exit()
   self.new_name_vm_ip = self.ip_physique
   return self.name_vm_dest
Exemple #2
0
 def __init__(self, ip_srv_phy="", xenmgtconf="", ds="", vg_name=""):
     xenmgtconf = {}
     execfile("/etc/xen/xenmgt-p2v.conf", xenmgtconf)
     self.xenmgtconf = xenmgtconf
     self.bs = self.xenmgtconf["DD_BS"]
     self.P = physical_host(ip_srv_phy)
     self.ip_srv_phy = ip_srv_phy
     self.ssh = Ssh(self.ip_srv_phy)
     self.type_p2v = "PARA"
     self.type_vm = "P2V"
     self.sysadmin = ds
     self.vgname = vg_name
Exemple #3
0
    def get_name_vm_dest(self):
        self.P = physical_host(self.ip_physique)
        self.ssh = Ssh(self.ip_physique)

        self.ssh.del_keyfile()
        self.ssh.del_keyfile_client(self.ip_physique)

        if self.no_pxe == True:
            print "no_pxe : %s" % self.no_pxe
            self.ssh.copy_id_client(self.ip_physique)

        name_vm_dest = self.ssh.exec_cmd("hostname")
        try:
            self.name_vm_dest = name_vm_dest[0].strip()
        except:
            if self.no_pxe == True:
                print ""
                print "!!! ATTENTION L'OPTION 'SERVEUR PXE' A ETE DESACTIVE AFIN DE PASSER PAR UN LIVECD DANS LE CEDEROM !!!"
                print ""
                print "Assurez vous que : "
                print "   - Le port du switch soit bien UnTaggé avec le vlan %s" % self.vlan
                print "   - Votre LiveCD soit bien inséré et configuré avec l'ip correspondante :"
                print "       # ip addr add %s/24 dev eth0" % self.ip_physique
                print "   - Le paquet 'dcfldd' soit installé sur le livecd"
            else:
                print "connexion impossible avec le serveur physique, le CFp2v est bien descendu sur le serveur physique ?"
            sys.exit()
        self.new_name_vm_ip = self.ip_physique
        return self.name_vm_dest
Exemple #4
0
 def __init__(self, ip_srv_phy="", xenmgtconf="", ds="", vg_name=""):
     xenmgtconf = {}
     execfile("/etc/xen/xenmgt-p2v.conf", xenmgtconf)
     self.xenmgtconf = xenmgtconf
     self.bs = self.xenmgtconf["DD_BS"]
     self.P = physical_host(ip_srv_phy)
     self.ip_srv_phy = ip_srv_phy
     self.ssh = Ssh(self.ip_srv_phy)
     self.type_p2v = "PARA"
     self.type_vm = "P2V"
     self.sysadmin = ds
     self.vgname = vg_name
Exemple #5
0
 def del_lock_dhcpd(self, ip_pxe, vm_name):
     ssh_pxe = Ssh(ip_pxe)
     ssh_pxe.exec_cmd("pxe_DelLockDhcpd %s" % vm_name)
Exemple #6
0
 def add_lock_dhcpd(self, ip_pxe, vm_name):
     ssh_pxe = Ssh(ip_pxe)
     ssh_pxe.exec_cmd("pxe_AddLockDhcpd %s" % vm_name)
Exemple #7
0
 def add_filter_dhcpd(self, ip_pxe, vm_name, mac_addr, ip_vm, vlan):
     print "Ajout du filtre dhcp  -> '%s, %s, %s, %s'" % (vm_name, mac_addr,
                                                          ip_vm, vlan)
     ssh_pxe = Ssh(ip_pxe)
     ssh_pxe.exec_cmd("pxe_AddFilterDhcpd %s %s %s %s" %
                      (vm_name, mac_addr, ip_vm, vlan))
Exemple #8
0
 def add_filter_dhcpd(self,ip_pxe,vm_name,mac_addr,ip_vm,vlan):
   print "Ajout du filtre dhcp  -> '%s, %s, %s, %s'" % (vm_name,mac_addr,ip_vm,vlan)
   ssh_pxe = Ssh(ip_pxe)
   ssh_pxe.exec_cmd("pxe_AddFilterDhcpd %s %s %s %s" % (vm_name,mac_addr,ip_vm,vlan))
Exemple #9
0
 def del_lock_dhcpd(self,ip_pxe,vm_name):
   ssh_pxe = Ssh(ip_pxe)
   ssh_pxe.exec_cmd("pxe_DelLockDhcpd %s" % vm_name)
Exemple #10
0
class physical_host:

  def __init__(self,server):
    self.server = server
    self.ssh = Ssh(self.server)

  def exec_cmd_ssh(self,cmd=''):
    result = self.ssh.exec_cmd(cmd)
    return result
  
  def get_memory(self):
    liste = self.exec_cmd_ssh('dmidecode -t memory | grep Size | grep MB')
    somme=0
    for i in liste:
      val = i.split(":")[1].strip().split()[0]
      somme = int(somme) + int(val)
    return somme

  def get_memory_swap(self):
    liste = self.exec_cmd_ssh('free -b | grep -i swap ')
    memory_swap = liste[0].split()[1]
    return memory_swap
  
  def get_memory1(self):
    liste = self.exec_cmd_ssh('free -m | grep -i Mem ')
    memory = float(liste[0].split()[1])
    memory_ceil = math.ceil(memory / 1024)
    memory = "%d" % (round(memory_ceil,0) * 1024)
    return memory

  def get_mac_addr(self,interface):
    cmd = "ifconfig %s | grep HWaddr" % interface
    liste = self.exec_cmd_ssh(cmd)
    for i in liste:
      mac_addr = i.split()[4]
    return mac_addr

  def get_interfaces(self):
    liste = self.exec_cmd_ssh("cat /proc/net/dev | sed '1,2'd | grep eth")
    INTERFACE={}
    for i in liste:
      NOM_INTERFACE = i.split(":")[0].strip()
      MAC_ADDR = self.get_mac_addr(NOM_INTERFACE)
      INTERFACE[NOM_INTERFACE] = MAC_ADDR
    return INTERFACE

  def get_cpu(self):
    line = self.exec_cmd_ssh('cat /proc/cpuinfo | grep processor | wc -l')
    nb_cpu = line[0].strip()
    return nb_cpu

  def is_livecd(self):
    #liste = self.exec_cmd_ssh('cat /etc/issue | grep -i slax | wc -l')
    liste = self.exec_cmd_ssh('hostname | grep -i sysresccd | wc -l')
    if int(liste[0].strip()) >= 1:
	    return "True"
    else:
      liste = self.exec_cmd_ssh('cat /etc/issue | grep -i slax | wc -l')
      if int(liste[0].strip()) >= 1:
        return "True"
      else:
	     return "False"

  def get_idev(self):
    version = self.get_version_os()
    if version["OS"] == "CentOS":
      self.idev="xvda"
    if version["OS"] == "Ubuntu":
      self.idev="xvda"
    if version["OS"] == "Debian":
      self.idev="hda"
    return self.idev

  def copy_fstab(self):
    copy_file_fstab = self.exec_cmd_ssh('cp /etc/fstab /etc/fstab_without_uuid')

  def cible_link_uuid(self,uuid):
    nom_device = self.exec_cmd_ssh('readlink -f /dev/disk/by-uuid/%s' % uuid)
    return nom_device

  def get_fstab_without_uuid(self):
    self.copy_fstab()
    liste = self.exec_cmd_ssh('cat /etc/fstab | grep ^UUID')
    for i in liste:
      uuid = i.split()[0].split('=')[1]
      cible = self.cible_link_uuid(uuid)
      self.exec_cmd_ssh('sed -i s#UUID=%s#%s# /etc/fstab_without_uuid' % (uuid,cible[0].strip()))

  def get_partitions_para(self):
    self.get_idev()
    self.get_fstab_without_uuid()
    liste = self.exec_cmd_ssh('cat /etc/fstab_without_uuid | grep ^/dev | grep -v iso9660 | grep -v floppy | grep -v vfat | grep -v cdrom')
    PARTITIONS={}
    cpt=1
    for i in liste:
      nom_device = i.split()[0].strip()
      fs = i.split()[2].strip()
      nom_part = i.split()[1].strip()
      taille = self.taille_part(nom_device,fs)
      UUID = ""
      if fs != "swap":
        UUID = self.GetUuidByDisk(nom_device)
        if cpt == 4:
          cpt=5
        nom_device_para="%s%s" % (self.idev,cpt)
        PARTITIONS[nom_device_para] = {"DEVICE":nom_device,"FS":fs,"SIZE":taille,"PARTITION":nom_part,"UUID":UUID}
        cpt=(cpt + 1)
    for i in liste:
      nom_device = i.split()[0].strip()
      fs = i.split()[2].strip()
      if fs == "swap":
        nom_device_para="%s%s" % (self.idev,cpt)
        taille_swap = self.get_memory_swap()
        PARTITIONS[nom_device_para] = {"DEVICE":nom_device,"FS":fs,"SIZE":taille_swap,"PARTITION":nom_part}
    return PARTITIONS

  def get_partitions_hvm(self):
    self.get_fstab_without_uuid()
    #self.detect_lvm()
    cpt='`'
    #liste = self.exec_cmd_ssh('LANG=POSIX fdisk -l /dev/cciss/c0d0 2> /dev/null | grep "^Disk /dev" | grep -v "mapper" | sed "s/Disk//" | sed "s#/dev/##"')
    #liste = self.exec_cmd_ssh('LANG=POSIX fdisk -l /dev/sda 2> /dev/null | grep "^Disk /dev" | grep -v "mapper" | sed "s/Disk//" | sed "s#/dev/##"')
    liste = self.exec_cmd_ssh('LANG=POSIX fdisk -l 2> /dev/null | grep "^Disk /dev" | grep -v "mapper" | sed "s/Disk//" | sed "s#/dev/##"')
    PARTITION={}
    for i in liste:
      nom_part = i.split(":")[0].strip()
      nom_part_hvm = "hd%s" % chr(ord(cpt) + 1)
      taille = i.split(",")[1].split()[0]
      PARTITION[nom_part] = {"DEVICE":nom_part_hvm, "SIZE":taille}
    return PARTITION
    # {'cciss/c0d0': ('hda', '120034123776'),'cciss/c0d1': ('hdb', '120034123776')}


  def taille_part(self,partition,filesystem):
    if (filesystem == "ext2") or (filesystem == "ext3") or (filesystem == "ext4"):
      return self.taille_part_ext(partition)
  
  def taux_occupation(self,partition,filesystem):
    if (filesystem == "ext2") or (filesystem == "ext3") or (filesystem == "ext4"):
      return self.taux_occupation_ext(partition)


  ######################################################################################
  ###################  FONCTIONS RESERVER POUR FILESYSTEM EXT   ########################
  ######################################################################################
  def taille_part_ext(self,partition):
    Bl_count = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Block count"')
    Bl_size = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Block size"')
    Bloc_count = Bl_count[0].split(":")[1].strip()
    Bloc_size = Bl_size[0].split(":")[1].strip()
    Taille = (int(Bloc_count) * int(Bloc_size))
    return Taille
  
  def taille_part_free_ext(self,partition):
    Bl_free = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Free blocks"')
    Bl_size = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Block size"')
    Bloc_free = Bl_free[0].split(":")[1].strip()
    Bloc_size = Bl_size[0].split(":")[1].strip()
    Taille_free = (int(Bloc_free) * int(Bloc_size))
    return Taille_free

  def taux_occupation_ext(self,partition):
    taille_total = self.taille_part_ext(partition)
    taille_libre = self.taille_part_free_ext(partition)
    tx_occup = 100 - ((taille_libre * 100) / taille_total)
    return tx_occup


  def Convert_to_octects(self,taille):
    """ converti Ko, Mo, Go  et To en octets """
    

  def GetUuidByDisk(self,partition):
    version = self.get_version_os()    
    if version["VERSION"] == "3.1":
      UUID = ""
    else:
      UuidByDisk = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Filesystem UUID"')
      UUID = UuidByDisk[0].split(":")[1].strip()
    return UUID


  def get_all_partitions(self):
    ALL_PARTITIONS={}
    ALL_PARTITIONS["PARA"] = (self.get_partitions_para())
    ALL_PARTITIONS["HVM"] = (self.get_partitions_hvm())
    return ALL_PARTITIONS

  def detect_lvm(self):
    detect_lvm = "0"
    nb_lv = self.exec_cmd_ssh('LANG=POSIX fdisk -l 2> /dev/null| grep "^Disk /dev" | grep mapper | wc -l')
    if  nb_lv[0].strip() >= 1:
      detect_lvm = "1"
    if detect_lvm == "1": 
      print "LVM detecte, en mode HVM, le LVM sera fait dans la VM."

  def detect_lvdisplay(self):
    detect = self.exec_cmd_ssh('which lvdisplay | wc -l')
    return detect[0]

 
  def is_lv(self,fs):
    if self.detect_lvdisplay() != 0:
      check_is_lv = self.exec_cmd_ssh('lvdisplay | grep \"%s\" | wc -l' % fs)
      if check_is_lv[0] >= 1:
        return 1
      else:
        return 0
    else:
      return 0
 
  def get_version_os(self):
    liste = self.exec_cmd_ssh('cat /etc/issue')
    os_version=[]
    if liste[0].split()[0] == "CentOS":
      os_version = {"OS":liste[0].split()[0],"VERSION":liste[0].split()[2]}
    if liste[0].split()[0] == "Ubuntu":
      os_version = {"OS":liste[0].split()[0],"VERSION":liste[0].split()[1]}
    if liste[0].split()[0] == "Debian":
      os_version = {"OS":liste[0].split()[0],"VERSION":liste[0].split()[2]}
    return os_version

  def get_eligibility_check_fstab(self):
    CHECK_LABEL = self.exec_cmd_ssh('less /etc/fstab | grep ^LABEL= | wc -l')
    CHECK_LABEL = CHECK_LABEL[0].strip()
    if CHECK_LABEL == "0":
      ret = 1
    else:
      ret = 0
    return ret

  def get_eligibility_check_fs_ext(self):
    CHECK_FS_EXT = self.exec_cmd_ssh('df -PlT | egrep -v "Type|rootfs|none|udev|tmpfs|ext[234]" | wc -l')
    CHECK_FS_EXT = CHECK_FS_EXT[0].strip()
    if CHECK_FS_EXT == "0":
      ret = 1
    else:
      ret = 0
    return ret
Exemple #11
0
class physical_host:

  def __init__(self,server):
    self.server = server
    self.ssh = Ssh(self.server)

  def exec_cmd_ssh(self,cmd=''):
    result = self.ssh.exec_cmd(cmd)
    return result
  
  def get_memory(self):
    liste = self.exec_cmd_ssh('dmidecode -t memory | grep Size | grep MB')
    somme=0
    for i in liste:
      val = i.split(":")[1].strip().split()[0]
      somme = int(somme) + int(val)
    return somme

  def get_memory_swap(self):
    liste = self.exec_cmd_ssh('free -b | grep -i swap ')
    memory_swap = liste[0].split()[1]
    return memory_swap
  
  def get_memory1(self):
    liste = self.exec_cmd_ssh('free -m | grep -i Mem ')
    memory_swap = liste[0].split()[1]
    return memory_swap

  def get_mac_addr(self,interface):
    cmd = "ifconfig %s | grep HWaddr" % interface
    liste = self.exec_cmd_ssh(cmd)
    for i in liste:
      mac_addr = i.split()[4]
    return mac_addr

  def get_interfaces(self):
    liste = self.exec_cmd_ssh("cat /proc/net/dev | sed '1,2'd | grep eth")
    INTERFACE={}
    for i in liste:
      NOM_INTERFACE = i.split(":")[0].strip()
      MAC_ADDR = self.get_mac_addr(NOM_INTERFACE)
      INTERFACE[NOM_INTERFACE] = MAC_ADDR
    return INTERFACE

  def get_cpu(self):
    line = self.exec_cmd_ssh('cat /proc/cpuinfo | grep processor | wc -l')
    nb_cpu = line[0].strip()
    return nb_cpu

  def is_livecd(self):
    liste = self.exec_cmd_ssh('cat /etc/issue | grep -i slax | wc -l')
    if int(liste[0].strip()) >= 1:
	  return "true"
    else:
	  return "false"

  def get_idev(self):
    version = self.get_version_os()
    if version["OS"] == "CentOS":
      self.idev="xvda"
    if version["OS"] == "Ubuntu":
      self.idev="xvda"
    if version["OS"] == "Debian":
      self.idev="hda"
    return self.idev

  def copy_fstab(self):
    copy_file_fstab = self.exec_cmd_ssh('cp /etc/fstab /etc/fstab_without_uuid')

  def cible_link_uuid(self,uuid):
    nom_device = self.exec_cmd_ssh('readlink -f /dev/disk/by-uuid/%s' % uuid)
    return nom_device

  def get_fstab_without_uuid(self):
    self.copy_fstab()
    liste = self.exec_cmd_ssh('cat /etc/fstab | grep ^UUID')
    for i in liste:
      uuid = i.split()[0].split('=')[1]
      cible = self.cible_link_uuid(uuid)
      self.exec_cmd_ssh('sed -i s#UUID=%s#%s# /etc/fstab_without_uuid' % (uuid,cible[0].strip()))

  def get_partitions_para(self):
    self.get_idev()
    self.get_fstab_without_uuid()
    liste = self.exec_cmd_ssh('cat /etc/fstab_without_uuid | grep ^/dev | grep -v iso9660 | grep -v floppy | grep -v vfat | grep -v cdrom')
    PARTITIONS={}
    cpt=1
    for i in liste:
      nom_device = i.split()[0].strip()
      fs = i.split()[2].strip()
      nom_part = i.split()[1].strip()
      taille = self.taille_part(nom_device,fs)
      if fs != "swap": 
        if cpt == 4:
          cpt=5
        nom_device_para="%s%s" % (self.idev,cpt)
        PARTITIONS[nom_device_para] = (nom_device,fs,taille,nom_part)
        cpt=(cpt + 1)
    for i in liste:
      nom_device = i.split()[0].strip()
      fs = i.split()[2].strip()
      if fs == "swap":
        nom_device_para="%s%s" % (self.idev,cpt)
        taille_swap = self.get_memory_swap()
        PARTITIONS[nom_device_para] = (nom_device,fs,taille_swap,nom_part)
    return PARTITIONS

  def taille_part(self,partition,filesystem):
    if (filesystem == "ext2") or (filesystem == "ext3") or (filesystem == "ext4"):
      return self.taille_part_ext(partition)
  
  def taux_occupation(self,partition,filesystem):
    if (filesystem == "ext2") or (filesystem == "ext3") or (filesystem == "ext4"):
      return self.taux_occupation_ext(partition)


  ######################################################################################
  ###################  FONCTIONS RESERVER POUR FILESYSTEM EXT   ########################
  ######################################################################################
  def taille_part_ext(self,partition):
    Bl_count = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Block count"')
    Bl_size = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Block size"')
    Bloc_count = Bl_count[0].split(":")[1].strip()
    Bloc_size = Bl_size[0].split(":")[1].strip()
    Taille = (int(Bloc_count) * int(Bloc_size))
    return Taille
  
  def taille_part_free_ext(self,partition):
    Bl_free = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Free blocks"')
    Bl_size = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Block size"')
    Bloc_free = Bl_free[0].split(":")[1].strip()
    Bloc_size = Bl_size[0].split(":")[1].strip()
    Taille_free = (int(Bloc_free) * int(Bloc_size))
    return Taille_free

  def taux_occupation_ext(self,partition):
    taille_total = self.taille_part_ext(partition)
    taille_libre = self.taille_part_free_ext(partition)
    tx_occup = 100 - ((taille_libre * 100) / taille_total)
    return tx_occup


  def Convert_to_octects(self,taille):
    """ converti Ko, Mo, Go  et To en octets """
    



  def get_all_partitions(self):
    ALL_PARTITIONS={}
    ALL_PARTITIONS["PARA"] = (self.get_partitions_para())
    return ALL_PARTITIONS

  def detect_lvm(self):
    detect_lvm = "0"
    nb_lv = self.exec_cmd_ssh('LANG=POSIX fdisk -l 2> /dev/null| grep "^Disk /dev" | grep mapper | wc -l')
    if  nb_lv[0].strip() >= 1:
      detect_lvm = "1"
    if detect_lvm == "1": 
      print "LVM detecte, en mode HVM, le LVM sera fait dans la VM."

  def detect_lvdisplay(self):
    detect = self.exec_cmd_ssh('which lvdisplay | wc -l')
    return detect[0]

 
  def is_lv(self,fs):
    if self.detect_lvdisplay() != 0:
      check_is_lv = self.exec_cmd_ssh('lvdisplay | grep \"%s\" | wc -l' % fs)
      if check_is_lv[0] >= 1:
        return 1
      else:
        return 0
    else:
      return 0
 
  def get_version_os(self):
    liste = self.exec_cmd_ssh('cat /etc/issue')
    os_version=[]
    if liste[0].split()[0] == "CentOS":
      os_version = {"OS":liste[0].split()[0],"VERSION":liste[0].split()[2]}
    if liste[0].split()[0] == "Ubuntu":
      os_version = {"OS":liste[0].split()[0],"VERSION":liste[0].split()[1]}
    if liste[0].split()[0] == "Debian":
      os_version = {"OS":liste[0].split()[0],"VERSION":liste[0].split()[2]}
    return os_version

  def get_eligibility_check_fstab(self):
    CHECK_LABEL = self.exec_cmd_ssh('less /etc/fstab | grep ^LABEL= | wc -l')
    CHECK_LABEL = CHECK_LABEL[0].strip()
    if CHECK_LABEL == "0":
      ret = 1
    else:
      ret = 0
    return ret

  def get_eligibility_check_fs_ext(self):
    CHECK_FS_EXT = self.exec_cmd_ssh('df -x tmpfs -x swap -x proc -x sys -x ext2 -x ext3 -x ext4 2>/dev/null | wc -l')
    CHECK_FS_EXT = CHECK_FS_EXT[0].strip()
    if CHECK_FS_EXT == "0":
      ret = 1
    else:
      ret = 0
    return ret

  def get_eligibility_check_network_file_p2v(self):
    CHECK_NETWORK_FILE_P2V = self.exec_cmd_ssh('ls /etc/network/interfaces.pre.p2v 2>/dev/null | wc -l')
    CHECK_NETWORK_FILE_P2V = CHECK_NETWORK_FILE_P2V[0].strip()
    if CHECK_NETWORK_FILE_P2V == "1":
      ret = 1
    else:
      ret = 0
    return ret
Exemple #12
0
class xen_host:
    def __init__(self, confp2v="/etc/xen/xenmgt-p2v.conf"):
        xenmgtconf = {}
        template_hvm = {}
        execfile(confp2v, xenmgtconf)
        execfile(xenmgtconf["TEMPLATE_HVM"], template_hvm)
        self.xenmgtconf = xenmgtconf
        self.template_hvm = template_hvm
        self.bs = self.xenmgtconf["DD_BS"]
        self.type_vm = "P2V"

    def exec_cmd(self, cmd=""):
        CMD = os.popen(cmd, "r")
        ret = CMD.readlines()
        return ret

    def get_os_version(self):
        self.version_os = self.P.get_version_os()
        return self.version_os

    def check_vgname(self):
        liste = self.exec_cmd("vgdisplay | grep Name | grep %s | wc -l" % self.vgname)
        vg = liste[0].strip()
        if vg == "1":
            return self.vgname
        else:
            print "!!!ERROR !!! Le VG %s n'existe pas, utiliser l'option --vg=<VG_NAME> par défaut c'est LVM_XEN" % self.vgname
            sys.exit()

    def get_name_vm_dest(self):
        self.P = physical_host(self.ip_physique)
        self.ssh = Ssh(self.ip_physique)

        self.ssh.del_keyfile()
        self.ssh.del_keyfile_client(self.ip_physique)

        if self.no_pxe == True:
            print "no_pxe : %s" % self.no_pxe
            self.ssh.copy_id_client(self.ip_physique)

        name_vm_dest = self.ssh.exec_cmd("hostname")
        try:
            self.name_vm_dest = name_vm_dest[0].strip()
        except:
            if self.no_pxe == True:
                print ""
                print "!!! ATTENTION L'OPTION 'SERVEUR PXE' A ETE DESACTIVE AFIN DE PASSER PAR UN LIVECD DANS LE CEDEROM !!!"
                print ""
                print "Assurez vous que : "
                print "   - Le port du switch soit bien UnTaggé avec le vlan %s" % self.vlan
                print "   - Votre LiveCD soit bien inséré et configuré avec l'ip correspondante :"
                print "       # ip addr add %s/24 dev eth0" % self.ip_physique
                print "   - Le paquet 'dcfldd' soit installé sur le livecd"
            else:
                print "connexion impossible avec le serveur physique, le CFp2v est bien descendu sur le serveur physique ?"
            sys.exit()
        self.new_name_vm_ip = self.ip_physique
        return self.name_vm_dest

    def get_interfaces(self):
        self.interfaces = self.P.get_interfaces()
        # P = physical_host(self.ip_physique)
        # nb_vlan = len(self.ConfCFengine["NETWORK"])
        MacAddrPerInterfaces = self.P.get_interfaces()
        mac = GenerateMacAddr()

        for i in self.tri(self.ConfCFengine["NETWORK"]):
            try:
                self.ConfCFengine["NETWORK"][i]["MAC"] = MacAddrPerInterfaces[i]
                mac(MacAddrPerInterfaces[i])
            except:
                self.ConfCFengine["NETWORK"][i]["MAC"] = mac.generate()
        self.interfaces = self.ConfCFengine["NETWORK"]
        return self.interfaces

    def get_memory(self):
        self.memory = self.P.get_memory1()
        return self.memory

    def get_partitions(self):
        self.partitions = self.P.get_all_partitions()
        return self.partitions

    def get_cpu(self):
        self.cpu = self.P.get_cpu()
        return self.cpu

    def get_info_cfp2v(self):
        ConfCFengine = {}
        ConfCFengine["MAC_ADDR"] = ""
        try:
            execfile("/etc/xen/P2V/conf/%s/%s/config" % (self.projet_p2v, self.vmnamecfengine), ConfCFengine)
            self.ConfCFengine = ConfCFengine
        except:
            print "Le produit CFp2V n'est pas descendu correctement ou %s n'a pas été ajouté au CFp2v" % self.vmnamecfengine
            sys.exit()
        ConfCFp2v = {}
        # ConfCFp2v[self.vmnamecfengine] = {"VLAN":ConfCFengine["ID_VLAN"], "IP_PXE":ConfCFengine["IP_PXE"], "IP_VM":ConfCFengine["IP_VM"], "IP_XEN":ConfCFengine["IP_XEN"]}
        ConfCFp2v[self.vmnamecfengine] = {
            "VLAN": ConfCFengine["NETWORK"],
            "IP_PXE": ConfCFengine["IP_PXE"],
            "IP_VM": ConfCFengine["IP_VM"],
            "IP_XEN": ConfCFengine["IP_XEN"],
            "MAC_ADDR": ConfCFengine["MAC_ADDR"],
        }
        self.ConfCFp2v = ConfCFp2v
        self.vlan = self.ConfCFp2v[self.vmnamecfengine]["VLAN"]["eth0"]["VLAN"]
        self.ip_pxe = self.ConfCFp2v[self.vmnamecfengine]["IP_PXE"]
        self.ip_physique = self.ConfCFp2v[self.vmnamecfengine]["IP_VM"]
        self.ip_xen = self.ConfCFp2v[self.vmnamecfengine]["IP_XEN"]
        self.mac_addr_v2p = self.ConfCFp2v[self.vmnamecfengine]["MAC_ADDR"]
        return self.ConfCFp2v

    def mount_gateway_intra_vlan(self):
        os.system("ip addr add %s/24 dev %s%s" % (self.ip_xen, self.bridge_prefix, self.vlan))

    def umount_gateway_intra_vlan(self):
        os.system("ip addr del %s/24 dev %s%s" % (self.ip_xen, self.bridge_prefix, self.vlan))

    def build_cnx(self):
        self.mount_gateway_intra_vlan()

    def get_info_srv_physique(self):
        self.get_os_version()
        self.get_memory()
        self.get_interfaces()
        self.get_partitions()
        self.get_cpu()

    def is_livecd(self):
        P = physical_host(self.ip_physique)
        self.is_livecd = P.is_livecd()
        return self.is_livecd

    #######################################################
    ###               ELIGIBILITY                       ###
    #######################################################

    def get_size_vg(self):
        TOTAL = self.exec_cmd("vgs --units B | grep %s | awk '{print $6}' | sed 's/B//'" % self.vgname)[0].strip()
        FREE = self.exec_cmd("vgs --units B | grep %s | awk '{print $7}' | sed 's/B//'" % self.vgname)[0].strip()
        vgsize = {"TOTAL": TOTAL, "FREE": FREE}
        return vgsize

    def get_eligibility_check_vgsize(self):
        TailleTotalPart = 0
        self.get_partitions()
        for i in self.tri(self.partitions[self.type_p2v]):
            TailleTotalPart = TailleTotalPart + int(self.partitions[self.type_p2v][i]["SIZE"])
        VGSIZE = self.get_size_vg()
        NEW_VGSIZE = int(VGSIZE["FREE"]) - ((self.xenmgtconf["VG_PERCENT_EMERGENCY"] * int(VGSIZE["FREE"])) / 100)
        if TailleTotalPart < NEW_VGSIZE:
            ret = 1
        else:
            ret = 0
        return ret

    def get_eligibility(self):
        self.eligibility_check_fstab = self.P.get_eligibility_check_fstab()
        self.eligibility_check_fs_ext = self.P.get_eligibility_check_fs_ext()
        # self.eligibility_check_network_file_p2v = self.P.get_eligibility_check_network_file_p2v()
        self.eligibility_check_vgsize = self.get_eligibility_check_vgsize()

    def rapport_eligibility_header(self):
        RAPPORT_HEADER = "\n##########################################\n"
        RAPPORT_HEADER += "######## RAPPORT D'ELIGIBILITE ###########\n"
        RAPPORT_HEADER += "##########################################"
        print RAPPORT_HEADER

    def rapport_eligibility_fstab(self):
        RAPPORT_FSTAB = "\n*\n"
        if self.eligibility_check_fstab == 1:
            RAPPORT_FSTAB += "* Check fstab : OK\n"
        else:
            RAPPORT_FSTAB += "* Check fstab : NOK\n"
            RAPPORT_FSTAB += "* !!! Le fichier /etc/fstab contient un ou plusieurs LABEL\n"
            RAPPORT_FSTAB += "* Veuillez remplacer les entrées LABEL par les devices correspondant\n"
        RAPPORT_FSTAB += "*\n"
        print RAPPORT_FSTAB

    def rapport_eligibility_fs_ext(self):
        RAPPORT_FSEXT = "*\n"
        if self.eligibility_check_fs_ext == 1:
            RAPPORT_FSEXT += "* Check FileSystem : OK\n"
        else:
            RAPPORT_FSEXT += "* Check FileSystem : NOK\n"
            RAPPORT_FSEXT += "* !!! Une ou plusieurs partitions contiennent un FileSystem different de ext2,3,4\n"
        RAPPORT_FSEXT += "*\n"
        print RAPPORT_FSEXT

    # def rapport_eligibility_network(self):
    #  RAPPORT_NETWORK = "*\n"
    #  if self.eligibility_check_network_file_p2v == 1:
    #    RAPPORT_NETWORK += "* Check Network : OK\n"
    #  else:
    #    RAPPORT_NETWORK += "* Check Network : NOK\n"
    #    RAPPORT_NETWORK += "* !!! Copiez votre fichier '/etc/network/interfaces' en '/etc/network/interfaces.pre.p2v' en supprimant les vlans\n"
    #  RAPPORT_NETWORK += "*\n"
    #  print RAPPORT_NETWORK

    def rapport_eligibility_vgsize(self):
        RAPPORT_VGSIZE = "*\n"
        if self.eligibility_check_vgsize == 1:
            RAPPORT_VGSIZE += "* Check taille dispo sur le VG : OK\n"
        else:
            RAPPORT_VGSIZE += "* Check taille dispo sur le VG : NOK\n"
            RAPPORT_VGSIZE += "* !!! Il n y a pas assez de place sur le VG\n"
        RAPPORT_VGSIZE += "*\n"
        print RAPPORT_VGSIZE

    def rapport_eligibility_result(self):
        # somme = (self.eligibility_check_fstab + self.eligibility_check_fs_ext + self.eligibility_check_network_file_p2v + self.eligibility_check_vgsize)
        somme = self.eligibility_check_fstab + self.eligibility_check_fs_ext + self.eligibility_check_vgsize
        # if somme == 4:
        if somme == 3:
            print "* le Serveur est éligible\n"
        else:
            print "* !!! Le serveur n'est pas éligible !!!\n"

    def rapport_eligibility(self):
        self.rapport_eligibility_header()
        self.rapport_eligibility_fstab()
        self.rapport_eligibility_fs_ext()
        # self.rapport_eligibility_network()
        self.rapport_eligibility_vgsize()
        self.rapport_eligibility_result()

    #######################################################
    ###              FIN   ELIGIBILITY                  ###
    #######################################################

    #######################################################
    ### DEBUT PREPARATION POUR LA GENERATION DU RAPPORT ###
    #######################################################

    def prep_affiche_os_version(self):
        AFFICHE_OS_VERSION = "OS : %s %s\n\n" % (self.version_os["OS"], self.version_os["VERSION"])
        return AFFICHE_OS_VERSION

    def prep_affiche_vm_name(self):
        AFFICHE_VM_NAME = "VM : %s\n" % self.name_vm_dest
        AFFICHE_VM_NAME += "IP LiveCD : %s\n\n" % self.new_name_vm_ip
        return AFFICHE_VM_NAME

    def prep_affiche_memory(self):
        AFFICHE_MEMORY = "MEMORY : %s Mo\n" % self.memory
        return AFFICHE_MEMORY

    def prep_affiche_cpu(self):
        AFFICHE_CPU = "CPU : %s \n\n" % self.cpu
        return AFFICHE_CPU

    def prep_affiche_network(self):
        AFFICHE_NETWORK = "NETWORK :   \n"
        for i in self.tri(self.interfaces):
            AFFICHE_NETWORK += " %s -> Mac address : %s\n" % (i, self.interfaces[i]["MAC"])
        AFFICHE_NETWORK += "\n"
        AFFICHE_NETWORK += "\n"
        return AFFICHE_NETWORK

    def prep_affiche_partitions(self):
        AFFICHE_PARTITIONS = "PARTITIONS :\n"
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.type_p2v == "HVM":
                AFFICHE_PARTITIONS += " %s -> taille : %s\n" % (
                    self.partitions[self.type_p2v][i]["DEVICE"],
                    self.partitions[self.type_p2v][i]["SIZE"],
                )
            if self.type_p2v == "PARA":
                AFFICHE_PARTITIONS += " %s %s %s %s\n" % (
                    self.partitions[self.type_p2v][i]["DEVICE"],
                    self.partitions[self.type_p2v][i]["FS"],
                    self.partitions[self.type_p2v][i]["SIZE"],
                    self.partitions[self.type_p2v][i]["PARTITION"],
                )
        return AFFICHE_PARTITIONS

    def prep_cmd_create_partitions(self):
        AFFICHE_CREATE_LV = "\n"
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.type_p2v == "HVM":
                nom_partition = i.replace("/", "-")
                AFFICHE_CREATE_LV += "lvcreate -L %sB -n %s-%s %s\n" % (
                    self.partitions[self.type_p2v][i]["SIZE"],
                    self.partitions[self.type_p2v][i]["DEVICE"],
                    self.name_vm_dest,
                    self.vgname,
                )
            if self.type_p2v == "PARA":
                if self.partitions[self.type_p2v][i]["PARTITION"] == "/":
                    nom_part = "root"
                elif self.partitions[self.type_p2v][i]["FS"] == "swap":
                    nom_part = "swap"
                else:
                    nom_part = self.partitions[self.type_p2v][i]["PARTITION"].replace("/", "-")[1:]
                AFFICHE_CREATE_LV += "lvcreate -L %sB -n %s-%s %s\n" % (
                    self.partitions[self.type_p2v][i]["SIZE"],
                    nom_part,
                    self.name_vm_dest,
                    self.vgname,
                )
        return AFFICHE_CREATE_LV

    def prep_cmd_copy_dd(self):
        AFFICHE_DD = "\n"
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.type_p2v == "HVM":
                AFFICHE_DD += (
                    "ssh -i /etc/xen/P2V/ssh/p2v.key -c arcfour root@"
                    + self.new_name_vm_ip
                    + " 'dcfldd status=on sizeprobe=if statusinterval=100 if=/dev/"
                    + i
                    + "' bs="
                    + self.bs
                    + " | dd of=/dev/"
                    + self.vgname
                    + "/"
                    + self.partitions[self.type_p2v][i]["DEVICE"]
                    + "-"
                    + self.name_vm_dest
                    + " bs="
                    + self.bs
                    + "\n"
                )
            if self.type_p2v == "PARA":
                if self.partitions[self.type_p2v][i]["PARTITION"] == "/":
                    nom_part = "root"
                elif (
                    self.partitions[self.type_p2v][i]["FS"] == "swap"
                    or self.partitions[self.type_p2v][i]["PARTITION"] == "swap"
                ):
                    nom_part = "swap"
                else:
                    nom_part = self.partitions[self.type_p2v][i]["PARTITION"].replace("/", "-")[1:]
                AFFICHE_DD += (
                    'echo "Copie de la partition '
                    + self.partitions[self.type_p2v][i]["PARTITION"]
                    + " ( "
                    + self.partitions[self.type_p2v][i]["DEVICE"]
                    + ' )"\n'
                )
                if (
                    self.partitions[self.type_p2v][i]["PARTITION"] == "swap"
                    or self.partitions[self.type_p2v][i]["FS"] == "swap"
                ):
                    AFFICHE_DD += "mkswap -v1 /dev/" + self.vgname + "/" + nom_part + "-" + self.name_vm_dest + "\n"
                else:
                    ############# DETECTION POUR REDUCTION LV ET RESIZE FS ##################
                    # if self.P.is_lv(self.partitions[self.type_p2v][i]["DEVICE"]):
                    #  if self.partitions[self.type_p2v][i]["SIZE"] > 15106127360:
                    #    if self.P.taux_occupation(self.partitions[self.type_p2v][i]["DEVICE"],self.partitions[self.type_p2v][i]["FS"]) < 20:
                    #      print "Elu pour la reduction"
                    #      print "%s is LVM LV" % self.partitions[self.type_p2v][i]["DEVICE"]
                    #      print self.P.taux_occupation(self.partitions[self.type_p2v][i]["DEVICE"],self.partitions[self.type_p2v][i]["FS"])
                    ############# FIN DETECTION POUR REDUCTION LV ET RESIZE FS ##################
                    if self.version_os["VERSION"] == "3.1":
                        AFFICHE_DD += (
                            "ssh -i /etc/xen/P2V/ssh/p2v.key -c arcfour root@"
                            + self.new_name_vm_ip
                            + " 'dcfldd status=on sizeprobe=if statusinterval=100 if="
                            + self.partitions[self.type_p2v][i]["DEVICE"]
                            + "' bs="
                            + self.bs
                            + " | dd of=/dev/"
                            + self.vgname
                            + "/"
                            + nom_part
                            + "-"
                            + self.name_vm_dest
                            + " bs="
                            + self.bs
                            + "\n"
                        )
                    else:
                        AFFICHE_DD += (
                            "ssh -i /etc/xen/P2V/ssh/p2v.key -c arcfour root@"
                            + self.new_name_vm_ip
                            + " 'dcfldd status=on sizeprobe=if statusinterval=100 if=/dev/disk/by-uuid/"
                            + self.partitions[self.type_p2v][i]["UUID"]
                            + "' bs="
                            + self.bs
                            + " | dd of=/dev/"
                            + self.vgname
                            + "/"
                            + nom_part
                            + "-"
                            + self.name_vm_dest
                            + " bs="
                            + self.bs
                            + "\n"
                        )
        return AFFICHE_DD

    def affiche_rapport(self):
        affiche = "---------- RAPPORT ----------\n"
        affiche += self.prep_affiche_os_version()
        affiche += self.prep_affiche_vm_name()
        affiche += self.prep_affiche_memory()
        affiche += self.prep_affiche_cpu()
        affiche += self.prep_affiche_network()
        affiche += self.prep_affiche_partitions()
        self.affiche_rapport = affiche
        return affiche

    #####################################################
    ### FIN PREPARATION POUR LA GENERATION DU RAPPORT ###
    #####################################################

    def get_exec_cmd(self):
        ecrit_cmd = ""
        ecrit_cmd += self.prep_cmd_create_partitions()
        ecrit_cmd += self.prep_cmd_copy_dd()
        return ecrit_cmd

    #############################################################
    ### DEBUT GENERATION DU FICHIER DE CONF XEN (HVM OU PARA) ###
    #############################################################

    def generate_conf_xen(self):
        if self.type_p2v == "PARA":
            GEN_CONF = self.generate_conf_xen_para()
        if self.type_p2v == "HVM":
            GEN_CONF = self.generate_conf_xen_hvm()
        return GEN_CONF

    def ecrit_conf_partitions(self):
        conf = "["
        count = len(self.partitions[self.type_p2v].keys())
        cpt = 1
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.type_p2v == "HVM":
                conf += "'phy:/dev/%s/%s-%s,%s,w'" % (
                    self.vgname,
                    self.partitions[self.type_p2v][i]["DEVICE"],
                    self.name_vm_dest,
                    self.partitions[self.type_p2v][i]["DEVICE"],
                )
            if self.type_p2v == "PARA":
                if self.partitions[self.type_p2v][i]["PARTITION"] == "/":
                    nom_part = "root"
                elif self.partitions[self.type_p2v][i]["FS"] == "swap":
                    nom_part = "swap"
                else:
                    nom_part = self.partitions[self.type_p2v][i]["PARTITION"].replace("/", "-")[1:]
                conf += "'phy:/dev/%s/%s-%s,%s,w'" % (self.vgname, nom_part, self.name_vm_dest, i)
            if cpt != int(count):
                conf += ",\n\t"
            cpt = int(cpt) + 1
        conf += "]"
        return conf

    def uuid_gen(self):
        uuid_gen = self.exec_cmd("uuidgen")
        return uuid_gen[0].strip()

    def generate_conf_xen_hvm(self):
        self.ecrit_conf_partitions()
        CONF_HVM = ""
        for i in self.template_hvm.keys():
            if i != "__builtins__":
                print i
                if i == "vif":
                    CONF_HVM += "" + str(i) + " = " + self.ecrit_conf_interfaces() + "\n"
                elif i == "disk":
                    CONF_HVM += "" + str(i) + " = " + self.ecrit_conf_partitions() + "\n"
                elif i == "uuid":
                    CONF_HVM += "" + str(i) + ' = "' + self.uuid_gen() + '"\n'
                elif i == "name":
                    CONF_HVM += "" + str(i) + ' = "' + self.name_vm_dest + '"\n'
                elif i == "memory":
                    CONF_HVM += "" + str(i) + " = " + self.ecrit_memory() + "\n"
                elif i == "vcpus":
                    CONF_HVM += "" + str(i) + " = " + self.cpu + "\n"
                else:
                    CONF_HVM += "" + str(i) + " = '" + str(self.template_hvm[i]) + "'\n"
        return CONF_HVM

    def ecrit_conf_interfaces(self):
        conf = "["
        count = len(self.interfaces.keys())
        cpt = 1
        for i in self.tri(self.interfaces):
            if self.type_p2v == "HVM":
                conf += "'mac=%s , bridge=%s%s'" % (
                    self.interfaces[i]["MAC"],
                    self.bridge_prefix,
                    self.interfaces[i]["VLAN"],
                )
            else:
                if self.version_os["VERSION"] == "3.1":
                    conf += "'mac=%s , bridge=%s%s'" % (
                        self.interfaces[i]["MAC"],
                        self.bridge_prefix,
                        self.interfaces[i]["VLAN"],
                    )
                else:
                    if self.keep_mac_addr:
                        conf += "'mac=%s , bridge=%s%s'" % (
                            self.interfaces[i]["MAC"],
                            self.bridge_prefix,
                            self.interfaces[i]["VLAN"],
                        )
                    else:
                        conf += "'bridge=%s%s'" % (self.bridge_prefix, self.interfaces[i]["VLAN"])
                if cpt != int(count):
                    conf += ","
                cpt = int(cpt) + 1
        conf += "]"
        return conf

    def ecrit_memory(self):
        return self.memory

    def ecrit_maxmem(self):
        if int(self.memory) <= 4096:
            maxmem = 6144
        elif int(self.memory) > 4096:
            maxmem = (int(self.memory) / 2) + 8192
        return maxmem

    def ecrit_vcpus(self):
        vcpus = "12"
        return vcpus

    def ecrit_vcpu_avail(self):
        if int(self.cpu) == 1:
            vcpu_avail = "3"
        elif int(self.cpu) == 2:
            vcpu_avail = "3"
        elif int(self.cpu) == 3:
            vcpu_avail = "7"
        elif int(self.cpu) == 4:
            vcpu_avail = "15"
        elif int(self.cpu) == 5:
            vcpu_avail = "31"
        elif int(self.cpu) == 6:
            vcpu_avail = "63"
        elif int(self.cpu) == 7:
            vcpu_avail = "127"
        elif int(self.cpu) == 8:
            vcpu_avail = "255"
        elif int(self.cpu) > 8:
            vcpu_avail = "255"
        return vcpu_avail

    def ecrit_name_vm_dest(self):
        return self.name_vm_dest

    def ecrit_num_sysadmin(self):
        return self.sysadmin

    def ecrit_type_vm(self):
        return self.type_vm

    def ecrit_extra(self):
        extra = "console=xvc0 elevator=noop"
        return extra

    def ecrit_root_kernel(self):
        root = ""
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.partitions[self.type_p2v][i]["PARTITION"] == "/":
                root = i
        root_kernel = "/dev/%s ro" % root
        return root_kernel

    def get_kernel(self):
        self.kernel_vm = []
        if self.version_os["OS"] == "Ubuntu":
            kernel = self.xenmgtconf["KERNEL_UBUNTU"]
            initrd = self.xenmgtconf["INITRD_UBUNTU"]
        elif self.version_os["OS"] == "Debian":
            kernel = self.xenmgtconf["KERNEL_DEBIAN"]
            initrd = self.xenmgtconf["INITRD_DEBIAN"]
        else:
            kernel = self.xenmgtconf["KERNEL_CENTOS"]
            initrd = self.xenmgtconf["INITRD_CENTOS"]
        self.kernel_vm = [kernel, initrd]
        return self.kernel_vm

    def generate_conf_xen_para(self):
        self.get_kernel()
        CONF_PARA = ""
        CONF_PARA += 'kernel = "' + self.kernel_vm[0] + '"\n'
        CONF_PARA += 'ramdisk = "' + self.kernel_vm[1] + '"\n'
        CONF_PARA += "memory = " + str(self.ecrit_memory()) + "\n"
        CONF_PARA += "maxmem = " + str(self.ecrit_maxmem()) + "\n\n"
        CONF_PARA += "vcpus = " + self.ecrit_vcpus() + "\n"
        CONF_PARA += "vcpu_avail = " + self.ecrit_vcpu_avail() + "\n\n"
        CONF_PARA += 'name = "' + self.ecrit_name_vm_dest() + '"\n\n'
        CONF_PARA += "vif = " + self.ecrit_conf_interfaces() + "\n"
        CONF_PARA += "disk = " + self.ecrit_conf_partitions() + "\n"
        CONF_PARA += 'root = "' + self.ecrit_root_kernel() + '"\n'
        CONF_PARA += 'extra = "' + self.ecrit_extra() + '"\n\n'
        CONF_PARA += '#SYSADMIN="' + self.ecrit_num_sysadmin() + '"\n'
        CONF_PARA += '#VMTYPE="' + self.ecrit_type_vm() + '"\n'
        return CONF_PARA

    ###########################################################
    ### FIN GENERATION DU FICHIER DE CONF XEN (HVM OU PARA) ###
    ###########################################################

    def create_rep_p2v(self):
        if not os.path.isdir(self.rep_p2v):
            self.exec_cmd("mkdir -p %s" % self.rep_p2v)

    def check_rep_p2v(self):
        self.rep_p2v = "/etc/xen/P2V/" + self.name_vm_dest + ""
        self.create_rep_p2v()

    def set_fichier_p2v(self, ext="", contenu=""):
        fichier = "%s/%s.%s" % (self.rep_p2v, self.name_vm_dest, ext)
        fd = open(fichier, "w")
        fd.write(contenu)
        fd.close()

    def generation_fichier_p2v(self):
        self.check_rep_p2v()

        GenConf = GenConfNetwork()
        GenConf.interfaces = self.interfaces
        GenConf.LocalDir = self.rep_p2v
        GenConf.ip_physique = self.ip_physique

        self.set_fichier_p2v("sh", self.get_exec_cmd())
        self.set_fichier_p2v("cfg", self.generate_conf_xen())
        self.set_fichier_p2v("var", self.export_variables())
        self.set_fichier_p2v("rapport", self.affiche_rapport)

        GenConf.GenerateFileNetwork()

    def endphase(self):
        AFFICHE = "\n\n"
        AFFICHE += "*****************************************************************\n"
        AFFICHE += "FIN de L'ETAPE 1/3\n\n"
        AFFICHE += "Pour passer a la 2eme Etape, il faut relancer le meme script\n"
        AFFICHE += "*****************************************************************\n"
        return AFFICHE

    ##############################
    ###   DEBUT POST INSTALL   ###
    ##############################

    def export_variables(self):
        export_variable = ""
        export_variable += "partitions=%s\n" % self.partitions
        export_variable += 'vgname="%s"\n' % self.vgname
        export_variable += 'type_p2v="%s"\n' % self.type_p2v
        export_variable += 'name_vm_dest="%s"\n' % self.name_vm_dest
        export_variable += "version_os=%s\n" % self.version_os
        export_variable += "vlan=%s\n" % self.vlan
        export_variable += 'mac_addr="%s"\n' % self.interfaces["eth0"]["MAC"]
        export_variable += 'ip_pxe="%s"\n' % self.ip_pxe
        export_variable += 'ip_physique="%s"\n' % self.ip_physique
        return export_variable

    def import_all_variables(self, VM):
        new_val = {}
        execfile("/etc/xen/P2V/" + VM + "/" + VM + ".var", new_val)
        self.new_variables = new_val
        for i in self.new_variables.keys():
            if i != "__builtins__":
                globals()[i] = self.new_variables[i]
        self.mac_addr = globals()["mac_addr"]

    def mkdir_rep_vhosts_vm(self):
        """ Creation du repertoire /vhosts/vm
    """
        print "Creation du repertoire /vhosts/%s" % name_vm_dest
        self.rep_vhosts_vm = "/vhosts/" + name_vm_dest + ""
        self.exec_cmd("mkdir -p %s" % self.rep_vhosts_vm)

    def modif_fstab(self):
        """ Genere le nouveau fichier fstab
    """
        print "preparation du fichier fstab"
        self.exec_cmd("cp %s/etc/fstab %s/etc/fstab.pre.p2v" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
        self.exec_cmd("cp %s/etc/fstab_without_uuid %s/etc/fstab" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
        line = open("/vhosts/" + name_vm_dest + "/etc/fstab_without_uuid", "r").read()
        for i in self.tri(partitions[type_p2v]):
            line = line.replace(partitions[type_p2v][i]["DEVICE"], "/dev/%s" % i, 1)
        fichier = open("/vhosts/" + name_vm_dest + "/etc/fstab", "w")
        fichier.write(line)
        fichier.close()

    def modif_devpts(self):
        if version_os["VERSION"] == "3.1":
            self.exec_cmd(
                'if [ $(grep "/bin/mkdir -p /dev/pts" %s/etc/init.d/mountvirtfs | wc -l) -eq 0 ] ; then sed -i \'/domount sysfs "" \/sys/a \/bin\/mkdir -p \/dev\/pts\' %s/etc/init.d/mountvirtfs ; else echo "" ; fi'
                % (self.rep_vhosts_vm, self.rep_vhosts_vm)
            )
            self.exec_cmd(
                'echo "none\t /dev/pts\t devpts\t gid=5,mode=620\t 0\t 0" >> %s/etc/fstab' % self.rep_vhosts_vm
            )

    def modif_network(self):
        """ Genere le nouveau fichier network (En cours)
    """
        print "preparation du fichier network interfaces"
        if version_os["OS"] == "CentOS":
            self.exec_cmd(
                "cp %s/etc/sysconfig/network_scripts/ifcfg-eth0 %s/etc/sysconfig/network_scripts/ifcfg-eth0.pre.p2v"
                % (self.rep_vhosts_vm, self.rep_vhosts_vm)
            )
        else:
            self.exec_cmd(
                "cp %s/etc/network/interfaces %s/etc/network/interfaces.post.p2v"
                % (self.rep_vhosts_vm, self.rep_vhosts_vm)
            )
            self.exec_cmd(
                "cp %s/etc/network/interfaces.pre.p2v %s/etc/network/interfaces"
                % (self.rep_vhosts_vm, self.rep_vhosts_vm)
            )

        if self.keep_mac_addr == False:
            self.exec_cmd("echo '' > %s/etc/udev/rules.d/*persistent-net.rules" % self.rep_vhosts_vm)

    def copie_modules(self):
        """ copie du module 2.6.37 ou 2.6.18.149
    """
        print "copie du module necessaire"
        if version_os["OS"] == "Ubuntu":
            self.exec_cmd(
                "cp -rpdf /lib/modules/%s %s/lib/modules/"
                % (self.xenmgtconf["KERNEL_UBUNTU"].split("/boot/vmlinuz-")[1], self.rep_vhosts_vm)
            )
        if version_os["OS"] == "Debian":
            self.exec_cmd(
                "cp -rpdf /lib/modules/%s %s/lib/modules/"
                % (self.xenmgtconf["KERNEL_DEBIAN"].split("/boot/vmlinuz-")[1], self.rep_vhosts_vm)
            )
        if version_os["OS"] == "CentOS":
            self.exec_cmd(
                "cp -rpdf /lib/modules/%s %s/lib/modules/"
                % (self.xenmgtconf["KERNEL_CENTOS"].split("/boot/vmlinuz-")[1], self.rep_vhosts_vm)
            )

    def set_ntp_sysctl(self):
        """ Modifie le sysctl pour la correction du ntp
    """
        print "Modification du sysctl"
        self.exec_cmd('echo "xen.independent_wallclock = 1" >> %s/etc/sysctl.conf' % self.rep_vhosts_vm)

    def mount_root_vm(self):
        """ Monte la partition root de la VM afin d'effectuer la post_install
    """
        print "montage de la partition root de %s" % name_vm_dest
        if type_p2v == "PARA":
            device_racine = "root"
            self.exec_cmd("mount /dev/%s/%s-%s %s" % (vgname, device_racine, name_vm_dest, self.rep_vhosts_vm))
        elif type_p2v == "HVM":
            device_racine = "hda"

    def umount_root_vm(self):
        """ Monte la partition root de la VM afin d'effectuer la post_install
    """
        print "demontage de la partition root de %s" % name_vm_dest
        self.exec_cmd("umount %s" % self.rep_vhosts_vm)

    def set_console_xen(self):
        """ Configuration de la console xen pour la VM
    """
        print ""
        self.exec_cmd('echo "xvc0" >> %s/etc/securetty' % self.rep_vhosts_vm)
        if os.path.isfile("%s/etc/inittab" % self.rep_vhosts_vm):
            self.exec_cmd('echo "7:2345:respawn:/sbin/getty 38400 xvc0" >> %s/etc/inittab' % self.rep_vhosts_vm)

        if os.path.isfile("%s/etc/event.d/tty1" % self.rep_vhosts_vm):
            self.exec_cmd("cp %s/etc/event.d/tty1 %s/etc/event.d/xvc0" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
            self.exec_cmd('sed -i "s@tty1@xvc0@" %s/etc/event.d/xvc0' % self.rep_vhosts_vm)

        if os.path.isfile("%s/etc/init/tty1.conf" % self.rep_vhosts_vm):
            self.exec_cmd("cp %s/etc/init/tty1.conf %s/etc/init/xvc0.conf" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
            self.exec_cmd('sed -i "s@tty1@xvc0@" %s/etc/init/xvc0.conf' % self.rep_vhosts_vm)

    def set_modprobe(self):
        """ active le modules xennet pour les interfaces reseaux
    """
        if version_os["OS"] == "Debian":
            self.exec_cmd('echo "alias eth0 xennet" >> %s/etc/modprobe.d/aliases' % self.rep_vhosts_vm)
        else:
            self.exec_cmd('echo "alias eth0 xennet" >> %s/etc/modprobe.d/aliases.conf' % self.rep_vhosts_vm)

    def copy_conf_to_xen(self):
        shutil.copy("/etc/xen/P2V/" + name_vm_dest + "/" + name_vm_dest + ".cfg", "/etc/xen/vm/" + name_vm_dest + "")
        date_generate_p2v = time.strftime("%d/%m/%y %H:%M", time.localtime())
        self.exec_cmd('echo "### P2V genere a %s " >> /etc/xen/vm/%s' % (date_generate_p2v, name_vm_dest))

    def auto_vm(self):
        self.exec_cmd("cd /etc/xen/auto ; ln -s /etc/xen/vm/" + name_vm_dest + "")

    def del_lock_dhcpd(self):
        self.pxe = pxe()
        self.pxe.del_lock_dhcpd(ip_pxe, name_vm_dest)

    def finish_p2v(self):
        self.exec_cmd("touch /etc/xen/P2V/" + name_vm_dest + "/" + name_vm_dest + ".finish")

    def post_install(self):
        if self.type_p2v == "PARA":
            self.post_install_para()
        if self.type_p2v == "HVM":
            self.post_install_hvm()

    def post_install_para(self):
        self.copy_conf_to_xen()
        self.mkdir_rep_vhosts_vm()
        self.mount_root_vm()
        self.copie_modules()
        self.modif_fstab()
        self.modif_network()
        self.set_ntp_sysctl()
        self.set_console_xen()
        self.set_modprobe()
        self.modif_devpts()
        self.umount_root_vm()
        self.auto_vm()
        self.del_lock_dhcpd()
        self.finish_p2v()
        self.umount_gateway_intra_vlan()

    def post_install_hvm(self):
        self.copy_conf_to_xen()
        # self.mkdir_rep_vhosts_vm()
        # self.mount_root_vm()
        # self.modif_fstab()
        # self.modif_network()
        # self.set_ntp_sysctl()
        # self.modif_devpts()
        # self.umount_root_vm()
        self.auto_vm()

    ############################
    ###   FIN POST INSTALL   ###
    ############################

    def tri(self, dico):
        """ Permet de tirer un dictionnaire
    """
        return sorted(dico.keys(), key=str)

    def tri_inverse(self, dico):
        """ Permet de tirer un dictionnaire
    """
        return sorted(dico.keys(), key=str, reverse=True)

    def copy_dd_bs(self, bs=""):
        self.bs = bs

    def check_vg_size(self, percent="10"):
        size_total = ()
        size_dispo = ()

    def exec_cmd_p2v(self):
        self.MappingDevice()
        os.system("/bin/bash /etc/xen/P2V/" + name_vm_dest + "/" + name_vm_dest + ".sh")

    def MappingDevice(self):
        P = physical_host(ip_physique)
        # Verifiaction de la presence du device
        New_dev = "0"
        AFFICHE_PARTITIONS = ""
        for i in self.tri(partitions[type_p2v]):
            if type_p2v == "HVM":
                AFFICHE_PARTITIONS += " %s -> taille : %s\n" % (
                    partitions[type_p2v][i]["DEVICE"],
                    partitions[type_p2v][i]["SIZE"],
                )
            if type_p2v == "PARA":
                if partitions[type_p2v][i]["DEVICE"] == "/dev/cciss/c0d0p1":
                    CheckDevice = P.exec_cmd_ssh("file %s | grep -v ERROR | wc -l" % partitions[type_p2v][i]["DEVICE"])[
                        0
                    ].strip()
                    if CheckDevice == "0":
                        New_dev = "/dev/sda"
                        pass
        if New_dev <> "0":
            fic_exec = "/etc/xen/P2V/%s/%s.sh" % (name_vm_dest, name_vm_dest)
            result = file(fic_exec, "r").read().replace("/dev/cciss/c0d0p", "/dev/sda")
            file(fic_exec, "w").write(result)

    def is_created_cfg(self, vm):
        return os.path.isfile("/etc/xen/P2V/" + vm + "/" + vm + ".cfg")

    def is_created_lv(self, vm):
        is_lv = self.exec_cmd("ls  /dev/" + self.vgname + "/root-" + vm + " 2>/dev/null | wc -l")
        return is_lv[0].strip()

    def is_finish_p2v(self, vm):
        self.import_all_variables(vm)
        if os.path.isfile("/etc/xen/P2V/" + vm + "/" + vm + ".finish"):
            return "true"
        else:
            return "false"
Exemple #13
0
class xen_host:
    def __init__(self, ip_srv_phy="", xenmgtconf="", ds="", vg_name=""):
        xenmgtconf = {}
        execfile("/etc/xen/xenmgt-p2v.conf", xenmgtconf)
        self.xenmgtconf = xenmgtconf
        self.bs = self.xenmgtconf["DD_BS"]
        self.P = physical_host(ip_srv_phy)
        self.ip_srv_phy = ip_srv_phy
        self.ssh = Ssh(self.ip_srv_phy)
        self.type_p2v = "PARA"
        self.type_vm = "P2V"
        self.sysadmin = ds
        self.vgname = vg_name

    def exec_cmd(self, cmd=""):
        CMD = os.popen(cmd, "r")
        ret = CMD.readlines()
        return ret

    def get_os_version(self):
        self.version_os = self.P.get_version_os()
        return self.version_os

    def check_vgname(self):
        liste = self.exec_cmd("vgdisplay | grep Name | grep %s | wc -l" % self.vgname)
        vg = liste[0].strip()
        if vg == "1":
            return self.vgname
        else:
            print "!!!ERROR !!! Le VG %s n'existe pas, utiliser l'option --vg=<VG_NAME> par defaut c'est LVM_XEN" % self.vgname
            sys.exit()

    def get_name_vm_dest(self):
        self.ssh.del_keyfile()
        self.ssh.copy_id()
        name_vm_dest = self.ssh.exec_cmd("hostname")
        self.name_vm_dest = name_vm_dest[0].strip()
        self.new_name_vm_ip = self.ip_srv_phy
        return self.name_vm_dest

    def get_interfaces(self):
        self.interfaces = self.P.get_interfaces()
        return self.interfaces

    def get_memory(self):
        self.memory = self.P.get_memory1()
        return self.memory

    def get_partitions(self):
        self.partitions = self.P.get_all_partitions()
        return self.partitions

    def get_cpu(self):
        self.cpu = self.P.get_cpu()
        return self.cpu

    def get_info_srv_physique(self):
        self.get_os_version()
        self.get_memory()
        self.get_interfaces()
        self.get_partitions()
        self.get_cpu()

    def is_livecd(self):
        self.is_livecd = self.P.is_livecd()
        return self.is_livecd

    #######################################################
    ###               ELIGIBILITY                       ###
    #######################################################

    def get_size_vg(self):
        TOTAL = self.exec_cmd("vgs --units B | grep %s | awk '{print $6}' | sed 's/B//'" % self.vgname)[0].strip()
        FREE = self.exec_cmd("vgs --units B | grep %s | awk '{print $7}' | sed 's/B//'" % self.vgname)[0].strip()
        vgsize = {"TOTAL": TOTAL, "FREE": FREE}
        return vgsize

    def get_eligibility_check_vgsize(self):
        TailleTotalPart = 0
        self.get_partitions()
        for i in self.tri(self.partitions[self.type_p2v]):
            TailleTotalPart = TailleTotalPart + int(self.partitions[self.type_p2v][i][2])
        VGSIZE = self.get_size_vg()
        NEW_VGSIZE = int(VGSIZE["FREE"]) - ((self.xenmgtconf["VG_PERCENT_EMERGENCY"] * int(VGSIZE["FREE"])) / 100)
        if TailleTotalPart < NEW_VGSIZE:
            ret = 1
        else:
            ret = 0
        return ret

    def get_eligibility(self):
        self.eligibility_check_fstab = self.P.get_eligibility_check_fstab()
        self.eligibility_check_fs_ext = self.P.get_eligibility_check_fs_ext()
        self.eligibility_check_network_file_p2v = self.P.get_eligibility_check_network_file_p2v()
        self.eligibility_check_vgsize = self.get_eligibility_check_vgsize()

    def rapport_eligibility_header(self):
        RAPPORT_HEADER = "\n##########################################\n"
        RAPPORT_HEADER += "######## RAPPORT D'ELIGIBILITE ###########\n"
        RAPPORT_HEADER += "##########################################"
        print RAPPORT_HEADER

    def rapport_eligibility_fstab(self):
        RAPPORT_FSTAB = "\n*\n"
        if self.eligibility_check_fstab == 1:
            RAPPORT_FSTAB += "* Check fstab : OK\n"
        else:
            RAPPORT_FSTAB += "* Check fstab : NOK\n"
            RAPPORT_FSTAB += "* !!! Le fichier /etc/fstab contient un ou plusieurs LABEL\n"
            RAPPORT_FSTAB += "* Veuillez remplacer les entrees LABEL par les devices correspondant\n"
        RAPPORT_FSTAB += "*\n"
        print RAPPORT_FSTAB

    def rapport_eligibility_fs_ext(self):
        RAPPORT_FSEXT = "*\n"
        if self.eligibility_check_fs_ext == 1:
            RAPPORT_FSEXT += "* Check FileSystem : OK\n"
        else:
            RAPPORT_FSEXT += "* Check FileSystem : NOK\n"
            RAPPORT_FSEXT += "* !!! Une ou plusieurs partitions contienent un FileSystem different de ext2,3,4\n"
        RAPPORT_FSEXT += "*\n"
        print RAPPORT_FSEXT

    def rapport_eligibility_network(self):
        RAPPORT_NETWORK = "*\n"
        if self.eligibility_check_network_file_p2v == 1:
            RAPPORT_NETWORK += "* Check Network : OK\n"
        else:
            RAPPORT_NETWORK += "* Check Network : NOK\n"
            RAPPORT_NETWORK += "* !!! Copiez votre fichier '/etc/network/interfaces' en '/etc/network/interfaces.pre.p2v' en supprimant les vlans\n"
        RAPPORT_NETWORK += "*\n"
        print RAPPORT_NETWORK

    def rapport_eligibility_vgsize(self):
        RAPPORT_VGSIZE = "*\n"
        if self.eligibility_check_vgsize == 1:
            RAPPORT_VGSIZE += "* Check taille dispo sur le VG : OK\n"
        else:
            RAPPORT_VGSIZE += "* Check taille dispo sur le VG : NOK\n"
            RAPPORT_VGSIZE += "* !!! Il n y a pas assez de place sur le VG\n"
        RAPPORT_VGSIZE += "*\n"
        print RAPPORT_VGSIZE

    def rapport_eligibility_result(self):
        somme = (
            self.eligibility_check_fstab
            + self.eligibility_check_fs_ext
            + self.eligibility_check_network_file_p2v
            + self.eligibility_check_vgsize
        )
        if somme == 4:
            print "* le Serveur est eligible\n"
        else:
            print "* !!! Le serveur n'est pas eligible !!!\n"

    def rapport_eligibility(self):
        self.rapport_eligibility_header()
        self.rapport_eligibility_fstab()
        self.rapport_eligibility_fs_ext()
        self.rapport_eligibility_network()
        self.rapport_eligibility_vgsize()
        self.rapport_eligibility_result()

    #######################################################
    ###              FIN   ELIGIBILITY                  ###
    #######################################################

    #######################################################
    ### DEBUT PREPARATION POUR LA GENERATION DU RAPPORT ###
    #######################################################

    def prep_affiche_os_version(self):
        AFFICHE_OS_VERSION = "OS : %s %s\n\n" % (self.version_os["OS"], self.version_os["VERSION"])
        return AFFICHE_OS_VERSION

    def prep_affiche_vm_name(self):
        AFFICHE_VM_NAME = "VM : %s\n" % self.name_vm_dest
        AFFICHE_VM_NAME += "IP LiveCD : %s\n\n" % self.new_name_vm_ip
        return AFFICHE_VM_NAME

    def prep_affiche_memory(self):
        AFFICHE_MEMORY = "MEMORY : %s Mo\n" % self.memory
        return AFFICHE_MEMORY

    def prep_affiche_cpu(self):
        AFFICHE_CPU = "CPU : %s \n\n" % self.cpu
        return AFFICHE_CPU

    def prep_affiche_network(self):
        AFFICHE_NETWORK = "NETWORK :   \n"
        for i in self.tri(self.interfaces):
            AFFICHE_NETWORK += " %s -> Mac address : %s\n" % (i, self.interfaces[i])
        AFFICHE_NETWORK += "\n"
        return AFFICHE_NETWORK

    def prep_affiche_partitions(self):
        AFFICHE_PARTITIONS = "PARTITIONS :\n"
        for i in self.tri(self.partitions[self.type_p2v]):
            AFFICHE_PARTITIONS += " %s %s %s %s\n" % (
                self.partitions[self.type_p2v][i][0],
                self.partitions[self.type_p2v][i][1],
                self.partitions[self.type_p2v][i][2],
                self.partitions[self.type_p2v][i][3],
            )
        return AFFICHE_PARTITIONS

    def prep_cmd_create_partitions(self):
        AFFICHE_CREATE_LV = "\n"
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.partitions[self.type_p2v][i][3] == "/":
                nom_part = "root"
            elif self.partitions[self.type_p2v][i][1] == "swap":
                nom_part = "swap"
            else:
                nom_part = self.partitions[self.type_p2v][i][3].replace("/", "-")[1:]
            AFFICHE_CREATE_LV += "lvcreate -L %sB -n %s-%s %s\n" % (
                self.partitions[self.type_p2v][i][2],
                nom_part,
                self.name_vm_dest,
                self.vgname,
            )
        return AFFICHE_CREATE_LV

    def prep_cmd_copy_dd(self):
        AFFICHE_DD = "\n"
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.partitions[self.type_p2v][i][3] == "/":
                nom_part = "root"
            elif self.partitions[self.type_p2v][i][1] == "swap":
                nom_part = "swap"
            else:
                nom_part = self.partitions[self.type_p2v][i][3].replace("/", "-")[1:]
            AFFICHE_DD += 'echo "Copie de la partition ' + self.partitions[self.type_p2v][i][3] + '"\n'
            if self.partitions[self.type_p2v][i][3] == "swap":
                AFFICHE_DD += "mkswap -v1 /dev/" + self.vgname + "/" + nom_part + "-" + self.name_vm_dest + "\n"
            else:
                ############# DETECTION POUR REDUCTION LV ET RESIZE FS ##################
                # if self.P.is_lv(self.partitions[self.type_p2v][i][0]):
                #  if self.partitions[self.type_p2v][i][2] > 15106127360:
                #    if self.P.taux_occupation(self.partitions[self.type_p2v][i][0],self.partitions[self.type_p2v][i][1]) < 20:
                #      print "Elu pour la reduction"
                #      print "%s is LVM LV" % self.partitions[self.type_p2v][i][0]
                #      print self.P.taux_occupation(self.partitions[self.type_p2v][i][0],self.partitions[self.type_p2v][i][1])
                ############# FIN DETECTION POUR REDUCTION LV ET RESIZE FS ##################
                AFFICHE_DD += (
                    "ssh -c arcfour root@"
                    + self.new_name_vm_ip
                    + " 'dcfldd status=on sizeprobe=if statusinterval=100 if="
                    + self.partitions[self.type_p2v][i][0]
                    + "' bs="
                    + self.bs
                    + " | dd of=/dev/"
                    + self.vgname
                    + "/"
                    + nom_part
                    + "-"
                    + self.name_vm_dest
                    + " bs="
                    + self.bs
                    + "\n"
                )
        return AFFICHE_DD

    def affiche_rapport(self):
        affiche = "---------- RAPPORT ----------\n"
        affiche += self.prep_affiche_os_version()
        affiche += self.prep_affiche_vm_name()
        affiche += self.prep_affiche_memory()
        affiche += self.prep_affiche_cpu()
        affiche += self.prep_affiche_network()
        affiche += self.prep_affiche_partitions()
        self.affiche_rapport = affiche
        return affiche

    #####################################################
    ### FIN PREPARATION POUR LA GENERATION DU RAPPORT ###
    #####################################################

    def get_exec_cmd(self):
        ecrit_cmd = ""
        ecrit_cmd += self.prep_cmd_create_partitions()
        ecrit_cmd += self.prep_cmd_copy_dd()
        return ecrit_cmd

    #############################################################
    ### DEBUT GENERATION DU FICHIER DE CONF XEN (HVM OU PARA) ###
    #############################################################

    def generate_conf_xen(self):
        GEN_CONF = self.generate_conf_xen_para()
        return GEN_CONF

    def ecrit_conf_partitions(self):
        conf = "["
        count = len(self.partitions[self.type_p2v].keys())
        cpt = 1
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.partitions[self.type_p2v][i][3] == "/":
                nom_part = "root"
            elif self.partitions[self.type_p2v][i][1] == "swap":
                nom_part = "swap"
            else:
                nom_part = self.partitions[self.type_p2v][i][3].replace("/", "-")[1:]
            conf += "'phy:/dev/%s/%s-%s,%s,w'" % (self.vgname, nom_part, self.name_vm_dest, i)
            if cpt != int(count):
                conf += ",\n\t"
            cpt = int(cpt) + 1
        conf += "]"
        return conf

    def ecrit_conf_interfaces(self):
        conf = "["
        count = len(self.interfaces.keys())
        cpt = 1
        for i in self.tri(self.interfaces):
            if self.version_os["VERSION"] == "3.1":
                conf += "'mac=%s , bridge=xenbr2003'" % self.interfaces[i]
            else:
                conf += "'bridge=xenbr2003'"
            if cpt != int(count):
                conf += ","
            cpt = int(cpt) + 1
        conf += "]"
        return conf

    def ecrit_memory(self):
        return self.memory

    def ecrit_maxmem(self):
        if self.memory < 4096:
            maxmem = 6144
        elif self.memory > 4096:
            maxmem = (int(self.memory) / 2) + 8192
        return maxmem

    def ecrit_vcpus(self):
        vcpus = "12"
        return vcpus

    def ecrit_vcpu_avail(self):
        if self.cpu == "1":
            vcpu_avail = "3"
        elif self.cpu == "2":
            vcpu_avail = "3"
        elif self.cpu == "3":
            vcpu_avail = "7"
        elif self.cpu == "4":
            vcpu_avail = "15"
        elif self.cpu == "5":
            vcpu_avail = "31"
        elif self.cpu == "6":
            vcpu_avail = "63"
        elif self.cpu == "7":
            vcpu_avail = "127"
        elif self.cpu == "8":
            vcpu_avail = "255"
        return vcpu_avail

    def ecrit_name_vm_dest(self):
        return self.name_vm_dest

    def ecrit_num_sysadmin(self):
        return self.sysadmin

    def ecrit_type_vm(self):
        return self.type_vm

    def ecrit_extra(self):
        extra = "console=xvc0 elevator=noop"
        return extra

    def ecrit_root_kernel(self):
        root = ""
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.partitions[self.type_p2v][i][3] == "/":
                root = i
        root_kernel = "/dev/%s ro" % root
        return root_kernel

    def get_kernel(self):
        self.kernel_vm = []
        if self.version_os["OS"] == "Ubuntu":
            kernel = self.xenmgtconf["KERNEL_UBUNTU"]
            initrd = self.xenmgtconf["INITRD_UBUNTU"]
        elif self.version_os["OS"] == "Debian":
            kernel = self.xenmgtconf["KERNEL_DEBIAN"]
            initrd = self.xenmgtconf["INITRD_DEBIAN"]
        else:
            kernel = self.xenmgtconf["KERNEL_CENTOS"]
            initrd = self.xenmgtconf["INITRD_CENTOS"]
        self.kernel_vm = [kernel, initrd]
        return self.kernel_vm

    def generate_conf_xen_para(self):
        self.get_kernel()
        CONF_PARA = ""
        CONF_PARA += 'kernel = "' + self.kernel_vm[0] + '"\n'
        CONF_PARA += 'ramdisk = "' + self.kernel_vm[1] + '"\n'
        CONF_PARA += "memory = " + str(self.ecrit_memory()) + "\n"
        CONF_PARA += "maxmem = " + str(self.ecrit_maxmem()) + "\n\n"
        CONF_PARA += "vcpus = " + self.ecrit_vcpus() + "\n"
        CONF_PARA += "vcpu_avail = " + self.ecrit_vcpu_avail() + "\n\n"
        CONF_PARA += 'name = "' + self.ecrit_name_vm_dest() + '"\n\n'
        CONF_PARA += "vif = " + self.ecrit_conf_interfaces() + "\n"
        CONF_PARA += "disk = " + self.ecrit_conf_partitions() + "\n"
        CONF_PARA += 'root = "' + self.ecrit_root_kernel() + '"\n'
        CONF_PARA += 'extra = "' + self.ecrit_extra() + '"\n\n'
        CONF_PARA += '#SYSADMIN="' + self.ecrit_num_sysadmin() + '"\n'
        CONF_PARA += '#VMTYPE="' + self.ecrit_type_vm() + '"\n'
        return CONF_PARA

    ###########################################################
    ### FIN GENERATION DU FICHIER DE CONF XEN (HVM OU PARA) ###
    ###########################################################

    def create_rep_p2v(self):
        if not os.path.isdir(self.rep_p2v):
            self.exec_cmd("mkdir -p %s" % self.rep_p2v)

    def check_rep_p2v(self):
        self.rep_p2v = "/etc/xen/P2V/" + self.name_vm_dest + ""
        self.create_rep_p2v()

    def set_fichier_p2v(self, ext="", contenu=""):
        fichier = "%s/%s.%s" % (self.rep_p2v, self.name_vm_dest, ext)
        fd = open(fichier, "w")
        fd.write(contenu)
        fd.close()

    def generation_fichier_p2v(self):
        self.check_rep_p2v()
        self.set_fichier_p2v("sh", self.get_exec_cmd())
        self.set_fichier_p2v("cfg", self.generate_conf_xen())
        self.set_fichier_p2v("var", self.export_variables())
        self.set_fichier_p2v("rapport", self.affiche_rapport)

    def endphase(self):
        AFFICHE = "\n\n"
        AFFICHE += "*****************************************************************\n"
        AFFICHE += "FIN de L'ETAPE 1/2\n\n"
        AFFICHE += "Pour passer a la 2eme Etape, il faut inserer\n"
        AFFICHE += "le LiveCD SLAX, rebooter, puis configurer le reseau de celui-ci\n\n"
        AFFICHE += "Commande pour activer le reseau :\n"
        AFFICHE += "# vconfig add eth0 <NUM_VLAN>\n"
        AFFICHE += "# ip addr add %s/<MASK> dev eth0.<NUM_VLAN>\n" % self.new_name_vm_ip
        AFFICHE += "*****************************************************************\n"
        return AFFICHE

    ##############################
    ###   DEBUT POST INSTALL   ###
    ##############################

    def export_variables(self):
        export_variable = ""
        export_variable += "partitions=%s\n" % self.partitions
        export_variable += 'vgname="%s"\n' % self.vgname
        export_variable += 'type_p2v="%s"\n' % self.type_p2v
        export_variable += 'name_vm_dest="%s"\n' % self.name_vm_dest
        export_variable += "version_os=%s\n" % self.version_os
        return export_variable

    def import_all_variables(self, VM):
        new_val = {}
        execfile("/etc/xen/P2V/" + VM + "/" + VM + ".var", new_val)
        self.new_variables = new_val
        for i in self.new_variables.keys():
            if i != "__builtins__":
                globals()[i] = self.new_variables[i]

    def mkdir_rep_vhosts_vm(self):
        """ Creation du repertoire /vhosts/vm
    """
        print "Creation du repertoire /vhosts/%s" % name_vm_dest
        self.rep_vhosts_vm = "/vhosts/" + name_vm_dest + ""
        self.exec_cmd("mkdir -p %s" % self.rep_vhosts_vm)

    def modif_fstab(self):
        """ Genere le nouveau fichier fstab
    """
        print "preparation du fichier fstab"
        self.exec_cmd("cp %s/etc/fstab %s/etc/fstab.pre.p2v" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
        self.exec_cmd("cp %s/etc/fstab_without_uuid %s/etc/fstab" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
        line = open("/vhosts/" + name_vm_dest + "/etc/fstab_without_uuid", "r").read()
        for i in self.tri(partitions[type_p2v]):
            line = line.replace(partitions[type_p2v][i][0], "/dev/%s" % i, 1)
        fichier = open("/vhosts/" + name_vm_dest + "/etc/fstab", "w")
        fichier.write(line)
        fichier.close()

    def modif_devpts(self):
        if version_os["VERSION"] == "3.1":
            self.exec_cmd(
                'if [ $(grep "/bin/mkdir -p /dev/pts" %s/etc/init.d/mountvirtfs | wc -l) -eq 0 ] ; then sed -i \'/domount sysfs "" \/sys/a \/bin\/mkdir -p \/dev\/pts\' %s/etc/init.d/mountvirtfs ; else echo "" ; fi'
                % (self.rep_vhosts_vm, self.rep_vhosts_vm)
            )
            self.exec_cmd(
                'echo "none\t /dev/pts\t devpts\t gid=5,mode=620\t 0\t 0" >> %s/etc/fstab' % self.rep_vhosts_vm
            )

    def modif_network(self):
        """ Genere le nouveau fichier network (En cours)
    """
        print "preparation du fichier network interfaces"
        if version_os["OS"] == "CentOS":
            self.exec_cmd(
                "cp %s/etc/sysconfig/network_scripts/ifcfg-eth0 %s/etc/sysconfig/network_scripts/ifcfg-eth0.pre.p2v"
                % (self.rep_vhosts_vm, self.rep_vhosts_vm)
            )
        else:
            self.exec_cmd(
                "cp %s/etc/network/interfaces %s/etc/network/interfaces.post.p2v"
                % (self.rep_vhosts_vm, self.rep_vhosts_vm)
            )
            self.exec_cmd(
                "cp %s/etc/network/interfaces.pre.p2v %s/etc/network/interfaces"
                % (self.rep_vhosts_vm, self.rep_vhosts_vm)
            )

    def copie_modules(self):
        """ copie du module 2.6.37 ou 2.6.18.149
    """
        print "copie du module necessaire"
        if version_os["OS"] == "Ubuntu":
            self.exec_cmd(
                "cp -rpdf /lib/modules/%s %s/lib/modules/"
                % (self.xenmgtconf["KERNEL_UBUNTU"].split("/boot/vmlinuz-")[1], self.rep_vhosts_vm)
            )
        if version_os["OS"] == "Debian":
            self.exec_cmd(
                "cp -rpdf /lib/modules/%s %s/lib/modules/"
                % (self.xenmgtconf["KERNEL_DEBIAN"].split("/boot/vmlinuz-")[1], self.rep_vhosts_vm)
            )
        if version_os["OS"] == "CentOS":
            self.exec_cmd(
                "cp -rpdf /lib/modules/%s %s/lib/modules/"
                % (self.xenmgtconf["KERNEL_CENTOS"].split("/boot/vmlinuz-")[1], self.rep_vhosts_vm)
            )

    def set_ntp_sysctl(self):
        """ Modifie le sysctl pour la correction du ntp
    """
        print "Modification du sysctl"
        self.exec_cmd('echo "xen.independent_wallclock = 1" >> %s/etc/sysctl.conf' % self.rep_vhosts_vm)

    def mount_root_vm(self):
        """ Monte la partition root de la VM afin d'effectuer la post_install
    """
        print "montage de la partition root de %s" % name_vm_dest
        self.exec_cmd("mount /dev/%s/root-%s %s" % (vgname, name_vm_dest, self.rep_vhosts_vm))

    def umount_root_vm(self):
        """ Monte la partition root de la VM afin d'effectuer la post_install
    """
        print "demontage de la partition root de %s" % name_vm_dest
        self.exec_cmd("umount %s" % self.rep_vhosts_vm)

    def set_console_xen(self):
        """ Configuration de la console xen pour la VM
    """
        print ""
        self.exec_cmd('echo "xvc0" >> %s/etc/securetty' % self.rep_vhosts_vm)
        if os.path.isfile("%s/etc/inittab" % self.rep_vhosts_vm):
            self.exec_cmd('echo "7:2345:respawn:/sbin/getty 38400 xvc0" >> %s/etc/inittab' % self.rep_vhosts_vm)

        if os.path.isfile("%s/etc/event.d/tty1" % self.rep_vhosts_vm):
            self.exec_cmd("cp %s/etc/event.d/tty1 %s/etc/event.d/xvc0" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
            self.exec_cmd('sed -i "s@tty1@xvc0@" %s/etc/event.d/xvc0' % self.rep_vhosts_vm)

        if os.path.isfile("%s/etc/init/tty1.conf" % self.rep_vhosts_vm):
            self.exec_cmd("cp %s/etc/init/tty1.conf %s/etc/init/xvc0.conf" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
            self.exec_cmd('sed -i "s@tty1@xvc0@" %s/etc/init/xvc0.conf' % self.rep_vhosts_vm)

    def set_modprobe(self):
        """ active le modules xennet pour les interfaces reseaux
    """
        if version_os["OS"] == "Debian":
            self.exec_cmd('echo "alias eth0 xennet" >> %s/etc/modprobe.d/aliases' % self.rep_vhosts_vm)
        else:
            self.exec_cmd('echo "alias eth0 xennet" >> %s/etc/modprobe.d/aliases.conf' % self.rep_vhosts_vm)

    def copy_conf_to_xen(self):
        shutil.copy("/etc/xen/P2V/" + name_vm_dest + "/" + name_vm_dest + ".cfg", "/etc/xen/vm/" + name_vm_dest + "")
        date_generate_p2v = time.strftime("%d/%m/%y %H:%M", time.localtime())
        self.exec_cmd('echo "### P2V genere a %s " >> /etc/xen/vm/%s' % (date_generate_p2v, name_vm_dest))

    def post_install(self):
        self.copy_conf_to_xen()
        self.mkdir_rep_vhosts_vm()
        self.mount_root_vm()
        self.copie_modules()
        self.modif_fstab()
        self.modif_network()
        self.set_ntp_sysctl()
        self.set_console_xen()
        self.set_modprobe()
        self.modif_devpts()
        self.umount_root_vm()
        self.auto_vm()

    ############################
    ###   FIN POST INSTALL   ###
    ############################

    def tri(self, dico):
        """ Permet de tirer un dictionnaire
    """
        return sorted(dico.keys(), key=str)

    def copy_dd_bs(self, bs=""):
        self.bs = bs

    def check_vg_size(self, percent="10"):
        size_total = ()
        size_dispo = ()

    def auto_vm(self):
        self.exec_cmd("cd /etc/xen/auto ; ln -s /etc/xen/vm/" + name_vm_dest + "")

    def exec_cmd_p2v(self):
        os.system("/bin/bash /etc/xen/P2V/" + name_vm_dest + "/" + name_vm_dest + ".sh")

    def is_created_cfg(self, vm):
        return os.path.isfile("/etc/xen/P2V/" + vm + "/" + vm + ".cfg")

    def is_created_lv(self, vm):
        is_lv = self.exec_cmd("ls  /dev/" + self.vgname + "/root-" + vm + " 2>/dev/null | wc -l")
        return is_lv[0].strip()
Exemple #14
0
class xen_host:
 
  def __init__(self, confp2v="/etc/xen/xenmgt-p2v.conf"):
    xenmgtconf = {}
    template_hvm={}
    execfile(confp2v, xenmgtconf)
    execfile(xenmgtconf["TEMPLATE_HVM"],template_hvm)
    self.xenmgtconf = xenmgtconf
    self.template_hvm = template_hvm
    self.bs = self.xenmgtconf["DD_BS"]
    self.type_vm = "P2V"

  def exec_cmd(self, cmd=''):
    CMD = os.popen(cmd, "r")
    ret = CMD.readlines()
    return ret

  def get_os_version(self):
    self.version_os = self.P.get_version_os()
    return self.version_os

  def check_vgname(self):
    liste = self.exec_cmd('vgdisplay | grep Name | grep %s | wc -l' % self.vgname)
    vg = liste[0].strip()
    if vg == "1":
      return self.vgname
    else:
      print "!!!ERROR !!! Le VG %s n'existe pas, utiliser l'option --vg=<VG_NAME> par défaut c'est LVM_XEN" % self.vgname
      sys.exit()

  def get_name_vm_dest(self):
    self.P = physical_host(self.ip_physique)
    self.ssh = Ssh(self.ip_physique)
    
    self.ssh.del_keyfile()
    self.ssh.del_keyfile_client(self.ip_physique)
    
    if self.no_pxe == True:
      print "no_pxe : %s" % self.no_pxe
      self.ssh.copy_id_client(self.ip_physique)
    
    name_vm_dest = self.ssh.exec_cmd("hostname")
    try :
      self.name_vm_dest = name_vm_dest[0].strip()
    except:
      if self.no_pxe == True:
        print ""
        print "!!! ATTENTION L'OPTION 'SERVEUR PXE' A ETE DESACTIVE AFIN DE PASSER PAR UN LIVECD DANS LE CEDEROM !!!"
        print ""
        print "Assurez vous que : "
        print "   - Le port du switch soit bien UnTaggé avec le vlan %s" % self.vlan
        print "   - Votre LiveCD soit bien inséré et configuré avec l'ip correspondante :"
        print "       # ip addr add %s/24 dev eth0" % self.ip_physique
        print "   - Le paquet 'dcfldd' soit installé sur le livecd"
      else:
        print "connexion impossible avec le serveur physique, le CFp2v est bien descendu sur le serveur physique ?"
      sys.exit()
    self.new_name_vm_ip = self.ip_physique
    return self.name_vm_dest
    
  def get_interfaces(self):
    self.interfaces = self.P.get_interfaces()
    #P = physical_host(self.ip_physique)
    #nb_vlan = len(self.ConfCFengine["NETWORK"])
    MacAddrPerInterfaces = self.P.get_interfaces()
    mac = GenerateMacAddr()

    for i in self.tri(self.ConfCFengine["NETWORK"]):
      try:
        self.ConfCFengine["NETWORK"][i]["MAC"] = MacAddrPerInterfaces[i]
        mac(MacAddrPerInterfaces[i])
      except:
        self.ConfCFengine["NETWORK"][i]["MAC"] = mac.generate()
    self.interfaces = self.ConfCFengine["NETWORK"]
    return self.interfaces

  def get_memory(self):
    self.memory = self.P.get_memory1()
    return self.memory

  def get_partitions(self):
    self.partitions = self.P.get_all_partitions()
    return self.partitions
  
  def get_cpu(self):
    self.cpu = self.P.get_cpu()
    return self.cpu

  def get_info_cfp2v(self):
    ConfCFengine = {}
    ConfCFengine["MAC_ADDR"] = ""
    try:
      execfile("/etc/xen/P2V/conf/%s/%s/config" % (self.projet_p2v, self.vmnamecfengine), ConfCFengine)
      self.ConfCFengine = ConfCFengine
    except:
      print "Le produit CFp2V n'est pas descendu correctement ou %s n'a pas été ajouté au CFp2v" % self.vmnamecfengine
      sys.exit()
    ConfCFp2v = {}
    #ConfCFp2v[self.vmnamecfengine] = {"VLAN":ConfCFengine["ID_VLAN"], "IP_PXE":ConfCFengine["IP_PXE"], "IP_VM":ConfCFengine["IP_VM"], "IP_XEN":ConfCFengine["IP_XEN"]}
    ConfCFp2v[self.vmnamecfengine] = {"VLAN":ConfCFengine["NETWORK"], "IP_PXE":ConfCFengine["IP_PXE"], "IP_VM":ConfCFengine["IP_VM"], "IP_XEN":ConfCFengine["IP_XEN"],"MAC_ADDR":ConfCFengine["MAC_ADDR"]}
    self.ConfCFp2v = ConfCFp2v
    self.vlan = self.ConfCFp2v[self.vmnamecfengine]["VLAN"]["eth0"]["VLAN"]
    self.ip_pxe = self.ConfCFp2v[self.vmnamecfengine]["IP_PXE"]
    self.ip_physique = self.ConfCFp2v[self.vmnamecfengine]["IP_VM"]
    self.ip_xen = self.ConfCFp2v[self.vmnamecfengine]["IP_XEN"]
    self.mac_addr_v2p = self.ConfCFp2v[self.vmnamecfengine]["MAC_ADDR"]
    return self.ConfCFp2v

  def mount_gateway_intra_vlan(self):
    os.system('ip addr add %s/24 dev %s%s' % (self.ip_xen, self.bridge_prefix, self.vlan))
    
  def umount_gateway_intra_vlan(self):
    os.system('ip addr del %s/24 dev %s%s' % (self.ip_xen, self.bridge_prefix, self.vlan))



  def build_cnx(self):
    self.mount_gateway_intra_vlan()

  def get_info_srv_physique(self):
    self.get_os_version()
    self.get_memory()
    self.get_interfaces()
    self.get_partitions()
    self.get_cpu()

  def is_livecd(self):
    P = physical_host(self.ip_physique)
    self.is_livecd = P.is_livecd()
    return self.is_livecd


  #######################################################
  ###               ELIGIBILITY                       ###
  #######################################################

  def get_size_vg(self):
    TOTAL = self.exec_cmd("vgs --units B | grep %s | awk '{print $6}' | sed 's/B//'" % self.vgname)[0].strip()
    FREE = self.exec_cmd("vgs --units B | grep %s | awk '{print $7}' | sed 's/B//'" % self.vgname)[0].strip()
    vgsize = {"TOTAL":TOTAL, "FREE":FREE}
    return vgsize

  def get_eligibility_check_vgsize(self):
    TailleTotalPart = 0
    self.get_partitions()
    for i in self.tri(self.partitions[self.type_p2v]):
      TailleTotalPart = (TailleTotalPart + int(self.partitions[self.type_p2v][i]["SIZE"]))
    VGSIZE = self.get_size_vg()
    NEW_VGSIZE = (int(VGSIZE["FREE"]) - ((self.xenmgtconf["VG_PERCENT_EMERGENCY"] * int(VGSIZE["FREE"])) / 100))
    if TailleTotalPart < NEW_VGSIZE:
      ret = 1
    else:
      ret = 0
    return ret

  def get_eligibility(self):
    self.eligibility_check_fstab = self.P.get_eligibility_check_fstab()
    self.eligibility_check_fs_ext = self.P.get_eligibility_check_fs_ext()
    #self.eligibility_check_network_file_p2v = self.P.get_eligibility_check_network_file_p2v()
    self.eligibility_check_vgsize = self.get_eligibility_check_vgsize()

  def rapport_eligibility_header(self):
    RAPPORT_HEADER = "\n##########################################\n"
    RAPPORT_HEADER += "######## RAPPORT D'ELIGIBILITE ###########\n"
    RAPPORT_HEADER += "##########################################"
    print RAPPORT_HEADER

  def rapport_eligibility_fstab(self):
    RAPPORT_FSTAB = "\n*\n"
    if self.eligibility_check_fstab == 1:
      RAPPORT_FSTAB += "* Check fstab : OK\n"
    else:
      RAPPORT_FSTAB += "* Check fstab : NOK\n"
      RAPPORT_FSTAB += "* !!! Le fichier /etc/fstab contient un ou plusieurs LABEL\n"
      RAPPORT_FSTAB += "* Veuillez remplacer les entrées LABEL par les devices correspondant\n"
    RAPPORT_FSTAB += "*\n"
    print RAPPORT_FSTAB

  def rapport_eligibility_fs_ext(self):
    RAPPORT_FSEXT = "*\n"
    if self.eligibility_check_fs_ext == 1:
      RAPPORT_FSEXT += "* Check FileSystem : OK\n"
    else:
      RAPPORT_FSEXT += "* Check FileSystem : NOK\n"
      RAPPORT_FSEXT += "* !!! Une ou plusieurs partitions contiennent un FileSystem different de ext2,3,4\n"
    RAPPORT_FSEXT += "*\n"
    print RAPPORT_FSEXT

  #def rapport_eligibility_network(self):
  #  RAPPORT_NETWORK = "*\n"
  #  if self.eligibility_check_network_file_p2v == 1:
  #    RAPPORT_NETWORK += "* Check Network : OK\n"
  #  else:
  #    RAPPORT_NETWORK += "* Check Network : NOK\n"
  #    RAPPORT_NETWORK += "* !!! Copiez votre fichier '/etc/network/interfaces' en '/etc/network/interfaces.pre.p2v' en supprimant les vlans\n"
  #  RAPPORT_NETWORK += "*\n"
  #  print RAPPORT_NETWORK
  
  def rapport_eligibility_vgsize(self):
    RAPPORT_VGSIZE = "*\n"
    if self.eligibility_check_vgsize == 1:
      RAPPORT_VGSIZE += "* Check taille dispo sur le VG : OK\n"
    else:
      RAPPORT_VGSIZE += "* Check taille dispo sur le VG : NOK\n"
      RAPPORT_VGSIZE += "* !!! Il n y a pas assez de place sur le VG\n"
    RAPPORT_VGSIZE += "*\n"
    print RAPPORT_VGSIZE

  def rapport_eligibility_result(self):
    #somme = (self.eligibility_check_fstab + self.eligibility_check_fs_ext + self.eligibility_check_network_file_p2v + self.eligibility_check_vgsize)
    somme = (self.eligibility_check_fstab + self.eligibility_check_fs_ext + self.eligibility_check_vgsize)
    #if somme == 4:
    if somme == 3:
      print "* le Serveur est éligible\n"
    else:
      print "* !!! Le serveur n'est pas éligible !!!\n"

  def rapport_eligibility(self):
    self.rapport_eligibility_header()
    self.rapport_eligibility_fstab()
    self.rapport_eligibility_fs_ext()
    #self.rapport_eligibility_network()
    self.rapport_eligibility_vgsize() 
    self.rapport_eligibility_result()

  #######################################################
  ###              FIN   ELIGIBILITY                  ###
  #######################################################


  #######################################################  
  ### DEBUT PREPARATION POUR LA GENERATION DU RAPPORT ###
  #######################################################
      
  def prep_affiche_os_version(self):
    AFFICHE_OS_VERSION = "OS : %s %s\n\n" % (self.version_os["OS"], self.version_os["VERSION"]) 
    return AFFICHE_OS_VERSION
  
  def prep_affiche_vm_name(self):
    AFFICHE_VM_NAME = "VM : %s\n" % self.name_vm_dest
    AFFICHE_VM_NAME += "IP LiveCD : %s\n\n" % self.new_name_vm_ip
    return AFFICHE_VM_NAME

  def prep_affiche_memory(self):
    AFFICHE_MEMORY = "MEMORY : %s Mo\n" % self.memory
    return AFFICHE_MEMORY

  def prep_affiche_cpu(self):
    AFFICHE_CPU = "CPU : %s \n\n" % self.cpu
    return AFFICHE_CPU

  def prep_affiche_network(self):
    AFFICHE_NETWORK = "NETWORK :   \n"
    for i in self.tri(self.interfaces):
      AFFICHE_NETWORK += " %s -> Mac address : %s\n" % (i, self.interfaces[i]["MAC"])
    AFFICHE_NETWORK += "\n"
    AFFICHE_NETWORK += "\n"
    return AFFICHE_NETWORK

  def prep_affiche_partitions(self):
    AFFICHE_PARTITIONS = "PARTITIONS :\n"
    for i in self.tri(self.partitions[self.type_p2v]):
      if self.type_p2v == "HVM":
        AFFICHE_PARTITIONS += " %s -> taille : %s\n" % (self.partitions[self.type_p2v][i]["DEVICE"],self.partitions[self.type_p2v][i]["SIZE"])
      if self.type_p2v == "PARA":  
        AFFICHE_PARTITIONS += " %s %s %s %s\n" % (self.partitions[self.type_p2v][i]["DEVICE"], self.partitions[self.type_p2v][i]["FS"], self.partitions[self.type_p2v][i]["SIZE"], self.partitions[self.type_p2v][i]["PARTITION"])
    return AFFICHE_PARTITIONS

  def prep_cmd_create_partitions(self):
    AFFICHE_CREATE_LV = "\n"
    for i in self.tri(self.partitions[self.type_p2v]):
      if self.type_p2v == "HVM":
        nom_partition = i.replace('/','-')
        AFFICHE_CREATE_LV += "lvcreate -L %sB -n %s-%s %s\n" % (self.partitions[self.type_p2v][i]["SIZE"],self.partitions[self.type_p2v][i]["DEVICE"],self.name_vm_dest,self.vgname)
      if self.type_p2v == "PARA":
        if self.partitions[self.type_p2v][i]["PARTITION"] == "/":
          nom_part = "root"
        elif self.partitions[self.type_p2v][i]["FS"] == "swap":
          nom_part = "swap"
        else:
          nom_part = self.partitions[self.type_p2v][i]["PARTITION"].replace("/", "-")[1:]
        AFFICHE_CREATE_LV += "lvcreate -L %sB -n %s-%s %s\n" % (self.partitions[self.type_p2v][i]["SIZE"], nom_part, self.name_vm_dest, self.vgname)
    return AFFICHE_CREATE_LV

  def prep_cmd_copy_dd(self):
    AFFICHE_DD = "\n"
    for i in self.tri(self.partitions[self.type_p2v]):
      if self.type_p2v == "HVM":
        AFFICHE_DD += "ssh -i /etc/xen/P2V/ssh/p2v.key -c arcfour root@"+ self.new_name_vm_ip +" 'dcfldd status=on sizeprobe=if statusinterval=100 if=/dev/"+ i +"' bs="+ self.bs +" | dd of=/dev/"+ self.vgname +"/"+ self.partitions[self.type_p2v][i]["DEVICE"] +"-"+self.name_vm_dest+" bs="+ self.bs +"\n"
      if self.type_p2v == "PARA":
        if self.partitions[self.type_p2v][i]["PARTITION"] == "/":
          nom_part = "root"
        elif self.partitions[self.type_p2v][i]["FS"] == "swap" or self.partitions[self.type_p2v][i]["PARTITION"] == "swap":
          nom_part = "swap"
        else:
          nom_part = self.partitions[self.type_p2v][i]["PARTITION"].replace("/", "-")[1:]
        AFFICHE_DD += "echo \"Copie de la partition " + self.partitions[self.type_p2v][i]["PARTITION"] + " ( "+ self.partitions[self.type_p2v][i]["DEVICE"] +" )\"\n"
        if self.partitions[self.type_p2v][i]["PARTITION"] == "swap" or self.partitions[self.type_p2v][i]["FS"] == "swap":
          AFFICHE_DD += "mkswap -v1 /dev/" + self.vgname + "/" + nom_part + "-" + self.name_vm_dest + "\n"
        else:
          ############# DETECTION POUR REDUCTION LV ET RESIZE FS ##################
          #if self.P.is_lv(self.partitions[self.type_p2v][i]["DEVICE"]):
          #  if self.partitions[self.type_p2v][i]["SIZE"] > 15106127360:
          #    if self.P.taux_occupation(self.partitions[self.type_p2v][i]["DEVICE"],self.partitions[self.type_p2v][i]["FS"]) < 20:
          #      print "Elu pour la reduction"
          #      print "%s is LVM LV" % self.partitions[self.type_p2v][i]["DEVICE"]
          #      print self.P.taux_occupation(self.partitions[self.type_p2v][i]["DEVICE"],self.partitions[self.type_p2v][i]["FS"])
          ############# FIN DETECTION POUR REDUCTION LV ET RESIZE FS ##################
          if self.version_os["VERSION"] == "3.1":
            AFFICHE_DD += "ssh -i /etc/xen/P2V/ssh/p2v.key -c arcfour root@" + self.new_name_vm_ip + " 'dcfldd status=on sizeprobe=if statusinterval=100 if=" + self.partitions[self.type_p2v][i]["DEVICE"] + "' bs=" + self.bs + " | dd of=/dev/" + self.vgname + "/" + nom_part + "-" + self.name_vm_dest + " bs=" + self.bs + "\n"
          else:
            AFFICHE_DD += "ssh -i /etc/xen/P2V/ssh/p2v.key -c arcfour root@" + self.new_name_vm_ip + " 'dcfldd status=on sizeprobe=if statusinterval=100 if=/dev/disk/by-uuid/" + self.partitions[self.type_p2v][i]["UUID"] + "' bs=" + self.bs + " | dd of=/dev/" + self.vgname + "/" + nom_part + "-" + self.name_vm_dest + " bs=" + self.bs + "\n"
    return AFFICHE_DD

  def affiche_rapport(self):
    affiche = "---------- RAPPORT ----------\n"
    affiche += self.prep_affiche_os_version()
    affiche += self.prep_affiche_vm_name()
    affiche += self.prep_affiche_memory()
    affiche += self.prep_affiche_cpu()
    affiche += self.prep_affiche_network()
    affiche += self.prep_affiche_partitions()
    self.affiche_rapport = affiche
    return affiche

  #####################################################  
  ### FIN PREPARATION POUR LA GENERATION DU RAPPORT ###
  #####################################################


  def get_exec_cmd(self):
    ecrit_cmd = ""
    ecrit_cmd += self.prep_cmd_create_partitions()
    ecrit_cmd += self.prep_cmd_copy_dd()
    return ecrit_cmd



  #############################################################  
  ### DEBUT GENERATION DU FICHIER DE CONF XEN (HVM OU PARA) ###
  #############################################################

  def generate_conf_xen(self):
    if self.type_p2v == "PARA":
      GEN_CONF = self.generate_conf_xen_para()
    if self.type_p2v == "HVM":
      GEN_CONF = self.generate_conf_xen_hvm()
    return GEN_CONF

  def ecrit_conf_partitions(self):
    conf = "["
    count = len(self.partitions[self.type_p2v].keys())
    cpt = 1
    for i in self.tri(self.partitions[self.type_p2v]):
      if self.type_p2v == "HVM":
        conf += "'phy:/dev/%s/%s-%s,%s,w'" % (self.vgname,self.partitions[self.type_p2v][i]["DEVICE"],self.name_vm_dest,self.partitions[self.type_p2v][i]["DEVICE"])
      if self.type_p2v == "PARA":
        if self.partitions[self.type_p2v][i]["PARTITION"] == "/":
          nom_part = "root"
        elif self.partitions[self.type_p2v][i]["FS"] == "swap":
          nom_part = "swap"
        else:
          nom_part = self.partitions[self.type_p2v][i]["PARTITION"].replace("/", "-")[1:]
        conf += "'phy:/dev/%s/%s-%s,%s,w'" % (self.vgname, nom_part, self.name_vm_dest, i)
      if cpt != int(count):
        conf += ",\n\t"
      cpt = (int(cpt) + 1)
    conf += "]"
    return conf
  
  def uuid_gen(self):
    uuid_gen = self.exec_cmd('uuidgen')
    return uuid_gen[0].strip()
  
  def generate_conf_xen_hvm(self):
    self.ecrit_conf_partitions()
    CONF_HVM = ""
    for i in self.template_hvm.keys():
      if i != "__builtins__":
        print i
        if i == "vif":
          CONF_HVM += ""+str(i)+" = "+ self.ecrit_conf_interfaces() +"\n"
        elif i == "disk":
          CONF_HVM += ""+str(i)+" = "+ self.ecrit_conf_partitions() +"\n"
        elif i == "uuid":
          CONF_HVM += ""+str(i)+" = \""+ self.uuid_gen() +"\"\n"
        elif i == "name":
          CONF_HVM += ""+str(i)+" = \""+ self.name_vm_dest +"\"\n"
        elif i == "memory":
          CONF_HVM += ""+str(i)+" = "+ self.ecrit_memory() +"\n"
        elif i == "vcpus":
          CONF_HVM += ""+str(i)+" = "+ self.cpu +"\n"
        else:
          CONF_HVM += ""+str(i)+" = '"+ str(self.template_hvm[i]) +"'\n"
    return CONF_HVM

  def ecrit_conf_interfaces(self):
    conf = "["
    count = len(self.interfaces.keys())
    cpt = 1
    for i in self.tri(self.interfaces):
      if self.type_p2v == "HVM":
        conf += "'mac=%s , bridge=%s%s'" % (self.interfaces[i]["MAC"],self.bridge_prefix, self.interfaces[i]["VLAN"])
      else:
        if self.version_os["VERSION"] == "3.1":
          conf += "'mac=%s , bridge=%s%s'" % (self.interfaces[i]["MAC"],self.bridge_prefix, self.interfaces[i]["VLAN"])
        else:
          if self.keep_mac_addr:
            conf += "'mac=%s , bridge=%s%s'" % (self.interfaces[i]["MAC"],self.bridge_prefix, self.interfaces[i]["VLAN"])
          else:
            conf += "'bridge=%s%s'" % (self.bridge_prefix, self.interfaces[i]["VLAN"])
        if cpt != int(count):
          conf += ","
        cpt = (int(cpt) + 1)
    conf += "]"
    return conf

  def ecrit_memory(self):
    return self.memory

  def ecrit_maxmem(self):
    if int(self.memory) <= 4096:
      maxmem = 6144
    elif int(self.memory) > 4096:
      maxmem = ((int(self.memory) / 2) + 8192)
    return maxmem

  def ecrit_vcpus(self):
    vcpus = "12"
    return vcpus

  def ecrit_vcpu_avail(self):
    if int(self.cpu) == 1:
      vcpu_avail = "3"
    elif int(self.cpu) == 2:
      vcpu_avail = "3"
    elif int(self.cpu) == 3:
      vcpu_avail = "7"
    elif int(self.cpu) == 4:
      vcpu_avail = "15"
    elif int(self.cpu) == 5:
      vcpu_avail = "31"
    elif int(self.cpu) == 6:
      vcpu_avail = "63"
    elif int(self.cpu) == 7:
      vcpu_avail = "127"
    elif int(self.cpu) == 8:
      vcpu_avail = "255"
    elif int(self.cpu) > 8:
      vcpu_avail = "255"
    return vcpu_avail

  def ecrit_name_vm_dest(self):
    return self.name_vm_dest

  def ecrit_num_sysadmin(self):
    return self.sysadmin
  
  def ecrit_type_vm(self):
    return self.type_vm

  def ecrit_extra(self):
    extra = "console=xvc0 elevator=noop"
    return extra

  def ecrit_root_kernel(self):
    root = ""
    for i in self.tri(self.partitions[self.type_p2v]):
      if self.partitions[self.type_p2v][i]["PARTITION"] == "/":
        root = i
    root_kernel = "/dev/%s ro" % root 
    return root_kernel

  def get_kernel(self):
    self.kernel_vm = []
    if self.version_os["OS"] == "Ubuntu":
      kernel = self.xenmgtconf["KERNEL_UBUNTU"]
      initrd = self.xenmgtconf["INITRD_UBUNTU"]
    elif self.version_os["OS"] == "Debian":
      kernel = self.xenmgtconf["KERNEL_DEBIAN"]
      initrd = self.xenmgtconf["INITRD_DEBIAN"]
    else:
      kernel = self.xenmgtconf["KERNEL_CENTOS"]
      initrd = self.xenmgtconf["INITRD_CENTOS"]
    self.kernel_vm = [kernel, initrd]
    return self.kernel_vm

  def generate_conf_xen_para(self):
    self.get_kernel()
    CONF_PARA = ""
    CONF_PARA += "kernel = \"" + self.kernel_vm[0] + "\"\n"
    CONF_PARA += "ramdisk = \"" + self.kernel_vm[1] + "\"\n"
    CONF_PARA += "memory = " + str(self.ecrit_memory()) + "\n"
    CONF_PARA += "maxmem = " + str(self.ecrit_maxmem()) + "\n\n"
    CONF_PARA += "vcpus = " + self.ecrit_vcpus() + "\n"
    CONF_PARA += "vcpu_avail = " + self.ecrit_vcpu_avail() + "\n\n"
    CONF_PARA += "name = \"" + self.ecrit_name_vm_dest() + "\"\n\n"
    CONF_PARA += "vif = " + self.ecrit_conf_interfaces() + "\n"
    CONF_PARA += "disk = " + self.ecrit_conf_partitions() + "\n"
    CONF_PARA += "root = \"" + self.ecrit_root_kernel() + "\"\n"
    CONF_PARA += "extra = \"" + self.ecrit_extra() + "\"\n\n"
    CONF_PARA += "#SYSADMIN=\"" + self.ecrit_num_sysadmin() + "\"\n"
    CONF_PARA += "#VMTYPE=\"" + self.ecrit_type_vm() + "\"\n"
    return CONF_PARA

  ###########################################################  
  ### FIN GENERATION DU FICHIER DE CONF XEN (HVM OU PARA) ###
  ###########################################################

  def create_rep_p2v(self):
    if not os.path.isdir(self.rep_p2v):
      self.exec_cmd("mkdir -p %s" % self.rep_p2v)

  def check_rep_p2v(self):
    self.rep_p2v = "/etc/xen/P2V/" + self.name_vm_dest + ""
    self.create_rep_p2v()

  def set_fichier_p2v(self, ext="", contenu=""):
    fichier = "%s/%s.%s" % (self.rep_p2v, self.name_vm_dest, ext)
    fd = open(fichier, "w")
    fd.write(contenu)
    fd.close()
    
  def generation_fichier_p2v(self):
    self.check_rep_p2v()
    
    GenConf = GenConfNetwork()
    GenConf.interfaces = self.interfaces
    GenConf.LocalDir = self.rep_p2v
    GenConf.ip_physique = self.ip_physique
    
    self.set_fichier_p2v("sh", self.get_exec_cmd())
    self.set_fichier_p2v("cfg", self.generate_conf_xen())
    self.set_fichier_p2v("var", self.export_variables())
    self.set_fichier_p2v("rapport", self.affiche_rapport)
    
    GenConf.GenerateFileNetwork()


  def endphase(self):
    AFFICHE = "\n\n"
    AFFICHE += "*****************************************************************\n"
    AFFICHE += "FIN de L'ETAPE 1/3\n\n"
    AFFICHE += "Pour passer a la 2eme Etape, il faut relancer le meme script\n"
    AFFICHE += "*****************************************************************\n"
    return AFFICHE


  ##############################
  ###   DEBUT POST INSTALL   ###
  ##############################

  def export_variables(self):
    export_variable = ""
    export_variable += "partitions=%s\n" % self.partitions
    export_variable += "vgname=\"%s\"\n" % self.vgname
    export_variable += "type_p2v=\"%s\"\n" % self.type_p2v
    export_variable += "name_vm_dest=\"%s\"\n" % self.name_vm_dest
    export_variable += "version_os=%s\n" % self.version_os
    export_variable += "vlan=%s\n" % self.vlan
    export_variable += "mac_addr=\"%s\"\n" % self.interfaces["eth0"]["MAC"]
    export_variable += "ip_pxe=\"%s\"\n" % self.ip_pxe
    export_variable += "ip_physique=\"%s\"\n" % self.ip_physique
    return export_variable

  def import_all_variables(self, VM):
    new_val = {}
    execfile("/etc/xen/P2V/" + VM + "/" + VM + ".var", new_val)
    self.new_variables = new_val
    for i in self.new_variables.keys():
      if i != "__builtins__":
        globals()[i] = self.new_variables[i]
    self.mac_addr = globals()["mac_addr"]
     
 
  def mkdir_rep_vhosts_vm(self):
    """ Creation du repertoire /vhosts/vm
    """
    print "Creation du repertoire /vhosts/%s" % name_vm_dest
    self.rep_vhosts_vm = "/vhosts/" + name_vm_dest + ""
    self.exec_cmd("mkdir -p %s" % self.rep_vhosts_vm)

  def modif_fstab(self):
    """ Genere le nouveau fichier fstab
    """
    print "preparation du fichier fstab"
    self.exec_cmd("cp %s/etc/fstab %s/etc/fstab.pre.p2v" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
    self.exec_cmd("cp %s/etc/fstab_without_uuid %s/etc/fstab" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
    line = open("/vhosts/" + name_vm_dest + "/etc/fstab_without_uuid", "r").read()
    for i in self.tri(partitions[type_p2v]):
        line = line.replace(partitions[type_p2v][i]["DEVICE"], "/dev/%s" % i, 1)
    fichier = open("/vhosts/" + name_vm_dest + "/etc/fstab", "w")
    fichier.write(line)
    fichier.close()

  def modif_devpts(self):
    if version_os["VERSION"] == "3.1":
      self.exec_cmd("if [ $(grep \"/bin/mkdir -p /dev/pts\" %s/etc/init.d/mountvirtfs | wc -l) -eq 0 ] ; then sed -i '/domount sysfs \"\" \/sys/a \/bin\/mkdir -p \/dev\/pts' %s/etc/init.d/mountvirtfs ; else echo \"\" ; fi" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
      self.exec_cmd("echo \"none\t /dev/pts\t devpts\t gid=5,mode=620\t 0\t 0\" >> %s/etc/fstab" % self.rep_vhosts_vm)

  def modif_network(self):
    """ Genere le nouveau fichier network (En cours)
    """
    print "preparation du fichier network interfaces"
    if version_os["OS"] == "CentOS":
      self.exec_cmd("cp %s/etc/sysconfig/network_scripts/ifcfg-eth0 %s/etc/sysconfig/network_scripts/ifcfg-eth0.pre.p2v" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
    else:
      self.exec_cmd("cp %s/etc/network/interfaces %s/etc/network/interfaces.post.p2v" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
      self.exec_cmd("cp %s/etc/network/interfaces.pre.p2v %s/etc/network/interfaces" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
    
    if self.keep_mac_addr == False:
        self.exec_cmd("echo '' > %s/etc/udev/rules.d/*persistent-net.rules" % self.rep_vhosts_vm)

  def copie_modules(self):
    """ copie du module 2.6.37 ou 2.6.18.149
    """
    print "copie du module necessaire"
    if version_os["OS"] == "Ubuntu":
      self.exec_cmd("cp -rpdf /lib/modules/%s %s/lib/modules/" % (self.xenmgtconf["KERNEL_UBUNTU"].split("/boot/vmlinuz-")[1], self.rep_vhosts_vm))
    if version_os["OS"] == "Debian":
      self.exec_cmd("cp -rpdf /lib/modules/%s %s/lib/modules/" % (self.xenmgtconf["KERNEL_DEBIAN"].split("/boot/vmlinuz-")[1], self.rep_vhosts_vm))
    if version_os["OS"] == "CentOS":
      self.exec_cmd("cp -rpdf /lib/modules/%s %s/lib/modules/" % (self.xenmgtconf["KERNEL_CENTOS"].split("/boot/vmlinuz-")[1], self.rep_vhosts_vm))

  def set_ntp_sysctl(self):
    """ Modifie le sysctl pour la correction du ntp
    """
    print "Modification du sysctl"
    self.exec_cmd("echo \"xen.independent_wallclock = 1\" >> %s/etc/sysctl.conf" % self.rep_vhosts_vm)
 
  def mount_root_vm(self):
    """ Monte la partition root de la VM afin d'effectuer la post_install
    """
    print "montage de la partition root de %s" % name_vm_dest
    if type_p2v == "PARA":
      device_racine = "root"
      self.exec_cmd("mount /dev/%s/%s-%s %s" % (vgname, device_racine, name_vm_dest, self.rep_vhosts_vm))
    elif type_p2v == "HVM":
      device_racine = "hda"

  def umount_root_vm(self):
    """ Monte la partition root de la VM afin d'effectuer la post_install
    """
    print "demontage de la partition root de %s" % name_vm_dest
    self.exec_cmd("umount %s" % self.rep_vhosts_vm)

  def set_console_xen(self):
    """ Configuration de la console xen pour la VM
    """
    print ""
    self.exec_cmd("echo \"xvc0\" >> %s/etc/securetty" % self.rep_vhosts_vm) 
    if os.path.isfile("%s/etc/inittab" % self.rep_vhosts_vm):
      self.exec_cmd("echo \"7:2345:respawn:/sbin/getty 38400 xvc0\" >> %s/etc/inittab" % self.rep_vhosts_vm) 

    if os.path.isfile("%s/etc/event.d/tty1" % self.rep_vhosts_vm):
      self.exec_cmd("cp %s/etc/event.d/tty1 %s/etc/event.d/xvc0" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
      self.exec_cmd("sed -i \"s@tty1@xvc0@\" %s/etc/event.d/xvc0" % self.rep_vhosts_vm)
    
    if os.path.isfile("%s/etc/init/tty1.conf" % self.rep_vhosts_vm):
      self.exec_cmd("cp %s/etc/init/tty1.conf %s/etc/init/xvc0.conf" % (self.rep_vhosts_vm, self.rep_vhosts_vm))
      self.exec_cmd("sed -i \"s@tty1@xvc0@\" %s/etc/init/xvc0.conf" % self.rep_vhosts_vm)

  def set_modprobe(self):
    """ active le modules xennet pour les interfaces reseaux
    """
    if version_os["OS"] == "Debian":
      self.exec_cmd("echo \"alias eth0 xennet\" >> %s/etc/modprobe.d/aliases" % self.rep_vhosts_vm)
    else: 
      self.exec_cmd("echo \"alias eth0 xennet\" >> %s/etc/modprobe.d/aliases.conf" % self.rep_vhosts_vm)

  def copy_conf_to_xen(self):
    shutil.copy("/etc/xen/P2V/" + name_vm_dest + "/" + name_vm_dest + ".cfg", "/etc/xen/vm/" + name_vm_dest + "")
    date_generate_p2v = time.strftime("%d/%m/%y %H:%M", time.localtime())
    self.exec_cmd("echo \"### P2V genere a %s \" >> /etc/xen/vm/%s" % (date_generate_p2v, name_vm_dest))

  def auto_vm(self):
    self.exec_cmd("cd /etc/xen/auto ; ln -s /etc/xen/vm/" + name_vm_dest + "")

  def del_lock_dhcpd(self):
    self.pxe = pxe()
    self.pxe.del_lock_dhcpd(ip_pxe, name_vm_dest)

  def finish_p2v(self):
    self.exec_cmd("touch /etc/xen/P2V/" + name_vm_dest + "/" + name_vm_dest + ".finish")

  def post_install(self):
    if self.type_p2v == "PARA":
      self.post_install_para()
    if self.type_p2v == "HVM":
      self.post_install_hvm()

  def post_install_para(self):
    self.copy_conf_to_xen()
    self.mkdir_rep_vhosts_vm()
    self.mount_root_vm()
    self.copie_modules()
    self.modif_fstab()
    self.modif_network()
    self.set_ntp_sysctl()
    self.set_console_xen()
    self.set_modprobe()
    self.modif_devpts()
    self.umount_root_vm()
    self.auto_vm()
    self.del_lock_dhcpd()
    self.finish_p2v()
    self.umount_gateway_intra_vlan()

  def post_install_hvm(self):
    self.copy_conf_to_xen()
    #self.mkdir_rep_vhosts_vm()
    #self.mount_root_vm()
    #self.modif_fstab()
    #self.modif_network()
    #self.set_ntp_sysctl()
    #self.modif_devpts()
    #self.umount_root_vm()
    self.auto_vm()
    
    
  ############################
  ###   FIN POST INSTALL   ###
  ############################


  def tri(self, dico):
    """ Permet de tirer un dictionnaire
    """
    return sorted(dico.keys(), key=str) 

  def tri_inverse(self, dico):
    """ Permet de tirer un dictionnaire
    """
    return sorted(dico.keys(), key=str, reverse=True) 

  def copy_dd_bs(self, bs=''):
    self.bs = bs

  def check_vg_size(self, percent='10'):
    size_total = ()
    size_dispo = ()

  def exec_cmd_p2v(self):
    self.MappingDevice()
    os.system("/bin/bash /etc/xen/P2V/" + name_vm_dest + "/" + name_vm_dest + ".sh")

  def MappingDevice(self):
    P = physical_host(ip_physique)
    # Verifiaction de la presence du device
    New_dev = "0"
    AFFICHE_PARTITIONS = ""
    for i in self.tri(partitions[type_p2v]):
      if type_p2v == "HVM":
        AFFICHE_PARTITIONS += " %s -> taille : %s\n" % (partitions[type_p2v][i]["DEVICE"],partitions[type_p2v][i]["SIZE"])
      if type_p2v == "PARA":
        if partitions[type_p2v][i]["DEVICE"] == "/dev/cciss/c0d0p1":
          CheckDevice = P.exec_cmd_ssh("file %s | grep -v ERROR | wc -l" % partitions[type_p2v][i]["DEVICE"])[0].strip()
          if CheckDevice == "0":
            New_dev = "/dev/sda"
            pass
    if New_dev <> "0":
      fic_exec = "/etc/xen/P2V/%s/%s.sh" % (name_vm_dest,name_vm_dest)
      result = file(fic_exec,"r").read().replace("/dev/cciss/c0d0p","/dev/sda")
      file(fic_exec,"w").write(result)

  def is_created_cfg(self, vm):
    return os.path.isfile("/etc/xen/P2V/" + vm + "/" + vm + ".cfg")

  def is_created_lv(self, vm):
    is_lv = self.exec_cmd("ls  /dev/" + self.vgname + "/root-" + vm + " 2>/dev/null | wc -l")
    return is_lv[0].strip()

  def is_finish_p2v(self, vm):
    self.import_all_variables(vm)
    if os.path.isfile("/etc/xen/P2V/" + vm + "/" + vm + ".finish"):
      return "true"
    else: 
      return "false"
Exemple #15
0
class physical_host:

  def __init__(self,server):
    self.server = server
    self.ssh = Ssh(self.server)

  def exec_cmd_ssh(self,cmd=''):
    result = self.ssh.exec_cmd(cmd)
    return result
  
  def get_memory(self):
    liste = self.exec_cmd_ssh('dmidecode -t memory | grep Size | grep MB')
    somme=0
    for i in liste:
      val = i.split(":")[1].strip().split()[0]
      somme = int(somme) + int(val)
    return somme

  def get_memory_swap(self):
    liste = self.exec_cmd_ssh('free -b | grep -i swap ')
    memory_swap = liste[0].split()[1]
    return memory_swap
  
  def get_memory1(self):
    liste = self.exec_cmd_ssh('free -m | grep -i Mem ')
    memory_swap = liste[0].split()[1]
    return memory_swap

  def get_mac_addr(self,interface):
    cmd = "ifconfig %s | grep HWaddr" % interface
    liste = self.exec_cmd_ssh(cmd)
    for i in liste:
      mac_addr = i.split()[4]
    return mac_addr

  def get_interfaces(self):
    liste = self.exec_cmd_ssh("cat /proc/net/dev | sed '1,2'd | grep eth")
    INTERFACE={}
    for i in liste:
      NOM_INTERFACE = i.split(":")[0].strip()
      MAC_ADDR = self.get_mac_addr(NOM_INTERFACE)
      INTERFACE[NOM_INTERFACE] = MAC_ADDR
    return INTERFACE

  def get_cpu(self):
    line = self.exec_cmd_ssh('cat /proc/cpuinfo | grep processor | wc -l')
    nb_cpu = line[0].strip()
    return nb_cpu

  def is_livecd(self):
    liste = self.exec_cmd_ssh('cat /etc/issue | grep -i slax | wc -l')
    if int(liste[0].strip()) >= 1:
	  return "true"
    else:
	  return "false"

  def get_idev(self):
    version = self.get_version_os()
    if version["OS"] == "CentOS":
      self.idev="xvda"
    if version["OS"] == "Ubuntu":
      self.idev="xvda"
    if version["OS"] == "Debian":
      self.idev="hda"
    return self.idev

  def copy_fstab(self):
    copy_file_fstab = self.exec_cmd_ssh('cp /etc/fstab /etc/fstab_without_uuid')

  def cible_link_uuid(self,uuid):
    nom_device = self.exec_cmd_ssh('readlink -f /dev/disk/by-uuid/%s' % uuid)
    return nom_device

  def get_fstab_without_uuid(self):
    self.copy_fstab()
    liste = self.exec_cmd_ssh('cat /etc/fstab | grep ^UUID')
    for i in liste:
      uuid = i.split()[0].split('=')[1]
      cible = self.cible_link_uuid(uuid)
      self.exec_cmd_ssh('sed -i s#UUID=%s#%s# /etc/fstab_without_uuid' % (uuid,cible[0].strip()))

  def get_partitions_para(self):
    self.get_idev()
    self.get_fstab_without_uuid()
    liste = self.exec_cmd_ssh('cat /etc/fstab_without_uuid | grep ^/dev | grep -v iso9660 | grep -v floppy | grep -v vfat | grep -v cdrom')
    PARTITIONS={}
    cpt=1
    for i in liste:
      nom_device = i.split()[0].strip()
      fs = i.split()[2].strip()
      nom_part = i.split()[1].strip()
      taille = self.taille_part(nom_device,fs)
      if fs != "swap": 
        if cpt == 4:
          cpt=5
        nom_device_para="%s%s" % (self.idev,cpt)
        PARTITIONS[nom_device_para] = (nom_device,fs,taille,nom_part)
        cpt=(cpt + 1)
    for i in liste:
      nom_device = i.split()[0].strip()
      fs = i.split()[2].strip()
      if fs == "swap":
        nom_device_para="%s%s" % (self.idev,cpt)
        taille_swap = self.get_memory_swap()
        PARTITIONS[nom_device_para] = (nom_device,fs,taille_swap,nom_part)
    return PARTITIONS

  def taille_part(self,partition,filesystem):
    if (filesystem == "ext2") or (filesystem == "ext3") or (filesystem == "ext4"):
      return self.taille_part_ext(partition)
  
  def taux_occupation(self,partition,filesystem):
    if (filesystem == "ext2") or (filesystem == "ext3") or (filesystem == "ext4"):
      return self.taux_occupation_ext(partition)


  ######################################################################################
  ###################  FONCTIONS RESERVER POUR FILESYSTEM EXT   ########################
  ######################################################################################
  def taille_part_ext(self,partition):
    Bl_count = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Block count"')
    Bl_size = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Block size"')
    Bloc_count = Bl_count[0].split(":")[1].strip()
    Bloc_size = Bl_size[0].split(":")[1].strip()
    Taille = (int(Bloc_count) * int(Bloc_size))
    return Taille
  
  def taille_part_free_ext(self,partition):
    Bl_free = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Free blocks"')
    Bl_size = self.exec_cmd_ssh('tune2fs -l '+ partition +' | grep "Block size"')
    Bloc_free = Bl_free[0].split(":")[1].strip()
    Bloc_size = Bl_size[0].split(":")[1].strip()
    Taille_free = (int(Bloc_free) * int(Bloc_size))
    return Taille_free

  def taux_occupation_ext(self,partition):
    taille_total = self.taille_part_ext(partition)
    taille_libre = self.taille_part_free_ext(partition)
    tx_occup = 100 - ((taille_libre * 100) / taille_total)
    return tx_occup


  def Convert_to_octects(self,taille):
    """ converti Ko, Mo, Go  et To en octets """
    



  def get_all_partitions(self):
    ALL_PARTITIONS={}
    ALL_PARTITIONS["PARA"] = (self.get_partitions_para())
    return ALL_PARTITIONS

  def detect_lvm(self):
    detect_lvm = "0"
    nb_lv = self.exec_cmd_ssh('LANG=POSIX fdisk -l 2> /dev/null| grep "^Disk /dev" | grep mapper | wc -l')
    if  nb_lv[0].strip() >= 1:
      detect_lvm = "1"
    if detect_lvm == "1": 
      print("LVM detecte, en mode HVM, le LVM sera fait dans la VM.")

  def detect_lvdisplay(self):
    detect = self.exec_cmd_ssh('which lvdisplay | wc -l')
    return detect[0]

 
  def is_lv(self,fs):
    if self.detect_lvdisplay() != 0:
      check_is_lv = self.exec_cmd_ssh('lvdisplay | grep \"%s\" | wc -l' % fs)
      if check_is_lv[0] >= 1:
        return 1
      else:
        return 0
    else:
      return 0
 
  def get_version_os(self):
    liste = self.exec_cmd_ssh('cat /etc/issue')
    os_version=[]
    if liste[0].split()[0] == "CentOS":
      os_version = {"OS":liste[0].split()[0],"VERSION":liste[0].split()[2]}
    if liste[0].split()[0] == "Ubuntu":
      os_version = {"OS":liste[0].split()[0],"VERSION":liste[0].split()[1]}
    if liste[0].split()[0] == "Debian":
      os_version = {"OS":liste[0].split()[0],"VERSION":liste[0].split()[2]}
    return os_version

  def get_eligibility_check_fstab(self):
    CHECK_LABEL = self.exec_cmd_ssh('less /etc/fstab | grep ^LABEL= | wc -l')
    CHECK_LABEL = CHECK_LABEL[0].strip()
    if CHECK_LABEL == "0":
      ret = 1
    else:
      ret = 0
    return ret

  def get_eligibility_check_fs_ext(self):
    CHECK_FS_EXT = self.exec_cmd_ssh('df -x tmpfs -x swap -x proc -x sys -x ext2 -x ext3 -x ext4 2>/dev/null | wc -l')
    CHECK_FS_EXT = CHECK_FS_EXT[0].strip()
    if CHECK_FS_EXT == "0":
      ret = 1
    else:
      ret = 0
    return ret

  def get_eligibility_check_network_file_p2v(self):
    CHECK_NETWORK_FILE_P2V = self.exec_cmd_ssh('ls /etc/network/interfaces.pre.p2v 2>/dev/null | wc -l')
    CHECK_NETWORK_FILE_P2V = CHECK_NETWORK_FILE_P2V[0].strip()
    if CHECK_NETWORK_FILE_P2V == "1":
      ret = 1
    else:
      ret = 0
    return ret
Exemple #16
0
 def __init__(self,server):
   self.server = server
   self.ssh = Ssh(self.server)
Exemple #17
0
 def __init__(self,server):
   self.server = server
   self.ssh = Ssh(self.server)
Exemple #18
0
 def add_lock_dhcpd(self,ip_pxe,vm_name):
   ssh_pxe = Ssh(ip_pxe)
   ssh_pxe.exec_cmd("pxe_AddLockDhcpd %s" % vm_name)
Exemple #19
0
class xen_host:
    def __init__(self, ip_srv_phy="", xenmgtconf="", ds="", vg_name=""):
        xenmgtconf = {}
        execfile("/etc/xen/xenmgt-p2v.conf", xenmgtconf)
        self.xenmgtconf = xenmgtconf
        self.bs = self.xenmgtconf["DD_BS"]
        self.P = physical_host(ip_srv_phy)
        self.ip_srv_phy = ip_srv_phy
        self.ssh = Ssh(self.ip_srv_phy)
        self.type_p2v = "PARA"
        self.type_vm = "P2V"
        self.sysadmin = ds
        self.vgname = vg_name

    def exec_cmd(self, cmd=''):
        CMD = os.popen(cmd, "r")
        ret = CMD.readlines()
        return ret

    def get_os_version(self):
        self.version_os = self.P.get_version_os()
        return self.version_os

    def check_vgname(self):
        liste = self.exec_cmd('vgdisplay | grep Name | grep %s | wc -l' %
                              self.vgname)
        vg = liste[0].strip()
        if vg == "1":
            return self.vgname
        else:
            print "!!!ERROR !!! Le VG %s n'existe pas, utiliser l'option --vg=<VG_NAME> par defaut c'est LVM_XEN" % self.vgname
            sys.exit()

    def get_name_vm_dest(self):
        self.ssh.del_keyfile()
        self.ssh.copy_id()
        name_vm_dest = self.ssh.exec_cmd("hostname")
        self.name_vm_dest = name_vm_dest[0].strip()
        self.new_name_vm_ip = self.ip_srv_phy
        return self.name_vm_dest

    def get_interfaces(self):
        self.interfaces = self.P.get_interfaces()
        return self.interfaces

    def get_memory(self):
        self.memory = self.P.get_memory1()
        return self.memory

    def get_partitions(self):
        self.partitions = self.P.get_all_partitions()
        return self.partitions

    def get_cpu(self):
        self.cpu = self.P.get_cpu()
        return self.cpu

    def get_info_srv_physique(self):
        self.get_os_version()
        self.get_memory()
        self.get_interfaces()
        self.get_partitions()
        self.get_cpu()

    def is_livecd(self):
        self.is_livecd = self.P.is_livecd()
        return self.is_livecd

    #######################################################
    ###               ELIGIBILITY                       ###
    #######################################################

    def get_size_vg(self):
        TOTAL = self.exec_cmd(
            "vgs --units B | grep %s | awk '{print $6}' | sed 's/B//'" %
            self.vgname)[0].strip()
        FREE = self.exec_cmd(
            "vgs --units B | grep %s | awk '{print $7}' | sed 's/B//'" %
            self.vgname)[0].strip()
        vgsize = {"TOTAL": TOTAL, "FREE": FREE}
        return vgsize

    def get_eligibility_check_vgsize(self):
        TailleTotalPart = 0
        self.get_partitions()
        for i in self.tri(self.partitions[self.type_p2v]):
            TailleTotalPart = (TailleTotalPart +
                               int(self.partitions[self.type_p2v][i][2]))
        VGSIZE = self.get_size_vg()
        NEW_VGSIZE = (int(VGSIZE["FREE"]) - (
            (self.xenmgtconf["VG_PERCENT_EMERGENCY"] * int(VGSIZE["FREE"])) /
            100))
        if TailleTotalPart < NEW_VGSIZE:
            ret = 1
        else:
            ret = 0
        return ret

    def get_eligibility(self):
        self.eligibility_check_fstab = self.P.get_eligibility_check_fstab()
        self.eligibility_check_fs_ext = self.P.get_eligibility_check_fs_ext()
        self.eligibility_check_network_file_p2v = self.P.get_eligibility_check_network_file_p2v(
        )
        self.eligibility_check_vgsize = self.get_eligibility_check_vgsize()

    def rapport_eligibility_header(self):
        RAPPORT_HEADER = "\n##########################################\n"
        RAPPORT_HEADER += "######## RAPPORT D'ELIGIBILITE ###########\n"
        RAPPORT_HEADER += "##########################################"
        print RAPPORT_HEADER

    def rapport_eligibility_fstab(self):
        RAPPORT_FSTAB = "\n*\n"
        if self.eligibility_check_fstab == 1:
            RAPPORT_FSTAB += "* Check fstab : OK\n"
        else:
            RAPPORT_FSTAB += "* Check fstab : NOK\n"
            RAPPORT_FSTAB += "* !!! Le fichier /etc/fstab contient un ou plusieurs LABEL\n"
            RAPPORT_FSTAB += "* Veuillez remplacer les entrees LABEL par les devices correspondant\n"
        RAPPORT_FSTAB += "*\n"
        print RAPPORT_FSTAB

    def rapport_eligibility_fs_ext(self):
        RAPPORT_FSEXT = "*\n"
        if self.eligibility_check_fs_ext == 1:
            RAPPORT_FSEXT += "* Check FileSystem : OK\n"
        else:
            RAPPORT_FSEXT += "* Check FileSystem : NOK\n"
            RAPPORT_FSEXT += "* !!! Une ou plusieurs partitions contienent un FileSystem different de ext2,3,4\n"
        RAPPORT_FSEXT += "*\n"
        print RAPPORT_FSEXT

    def rapport_eligibility_network(self):
        RAPPORT_NETWORK = "*\n"
        if self.eligibility_check_network_file_p2v == 1:
            RAPPORT_NETWORK += "* Check Network : OK\n"
        else:
            RAPPORT_NETWORK += "* Check Network : NOK\n"
            RAPPORT_NETWORK += "* !!! Copiez votre fichier '/etc/network/interfaces' en '/etc/network/interfaces.pre.p2v' en supprimant les vlans\n"
        RAPPORT_NETWORK += "*\n"
        print RAPPORT_NETWORK

    def rapport_eligibility_vgsize(self):
        RAPPORT_VGSIZE = "*\n"
        if self.eligibility_check_vgsize == 1:
            RAPPORT_VGSIZE += "* Check taille dispo sur le VG : OK\n"
        else:
            RAPPORT_VGSIZE += "* Check taille dispo sur le VG : NOK\n"
            RAPPORT_VGSIZE += "* !!! Il n y a pas assez de place sur le VG\n"
        RAPPORT_VGSIZE += "*\n"
        print RAPPORT_VGSIZE

    def rapport_eligibility_result(self):
        somme = (self.eligibility_check_fstab + self.eligibility_check_fs_ext +
                 self.eligibility_check_network_file_p2v +
                 self.eligibility_check_vgsize)
        if somme == 4:
            print "* le Serveur est eligible\n"
        else:
            print "* !!! Le serveur n'est pas eligible !!!\n"

    def rapport_eligibility(self):
        self.rapport_eligibility_header()
        self.rapport_eligibility_fstab()
        self.rapport_eligibility_fs_ext()
        self.rapport_eligibility_network()
        self.rapport_eligibility_vgsize()
        self.rapport_eligibility_result()

    #######################################################
    ###              FIN   ELIGIBILITY                  ###
    #######################################################

    #######################################################
    ### DEBUT PREPARATION POUR LA GENERATION DU RAPPORT ###
    #######################################################

    def prep_affiche_os_version(self):
        AFFICHE_OS_VERSION = "OS : %s %s\n\n" % (self.version_os["OS"],
                                                 self.version_os["VERSION"])
        return AFFICHE_OS_VERSION

    def prep_affiche_vm_name(self):
        AFFICHE_VM_NAME = "VM : %s\n" % self.name_vm_dest
        AFFICHE_VM_NAME += "IP LiveCD : %s\n\n" % self.new_name_vm_ip
        return AFFICHE_VM_NAME

    def prep_affiche_memory(self):
        AFFICHE_MEMORY = "MEMORY : %s Mo\n" % self.memory
        return AFFICHE_MEMORY

    def prep_affiche_cpu(self):
        AFFICHE_CPU = "CPU : %s \n\n" % self.cpu
        return AFFICHE_CPU

    def prep_affiche_network(self):
        AFFICHE_NETWORK = "NETWORK :   \n"
        for i in self.tri(self.interfaces):
            AFFICHE_NETWORK += " %s -> Mac address : %s\n" % (
                i, self.interfaces[i])
        AFFICHE_NETWORK += "\n"
        return AFFICHE_NETWORK

    def prep_affiche_partitions(self):
        AFFICHE_PARTITIONS = "PARTITIONS :\n"
        for i in self.tri(self.partitions[self.type_p2v]):
            AFFICHE_PARTITIONS += " %s %s %s %s\n" % (
                self.partitions[self.type_p2v][i][0],
                self.partitions[self.type_p2v][i][1],
                self.partitions[self.type_p2v][i][2],
                self.partitions[self.type_p2v][i][3])
        return AFFICHE_PARTITIONS

    def prep_cmd_create_partitions(self):
        AFFICHE_CREATE_LV = "\n"
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.partitions[self.type_p2v][i][3] == "/":
                nom_part = "root"
            elif self.partitions[self.type_p2v][i][1] == "swap":
                nom_part = "swap"
            else:
                nom_part = self.partitions[self.type_p2v][i][3].replace(
                    "/", "-")[1:]
            AFFICHE_CREATE_LV += "lvcreate -L %sB -n %s-%s %s\n" % (
                self.partitions[self.type_p2v][i][2], nom_part,
                self.name_vm_dest, self.vgname)
        return AFFICHE_CREATE_LV

    def prep_cmd_copy_dd(self):
        AFFICHE_DD = "\n"
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.partitions[self.type_p2v][i][3] == "/":
                nom_part = "root"
            elif self.partitions[self.type_p2v][i][1] == "swap":
                nom_part = "swap"
            else:
                nom_part = self.partitions[self.type_p2v][i][3].replace(
                    "/", "-")[1:]
            AFFICHE_DD += "echo \"Copie de la partition " + self.partitions[
                self.type_p2v][i][3] + "\"\n"
            if self.partitions[self.type_p2v][i][3] == "swap":
                AFFICHE_DD += "mkswap -v1 /dev/" + self.vgname + "/" + nom_part + "-" + self.name_vm_dest + "\n"
            else:
                ############# DETECTION POUR REDUCTION LV ET RESIZE FS ##################
                #if self.P.is_lv(self.partitions[self.type_p2v][i][0]):
                #  if self.partitions[self.type_p2v][i][2] > 15106127360:
                #    if self.P.taux_occupation(self.partitions[self.type_p2v][i][0],self.partitions[self.type_p2v][i][1]) < 20:
                #      print "Elu pour la reduction"
                #      print "%s is LVM LV" % self.partitions[self.type_p2v][i][0]
                #      print self.P.taux_occupation(self.partitions[self.type_p2v][i][0],self.partitions[self.type_p2v][i][1])
                ############# FIN DETECTION POUR REDUCTION LV ET RESIZE FS ##################
                AFFICHE_DD += "ssh -c arcfour root@" + self.new_name_vm_ip + " 'dcfldd status=on sizeprobe=if statusinterval=100 if=" + self.partitions[
                    self.type_p2v][i][
                        0] + "' bs=" + self.bs + " | dd of=/dev/" + self.vgname + "/" + nom_part + "-" + self.name_vm_dest + " bs=" + self.bs + "\n"
        return AFFICHE_DD

    def affiche_rapport(self):
        affiche = "---------- RAPPORT ----------\n"
        affiche += self.prep_affiche_os_version()
        affiche += self.prep_affiche_vm_name()
        affiche += self.prep_affiche_memory()
        affiche += self.prep_affiche_cpu()
        affiche += self.prep_affiche_network()
        affiche += self.prep_affiche_partitions()
        self.affiche_rapport = affiche
        return affiche

    #####################################################
    ### FIN PREPARATION POUR LA GENERATION DU RAPPORT ###
    #####################################################

    def get_exec_cmd(self):
        ecrit_cmd = ""
        ecrit_cmd += self.prep_cmd_create_partitions()
        ecrit_cmd += self.prep_cmd_copy_dd()
        return ecrit_cmd

    #############################################################
    ### DEBUT GENERATION DU FICHIER DE CONF XEN (HVM OU PARA) ###
    #############################################################

    def generate_conf_xen(self):
        GEN_CONF = self.generate_conf_xen_para()
        return GEN_CONF

    def ecrit_conf_partitions(self):
        conf = "["
        count = len(self.partitions[self.type_p2v].keys())
        cpt = 1
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.partitions[self.type_p2v][i][3] == "/":
                nom_part = "root"
            elif self.partitions[self.type_p2v][i][1] == "swap":
                nom_part = "swap"
            else:
                nom_part = self.partitions[self.type_p2v][i][3].replace(
                    "/", "-")[1:]
            conf += "'phy:/dev/%s/%s-%s,%s,w'" % (self.vgname, nom_part,
                                                  self.name_vm_dest, i)
            if cpt != int(count):
                conf += ",\n\t"
            cpt = (int(cpt) + 1)
        conf += "]"
        return conf

    def ecrit_conf_interfaces(self):
        conf = "["
        count = len(self.interfaces.keys())
        cpt = 1
        for i in self.tri(self.interfaces):
            if self.version_os["VERSION"] == "3.1":
                conf += "'mac=%s , bridge=xenbr2003'" % self.interfaces[i]
            else:
                conf += "'bridge=xenbr2003'"
            if cpt != int(count):
                conf += ","
            cpt = (int(cpt) + 1)
        conf += "]"
        return conf

    def ecrit_memory(self):
        return self.memory

    def ecrit_maxmem(self):
        if self.memory < 4096:
            maxmem = 6144
        elif self.memory > 4096:
            maxmem = ((int(self.memory) / 2) + 8192)
        return maxmem

    def ecrit_vcpus(self):
        vcpus = "12"
        return vcpus

    def ecrit_vcpu_avail(self):
        if self.cpu == "1":
            vcpu_avail = "3"
        elif self.cpu == "2":
            vcpu_avail = "3"
        elif self.cpu == "3":
            vcpu_avail = "7"
        elif self.cpu == "4":
            vcpu_avail = "15"
        elif self.cpu == "5":
            vcpu_avail = "31"
        elif self.cpu == "6":
            vcpu_avail = "63"
        elif self.cpu == "7":
            vcpu_avail = "127"
        elif self.cpu == "8":
            vcpu_avail = "255"
        return vcpu_avail

    def ecrit_name_vm_dest(self):
        return self.name_vm_dest

    def ecrit_num_sysadmin(self):
        return self.sysadmin

    def ecrit_type_vm(self):
        return self.type_vm

    def ecrit_extra(self):
        extra = "console=xvc0 elevator=noop"
        return extra

    def ecrit_root_kernel(self):
        root = ""
        for i in self.tri(self.partitions[self.type_p2v]):
            if self.partitions[self.type_p2v][i][3] == "/":
                root = i
        root_kernel = "/dev/%s ro" % root
        return root_kernel

    def get_kernel(self):
        self.kernel_vm = []
        if self.version_os["OS"] == "Ubuntu":
            kernel = self.xenmgtconf["KERNEL_UBUNTU"]
            initrd = self.xenmgtconf["INITRD_UBUNTU"]
        elif self.version_os["OS"] == "Debian":
            kernel = self.xenmgtconf["KERNEL_DEBIAN"]
            initrd = self.xenmgtconf["INITRD_DEBIAN"]
        else:
            kernel = self.xenmgtconf["KERNEL_CENTOS"]
            initrd = self.xenmgtconf["INITRD_CENTOS"]
        self.kernel_vm = [kernel, initrd]
        return self.kernel_vm

    def generate_conf_xen_para(self):
        self.get_kernel()
        CONF_PARA = ""
        CONF_PARA += "kernel = \"" + self.kernel_vm[0] + "\"\n"
        CONF_PARA += "ramdisk = \"" + self.kernel_vm[1] + "\"\n"
        CONF_PARA += "memory = " + str(self.ecrit_memory()) + "\n"
        CONF_PARA += "maxmem = " + str(self.ecrit_maxmem()) + "\n\n"
        CONF_PARA += "vcpus = " + self.ecrit_vcpus() + "\n"
        CONF_PARA += "vcpu_avail = " + self.ecrit_vcpu_avail() + "\n\n"
        CONF_PARA += "name = \"" + self.ecrit_name_vm_dest() + "\"\n\n"
        CONF_PARA += "vif = " + self.ecrit_conf_interfaces() + "\n"
        CONF_PARA += "disk = " + self.ecrit_conf_partitions() + "\n"
        CONF_PARA += "root = \"" + self.ecrit_root_kernel() + "\"\n"
        CONF_PARA += "extra = \"" + self.ecrit_extra() + "\"\n\n"
        CONF_PARA += "#SYSADMIN=\"" + self.ecrit_num_sysadmin() + "\"\n"
        CONF_PARA += "#VMTYPE=\"" + self.ecrit_type_vm() + "\"\n"
        return CONF_PARA

    ###########################################################
    ### FIN GENERATION DU FICHIER DE CONF XEN (HVM OU PARA) ###
    ###########################################################

    def create_rep_p2v(self):
        if not os.path.isdir(self.rep_p2v):
            self.exec_cmd("mkdir -p %s" % self.rep_p2v)

    def check_rep_p2v(self):
        self.rep_p2v = "/etc/xen/P2V/" + self.name_vm_dest + ""
        self.create_rep_p2v()

    def set_fichier_p2v(self, ext="", contenu=""):
        fichier = "%s/%s.%s" % (self.rep_p2v, self.name_vm_dest, ext)
        fd = open(fichier, "w")
        fd.write(contenu)
        fd.close()

    def generation_fichier_p2v(self):
        self.check_rep_p2v()
        self.set_fichier_p2v("sh", self.get_exec_cmd())
        self.set_fichier_p2v("cfg", self.generate_conf_xen())
        self.set_fichier_p2v("var", self.export_variables())
        self.set_fichier_p2v("rapport", self.affiche_rapport)

    def endphase(self):
        AFFICHE = "\n\n"
        AFFICHE += "*****************************************************************\n"
        AFFICHE += "FIN de L'ETAPE 1/2\n\n"
        AFFICHE += "Pour passer a la 2eme Etape, il faut inserer\n"
        AFFICHE += "le LiveCD SLAX, rebooter, puis configurer le reseau de celui-ci\n\n"
        AFFICHE += "Commande pour activer le reseau :\n"
        AFFICHE += "# vconfig add eth0 <NUM_VLAN>\n"
        AFFICHE += "# ip addr add %s/<MASK> dev eth0.<NUM_VLAN>\n" % self.new_name_vm_ip
        AFFICHE += "*****************************************************************\n"
        return AFFICHE

    ##############################
    ###   DEBUT POST INSTALL   ###
    ##############################

    def export_variables(self):
        export_variable = ""
        export_variable += "partitions=%s\n" % self.partitions
        export_variable += "vgname=\"%s\"\n" % self.vgname
        export_variable += "type_p2v=\"%s\"\n" % self.type_p2v
        export_variable += "name_vm_dest=\"%s\"\n" % self.name_vm_dest
        export_variable += "version_os=%s\n" % self.version_os
        return export_variable

    def import_all_variables(self, VM):
        new_val = {}
        execfile("/etc/xen/P2V/" + VM + "/" + VM + ".var", new_val)
        self.new_variables = new_val
        for i in self.new_variables.keys():
            if i != "__builtins__":
                globals()[i] = self.new_variables[i]

    def mkdir_rep_vhosts_vm(self):
        """ Creation du repertoire /vhosts/vm
    """
        print "Creation du repertoire /vhosts/%s" % name_vm_dest
        self.rep_vhosts_vm = "/vhosts/" + name_vm_dest + ""
        self.exec_cmd("mkdir -p %s" % self.rep_vhosts_vm)

    def modif_fstab(self):
        """ Genere le nouveau fichier fstab
    """
        print "preparation du fichier fstab"
        self.exec_cmd("cp %s/etc/fstab %s/etc/fstab.pre.p2v" %
                      (self.rep_vhosts_vm, self.rep_vhosts_vm))
        self.exec_cmd("cp %s/etc/fstab_without_uuid %s/etc/fstab" %
                      (self.rep_vhosts_vm, self.rep_vhosts_vm))
        line = open("/vhosts/" + name_vm_dest + "/etc/fstab_without_uuid",
                    "r").read()
        for i in self.tri(partitions[type_p2v]):
            line = line.replace(partitions[type_p2v][i][0], "/dev/%s" % i, 1)
        fichier = open("/vhosts/" + name_vm_dest + "/etc/fstab", "w")
        fichier.write(line)
        fichier.close()

    def modif_devpts(self):
        if version_os["VERSION"] == "3.1":
            self.exec_cmd(
                "if [ $(grep \"/bin/mkdir -p /dev/pts\" %s/etc/init.d/mountvirtfs | wc -l) -eq 0 ] ; then sed -i '/domount sysfs \"\" \/sys/a \/bin\/mkdir -p \/dev\/pts' %s/etc/init.d/mountvirtfs ; else echo \"\" ; fi"
                % (self.rep_vhosts_vm, self.rep_vhosts_vm))
            self.exec_cmd(
                "echo \"none\t /dev/pts\t devpts\t gid=5,mode=620\t 0\t 0\" >> %s/etc/fstab"
                % self.rep_vhosts_vm)

    def modif_network(self):
        """ Genere le nouveau fichier network (En cours)
    """
        print "preparation du fichier network interfaces"
        if version_os["OS"] == "CentOS":
            self.exec_cmd(
                "cp %s/etc/sysconfig/network_scripts/ifcfg-eth0 %s/etc/sysconfig/network_scripts/ifcfg-eth0.pre.p2v"
                % (self.rep_vhosts_vm, self.rep_vhosts_vm))
        else:
            self.exec_cmd(
                "cp %s/etc/network/interfaces %s/etc/network/interfaces.post.p2v"
                % (self.rep_vhosts_vm, self.rep_vhosts_vm))
            self.exec_cmd(
                "cp %s/etc/network/interfaces.pre.p2v %s/etc/network/interfaces"
                % (self.rep_vhosts_vm, self.rep_vhosts_vm))

    def copie_modules(self):
        """ copie du module 2.6.37 ou 2.6.18.149
    """
        print "copie du module necessaire"
        if version_os["OS"] == "Ubuntu":
            self.exec_cmd(
                "cp -rpdf /lib/modules/%s %s/lib/modules/" %
                (self.xenmgtconf["KERNEL_UBUNTU"].split("/boot/vmlinuz-")[1],
                 self.rep_vhosts_vm))
        if version_os["OS"] == "Debian":
            self.exec_cmd(
                "cp -rpdf /lib/modules/%s %s/lib/modules/" %
                (self.xenmgtconf["KERNEL_DEBIAN"].split("/boot/vmlinuz-")[1],
                 self.rep_vhosts_vm))
        if version_os["OS"] == "CentOS":
            self.exec_cmd(
                "cp -rpdf /lib/modules/%s %s/lib/modules/" %
                (self.xenmgtconf["KERNEL_CENTOS"].split("/boot/vmlinuz-")[1],
                 self.rep_vhosts_vm))

    def set_ntp_sysctl(self):
        """ Modifie le sysctl pour la correction du ntp
    """
        print "Modification du sysctl"
        self.exec_cmd(
            "echo \"xen.independent_wallclock = 1\" >> %s/etc/sysctl.conf" %
            self.rep_vhosts_vm)

    def mount_root_vm(self):
        """ Monte la partition root de la VM afin d'effectuer la post_install
    """
        print "montage de la partition root de %s" % name_vm_dest
        self.exec_cmd("mount /dev/%s/root-%s %s" %
                      (vgname, name_vm_dest, self.rep_vhosts_vm))

    def umount_root_vm(self):
        """ Monte la partition root de la VM afin d'effectuer la post_install
    """
        print "demontage de la partition root de %s" % name_vm_dest
        self.exec_cmd("umount %s" % self.rep_vhosts_vm)

    def set_console_xen(self):
        """ Configuration de la console xen pour la VM
    """
        print ""
        self.exec_cmd("echo \"xvc0\" >> %s/etc/securetty" % self.rep_vhosts_vm)
        if os.path.isfile("%s/etc/inittab" % self.rep_vhosts_vm):
            self.exec_cmd(
                "echo \"7:2345:respawn:/sbin/getty 38400 xvc0\" >> %s/etc/inittab"
                % self.rep_vhosts_vm)

        if os.path.isfile("%s/etc/event.d/tty1" % self.rep_vhosts_vm):
            self.exec_cmd("cp %s/etc/event.d/tty1 %s/etc/event.d/xvc0" %
                          (self.rep_vhosts_vm, self.rep_vhosts_vm))
            self.exec_cmd("sed -i \"s@tty1@xvc0@\" %s/etc/event.d/xvc0" %
                          self.rep_vhosts_vm)

        if os.path.isfile("%s/etc/init/tty1.conf" % self.rep_vhosts_vm):
            self.exec_cmd("cp %s/etc/init/tty1.conf %s/etc/init/xvc0.conf" %
                          (self.rep_vhosts_vm, self.rep_vhosts_vm))
            self.exec_cmd("sed -i \"s@tty1@xvc0@\" %s/etc/init/xvc0.conf" %
                          self.rep_vhosts_vm)

    def set_modprobe(self):
        """ active le modules xennet pour les interfaces reseaux
    """
        if version_os["OS"] == "Debian":
            self.exec_cmd(
                "echo \"alias eth0 xennet\" >> %s/etc/modprobe.d/aliases" %
                self.rep_vhosts_vm)
        else:
            self.exec_cmd(
                "echo \"alias eth0 xennet\" >> %s/etc/modprobe.d/aliases.conf"
                % self.rep_vhosts_vm)

    def copy_conf_to_xen(self):
        shutil.copy(
            "/etc/xen/P2V/" + name_vm_dest + "/" + name_vm_dest + ".cfg",
            "/etc/xen/vm/" + name_vm_dest + "")
        date_generate_p2v = time.strftime("%d/%m/%y %H:%M", time.localtime())
        self.exec_cmd("echo \"### P2V genere a %s \" >> /etc/xen/vm/%s" %
                      (date_generate_p2v, name_vm_dest))

    def post_install(self):
        self.copy_conf_to_xen()
        self.mkdir_rep_vhosts_vm()
        self.mount_root_vm()
        self.copie_modules()
        self.modif_fstab()
        self.modif_network()
        self.set_ntp_sysctl()
        self.set_console_xen()
        self.set_modprobe()
        self.modif_devpts()
        self.umount_root_vm()
        self.auto_vm()

    ############################
    ###   FIN POST INSTALL   ###
    ############################

    def tri(self, dico):
        """ Permet de tirer un dictionnaire
    """
        return sorted(dico.keys(), key=str)

    def copy_dd_bs(self, bs=''):
        self.bs = bs

    def check_vg_size(self, percent='10'):
        size_total = ()
        size_dispo = ()

    def auto_vm(self):
        self.exec_cmd("cd /etc/xen/auto ; ln -s /etc/xen/vm/" + name_vm_dest +
                      "")

    def exec_cmd_p2v(self):
        os.system("/bin/bash /etc/xen/P2V/" + name_vm_dest + "/" +
                  name_vm_dest + ".sh")

    def is_created_cfg(self, vm):
        return os.path.isfile("/etc/xen/P2V/" + vm + "/" + vm + ".cfg")

    def is_created_lv(self, vm):
        is_lv = self.exec_cmd("ls  /dev/" + self.vgname + "/root-" + vm +
                              " 2>/dev/null | wc -l")
        return is_lv[0].strip()
Exemple #20
0
class physical_host:
    def __init__(self, server):
        self.server = server
        self.ssh = Ssh(self.server)

    def exec_cmd_ssh(self, cmd=''):
        result = self.ssh.exec_cmd(cmd)
        return result

    def get_memory(self):
        liste = self.exec_cmd_ssh('dmidecode -t memory | grep Size | grep MB')
        somme = 0
        for i in liste:
            val = i.split(":")[1].strip().split()[0]
            somme = int(somme) + int(val)
        return somme

    def get_memory_swap(self):
        liste = self.exec_cmd_ssh('free -b | grep -i swap ')
        memory_swap = liste[0].split()[1]
        return memory_swap

    def get_memory1(self):
        liste = self.exec_cmd_ssh('free -m | grep -i Mem ')
        memory = float(liste[0].split()[1])
        memory_ceil = math.ceil(memory / 1024)
        memory = "%d" % (round(memory_ceil, 0) * 1024)
        return memory

    def get_mac_addr(self, interface):
        cmd = "ifconfig %s | grep HWaddr" % interface
        liste = self.exec_cmd_ssh(cmd)
        for i in liste:
            mac_addr = i.split()[4]
        return mac_addr

    def get_interfaces(self):
        liste = self.exec_cmd_ssh("cat /proc/net/dev | sed '1,2'd | grep eth")
        INTERFACE = {}
        for i in liste:
            NOM_INTERFACE = i.split(":")[0].strip()
            MAC_ADDR = self.get_mac_addr(NOM_INTERFACE)
            INTERFACE[NOM_INTERFACE] = MAC_ADDR
        return INTERFACE

    def get_cpu(self):
        line = self.exec_cmd_ssh('cat /proc/cpuinfo | grep processor | wc -l')
        nb_cpu = line[0].strip()
        return nb_cpu

    def is_livecd(self):
        #liste = self.exec_cmd_ssh('cat /etc/issue | grep -i slax | wc -l')
        liste = self.exec_cmd_ssh('hostname | grep -i sysresccd | wc -l')
        if int(liste[0].strip()) >= 1:
            return "True"
        else:
            liste = self.exec_cmd_ssh('cat /etc/issue | grep -i slax | wc -l')
            if int(liste[0].strip()) >= 1:
                return "True"
            else:
                return "False"

    def get_idev(self):
        version = self.get_version_os()
        if version["OS"] == "CentOS":
            self.idev = "xvda"
        if version["OS"] == "Ubuntu":
            self.idev = "xvda"
        if version["OS"] == "Debian":
            self.idev = "hda"
        return self.idev

    def copy_fstab(self):
        copy_file_fstab = self.exec_cmd_ssh(
            'cp /etc/fstab /etc/fstab_without_uuid')

    def cible_link_uuid(self, uuid):
        nom_device = self.exec_cmd_ssh('readlink -f /dev/disk/by-uuid/%s' %
                                       uuid)
        return nom_device

    def get_fstab_without_uuid(self):
        self.copy_fstab()
        liste = self.exec_cmd_ssh('cat /etc/fstab | grep ^UUID')
        for i in liste:
            uuid = i.split()[0].split('=')[1]
            cible = self.cible_link_uuid(uuid)
            self.exec_cmd_ssh('sed -i s#UUID=%s#%s# /etc/fstab_without_uuid' %
                              (uuid, cible[0].strip()))

    def get_partitions_para(self):
        self.get_idev()
        self.get_fstab_without_uuid()
        liste = self.exec_cmd_ssh(
            'cat /etc/fstab_without_uuid | grep ^/dev | grep -v iso9660 | grep -v floppy | grep -v vfat | grep -v cdrom'
        )
        PARTITIONS = {}
        cpt = 1
        for i in liste:
            nom_device = i.split()[0].strip()
            fs = i.split()[2].strip()
            nom_part = i.split()[1].strip()
            taille = self.taille_part(nom_device, fs)
            UUID = ""
            if fs != "swap":
                UUID = self.GetUuidByDisk(nom_device)
                if cpt == 4:
                    cpt = 5
                nom_device_para = "%s%s" % (self.idev, cpt)
                PARTITIONS[nom_device_para] = {
                    "DEVICE": nom_device,
                    "FS": fs,
                    "SIZE": taille,
                    "PARTITION": nom_part,
                    "UUID": UUID
                }
                cpt = (cpt + 1)
        for i in liste:
            nom_device = i.split()[0].strip()
            fs = i.split()[2].strip()
            if fs == "swap":
                nom_device_para = "%s%s" % (self.idev, cpt)
                taille_swap = self.get_memory_swap()
                PARTITIONS[nom_device_para] = {
                    "DEVICE": nom_device,
                    "FS": fs,
                    "SIZE": taille_swap,
                    "PARTITION": nom_part
                }
        return PARTITIONS

    def get_partitions_hvm(self):
        self.get_fstab_without_uuid()
        #self.detect_lvm()
        cpt = '`'
        #liste = self.exec_cmd_ssh('LANG=POSIX fdisk -l /dev/cciss/c0d0 2> /dev/null | grep "^Disk /dev" | grep -v "mapper" | sed "s/Disk//" | sed "s#/dev/##"')
        #liste = self.exec_cmd_ssh('LANG=POSIX fdisk -l /dev/sda 2> /dev/null | grep "^Disk /dev" | grep -v "mapper" | sed "s/Disk//" | sed "s#/dev/##"')
        liste = self.exec_cmd_ssh(
            'LANG=POSIX fdisk -l 2> /dev/null | grep "^Disk /dev" | grep -v "mapper" | sed "s/Disk//" | sed "s#/dev/##"'
        )
        PARTITION = {}
        for i in liste:
            nom_part = i.split(":")[0].strip()
            nom_part_hvm = "hd%s" % chr(ord(cpt) + 1)
            taille = i.split(",")[1].split()[0]
            PARTITION[nom_part] = {"DEVICE": nom_part_hvm, "SIZE": taille}
        return PARTITION
        # {'cciss/c0d0': ('hda', '120034123776'),'cciss/c0d1': ('hdb', '120034123776')}

    def taille_part(self, partition, filesystem):
        if (filesystem == "ext2") or (filesystem == "ext3") or (filesystem
                                                                == "ext4"):
            return self.taille_part_ext(partition)

    def taux_occupation(self, partition, filesystem):
        if (filesystem == "ext2") or (filesystem == "ext3") or (filesystem
                                                                == "ext4"):
            return self.taux_occupation_ext(partition)

    ######################################################################################
    ###################  FONCTIONS RESERVER POUR FILESYSTEM EXT   ########################
    ######################################################################################
    def taille_part_ext(self, partition):
        Bl_count = self.exec_cmd_ssh('tune2fs -l ' + partition +
                                     ' | grep "Block count"')
        Bl_size = self.exec_cmd_ssh('tune2fs -l ' + partition +
                                    ' | grep "Block size"')
        Bloc_count = Bl_count[0].split(":")[1].strip()
        Bloc_size = Bl_size[0].split(":")[1].strip()
        Taille = (int(Bloc_count) * int(Bloc_size))
        return Taille

    def taille_part_free_ext(self, partition):
        Bl_free = self.exec_cmd_ssh('tune2fs -l ' + partition +
                                    ' | grep "Free blocks"')
        Bl_size = self.exec_cmd_ssh('tune2fs -l ' + partition +
                                    ' | grep "Block size"')
        Bloc_free = Bl_free[0].split(":")[1].strip()
        Bloc_size = Bl_size[0].split(":")[1].strip()
        Taille_free = (int(Bloc_free) * int(Bloc_size))
        return Taille_free

    def taux_occupation_ext(self, partition):
        taille_total = self.taille_part_ext(partition)
        taille_libre = self.taille_part_free_ext(partition)
        tx_occup = 100 - ((taille_libre * 100) / taille_total)
        return tx_occup

    def Convert_to_octects(self, taille):
        """ converti Ko, Mo, Go  et To en octets """

    def GetUuidByDisk(self, partition):
        version = self.get_version_os()
        if version["VERSION"] == "3.1":
            UUID = ""
        else:
            UuidByDisk = self.exec_cmd_ssh('tune2fs -l ' + partition +
                                           ' | grep "Filesystem UUID"')
            UUID = UuidByDisk[0].split(":")[1].strip()
        return UUID

    def get_all_partitions(self):
        ALL_PARTITIONS = {}
        ALL_PARTITIONS["PARA"] = (self.get_partitions_para())
        ALL_PARTITIONS["HVM"] = (self.get_partitions_hvm())
        return ALL_PARTITIONS

    def detect_lvm(self):
        detect_lvm = "0"
        nb_lv = self.exec_cmd_ssh(
            'LANG=POSIX fdisk -l 2> /dev/null| grep "^Disk /dev" | grep mapper | wc -l'
        )
        if nb_lv[0].strip() >= 1:
            detect_lvm = "1"
        if detect_lvm == "1":
            print "LVM detecte, en mode HVM, le LVM sera fait dans la VM."

    def detect_lvdisplay(self):
        detect = self.exec_cmd_ssh('which lvdisplay | wc -l')
        return detect[0]

    def is_lv(self, fs):
        if self.detect_lvdisplay() != 0:
            check_is_lv = self.exec_cmd_ssh('lvdisplay | grep \"%s\" | wc -l' %
                                            fs)
            if check_is_lv[0] >= 1:
                return 1
            else:
                return 0
        else:
            return 0

    def get_version_os(self):
        liste = self.exec_cmd_ssh('cat /etc/issue')
        os_version = []
        if liste[0].split()[0] == "CentOS":
            os_version = {
                "OS": liste[0].split()[0],
                "VERSION": liste[0].split()[2]
            }
        if liste[0].split()[0] == "Ubuntu":
            os_version = {
                "OS": liste[0].split()[0],
                "VERSION": liste[0].split()[1]
            }
        if liste[0].split()[0] == "Debian":
            os_version = {
                "OS": liste[0].split()[0],
                "VERSION": liste[0].split()[2]
            }
        return os_version

    def get_eligibility_check_fstab(self):
        CHECK_LABEL = self.exec_cmd_ssh(
            'less /etc/fstab | grep ^LABEL= | wc -l')
        CHECK_LABEL = CHECK_LABEL[0].strip()
        if CHECK_LABEL == "0":
            ret = 1
        else:
            ret = 0
        return ret

    def get_eligibility_check_fs_ext(self):
        CHECK_FS_EXT = self.exec_cmd_ssh(
            'df -PlT | egrep -v "Type|rootfs|none|udev|tmpfs|ext[234]" | wc -l'
        )
        CHECK_FS_EXT = CHECK_FS_EXT[0].strip()
        if CHECK_FS_EXT == "0":
            ret = 1
        else:
            ret = 0
        return ret