213 lines
7.6 KiB
Python
213 lines
7.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import libvirt
|
|
import subprocess
|
|
from SWSCloudNode.common import Common
|
|
|
|
|
|
class QEMU:
|
|
def __init__(self):
|
|
# qemu+ssh://root@laforge.usersys.redhat.com/system
|
|
self.conn = libvirt.open("qemu:///system")
|
|
|
|
def list(self):
|
|
names = self.conn.listDefinedDomains()
|
|
domains = map(self.conn.lookupByName, names)
|
|
|
|
ids = self.conn.listDomainsID()
|
|
running = map(self.conn.lookupByID, ids)
|
|
|
|
columns = 3
|
|
|
|
c = 0
|
|
n = 0
|
|
online = {}
|
|
offline = {}
|
|
|
|
for row in map(None, *[iter(domains)] * columns):
|
|
for domain in row:
|
|
if domain:
|
|
#print str(info(domain))
|
|
c += 1
|
|
offline[c] = self._info(domain)
|
|
|
|
for row in map(None, *[iter(running)] * columns):
|
|
for domain in row:
|
|
if domain:
|
|
n += 1
|
|
online[n] = self._info(domain)
|
|
|
|
return {
|
|
"online": online,
|
|
"offline": offline
|
|
}
|
|
|
|
def _info(self, dom):
|
|
states = {
|
|
libvirt.VIR_DOMAIN_NOSTATE: 'no state',
|
|
libvirt.VIR_DOMAIN_RUNNING: 'running',
|
|
libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
|
|
libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
|
|
libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
|
|
libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
|
|
libvirt.VIR_DOMAIN_CRASHED: 'crashed',
|
|
}
|
|
|
|
[state, maxmem, mem, ncpu, cputime] = dom.info()
|
|
#return '%s is %s,' % (dom.name(), states.get(state, state))
|
|
return {
|
|
"hostname": dom.name(),
|
|
"info": {
|
|
"state": state,
|
|
"maxmem": maxmem,
|
|
"memory": mem,
|
|
"ncpu": ncpu,
|
|
"cputime": cputime
|
|
}
|
|
}
|
|
|
|
def start(self, hostname):
|
|
"""
|
|
Function VM Start
|
|
использует нативную библиотеку qemu
|
|
|
|
Params
|
|
:hostname: str
|
|
Return:
|
|
:bool:
|
|
"""
|
|
conn = libvirt.open("qemu:///system")
|
|
action = conn.lookupByName(hostname)
|
|
|
|
try:
|
|
action.create()
|
|
except:
|
|
print conn.virConnGetLastError()
|
|
return False
|
|
|
|
return True
|
|
|
|
def stop(self, hostname):
|
|
"""
|
|
функция остановки фиртуальной машины
|
|
использует
|
|
"""
|
|
conn = libvirt.open("qemu:///system")
|
|
action = conn.lookupByName(hostname)
|
|
|
|
try:
|
|
action.destroy()
|
|
except:
|
|
print conn.virConnGetLastError()
|
|
return False
|
|
|
|
return True
|
|
|
|
def restart(self, hostname):
|
|
"""
|
|
функция перезапуска виртуальной машины,
|
|
использует функции stop и start
|
|
"""
|
|
self.stop(hostname)
|
|
|
|
return self.start(hostname)
|
|
|
|
def delete(self, hostname):
|
|
"""
|
|
функция удаления виртуальной машины
|
|
"""
|
|
os.popen("/usr/bin/virsh shutdown %s" % hostname, "r")
|
|
os.popen("/usr/bin/virsh undefine %s" % hostname, "r")
|
|
os.popen("/bin/rm -r /var/lib/qemu/images/%s/" % hostname, "r")
|
|
|
|
return True
|
|
|
|
def create(self, cores, memory, storage, hostname, ip, dns1, dns2, password, os_name, os_suite):
|
|
"""
|
|
функция создания виртуальной машины
|
|
"""
|
|
comm = Common()
|
|
comm.load_config()
|
|
|
|
#print os_suite
|
|
|
|
#return {}
|
|
#load plan list
|
|
#with open('../config/plan.json') as plandata_file:
|
|
# plan = json.load(plandata_file)
|
|
|
|
# Create directory for new VM
|
|
#subprocess.Popen(['mkdir', '-p', '/var/lib/qemu/images/%s/templates/qemu' % hostname], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
os.popen('mkdir -p /var/lib/qemu/images/%s/templates/qemu' % hostname, "r")
|
|
#subprocess.Popen(['cp', '/etc/vmbuilder/qemu/*', '/var/lib/qemu/images/%s/templates/qemu' % hostname], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
os.popen("cp /etc/vmbuilder/qemu/* /var/lib/qemu/images/%s/templates/qemu" % hostname)
|
|
|
|
# generate partition file
|
|
os.popen("cp vm/storage/%s.partition /var/lib/qemu/images/%s/partition" % (storage, hostname), "r")
|
|
os.popen("cp vm/firstboot.sh /var/lib/qemu/images/%s/boot.sh" % hostname, "r")
|
|
|
|
values = {
|
|
"hostname": hostname,
|
|
"os_name": os_name,
|
|
"os_suite": os_suite,
|
|
"mirror": comm.settings_vm['mirror'],
|
|
"ip": ip,
|
|
"gw": comm.settings_vm['gw'],
|
|
"password": password,
|
|
"memory": memory,
|
|
"cores": cores
|
|
}
|
|
print "------"
|
|
print values
|
|
print "------"
|
|
|
|
#qqq = [
|
|
# "cd",
|
|
# "/var/lib/qemu/images/%s;" % values['hostname'],
|
|
# "/usr/bin/vmbuilder", "kvm", values['os_name'], "--suite=%s" % values['os_suite']]
|
|
#aaa = subprocess.Popen(qqq, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
|
|
|
|
#print "cd /var/lib/qemu/images/%(hostname)s;/usr/bin/vmbuilder kvm %(os_name)s --suite=%(os_suite)s
|
|
# --flavour=virtual --arch=amd64 --mirror=%(mirror)s -o --qemu=qemu:///system --ip=%(ip)s
|
|
# --gw=%(gw)s --part=/var/lib/qemu/images/%(hostname)s/partition --templates=templates --user=administrator
|
|
# --name=administrator --pass=%(password)s --addpkg=linux-image-generic --addpkg=vim-nox --addpkg=nano
|
|
# --addpkg=unattended-upgrades --addpkg=acpid --firstboot=/var/lib/qemu/images/%(hostname)s/boot.sh
|
|
# --mem=%(memory)s --cpus=%(cores)s --hostname=%(hostname)s --bridge=br0" % values
|
|
|
|
c = [
|
|
"cd",
|
|
"/var/lib/qemu/images/%s;" % values['hostname'],
|
|
"/usr/bin/vmbuilder",
|
|
"kvm",
|
|
values['os_name'],
|
|
"--suite=%s" % values['os_suite'],
|
|
"--flavour=virtual",
|
|
"--arch=amd64",
|
|
"--mirror=%s" % values['mirror'],
|
|
"-o",
|
|
"--qemu=qemu:///system",
|
|
"--ip=%s" % values['ip'],
|
|
"--gw=%s" % values['gw'],
|
|
"--part=/var/lib/qemu/images/%s/partition" % values['hostname'],
|
|
"--templates=templates",
|
|
"--user=administrator",
|
|
"--name=administrator",
|
|
"--pass=%s" % values['password'],
|
|
"--addpkg=linux-image-generic",
|
|
"--addpkg=vim-nox",
|
|
"--addpkg=nano",
|
|
"--addpkg=unattended-upgrades",
|
|
"--addpkg=acpid",
|
|
"--firstboot=/var/lib/qemu/images/%s/boot.sh" % values['hostname'],
|
|
"--mem=%s" % values['memory'],
|
|
"--cpus=%s" % values['cores'],
|
|
"--hostname=%s" % values['hostname'],
|
|
"--bridge=br0"
|
|
]
|
|
#subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
os.popen("cd /var/lib/qemu/images/%(hostname)s;/usr/bin/vmbuilder kvm %(os_name)s --suite=%(os_suite)s --flavour=virtual --arch=amd64 --mirror=%(mirror)s -o --qemu=qemu:///system --ip=%(ip)s --gw=%(gw)s --part=/var/lib/qemu/images/%(hostname)s/partition --templates=templates --user=administrator --name=administrator --pass=%(password)s --addpkg=linux-image-generic --addpkg=vim-nox --addpkg=nano --addpkg=unattended-upgrades --addpkg=acpid --firstboot=/var/lib/qemu/images/%(hostname)s/boot.sh --mem=%(memory)s --cpus=%(cores)s --hostname=%(hostname)s --bridge=br0" % values)
|
|
#sys.exit(2)
|
|
print "-----"
|
|
return True
|