added hackerprefix argument, changed the commandline structure of vm to work better with sudo

This commit is contained in:
Dominique Roux 2020-01-24 17:12:50 +01:00
parent 8cc58726d0
commit 5d05e91335
2 changed files with 26 additions and 15 deletions

View file

@ -25,6 +25,7 @@ arg_parser.add_argument('--use-sudo', help="Use sudo for command requiring root!
arg_parser.add_argument('--memory', help="Size of memory (GB)", type=int) arg_parser.add_argument('--memory', help="Size of memory (GB)", type=int)
arg_parser.add_argument('--cores', help="Amount of CPU cores", type=int) arg_parser.add_argument('--cores', help="Amount of CPU cores", type=int)
arg_parser.add_argument('--no-db', help="Disable connection to etcd. For local testing only!", action='store_true') arg_parser.add_argument('--no-db', help="Disable connection to etcd. For local testing only!", action='store_true')
arg_parser.add_argument('--hackprefix', help="hackprefix, if you need it you know it (it's where the iso is located and ifup/down.sh")
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View file

@ -23,10 +23,14 @@
import subprocess import subprocess
import uuid import uuid
import os import os
import logging
from uncloud.hack.db import DB from uncloud.hack.db import DB
from uncloud.hack.mac import MAC from uncloud.hack.mac import MAC
log = logging.getLogger(__name__)
class VM(object): class VM(object):
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
@ -36,8 +40,9 @@ class VM(object):
self.db = DB(self.config, prefix="/vm") self.db = DB(self.config, prefix="/vm")
#TODO: Select generic #TODO: Select generic
self.hackprefix="/home/nico/vcs/uncloud/uncloud/hack/hackcloud" #TODO: Should be removed midterm #self.hackprefix="/home/nico/vcs/uncloud/uncloud/hack/hackcloud" #TODO: Should be removed midterm
#self.hackprefix="/home/rouxdo/Work/ungleich/uncloud/uncloud/hack/hackcloud" #TODO: Dominique testing #self.hackprefix="/home/rouxdo/Work/ungleich/uncloud/uncloud/hack/hackcloud" #TODO: Dominique testing
self.hackprefix=self.config.arguments['hackprefix']
self.qemu="/usr/bin/qemu-system-x86_64" #TODO: should be in config self.qemu="/usr/bin/qemu-system-x86_64" #TODO: should be in config
self.accel="kvm" #TODO: should be config self.accel="kvm" #TODO: should be config
@ -69,30 +74,36 @@ class VM(object):
#TODO: Enable sudo -- FIXME! #TODO: Enable sudo -- FIXME!
if self.config.arguments['use_sudo']: if self.config.arguments['use_sudo']:
self.sudo = "sudo" self.sudo = "sudo "
else: else:
self.sudo = "" self.sudo = ""
self.mac=MAC(self.config) self.mac=MAC(self.config)
self.mac.create() self.mac.create()
self.vm['mac'] = self.mac
self.vm['ifname'] = "uc{}".format(self.mac.to_str_format()) self.vm['ifname'] = "uc{}".format(self.mac.to_str_format())
# FIXME: TODO: turn this into a string and THEN # FIXME: TODO: turn this into a string and THEN
# .split() it later -- easier for using .format() # .split() it later -- easier for using .format()
#self.vm['commandline'] = [ "{}".format(self.sudo), #self.vm['commandline'] = [ "{}".format(self.sudo),
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}"
"{}".format(self.qemu), # self.vm['commandline'] = [ "{}".format(self.sudo),
"-name", "uncloud-{}".format(self.vm['uuid']), # "{}".format(self.qemu),
"-machine", "pc,accel={}".format(self.accel), # "-name", "uncloud-{}".format(self.vm['uuid']),
"-m", "{}".format(self.vm['memory']), # "-machine", "pc,accel={}".format(self.accel),
"-smp", "{}".format(self.vm['cores']), # "-m", "{}".format(self.vm['memory']),
"-uuid", "{}".format(self.vm['uuid']), # "-smp", "{}".format(self.vm['cores']),
"-drive", "file={},media=cdrom".format(self.vm['os_image']), # "-uuid", "{}".format(self.vm['uuid']),
"-netdev", "tap,id=netmain,script={},downscript={},ifname={}".format(self.ifup, self.ifdown, self.vm['ifname']), # "-drive", "file={},media=cdrom".format(self.vm['os_image']),
"-device", "virtio-net-pci,netdev=netmain,id=net0,mac={}".format(self.mac) # "-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'])
# ]
def _execute_cmd(self, cmd_string, **kwargs):
cmd = cmd_string.format(**self.vm, **kwargs)
log.info("Executing: {}".format(cmd))
subprocess.run(cmd.split())
def create(self): def create(self):
if not self.no_db: if not self.no_db:
@ -100,6 +111,5 @@ class VM(object):
self.vm, self.vm,
as_json=True) as_json=True)
print(" ".join(self.vm['commandline'])) self._execute_cmd(self.vm['commandline'], sudo=self.sudo, qemu=self.qemu, accel=self.accel, ifup=self.ifup, ifdown=self.ifdown)
subprocess.run(self.vm['commandline']) #TODO: run in background
#TODO: Add interface ifname to bridge brXX (via net.py: public function add iface to bridge) #TODO: Add interface ifname to bridge brXX (via net.py: public function add iface to bridge)