示例#1
0
文件: vm.py 项目: dguglielmi/vmc-core
 def install(self):
     if re.search("^centos4", self.template_name):
         d = centos4(self)
     elif re.search("^centos5", self.template_name):
         self.os = centos5(self)
         self.os.install()
     elif re.search("^centos6", self.template_name):
         self.os = centos6(self)
         self.os.install()
     elif re.search("^centos7", self.template_name):
         self.os = centos7(self)
         self.os.install()
     elif re.search("^rhel6", self.template_name):
         d = rhel6(self)
     elif re.search("^debian5", self.template_name):
         self.os = debian5(self)
         self.os.install()
     elif re.search("^debian6", self.template_name):
         self.os = debian6(self)
         self.os.install()
     elif re.search("^debian7", self.template_name):
         self.os = debian7(self)
         self.os.install()
     elif re.search("^debian8", self.template_name):
         self.os = debian8(self)
         self.os.install()
     elif self.template_name == "dummy":
         self.os = dummy(self)
         self.os.install()
         pinfo("Install dummy template (only create disk and VM definition file)")
     elif re.search("^ubuntu1604", self.template_name):
         self.os = ubuntu1604(self)
         self.os.install()
     else:
         die(self.template_name + " is unsupported")
示例#2
0
文件: vm.py 项目: dguglielmi/vmc-core
    def create_disk(self):
        volume_size = self.root_size + self.swap_size + self.data_size
        if volume_size < 1:
            die("Volume size is too small")

        if self.do_lvm:
            pinfo("Create virtual machine logical volume")

            lvcreate_cmd = ["lvcreate", "-y", self.lvm_vg, "-L", str(volume_size) + "G", "-n", self.lvm_lv ]
            if not self.lvm_physical_disk == None:
                lvcreate_cmd.append(self.lvm_physical_disk)

            if vmc.utils.lvm.locking_type() == 4:
                vmc.utils.lvm.unlock()

                subprocess.call(lvcreate_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

                vmc.utils.lvm.lock(4)
                vmc.utils.lvm.lv_activate(self.disk_path, self.cluster_nodes)
            # CLVM locking or single host
            else:
                subprocess.call(lvcreate_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        else:
            pinfo("Create virtual machine disk image")
            subprocess.call(["qemu-img", "create", "-f", "raw", \
                            self.disk_path, \
                            str(volume_size) + "G"], \
                            stdout=subprocess.PIPE, \
                            stderr=subprocess.PIPE)
示例#3
0
def vmc_lock(lockfile, action):
    if not os.path.exists(lockfile):
        f = open(lockfile, "w")
        f.write(socket.gethostname() + " -> " + action + "\n")
        f.close()
    else:
        f = open(lockfile, "r")
        o = f.read().strip()
        f.close
        die("Locked by " + o)
示例#4
0
def create_local_config(bridge_id, do_vlan=False, phydev="bond0"):
    pinfo("Create bridge config file")
    bridge_cfg_file = "/etc/sysconfig/network-scripts/ifcfg-br%d" % bridge_id
    if not os.path.exists(bridge_cfg_file):
        with open(bridge_cfg_file, "w") as f:
            f.write(netbridge_file(bridge_id))
    else:
        die("\"br%d\" bridge already exists, exiting..." % bridge_id)

    pinfo("Activating bridge")
    call(["ifup", "br%d" % bridge_id])

    if do_vlan:
        pinfo("Create VLAN config file")
        vlan_cfg_file = "/etc/sysconfig/network-scripts/ifcfg-%s.%d" % (phydev, bridge_id)
        if not os.path.exists(vlan_cfg_file):
            with open(vlan_cfg_file, "w") as f:
                f.write(netvlan_file(phydev, bridge_id, bridge_id))
        else:
            die("\"%d\" bridge already exists, exiting...")

        pinfo("Activating VLAN")
        call(["ifup", "%s.%d" % (phydev, bridge_id)])
示例#5
0
    )


try:
    dest = sys.argv[1]
    if dest == "--help" or dest == "-h":
        usage()
except:
    usage()
    sys.exit(1)

try:
    conn = libvirt.open(None)
    dconn = libvirt.open("qemu+ssh://" + dest + "/system")
except:
    perror("Unable to connect to the libvirt daemon")
    sys.exit(1)

for domain in conn.listAllDomains():
    if domain.isActive():
        try:
            pinfo("Migrate %s on %s" % (domain.name(), dest))
            domain.migrate(dconn, libvirt.VIR_MIGRATE_LIVE)
        except:
            die("Something goes wrong during the migration")

dconn.close()
conn.close()

sys.exit(0)
示例#6
0
文件: vm.py 项目: dguglielmi/vmc-core
    def check(self):
        if self.name == None:
            die("VM name is not set")
        if self.arch == None:
            die("VM arch is not set")
        if self.vcpu == None:
            die("VM vcpu is not set")
        if self.memory == None:
            die("VM memory is not set")
        if self.bridge_intf == None:
            die("VM bridge interface is not set")
        if self.template_name == None:
            die("VM template name is not set")
        if self.libvirt_domain_dir == None:
            die("VM libvirt domain directory is not set")

        external_tools = ["brctl", "blkid", "du", "fdisk", "mount", "sync", "tar", "umount", "uuidgen", "losetup"]

        if self.do_lvm:
            if self.lvm_vg == None:
                die("Volume group is not set")

            self.lvm_lv = "VM_" + self.name
            self.disk_path = "/dev/" + self.lvm_vg + "/" + self.lvm_lv

            external_tools.append("lvcreate")
            external_tools.append("pvs")

        else:
            if self.img_disk_dir == None:
                die("VM image disk directory is not set")

            if not os.path.exists(self.img_disk_dir):
                die(self.img_disk_dir + " : directory doesn't exists")

            self.disk_path = self.img_disk_dir + "/VM_" + self.name + ".img"
            external_tools.append("qemu-img")

        if os.path.exists(self.disk_path):
            die(self.disk_path + " : already exists")

        for cmd in external_tools:
            if which(cmd) == None:
                die(cmd + " : command not found")

        if not os.path.exists(self.libvirt_domain_dir):
            die(self.libvirt_domain_dir + " : directory doesn't exists")

        self.libvirt_domain_file = self.libvirt_domain_dir + "/" + self.name + "." + self.domain + ".xml"
        if os.path.exists(self.libvirt_domain_file):
            die(self.libvirt_domain_file + " : already exists")

        if not len(self.list_available_templates()) > 0:
            die("No templates available")

        if self.do_cluster:
            if not unique_domain(self.name + "." + self.domain, self.cluster_nodes):
                die(vm.name + "." + vm.domain + " is already defined")
        else:
            if not unique_domain(self.name + "." + self.domain):
                die(self.name + "." + self.domain + " is already defined")

        self.template_path = self.template_dir + '/' + self.template_name + \
                   '-' + self.arch + ".tar"

        if not os.path.exists(self.template_path):
            die(self.template_path + " : the selected template does not exists")
示例#7
0
""" % os.path.basename(sys.argv[0]))

try:
    todelete = sys.argv[1]
    if todelete == "--help" or todelete == "-h":
        usage()
        os._exit(0)
except:
    usage()
    sys.exit(1)

conf = vmcConfig()

xml_file_path = conf.libvirt_domain_dir + "/" + todelete + ".xml"
if not os.path.exists(xml_file_path):
    die(xml_file_path + " : doesn't exists")

xml_dom = parse(xml_file_path)

disk_todelete = []

for disk in xml_dom.getElementsByTagName('disk'):
        if disk.attributes['device'].value == 'disk':
            disk_todelete.append(disk.getElementsByTagName('source')[0].attributes['dev'].value)

if conf.do_cluster:
    # Check if the domain is running somewhere
    # And undefine everywhere
    for h in conf.cluster_nodes:
        conn = libvirt.open("qemu+ssh://" + h + "/system")
        try:
示例#8
0
import libvirt
from vmc.common import pinfo, perror, die
from vmc.utils.openwide import vmc_unlock, vmc_lock
import vmc.utils.lvm

def usage():
    print ("""Usage : %s

Lock LVM metadata

-h, --help	Display help information
""" % os.path.basename(sys.argv[0]))
try:
    dest = sys.argv[1]
    if dest == "--help" or dest == "-h":
        usage()
except: 
    pass

conf = vmcConfig()

if vmc.utils.lvm.locking_type() == 4:
    die("LVM metadata are already locked")

vmc.utils.lvm.lock()
vmc_unlock(conf.lockfile)

pinfo("LVM metadata are now locked")

sys.exit(0)
-h, --help      Display help information
""" % os.path.basename(sys.argv[0]))

