Merge branch 'master' of code.ungleich.ch:uncloud/uncloud
This commit is contained in:
commit
bdbf26cfd4
3 changed files with 117 additions and 48 deletions
|
@ -10,7 +10,6 @@ from uncloud import UncloudException
|
||||||
|
|
||||||
arg_parser = argparse.ArgumentParser('hack', add_help=False)
|
arg_parser = argparse.ArgumentParser('hack', add_help=False)
|
||||||
#description="Commands that are unfinished - use at own risk")
|
#description="Commands that are unfinished - use at own risk")
|
||||||
arg_parser.add_argument('--create-vm', action='store_true')
|
|
||||||
arg_parser.add_argument('--last-used-mac', action='store_true')
|
arg_parser.add_argument('--last-used-mac', action='store_true')
|
||||||
arg_parser.add_argument('--get-new-mac', action='store_true')
|
arg_parser.add_argument('--get-new-mac', action='store_true')
|
||||||
|
|
||||||
|
@ -22,8 +21,17 @@ arg_parser.add_argument('--vni', help="VXLAN ID (decimal)", type=int)
|
||||||
arg_parser.add_argument('--run-dns-ra', action='store_true',
|
arg_parser.add_argument('--run-dns-ra', action='store_true',
|
||||||
help="Provide router advertisements and DNS resolution via dnsmasq")
|
help="Provide router advertisements and DNS resolution via dnsmasq")
|
||||||
arg_parser.add_argument('--use-sudo', help="Use sudo for command requiring root!", action='store_true')
|
arg_parser.add_argument('--use-sudo', help="Use sudo for command requiring root!", action='store_true')
|
||||||
|
|
||||||
|
arg_parser.add_argument('--create-vm', action='store_true')
|
||||||
|
arg_parser.add_argument('--destroy-vm', action='store_true')
|
||||||
|
arg_parser.add_argument('--get-vm-status', action='store_true')
|
||||||
|
arg_parser.add_argument('--get-vm-vnc', action='store_true')
|
||||||
|
arg_parser.add_argument('--list-vms', action='store_true')
|
||||||
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('--image', help="Path (under hackprefix) to OS image")
|
||||||
|
arg_parser.add_argument('--uuid', help="VM UUID")
|
||||||
|
|
||||||
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")
|
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")
|
||||||
|
|
||||||
|
@ -32,13 +40,27 @@ log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def main(arguments):
|
def main(arguments):
|
||||||
log.debug("args={}".format(arguments))
|
|
||||||
config = Config(arguments)
|
config = Config(arguments)
|
||||||
|
|
||||||
if arguments['create_vm']:
|
if arguments['create_vm']:
|
||||||
print("Creating VM")
|
|
||||||
vm = VM(config)
|
vm = VM(config)
|
||||||
vm.commandline()
|
vm.create()
|
||||||
|
|
||||||
|
if arguments['destroy_vm']:
|
||||||
|
vm = VM(config)
|
||||||
|
vm.stop()
|
||||||
|
|
||||||
|
if arguments['get_vm_status']:
|
||||||
|
vm = VM(config)
|
||||||
|
vm.status()
|
||||||
|
|
||||||
|
if arguments['get_vm_vnc']:
|
||||||
|
vm = VM(config)
|
||||||
|
vm.vnc_addr()
|
||||||
|
|
||||||
|
if arguments['list_vms']:
|
||||||
|
vm = VM(config)
|
||||||
|
vm.list()
|
||||||
|
|
||||||
if arguments['last_used_mac']:
|
if arguments['last_used_mac']:
|
||||||
m = MAC(config)
|
m = MAC(config)
|
||||||
|
|
|
@ -17,8 +17,21 @@
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with uncloud. If not, see <http://www.gnu.org/licenses/>.
|
# along with uncloud. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# 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
|
||||||
|
#
|
||||||
|
# Get VM status:
|
||||||
|
# `uncloud hack --hackprefix /tmp/hackcloud --get-vm-status --uuid my-vm-uuid`
|
||||||
|
#
|
||||||
|
# Stop a VM:
|
||||||
|
# `uncloud hack --hackprefix /tmp/hackcloud --destroy-vm --uuid my-vm-uuid`
|
||||||
|
# ``
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import uuid
|
import uuid
|
||||||
|
@ -27,57 +40,75 @@ 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
|
||||||
|
from uncloud.vmm import VMM
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
log.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
class VM(object):
|
class VM(object):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
#TODO: Enable etcd lookup
|
#TODO: Enable etcd lookup
|
||||||
self.no_db = self.config.arguments['no_db']
|
self.no_db = self.config.arguments['no_db']
|
||||||
if not self.no_db:
|
if not self.no_db:
|
||||||
self.db = DB(self.config, prefix="/vm")
|
self.db = DB(self.config, prefix="/vm")
|
||||||
|
|
||||||
#TODO: Select generic
|
# General CLI arguments.
|
||||||
#self.hackprefix="/home/nico/vcs/uncloud/uncloud/hack/hackcloud" #TODO: Should be removed midterm
|
self.hackprefix = self.config.arguments['hackprefix']
|
||||||
#self.hackprefix="/home/rouxdo/Work/ungleich/uncloud/uncloud/hack/hackcloud" #TODO: Dominique testing
|
self.uuid = self.config.arguments['uuid']
|
||||||
self.hackprefix=self.config.arguments['hackprefix']
|
self.memory = self.config.arguments['memory'] or '1024M'
|
||||||
self.qemu="/usr/bin/qemu-system-x86_64" #TODO: should be in config
|
self.cores = self.config.arguments['cores'] or 1
|
||||||
self.accel="kvm" #TODO: should be config
|
if self.config.arguments['image']:
|
||||||
|
self.image = os.path.join(self.hackprefix, self.config.arguments['image'])
|
||||||
|
else:
|
||||||
|
self.image = None
|
||||||
|
|
||||||
self.vm = {}
|
# External components.
|
||||||
|
self.vmm = VMM(vmm_backend=self.hackprefix)
|
||||||
|
self.mac = MAC(self.config)
|
||||||
|
|
||||||
#TODO: Touch later! (when necessary)
|
# Harcoded & generated values.
|
||||||
|
self.owner = 'uncoud'
|
||||||
|
self.image_format='qcow2'
|
||||||
|
self.accel = 'kvm'
|
||||||
|
self.threads = 1
|
||||||
self.ifup = os.path.join(self.hackprefix, "ifup.sh")
|
self.ifup = os.path.join(self.hackprefix, "ifup.sh")
|
||||||
self.ifdown = os.path.join(self.hackprefix, "ifdown.sh")
|
self.ifdown = os.path.join(self.hackprefix, "ifdown.sh")
|
||||||
|
self.ifname = "uc{}".format(self.mac.to_str_format())
|
||||||
|
|
||||||
def commandline(self):
|
def get_qemu_args(self):
|
||||||
"""This method is used to trigger / create a vm from the cli"""
|
command = (
|
||||||
#TODO: read arguments from cli
|
"-name {owner}-{name}"
|
||||||
#TODO: create etcd json object
|
" -machine pc,accel={accel}"
|
||||||
self.vm['owner']= "nico"
|
" -drive file={image},format={image_format},if=virtio"
|
||||||
self.vm['memory'] = self.config.arguments['memory']
|
" -device virtio-rng-pci"
|
||||||
self.vm['cores'] = self.config.arguments['cores']
|
" -m {memory} -smp cores={cores},threads={threads}"
|
||||||
self.vm['os_image'] = os.path.join(self.hackprefix, "alpine-virt-3.11.3-x86_64.iso")
|
" -netdev tap,id=netmain,script={ifup},downscript={ifdown},ifname={ifname}"
|
||||||
self.create_template()
|
" -device virtio-net-pci,netdev=netmain,id=net0,mac={mac}"
|
||||||
# mimics api call = this will already be in etcd
|
).format(
|
||||||
#self.vm['os_image'] = self.db.get("os_image")
|
owner=self.owner, name=self.uuid,
|
||||||
self.create()
|
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
|
||||||
|
)
|
||||||
|
|
||||||
def create_template(self):
|
return command.split(" ")
|
||||||
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']))
|
|
||||||
|
|
||||||
#TODO: Enable sudo -- FIXME!
|
def create(self):
|
||||||
if self.config.arguments['use_sudo']:
|
# New VM: new UUID, new MAC.
|
||||||
self.sudo = "sudo "
|
self.uuid = str(uuid.uuid4())
|
||||||
else:
|
self.mac.create()
|
||||||
self.sudo = ""
|
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
self.mac=MAC(self.config)
|
self.mac=MAC(self.config)
|
||||||
self.mac.create()
|
self.mac.create()
|
||||||
|
@ -105,11 +136,27 @@ class VM(object):
|
||||||
log.info("Executing: {}".format(cmd))
|
log.info("Executing: {}".format(cmd))
|
||||||
subprocess.run(cmd.split())
|
subprocess.run(cmd.split())
|
||||||
|
|
||||||
def create(self):
|
def stop(self):
|
||||||
if not self.no_db:
|
if not self.uuid:
|
||||||
self.db.set(str(self.vm['uuid']),
|
print("Please specific an UUID with the --uuid flag.")
|
||||||
self.vm,
|
exit(1)
|
||||||
as_json=True)
|
|
||||||
|
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))
|
||||||
|
|
||||||
|
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())
|
||||||
|
|
||||||
self._execute_cmd(self.vm['commandline'], sudo=self.sudo, qemu=self.qemu, accel=self.accel, ifup=self.ifup, ifdown=self.ifdown)
|
|
||||||
#TODO: Add interface ifname to bridge brXX (via net.py: public function add iface to bridge)
|
|
||||||
|
|
|
@ -125,7 +125,7 @@ class VMM:
|
||||||
os.makedirs(self.socket_dir, exist_ok=True)
|
os.makedirs(self.socket_dir, exist_ok=True)
|
||||||
|
|
||||||
def is_running(self, uuid):
|
def is_running(self, uuid):
|
||||||
sock_path = os.path.join(self.vmm_backend, uuid)
|
sock_path = os.path.join(self.socket_dir, uuid)
|
||||||
try:
|
try:
|
||||||
sock = socket.socket(socket.AF_UNIX)
|
sock = socket.socket(socket.AF_UNIX)
|
||||||
sock.connect(sock_path)
|
sock.connect(sock_path)
|
||||||
|
@ -163,7 +163,7 @@ class VMM:
|
||||||
qmp_arg = (
|
qmp_arg = (
|
||||||
"-qmp",
|
"-qmp",
|
||||||
"unix:{},server,nowait".format(
|
"unix:{},server,nowait".format(
|
||||||
join_path(self.vmm_backend, uuid)
|
join_path(self.socket_dir, uuid)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
vnc_arg = (
|
vnc_arg = (
|
||||||
|
@ -212,7 +212,7 @@ class VMM:
|
||||||
def execute_command(self, uuid, command, **kwargs):
|
def execute_command(self, uuid, command, **kwargs):
|
||||||
# execute_command -> sucess?, output
|
# execute_command -> sucess?, output
|
||||||
try:
|
try:
|
||||||
with VMQMPHandles(os.path.join(self.vmm_backend, uuid)) as (
|
with VMQMPHandles(os.path.join(self.socket_dir, uuid)) as (
|
||||||
sock_handle,
|
sock_handle,
|
||||||
file_handle,
|
file_handle,
|
||||||
):
|
):
|
||||||
|
@ -255,8 +255,8 @@ class VMM:
|
||||||
def discover(self):
|
def discover(self):
|
||||||
vms = [
|
vms = [
|
||||||
uuid
|
uuid
|
||||||
for uuid in os.listdir(self.vmm_backend)
|
for uuid in os.listdir(self.socket_dir)
|
||||||
if not isdir(join_path(self.vmm_backend, uuid))
|
if not isdir(join_path(self.socket_dir, uuid))
|
||||||
]
|
]
|
||||||
return vms
|
return vms
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue