new version with QEMU

This commit is contained in:
Vyacheslav Anzhiganov 2016-05-17 02:04:44 +03:00
parent 4720e11c72
commit 15f047dddb
2 changed files with 92 additions and 71 deletions

View file

@ -28,7 +28,7 @@ class QEMU:
for row in map(None, *[iter(domains)] * columns):
for domain in row:
if domain:
#print str(info(domain))
# print str(info(domain))
c += 1
offline[c] = self._info(domain)
@ -123,99 +123,107 @@ class QEMU:
return True
def create(self, cores, memory, storage, swap, hostname, ip, dns1, dns2, password, os_name, os_suite, ssh_key=None):
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)
os.popen("cp vm/firstboot.sh /var/lib/qemu/images/%s/boot.sh" % hostname, "r")
return True
def __storage(self, hostname, storage, swap=0):
# generate partition file
# Open new data file
# f = open("/var/lib/qemu/images/%s/partition" % hostname, "w")
f = open("/tmp/libvirt/%s/partition" % hostname, "w")
f.write("root %s\n" % storage)
if swap > 0:
# f.write("---\n" % swap)
f.write("swap %s\n" % swap)
f.close()
return True
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):
"""
функция создания виртуальной машины
"""
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)
self.__prepare(hostname)
# generate partition file
#Open new data file
f = open("/var/lib/qemu/images/%s/partition" % hostname, "w")
f.write("root %s\n" % storage)
if swap > 0:
f.write("---\n" % swap)
f.write("swap %s\n" % swap)
f.close()
# self.__storage(hostname, storage, swap)
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")
if ssh_key:
self.__ssh_key(hostname, ssh_key)
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
# 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'
c = [
"cd",
"/var/lib/qemu/images/%s;" % values['hostname'],
"/var/lib/libvirt/qemu/images/%s;" % hostname,
"/usr/bin/vmbuilder",
"kvm",
values['os_name'],
"--suite=%s" % values['os_suite'],
os_name,
"--suite=%s" % os_suite,
"--flavour=virtual",
"--arch=amd64",
"--mirror=%s" % values['mirror'],
"--mirror=%s" % mirror,
"-o",
"--qemu=qemu:///system",
"--ip=%s" % values['ip'],
"--gw=%s" % values['gw'],
"--part=/var/lib/qemu/images/%s/partition" % values['hostname'],
"--ip=%s" % ipv4,
"--gw=%s" % gw,
# PARTITIONING
# "--part=/var/lib/qemu/images/%s/partition" % values['hostname'],
"--rootsize=%s" % storage,
"--swapsize=%s" % swap,
"--templates=templates",
"--user=administrator",
"--name=administrator",
"--pass=%s" % values['password'],
# "--user=administrator",
# "--name=administrator",
# "--pass=%s" % values['password'],
"--rootpass=%s" % password,
"--ssh-user-key=/tmp/libvirt/%s/templates/libvirt" % hostname,
"--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"
"--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,
]
#subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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 "-----"
# 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)
return True

View file

@ -6,7 +6,7 @@ from SWSCloudNode.settings import settings
from SWSCloudNode.logger import logging
from SWSCloudNode import Node
from SWSCloudNode import Tasks
from SWSCloudNode import lxc
from SWSCloudNode import lxc, qemu
allowed_actions = [
'container_delete',
@ -130,7 +130,20 @@ while True:
nodeclient.task_status_update(task['id'], 2)
if task.get('task') == 'vm_create':
pass
nodeclient.task_status_update(task['id'], 1)
vm_id = task['plain']['vm_id']
# TODO: if container doesn't exists then complete task and report about this fact
p = task['plain']
try:
qemu.QEMU().create(
p['cores'], p['memory'], p['storage'], p['swap'], p['hostname'],
p['ipv4'], p['dns1'], p['dns2'], p['password'],
p['os_name'], p['os_suite'], p['ssh_key']
)
except Exception as e:
logging.warning(e)
pass
nodeclient.task_status_update(task['id'], 2)
if task.get('task') == 'vm_start':
pass
if task.get('task') == 'vm_restart':