try:
    domain = sys.argv[1]
    if domain == "--help" or domain == "-h":
        usage()
except:
    usage()
    sys.exit(1)

conf = vmcConfig()

if not conf.do_lvm:
    die("VM storage is not set to LVM")

xml_file_path = conf.libvirt_domain_dir + "/" + domain + ".xml"
if not os.path.exists(xml_file_path):
    die(xml_file_path + " : doesn't exists")

xml_dom = parse(xml_file_path)

for disk in xml_dom.getElementsByTagName('disk'):
        if disk.attributes['device'].value == 'disk':
            d = disk.getElementsByTagName('source')[0].attributes['dev'].value
            if conf.do_cluster:
                pinfo("Deactivate LV %s on: %s" % (d, ', '.join(conf.cluster_nodes)))
                lv_deactivate(d, conf.cluster_nodes)
            else:
                pinfo("Deactivate LV %s " + d)
示例#10
0
            call(["ssh", "-l", "root", node, "%s -b %d" % (script_path, bridge_id)])

bridge_id = 0
do_vlan = False
phydev = "bond0"
export_config = False

if len(sys.argv) <= 1:

    message = "\n"
    try:
        resp = input("Entrer the bridge id (exemple 13 for br13) :\n")
        bridge_id = int(resp)
    except:
        pass
        die("\"%s\" is not a valid birdge id value" % resp)

    message += "Bridge interface: br%d\n" % bridge_id

    resp = input ("Do you want to create associated VLAN ? (Y/n): ")
    if resp.lower() == "y" or resp.lower() == "yes":
        do_vlan = True

        resp = input ("Enter the physical device used for VLAN %d (default bond0) :\n" % bridge_id)
        if resp:
            phydev = resp
    
        message += "VLAN interface: %s.%s\n" % (phydev, bridge_id)
        message += "Physical device : %s\n\n" % phydev

    resp = input(message + "Do you confirm creation ? (Y/n): ")
示例#11
0
-h, --help      Display help information
""" % os.path.basename(sys.argv[0]))

try:
    todefine = sys.argv[1]
    if todefine == "--help" or todefine == "-h":
        usage()
except:
    usage()
    sys.exit(1)

conf = vmcConfig()

xml_file_path = conf.libvirt_domain_dir + "/" + todefine + ".xml"
if not os.path.exists(xml_file_path):
    die(xml_file_path + " : doesn't exists")

try:
    with open(xml_file_path, "r") as f:
        xmldom = f.read()
except:
    pass
    die("Unable to read %s file" % xml_file_path,)

if conf.do_cluster:
    for h in conf.cluster_nodes:
        pinfo("Define %s on %s" % (xml_file_path, h))
        conn = libvirt.open("qemu+ssh://" + h + "/system")
        conn.defineXML(xmldom)
        conn.close()