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

@ -123,99 +123,107 @@ class QEMU:
return True 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 = Common()
comm.load_config() comm.load_config()
#print os_suite self.__prepare(hostname)
#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 # generate partition file
#Open new data file # self.__storage(hostname, storage, swap)
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()
os.popen("cp vm/storage/%s.partition /var/lib/qemu/images/%s/partition" % (storage, hostname), "r") if ssh_key:
os.popen("cp vm/firstboot.sh /var/lib/qemu/images/%s/boot.sh" % hostname, "r") self.__ssh_key(hostname, ssh_key)
values = { # TODO: move to settings file
"hostname": hostname, mirror = "http://ru.archive.ubuntu.com/ubuntu/"
"os_name": os_name, # TODO: move to settings file
"os_suite": os_suite, gw = "192.168.1.254"
"mirror": comm.settings_vm['mirror'], # TODO: move to settings file
"ip": ip, interface = 'br0'
"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 = [ c = [
"cd", "cd",
"/var/lib/qemu/images/%s;" % values['hostname'], "/var/lib/libvirt/qemu/images/%s;" % hostname,
"/usr/bin/vmbuilder", "/usr/bin/vmbuilder",
"kvm", "kvm",
values['os_name'], os_name,
"--suite=%s" % values['os_suite'], "--suite=%s" % os_suite,
"--flavour=virtual", "--flavour=virtual",
"--arch=amd64", "--arch=amd64",
"--mirror=%s" % values['mirror'], "--mirror=%s" % mirror,
"-o", "-o",
"--qemu=qemu:///system", "--qemu=qemu:///system",
"--ip=%s" % values['ip'], "--ip=%s" % ipv4,
"--gw=%s" % values['gw'], "--gw=%s" % gw,
"--part=/var/lib/qemu/images/%s/partition" % values['hostname'], # PARTITIONING
# "--part=/var/lib/qemu/images/%s/partition" % values['hostname'],
"--rootsize=%s" % storage,
"--swapsize=%s" % swap,
"--templates=templates", "--templates=templates",
"--user=administrator", # "--user=administrator",
"--name=administrator", # "--name=administrator",
"--pass=%s" % values['password'], # "--pass=%s" % values['password'],
"--rootpass=%s" % password,
"--ssh-user-key=/tmp/libvirt/%s/templates/libvirt" % hostname,
"--addpkg=linux-image-generic", "--addpkg=linux-image-generic",
"--addpkg=vim-nox", "--addpkg=vim-nox",
"--addpkg=nano", "--addpkg=nano",
"--addpkg=unattended-upgrades", "--addpkg=unattended-upgrades",
"--addpkg=acpid", "--addpkg=acpid",
"--firstboot=/var/lib/qemu/images/%s/boot.sh" % values['hostname'], "--addpkg=fail2ban",
"--mem=%s" % values['memory'], "--firstboot=/var/lib/qemu/images/%s/boot.sh" % hostname,
"--cpus=%s" % values['cores'], "--mem=%s" % memory,
"--hostname=%s" % values['hostname'], "--cpus=%s" % cores,
"--bridge=br0" "--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) # 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 return True

View file

@ -6,7 +6,7 @@ from SWSCloudNode.settings import settings
from SWSCloudNode.logger import logging from SWSCloudNode.logger import logging
from SWSCloudNode import Node from SWSCloudNode import Node
from SWSCloudNode import Tasks from SWSCloudNode import Tasks
from SWSCloudNode import lxc from SWSCloudNode import lxc, qemu
allowed_actions = [ allowed_actions = [
'container_delete', 'container_delete',
@ -130,7 +130,20 @@ while True:
nodeclient.task_status_update(task['id'], 2) nodeclient.task_status_update(task['id'], 2)
if task.get('task') == 'vm_create': if task.get('task') == 'vm_create':
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 pass
nodeclient.task_status_update(task['id'], 2)
if task.get('task') == 'vm_start': if task.get('task') == 'vm_start':
pass pass
if task.get('task') == 'vm_restart': if task.get('task') == 'vm_restart':