2016-05-07 18:29:28 +03:00
|
|
|
# -*- 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:
|
2016-05-17 02:04:44 +03:00
|
|
|
# print str(info(domain))
|
2016-05-07 18:29:28 +03:00
|
|
|
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
|
|
|
|
|
2016-05-17 02:04:44 +03:00
|
|
|
def __prepare(self, hostname):
|
|
|
|
# Create directory for new VM
|
|
|
|
# os.popen('mkdir -p /var/lib/qemu/images/%s/templates/qemu' % hostname, "r")
|
|
|
|
subprocess.Popen([
|
|
|
|
# 'mkdir', '-p', '/var/lib/libvirt/images/%s/templates/qemu' % hostname
|
|
|
|
'mkdir', '-p', '/tmp/libvirt/%s/templates/libvirt' % hostname
|
|
|
|
],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
# Copy tempaltes
|
|
|
|
subprocess.Popen([
|
|
|
|
'cp', '/etc/vmbuilder/libvirt/*', '/tmp/libvirt/%s/templates/libvirt' % hostname],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
|
|
|
)
|
|
|
|
# os.popen("cp /etc/vmbuilder/libvirt/* /tmp/libvirt/%s/templates/libvirt" % hostname)
|
2016-05-07 18:29:28 +03:00
|
|
|
|
2016-05-17 02:11:48 +03:00
|
|
|
# os.popen("cp vm/firstboot.sh /var/lib/qemu/images/%s/boot.sh" % hostname, "r")
|
|
|
|
os.popen("cp vm/firstboot.sh /tmp/libvirt/images/%s/boot.sh" % hostname, "r")
|
2016-05-07 18:29:28 +03:00
|
|
|
|
2016-05-17 02:04:44 +03:00
|
|
|
return True
|
2016-05-07 18:29:28 +03:00
|
|
|
|
2016-05-17 02:04:44 +03:00
|
|
|
def __storage(self, hostname, storage, swap=0):
|
2016-05-07 18:29:28 +03:00
|
|
|
# generate partition file
|
2016-05-17 02:04:44 +03:00
|
|
|
# Open new data file
|
|
|
|
# f = open("/var/lib/qemu/images/%s/partition" % hostname, "w")
|
|
|
|
f = open("/tmp/libvirt/%s/partition" % hostname, "w")
|
2016-05-13 16:41:18 +03:00
|
|
|
f.write("root %s\n" % storage)
|
|
|
|
if swap > 0:
|
2016-05-17 02:04:44 +03:00
|
|
|
# f.write("---\n" % swap)
|
2016-05-13 16:41:18 +03:00
|
|
|
f.write("swap %s\n" % swap)
|
|
|
|
f.close()
|
|
|
|
|
2016-05-17 02:04:44 +03:00
|
|
|
return True
|
2016-05-07 18:29:28 +03:00
|
|
|
|
2016-05-17 02:04:44 +03:00
|
|
|
def __ssh_key(self, hostname, sshkey):
|
|
|
|
f = open("/tmp/libvirt/%s/authorized_keys" % hostname, "w")
|
|
|
|
f.write(sshkey)
|
|
|
|
f.close()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def create(self, cores, memory, storage, swap, hostname, ipv4, dns1, dns2, password, os_name, os_suite, ssh_key=None):
|
|
|
|
"""
|
|
|
|
функция создания виртуальной машины
|
|
|
|
"""
|
2016-05-17 02:05:50 +03:00
|
|
|
# comm = Common()
|
|
|
|
# comm.load_config()
|
2016-05-17 02:04:44 +03:00
|
|
|
|
|
|
|
self.__prepare(hostname)
|
|
|
|
|
|
|
|
# generate partition file
|
|
|
|
# self.__storage(hostname, storage, swap)
|
|
|
|
|
|
|
|
if ssh_key:
|
|
|
|
self.__ssh_key(hostname, ssh_key)
|
|
|
|
|
|
|
|
# TODO: move to settings file
|
|
|
|
mirror = "http://ru.archive.ubuntu.com/ubuntu/"
|
|
|
|
# TODO: move to settings file
|
|
|
|
gw = "192.168.1.254"
|
|
|
|
# TODO: move to settings file
|
|
|
|
interface = 'br0'
|
2016-05-07 18:29:28 +03:00
|
|
|
|
|
|
|
c = [
|
|
|
|
"cd",
|
2016-05-17 02:04:44 +03:00
|
|
|
"/var/lib/libvirt/qemu/images/%s;" % hostname,
|
2016-05-07 18:29:28 +03:00
|
|
|
"/usr/bin/vmbuilder",
|
|
|
|
"kvm",
|
2016-05-17 02:04:44 +03:00
|
|
|
os_name,
|
|
|
|
"--suite=%s" % os_suite,
|
2016-05-07 18:29:28 +03:00
|
|
|
"--flavour=virtual",
|
|
|
|
"--arch=amd64",
|
2016-05-17 02:04:44 +03:00
|
|
|
"--mirror=%s" % mirror,
|
2016-05-07 18:29:28 +03:00
|
|
|
"-o",
|
|
|
|
"--qemu=qemu:///system",
|
2016-05-17 02:04:44 +03:00
|
|
|
"--ip=%s" % ipv4,
|
|
|
|
"--gw=%s" % gw,
|
|
|
|
# PARTITIONING
|
|
|
|
# "--part=/var/lib/qemu/images/%s/partition" % values['hostname'],
|
|
|
|
"--rootsize=%s" % storage,
|
|
|
|
"--swapsize=%s" % swap,
|
2016-05-07 18:29:28 +03:00
|
|
|
"--templates=templates",
|
2016-05-17 02:04:44 +03:00
|
|
|
# "--user=administrator",
|
|
|
|
# "--name=administrator",
|
|
|
|
# "--pass=%s" % values['password'],
|
|
|
|
"--rootpass=%s" % password,
|
|
|
|
"--ssh-user-key=/tmp/libvirt/%s/templates/libvirt" % hostname,
|
2016-05-07 18:29:28 +03:00
|
|
|
"--addpkg=linux-image-generic",
|
|
|
|
"--addpkg=vim-nox",
|
|
|
|
"--addpkg=nano",
|
|
|
|
"--addpkg=unattended-upgrades",
|
|
|
|
"--addpkg=acpid",
|
2016-05-17 02:04:44 +03:00
|
|
|
"--addpkg=fail2ban",
|
|
|
|
"--firstboot=/var/lib/qemu/images/%s/boot.sh" % hostname,
|
|
|
|
"--mem=%s" % memory,
|
|
|
|
"--cpus=%s" % cores,
|
|
|
|
"--hostname=%s" % hostname,
|
|
|
|
"--bridge=%s" % interface,
|
|
|
|
"--dns=%s" % dns1,
|
|
|
|
"--dns=%s" % dns2,
|
2016-05-07 18:29:28 +03:00
|
|
|
]
|
2016-05-17 02:04:44 +03:00
|
|
|
subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2016-05-07 18:29:28 +03:00
|
|
|
|
2016-05-17 02:04:44 +03:00
|
|
|
# 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)
|
2016-05-07 18:29:28 +03:00
|
|
|
return True
|