uncloud-mravi/uncloud_etcd_based/uncloud/hack/vm.py

194 lines
7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# 2020 Nico Schottelius (nico.schottelius at ungleich.ch)
#
# This file is part of uncloud.
#
# uncloud is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# uncloud is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with uncloud. If not, see <http://www.gnu.org/licenses/>.
2020-01-28 11:33:36 +00:00
# This module is directly called from the hack module, and can be used as follow:
#
# Create a new VM with default CPU/Memory. The path of the image file is relative to $hackprefix.
# `uncloud hack --hackprefix /tmp/hackcloud --create-vm --image mysuperimage.qcow2`
#
# List running VMs (returns a list of UUIDs).
# `uncloud hack --hackprefix /tmp/hackcloud --list-vms
#
2020-01-28 11:33:36 +00:00
# Get VM status:
# `uncloud hack --hackprefix /tmp/hackcloud --get-vm-status --uuid my-vm-uuid`
#
2020-01-28 11:33:36 +00:00
# Stop a VM:
# `uncloud hack --hackprefix /tmp/hackcloud --destroy-vm --uuid my-vm-uuid`
2020-01-28 12:45:20 +00:00
# ``
import subprocess
import uuid
import os
import logging
2020-01-14 13:23:26 +00:00
from uncloud.hack.db import DB
from uncloud.hack.mac import MAC
2020-01-28 08:25:25 +00:00
from uncloud.vmm import VMM
2020-02-09 08:36:50 +00:00
from uncloud.hack.product import Product
log = logging.getLogger(__name__)
2020-01-28 08:25:25 +00:00
log.setLevel(logging.DEBUG)
class VM(object):
2020-02-09 18:27:24 +00:00
def __init__(self, config, db_entry=None):
2020-01-24 12:56:08 +00:00
self.config = config
2020-01-28 08:25:25 +00:00
2020-01-24 12:56:08 +00:00
#TODO: Enable etcd lookup
self.no_db = self.config.arguments['no_db']
if not self.no_db:
self.db = DB(self.config, prefix="/vm")
2020-02-09 18:27:24 +00:00
if db_entry:
self.db_entry = db_entry
2020-01-28 08:25:25 +00:00
# General CLI arguments.
self.hackprefix = self.config.arguments['hackprefix']
self.uuid = self.config.arguments['uuid']
self.memory = self.config.arguments['memory'] or '1024M'
self.cores = self.config.arguments['cores'] or 1
2020-02-06 14:13:08 +00:00
2020-01-28 08:25:25 +00:00
if self.config.arguments['image']:
self.image = os.path.join(self.hackprefix, self.config.arguments['image'])
else:
self.image = None
2020-01-14 13:23:26 +00:00
2020-02-06 14:13:08 +00:00
if self.config.arguments['image_format']:
self.image_format=self.config.arguments['image_format']
else:
self.image_format='qcow2'
2020-01-28 08:25:25 +00:00
# External components.
2020-02-09 08:36:50 +00:00
# This one is broken:
# TypeError: expected str, bytes or os.PathLike object, not NoneType
# Fix before re-enabling
# self.vmm = VMM(vmm_backend=self.hackprefix)
2020-01-28 08:25:25 +00:00
self.mac = MAC(self.config)
2020-01-14 13:23:26 +00:00
2020-01-28 08:25:25 +00:00
# Harcoded & generated values.
2020-02-09 08:36:50 +00:00
self.owner = 'uncloud'
2020-01-28 08:25:25 +00:00
self.accel = 'kvm'
self.threads = 1
2020-01-24 12:56:08 +00:00
self.ifup = os.path.join(self.hackprefix, "ifup.sh")
self.ifdown = os.path.join(self.hackprefix, "ifdown.sh")
2020-01-28 08:25:25 +00:00
self.ifname = "uc{}".format(self.mac.to_str_format())
2020-01-29 18:55:55 +00:00
self.vm = {}
2020-02-09 18:27:24 +00:00
self.product = Product(config, product_name="dualstack-vm",
product_class=self.__class__)
self.product.define_feature(name="base",
one_time_price=0,
recurring_price=9,
recurring_period="per_month",
minimum_period="per_hour")
2020-02-09 08:36:50 +00:00
self.features = []
2020-01-28 08:25:25 +00:00
def get_qemu_args(self):
command = (
"-name {owner}-{name}"
" -machine pc,accel={accel}"
" -drive file={image},format={image_format},if=virtio"
" -device virtio-rng-pci"
" -m {memory} -smp cores={cores},threads={threads}"
" -netdev tap,id=netmain,script={ifup},downscript={ifdown},ifname={ifname}"
" -device virtio-net-pci,netdev=netmain,id=net0,mac={mac}"
).format(
owner=self.owner, name=self.uuid,
accel=self.accel,
image=self.image, image_format=self.image_format,
memory=self.memory, cores=self.cores, threads=self.threads,
ifup=self.ifup, ifdown=self.ifdown, ifname=self.ifname,
mac=self.mac
)
return command.split(" ")
2020-01-14 13:23:26 +00:00
2020-02-09 18:27:24 +00:00
def create_product(self):
"""Find a VM host and schedule on it"""
2020-02-09 08:36:50 +00:00
pass
2020-01-28 08:25:25 +00:00
def create(self):
# New VM: new UUID, new MAC.
self.uuid = str(uuid.uuid4())
Fix AttributeError: 'VM' object has no attribute 'vm' ERROR:uncloud.vmm:Error occurred while starting VM. Detail qemu-system-x86_64: -drive file=/home/nico/vcs/uncloud/uncloud/hack/hackcloud/alpine-virt-3.11.2-x86_64.iso,format=qcow2,if=virtio: Image is not in qcow2 format Traceback (most recent call last): File "/home/nico/vcs/uncloud/uncloud/vmm/__init__.py", line 186, in start sp.check_output(command, stderr=sp.PIPE) File "/usr/lib/python3.8/subprocess.py", line 411, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, File "/usr/lib/python3.8/subprocess.py", line 512, in run raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command '['sudo', '-p', 'Enter password to start VM 87230168-1b74-49f7-97c3-c968a26fc65e: ', '/usr/bin/qemu-system-x86_64', '-name', 'uncoud-87230168-1b74-49f7-97c3-c968a26fc65e', '-machine', 'pc,accel=kvm', '-drive', 'file=/home/nico/vcs/uncloud/uncloud/hack/hackcloud/alpine-virt-3.11.2-x86_64.iso,format=qcow2,if=virtio', '-device', 'virtio-rng-pci', '-m', '1024M', '-smp', 'cores=1,threads=1', '-netdev', 'tap,id=netmain,script=/home/nico/vcs/uncloud/uncloud/hack/hackcloud/ifup.sh,downscript=/home/nico/vcs/uncloud/uncloud/hack/hackcloud/ifdown.sh,ifname=uc000000000000', '-device', 'virtio-net-pci,netdev=netmain,id=net0,mac=42:00:00:00:00:01', '-qmp', 'unix:/home/nico/vcs/uncloud/uncloud/hack/hackcloud/sock/87230168-1b74-49f7-97c3-c968a26fc65e,server,nowait', '-vnc', 'unix:/tmp/tmpep71nz1f', '-daemonize']' returned non-zero exit status 1. ERROR:root:'VM' object has no attribute 'vm' Traceback (most recent call last): File "./bin/../scripts/uncloud", line 82, in <module> main(arguments) File "/home/nico/vcs/uncloud/uncloud/hack/main.py", line 47, in main vm.create() File "/home/nico/vcs/uncloud/uncloud/hack/vm.py", line 115, in create self.vm['mac'] = self.mac AttributeError: 'VM' object has no attribute 'vm' (venv) [18:49] diamond:uncloud% ./bin/uncloud-run-reinstall hack --create-vm --hackprefix ~/vcs/uncloud/uncloud/hack/hackcloud/ --image alpine-virt-3.11.2-x86_64.iso --no-db
2020-01-29 18:30:19 +00:00
self.mac=MAC(self.config)
2020-01-28 08:25:25 +00:00
self.mac.create()
2020-01-24 13:21:38 +00:00
2020-01-28 08:25:25 +00:00
qemu_args = self.get_qemu_args()
log.debug("QEMU args passed to VMM: {}".format(qemu_args))
self.vmm.start(
uuid=self.uuid,
migration=False,
*qemu_args
)
Fix AttributeError: 'VM' object has no attribute 'vm' ERROR:uncloud.vmm:Error occurred while starting VM. Detail qemu-system-x86_64: -drive file=/home/nico/vcs/uncloud/uncloud/hack/hackcloud/alpine-virt-3.11.2-x86_64.iso,format=qcow2,if=virtio: Image is not in qcow2 format Traceback (most recent call last): File "/home/nico/vcs/uncloud/uncloud/vmm/__init__.py", line 186, in start sp.check_output(command, stderr=sp.PIPE) File "/usr/lib/python3.8/subprocess.py", line 411, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, File "/usr/lib/python3.8/subprocess.py", line 512, in run raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command '['sudo', '-p', 'Enter password to start VM 87230168-1b74-49f7-97c3-c968a26fc65e: ', '/usr/bin/qemu-system-x86_64', '-name', 'uncoud-87230168-1b74-49f7-97c3-c968a26fc65e', '-machine', 'pc,accel=kvm', '-drive', 'file=/home/nico/vcs/uncloud/uncloud/hack/hackcloud/alpine-virt-3.11.2-x86_64.iso,format=qcow2,if=virtio', '-device', 'virtio-rng-pci', '-m', '1024M', '-smp', 'cores=1,threads=1', '-netdev', 'tap,id=netmain,script=/home/nico/vcs/uncloud/uncloud/hack/hackcloud/ifup.sh,downscript=/home/nico/vcs/uncloud/uncloud/hack/hackcloud/ifdown.sh,ifname=uc000000000000', '-device', 'virtio-net-pci,netdev=netmain,id=net0,mac=42:00:00:00:00:01', '-qmp', 'unix:/home/nico/vcs/uncloud/uncloud/hack/hackcloud/sock/87230168-1b74-49f7-97c3-c968a26fc65e,server,nowait', '-vnc', 'unix:/tmp/tmpep71nz1f', '-daemonize']' returned non-zero exit status 1. ERROR:root:'VM' object has no attribute 'vm' Traceback (most recent call last): File "./bin/../scripts/uncloud", line 82, in <module> main(arguments) File "/home/nico/vcs/uncloud/uncloud/hack/main.py", line 47, in main vm.create() File "/home/nico/vcs/uncloud/uncloud/hack/vm.py", line 115, in create self.vm['mac'] = self.mac AttributeError: 'VM' object has no attribute 'vm' (venv) [18:49] diamond:uncloud% ./bin/uncloud-run-reinstall hack --create-vm --hackprefix ~/vcs/uncloud/uncloud/hack/hackcloud/ --image alpine-virt-3.11.2-x86_64.iso --no-db
2020-01-29 18:30:19 +00:00
2020-01-24 12:56:08 +00:00
self.mac.create()
self.vm['mac'] = self.mac
self.vm['ifname'] = "uc{}".format(self.mac.__repr__())
2020-01-14 13:23:26 +00:00
2020-01-24 13:21:38 +00:00
# FIXME: TODO: turn this into a string and THEN
# .split() it later -- easier for using .format()
2020-01-24 12:56:08 +00:00
#self.vm['commandline'] = [ "{}".format(self.sudo),
self.vm['commandline'] = "{sudo}{qemu} -name uncloud-{uuid} -machine pc,accel={accel} -m {memory} -smp {cores} -uuid {uuid} -drive file={os_image},media=cdrom -netdev tap,id=netmain,script={ifup},downscript={ifdown},ifname={ifname} -device virtio-net-pci,netdev=netmain,id=net0,mac={mac}"
# self.vm['commandline'] = [ "{}".format(self.sudo),
# "{}".format(self.qemu),
# "-name", "uncloud-{}".format(self.vm['uuid']),
# "-machine", "pc,accel={}".format(self.accel),
# "-m", "{}".format(self.vm['memory']),
# "-smp", "{}".format(self.vm['cores']),
# "-uuid", "{}".format(self.vm['uuid']),
# "-drive", "file={},media=cdrom".format(self.vm['os_image']),
# "-netdev", "tap,id=netmain,script={},downscript={},ifname={}".format(self.ifup, self.ifdown, self.vm['ifname']),
# "-device", "virtio-net-pci,netdev=netmain,id=net0,mac={}".format(self.vm['mac'])
# ]
2020-01-14 13:23:26 +00:00
def _execute_cmd(self, cmd_string, **kwargs):
cmd = cmd_string.format(**self.vm, **kwargs)
log.info("Executing: {}".format(cmd))
subprocess.run(cmd.split())
2020-01-23 20:20:16 +00:00
2020-01-28 08:25:25 +00:00
def stop(self):
if not self.uuid:
print("Please specific an UUID with the --uuid flag.")
exit(1)
2020-01-23 20:20:16 +00:00
2020-01-28 08:25:25 +00:00
self.vmm.stop(self.uuid)
def status(self):
if not self.uuid:
print("Please specific an UUID with the --uuid flag.")
exit(1)
print(self.vmm.get_status(self.uuid))
2020-01-28 10:02:18 +00:00
def vnc_addr(self):
if not self.uuid:
print("Please specific an UUID with the --uuid flag.")
exit(1)
print(self.vmm.get_vnc(self.uuid))
def list(self):
print(self.vmm.discover())