forked from uncloud/uncloud
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
import uuid
|
|
import os
|
|
|
|
from uncloud.oneshot import logger
|
|
|
|
class VM(object):
|
|
def __init__(self, vmm, config):
|
|
self.config = config
|
|
self.vmm = vmm
|
|
|
|
# Extract VM specs/metadata from configuration.
|
|
self.name = config.get('name')
|
|
self.memory = config.get('memory', 1024)
|
|
self.cores = config.get('cores', 1)
|
|
self.threads = config.get('threads', 1)
|
|
self.image_format = config.get('image_format', 'qcow2')
|
|
self.image = config.get('image')
|
|
self.uuid = config.get('uuid', uuid.uuid4())
|
|
self.mac = config.get('mac', 'spuik')
|
|
|
|
# Harcoded & generated values.
|
|
self.image_format='qcow2'
|
|
self.accel = 'kvm'
|
|
|
|
def get_qemu_args(self):
|
|
command = (
|
|
"-uuid {uuid} -name {name}"
|
|
" -drive file={image},format={image_format},if=virtio"
|
|
" -device virtio-rng-pci"
|
|
" -m {memory} -smp cores={cores},threads={threads}"
|
|
).format(
|
|
uuid=self.uuid, name=self.name,
|
|
image=self.image, image_format=self.image_format,
|
|
memory=self.memory, cores=self.cores, threads=self.threads,
|
|
)
|
|
|
|
return command.split(" ")
|
|
|
|
def start(self):
|
|
# Check that VM image is available.
|
|
if not os.path.isfile(self.image):
|
|
logger.error("Image {} does not exist. Aborting.".format(self.image))
|
|
|
|
# Generate config for and run QEMU.
|
|
qemu_args = self.get_qemu_args()
|
|
logger.warning("QEMU args for VM {}: {}".format(self.uuid, qemu_args))
|
|
self.vmm.start(
|
|
uuid=self.uuid,
|
|
migration=False,
|
|
*qemu_args
|
|
)
|
|
|
|
def stop(self):
|
|
self.vmm.stop(self.uuid)
|
|
|
|
def get_status(self):
|
|
return self.vmm.get_status(self.uuid)
|
|
|
|
def get_vnc_addr(self):
|
|
return self.vmm.get_vnc(self.uuid)
|
|
|
|
def get_uuid(self):
|
|
return self.uuid
|
|
|
|
def get_name(self):
|
|
success, json = self.vmm.execute_command(uuid, 'query-name')
|
|
if success:
|
|
return json['return']['name']
|
|
|
|
return None
|