support QEMU

This commit is contained in:
Vyacheslav Anzhiganov 2016-05-21 14:25:53 +00:00
parent 43cd3c539d
commit f665da032a
2 changed files with 21 additions and 14 deletions

View file

@ -164,20 +164,20 @@ class QEMU:
return True return True
def create(self, cores, memory, storage, swap, hostname, ipv4, ipv4_gateway, dns1, dns2, def create(self, cores, memory, storage, swap, hostname, ipv4, ipv4_gateway, dns1, dns2,
password, os_name, os_suite, interface, ssh_key=None): password, os_name, os_suite, interface, vm_id, ssh_key=None):
""" """
функция создания виртуальной машины функция создания виртуальной машины
""" """
# comm = Common()
# comm.load_config()
self.__prepare(hostname) # self.__prepare(hostname)
self.__prepare(vm_id)
# generate partition file # generate partition file
# self.__storage(hostname, storage, swap) # self.__storage(hostname, storage, swap)
if ssh_key: if ssh_key:
self.__ssh_key(hostname, ssh_key) # self.__ssh_key(hostname, ssh_key)
self.__ssh_key(vm_id, ssh_key)
# TODO: move to settings file # TODO: move to settings file
mirror = "http://ru.archive.ubuntu.com/ubuntu/" mirror = "http://ru.archive.ubuntu.com/ubuntu/"
@ -200,22 +200,23 @@ class QEMU:
# "--part=/var/lib/qemu/images/%s/partition" % values['hostname'], # "--part=/var/lib/qemu/images/%s/partition" % values['hostname'],
"--rootsize=%s" % storage, "--rootsize=%s" % storage,
"--swapsize=%s" % swap, "--swapsize=%s" % swap,
"--templates=/tmp/libvirt/%s/templates/libvirt" % hostname, "--templates=/tmp/libvirt/%s/templates/libvirt" % vm_id,
# "--user=administrator", # "--user=administrator",
# "--name=administrator", # "--name=administrator",
# "--pass=%s" % values['password'], # "--pass=%s" % values['password'],
"--rootpass=%s" % password, "--rootpass=%s" % password,
"--ssh-user-key=/tmp/libvirt/%s/authorized_keys" % hostname, "--ssh-user-key=/tmp/libvirt/%s/authorized_keys" % vm_id,
"--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",
"--addpkg=fail2ban", "--addpkg=fail2ban",
"--firstboot=/tmp/libvirt/%s/boot.sh" % hostname, "--firstboot=/tmp/libvirt/%s/boot.sh" % vm_id,
"--mem=%s" % memory, "--mem=%s" % memory,
"--cpus=%s" % cores, "--cpus=%s" % cores,
"--hostname=%s" % hostname, # "--hostname=%s" % hostname,
"--hostname=%s" % vm_id,
"--bridge=%s" % interface, "--bridge=%s" % interface,
"--dns=%s" % dns1, "--dns=%s" % dns1,
"--dns=%s" % dns2, "--dns=%s" % dns2,

View file

@ -129,6 +129,8 @@ while True:
pass pass
nodeclient.task_status_update(task['id'], 2) nodeclient.task_status_update(task['id'], 2)
# Virtual machines operations
# Create new virtual machine
if task.get('task') == 'vm_create': if task.get('task') == 'vm_create':
nodeclient.task_status_update(task['id'], 1) nodeclient.task_status_update(task['id'], 1)
vm_id = task['plain']['vm_id'] vm_id = task['plain']['vm_id']
@ -138,7 +140,9 @@ while True:
qemu.QEMU().create( qemu.QEMU().create(
p['cores'], p['memory'], p['storage'], p['swap'], p['hostname'], p['cores'], p['memory'], p['storage'], p['swap'], p['hostname'],
p['ipv4'], p['ipv4_gateway'], p['dns1'], p['dns2'], p['password'], p['ipv4'], p['ipv4_gateway'], p['dns1'], p['dns2'], p['password'],
p['os_name'], p['os_suite'], settings.get('node', 'interface'), p['ssh_key'] p['os_name'], p['os_suite'], settings.get('node', 'interface'),
p['vm_id'],
p['ssh_key'],
) )
except Exception as e: except Exception as e:
# TODO: back to origin task status # TODO: back to origin task status
@ -146,24 +150,26 @@ while True:
pass pass
finally: finally:
nodeclient.task_status_update(task['id'], 2) nodeclient.task_status_update(task['id'], 2)
# Start virtual machine
if task.get('task') == 'vm_start': if task.get('task') == 'vm_start':
nodeclient.task_status_update(task['id'], 1) nodeclient.task_status_update(task['id'], 1)
qemu.QEMU().start(task['plain']['hostname']) qemu.QEMU().start(task['plain']['vm_id'])
nodeclient.task_status_update(task['id'], 2) nodeclient.task_status_update(task['id'], 2)
pass pass
# Restart virtual machine
if task.get('task') == 'vm_restart': if task.get('task') == 'vm_restart':
nodeclient.task_status_update(task['id'], 1) nodeclient.task_status_update(task['id'], 1)
qemu.QEMU().restart(task['plain']['hostname']) qemu.QEMU().restart(task['plain']['vm_id'])
nodeclient.task_status_update(task['id'], 2) nodeclient.task_status_update(task['id'], 2)
pass pass
if task.get('task') == 'vm_stop': if task.get('task') == 'vm_stop':
nodeclient.task_status_update(task['id'], 1) nodeclient.task_status_update(task['id'], 1)
qemu.QEMU().stop(task['plain']['hostname']) qemu.QEMU().stop(task['plain']['vm_id'])
nodeclient.task_status_update(task['id'], 2) nodeclient.task_status_update(task['id'], 2)
pass pass
if task.get('task') == 'vm_delete': if task.get('task') == 'vm_delete':
nodeclient.task_status_update(task['id'], 1) nodeclient.task_status_update(task['id'], 1)
qemu.QEMU().delete(task['plain']['hostname']) qemu.QEMU().delete(task['plain']['vm_id'])
nodeclient.task_status_update(task['id'], 2) nodeclient.task_status_update(task['id'], 2)
pass pass