uncloud-mravi/uncloud/hack/vm.py

116 lines
4.8 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/>.
#
#
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-14 13:23:26 +00:00
log = logging.getLogger(__name__)
class VM(object):
def __init__(self, config):
2020-01-24 12:56:08 +00:00
self.config = config
#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-01-24 12:56:08 +00:00
#TODO: Select generic
#self.hackprefix="/home/nico/vcs/uncloud/uncloud/hack/hackcloud" #TODO: Should be removed midterm
2020-01-24 13:21:38 +00:00
#self.hackprefix="/home/rouxdo/Work/ungleich/uncloud/uncloud/hack/hackcloud" #TODO: Dominique testing
self.hackprefix=self.config.arguments['hackprefix']
2020-01-24 12:56:08 +00:00
self.qemu="/usr/bin/qemu-system-x86_64" #TODO: should be in config
self.accel="kvm" #TODO: should be config
2020-01-14 13:23:26 +00:00
2020-01-24 12:56:08 +00:00
self.vm = {}
2020-01-14 13:23:26 +00:00
2020-01-24 12:56:08 +00:00
#TODO: Touch later! (when necessary)
self.ifup = os.path.join(self.hackprefix, "ifup.sh")
self.ifdown = os.path.join(self.hackprefix, "ifdown.sh")
2020-01-14 13:23:26 +00:00
2020-01-24 12:56:08 +00:00
def commandline(self):
"""This method is used to trigger / create a vm from the cli"""
#TODO: read arguments from cli
#TODO: create etcd json object
self.vm['owner']= "nico"
self.vm['memory'] = self.config.arguments['memory']
self.vm['cores'] = self.config.arguments['cores']
2020-01-24 13:34:34 +00:00
self.vm['os_image'] = os.path.join(self.hackprefix, "alpine-virt-3.11.3-x86_64.iso")
2020-01-24 12:56:08 +00:00
self.create_template()
# mimics api call = this will already be in etcd
#self.vm['os_image'] = self.db.get("os_image")
self.create()
2020-01-14 13:23:26 +00:00
2020-01-24 12:56:08 +00:00
def create_template(self):
self.uuid = uuid.uuid4()
#TODO: This all should be generic
self.vm['uuid'] = str(self.uuid)
#self.vni_hex = "{:x}".format(self.config.arguments['vni'])
self.bridgedev = "br{}".format("{:x}".format(self.config.arguments['vni']))
2020-01-24 13:21:38 +00:00
#TODO: Enable sudo -- FIXME!
2020-01-24 12:56:08 +00:00
if self.config.arguments['use_sudo']:
self.sudo = "sudo "
2020-01-24 13:21:38 +00:00
else:
self.sudo = ""
2020-01-24 12:56:08 +00:00
self.mac=MAC(self.config)
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-24 12:56:08 +00:00
def create(self):
if not self.no_db:
self.db.set(str(self.vm['uuid']),
self.vm,
as_json=True)
self._execute_cmd(self.vm['commandline'], sudo=self.sudo, qemu=self.qemu, accel=self.accel, ifup=self.ifup, ifdown=self.ifdown)
2020-01-24 12:56:08 +00:00
#TODO: Add interface ifname to bridge brXX (via net.py: public function add iface to bridge)