refactored vm.py to create a VM
This commit is contained in:
parent
b5409552d8
commit
58daf8191e
3 changed files with 75 additions and 51 deletions
|
@ -36,7 +36,9 @@ log = logging.getLogger(__name__)
|
||||||
class MAC(object):
|
class MAC(object):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.db = DB(config, prefix="/mac")
|
self.no_db = self.config.arguments['no_db']
|
||||||
|
if not self.no_db:
|
||||||
|
self.db = DB(config, prefix="/mac")
|
||||||
|
|
||||||
self.prefix = 0x420000000000
|
self.prefix = 0x420000000000
|
||||||
self._number = 0 # Not set by default
|
self._number = 0 # Not set by default
|
||||||
|
@ -47,10 +49,14 @@ class MAC(object):
|
||||||
raise Error("Not a valid mac address: %s" % mac)
|
raise Error("Not a valid mac address: %s" % mac)
|
||||||
|
|
||||||
def last_used_index(self):
|
def last_used_index(self):
|
||||||
value = self.db.get("last_used_index")
|
if not self.no_db:
|
||||||
if not value:
|
|
||||||
self.db.set("last_used_index", "0")
|
|
||||||
value = self.db.get("last_used_index")
|
value = self.db.get("last_used_index")
|
||||||
|
if not value:
|
||||||
|
self.db.set("last_used_index", "0")
|
||||||
|
value = self.db.get("last_used_index")
|
||||||
|
|
||||||
|
else:
|
||||||
|
value = "0"
|
||||||
|
|
||||||
return int(value)
|
return int(value)
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,10 @@ 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('--memory', help="Size of memory (GB)", 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')
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -33,7 +37,7 @@ def main(arguments):
|
||||||
if arguments['create_vm']:
|
if arguments['create_vm']:
|
||||||
print("Creating VM")
|
print("Creating VM")
|
||||||
vm = VM(config)
|
vm = VM(config)
|
||||||
vm.create()
|
vm.commandline()
|
||||||
|
|
||||||
if arguments['last_used_mac']:
|
if arguments['last_used_mac']:
|
||||||
m = MAC(config)
|
m = MAC(config)
|
||||||
|
|
|
@ -29,58 +29,72 @@ from uncloud.hack.mac import MAC
|
||||||
|
|
||||||
class VM(object):
|
class VM(object):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.db = DB(config, prefix="/vm")
|
#TODO: Enable etcd lookup
|
||||||
|
self.no_db = self.config.arguments['no_db']
|
||||||
|
if not self.no_db:
|
||||||
|
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.qemu="/usr/bin/qemu-system-x86_64" #TODO: should be in config
|
self.hackprefix="/home/rouxdo/Work/ungleich/uncloud/uncloud/hack/hackcloud" #TODO: Dominique testing
|
||||||
self.accel="kvm" #TODO: should be config
|
self.qemu="/usr/bin/qemu-system-x86_64" #TODO: should be in config
|
||||||
|
self.accel="kvm" #TODO: should be config
|
||||||
|
|
||||||
self.vm = {}
|
self.vm = {}
|
||||||
|
|
||||||
|
#TODO: Touch later! (when necessary)
|
||||||
|
self.ifup = os.path.join(self.hackprefix, "ifup.sh")
|
||||||
|
self.ifdown = os.path.join(self.hackprefix, "ifdown.sh")
|
||||||
|
|
||||||
#TODO: this should be generic
|
def commandline(self):
|
||||||
self.vm['owner']="nico" #TODO: Should in config.arguments
|
"""This method is used to trigger / create a vm from the cli"""
|
||||||
#self.config['vni_hex'] = "{:x}".format(self.config.vni)
|
#TODO: read arguments from cli
|
||||||
#self.config['bridgedev'] = "br{}".format(self.config['vni_hex'])
|
#TODO: create etcd json object
|
||||||
self.vni_hex = "{:x}".format(self.config.arguments['vni'])
|
self.vm['owner']= "nico"
|
||||||
self.bridgedev = "br{}".format(self.vni_hex)
|
self.vm['memory'] = self.config.arguments['memory']
|
||||||
|
self.vm['cores'] = self.config.arguments['cores']
|
||||||
|
self.vm['os_image'] = os.path.join(self.hackprefix, "alpine-virt-3.11.2-x86_64.iso")
|
||||||
|
self.create_template()
|
||||||
|
# mimics api call = this will already be in etcd
|
||||||
|
#self.vm['os_image'] = self.db.get("os_image")
|
||||||
|
self.create()
|
||||||
|
|
||||||
|
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']))
|
||||||
|
|
||||||
|
#TODO: Enable sudo
|
||||||
|
if self.config.arguments['use_sudo']:
|
||||||
|
self.sudo = "sudo"
|
||||||
|
|
||||||
|
self.mac=MAC(self.config)
|
||||||
|
self.mac.create()
|
||||||
|
self.vm['ifname'] = "uc{}".format(self.mac.to_str_format())
|
||||||
|
|
||||||
|
#self.vm['commandline'] = [ "{}".format(self.sudo),
|
||||||
|
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.mac),
|
||||||
|
"-device", "virtio-net-pci,netdev=netmain,id=net0,mac={}".format(self.mac)
|
||||||
|
]
|
||||||
|
|
||||||
#TODO: Touch later! (when necessary)
|
|
||||||
self.ifup = os.path.join(self.hackprefix, "ifup.sh")
|
|
||||||
self.ifdown = os.path.join(self.hackprefix, "ifdown.sh")
|
|
||||||
|
|
||||||
def create(self):
|
def create(self):
|
||||||
self.uuid = uuid.uuid4()
|
if not self.no_db:
|
||||||
#TODO: This all should be generic
|
self.db.set(str(self.vm['uuid']),
|
||||||
self.vm['uuid'] = str(self.uuid)
|
self.vm,
|
||||||
self.vm['memory'] = 1024
|
as_json=True)
|
||||||
self.vm['cores'] = 2
|
|
||||||
self.vm['os_image'] = os.path.join(self.hackprefix, "alpine-virt-3.11.2-x86_64.iso")
|
|
||||||
|
|
||||||
self.mac=MAC(self.config)
|
print(" ".join(self.vm['commandline']))
|
||||||
self.mac.create()
|
subprocess.run(self.vm['commandline']) #TODO: run in background
|
||||||
self.vm['ifname'] = "uc{}".format(self.mac.to_str_format())
|
#TODO: Add interface ifname to bridge brXX (via net.py: public function add iface to bridge)
|
||||||
|
|
||||||
self.vm['commandline' ] = [ "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.mac,
|
|
||||||
"-device", "virtio-net-pci,netdev=netmain,id=net0,mac={}".format(self.mac)
|
|
||||||
]
|
|
||||||
|
|
||||||
# TODO: Add ip link command afterwards (rouxdo)
|
|
||||||
|
|
||||||
self.db.set(str(self.vm['uuid']),
|
|
||||||
self.vm,
|
|
||||||
as_json=True)
|
|
||||||
|
|
||||||
print(" ".join(self.vm['commandline']))
|
|
||||||
subprocess.run(self.vm['commandline'])
|
|
||||||
|
|
Loading…
Reference in a new issue