Add working --last-used-mac
{'create_vm': False, 'last_used_mac': True, 'get_new_mac': False, 'debug': False, 'conf_dir': '/home/nico/uncloud', 'etcd_host': 'etcd1.ungleich.ch', 'etcd_port': None, 'etcd_ca_cert': '/home/nico/vcs/ungleich-dot-cdist/files/etcd/ca.pem', 'etcd_cert_cert': '/home/nico/vcs/ungleich-dot-cdist/files/etcd/nico.pem', 'etcd_cert_key': '/home/nico/vcs/ungleich-dot-cdist/files/etcd/nico-key.pem'} 00:20:00:00:00:00 (venv) [19:02] diamond:uncloud% ./bin/uncloud-run-reinstall hack --etcd-host etcd1.ungleich.ch --etcd-ca-cert /home/nico/vcs/ungleich-dot-cdist/files/etcd/ca.pem --etcd-cert-cert /home/nico/vcs/ungleich-dot-cdist/files/etcd/nico.pem --etcd-cert-key /home/nico/vcs/ungleich-dot-cdist/files/etcd/nico-key.pem --last-used-mac
This commit is contained in:
parent
1b36c2f96f
commit
8078ffae5a
4 changed files with 132 additions and 102 deletions
|
@ -22,30 +22,75 @@
|
|||
|
||||
import etcd3
|
||||
import json
|
||||
import logging
|
||||
|
||||
from functools import wraps
|
||||
from uncloud import UncloudException
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def readable_errors(func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except etcd3.exceptions.ConnectionFailedError as e:
|
||||
raise UncloudException('Cannot connect to etcd: is etcd running and reachable? {}'.format(e))
|
||||
except etcd3.exceptions.ConnectionTimeoutError as e:
|
||||
raise UncloudException('etcd connection timeout. {}'.format(e))
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
class DB(object):
|
||||
def __init__(self, config):
|
||||
def __init__(self, config, prefix="/"):
|
||||
self.config = config
|
||||
self.prefix= '/nicohack/'
|
||||
|
||||
# Root for everything
|
||||
self.base_prefix= '/nicohack'
|
||||
|
||||
# Can be set from outside
|
||||
self.prefix = prefix
|
||||
|
||||
self.connect()
|
||||
|
||||
@readable_errors
|
||||
def connect(self):
|
||||
self._db_clients = []
|
||||
for endpoint in self.config.etcd_hosts:
|
||||
client = etcd3.client(host=endpoint, **self.config.etcd_args)
|
||||
self._db_clients.append(client)
|
||||
|
||||
def get_value(self, key):
|
||||
pass
|
||||
def realkey(self, key):
|
||||
return "{}{}/{}".format(self.base_prefix,
|
||||
self.prefix,
|
||||
key)
|
||||
|
||||
def set(self, key, value, store_as_json=False, **kwargs):
|
||||
if store_as_json:
|
||||
@readable_errors
|
||||
def get(self, key, as_json=False, **kwargs):
|
||||
value, _ = self._db_clients[0].get(self.realkey(key), **kwargs)
|
||||
|
||||
if as_json:
|
||||
value = json.loads(value)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
@readable_errors
|
||||
def set(self, key, value, as_json=False, **kwargs):
|
||||
if as_json:
|
||||
value = json.dumps(value)
|
||||
|
||||
key = "{}/{}".format(self.prefix, key)
|
||||
|
||||
# FIXME: iterate over clients in case of failure ?
|
||||
return self._db_clients[0].put(key, value, **kwargs)
|
||||
return self._db_clients[0].put(self.realkey(key), value, **kwargs)
|
||||
|
||||
@readable_errors
|
||||
def increment(key, **kwargs):
|
||||
with self._db_clients[0].lock(key) as lock:
|
||||
value = int(self.get(self.realkey(key), **kwargs))
|
||||
self.set(self.realkey(key), str(value + 1), **kwargs)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
endpoints = [ "https://etcd1.ungleich.ch:2379",
|
||||
|
|
|
@ -25,93 +25,65 @@ import logging
|
|||
import os.path
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
|
||||
from uncloud import UncloudException
|
||||
from uncloud.hack.db import DB
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
class Error(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Mac(object):
|
||||
def __init__(self):
|
||||
self.base_dir = "."
|
||||
class MAC(object):
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.db = DB(config, prefix="/mac")
|
||||
|
||||
self.prefix = 0x002000000000
|
||||
#self.prefix = "{:012x}".format(self._prefix)
|
||||
|
||||
self.free = self.read_file("mac-free")
|
||||
self.last = self.read_file("mac-last")
|
||||
|
||||
def read_file(self, filename):
|
||||
fname = os.path.join(self.base_dir, filename)
|
||||
ret = []
|
||||
|
||||
try:
|
||||
with open(fname, "r") as fd:
|
||||
ret = fd.readlines()
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
return ret
|
||||
|
||||
def append_to_file(self, text, filename):
|
||||
fname = os.path.join(self.base_dir, filename)
|
||||
with open(fname, "a+") as fd:
|
||||
fd.write("{}\n".format(text))
|
||||
|
||||
@staticmethod
|
||||
def validate_mac(mac):
|
||||
if not re.match(r'([0-9A-F]{2}[-:]){5}[0-9A-F]{2}$', mac, re.I):
|
||||
raise Error("Not a valid mac address: %s" % mac)
|
||||
|
||||
def free_append(self, mac):
|
||||
if mac in self.free:
|
||||
raise Error("Mac already in free database: %s" % mac)
|
||||
|
||||
self.append_to_file(mac, "mac-free")
|
||||
self.free = self.read_file("mac-free")
|
||||
def last_used_index(self):
|
||||
value = self.db.get("last_used_index")
|
||||
if not value:
|
||||
return 0
|
||||
return int(value)
|
||||
|
||||
def last_used_mac(self):
|
||||
return self.int_to_mac(self.prefix + self.last_used_index())
|
||||
|
||||
@staticmethod
|
||||
def int_to_mac(number):
|
||||
b = number.to_bytes(6, byteorder="big")
|
||||
return ':'.join(format(s, '02x') for s in b)
|
||||
|
||||
def getnext(self):
|
||||
def get_next(self, vmuuid=None, as_int=False):
|
||||
# if self.free:
|
||||
# return self.free.pop()
|
||||
|
||||
# if not self.prefix:
|
||||
# raise Error("Cannot generate address without prefix - use prefix-set")
|
||||
last_number = self.last_used_index()
|
||||
|
||||
if self.last:
|
||||
last_number = int(self.last[0], 16)
|
||||
|
||||
if last_number == int('0xffffff', 16):
|
||||
raise Error("Exhausted all possible mac addresses - try to free some")
|
||||
|
||||
next_number = last_number + 1
|
||||
else:
|
||||
next_number = 0
|
||||
# FIXME: compare to 48bit minus prefix length
|
||||
if last_number == int('0xffffff', 16):
|
||||
raise UncloudException("Exhausted all possible mac addresses - try to free some")
|
||||
|
||||
next_number = last_number + 1
|
||||
next_number_string = "{:012x}".format(next_number)
|
||||
|
||||
next_mac_number = self.prefix + next_number
|
||||
next_mac = self.int_to_mac(next_mac_number)
|
||||
|
||||
with open(os.path.join(self.base_dir, "mac-last"), "w+") as fd:
|
||||
fd.write("{}\n".format(next_number_string))
|
||||
db_entry = {}
|
||||
db_entry['vm_uuid'] = vmuuid
|
||||
db_entry['index'] = next_number
|
||||
db_entry['mac_address'] = next_mac
|
||||
|
||||
return next_mac
|
||||
self.db.set("used/{}".format(next_mac),
|
||||
db_entry)
|
||||
|
||||
@classmethod
|
||||
def commandline(cls):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
m = Mac()
|
||||
m.commandline()
|
||||
# print(m.free)
|
||||
#print(m.last)
|
||||
print(m.getnext())
|
||||
if as_int:
|
||||
return next_mac_number
|
||||
else:
|
||||
return next_mac
|
||||
|
|
|
@ -2,10 +2,13 @@ import argparse
|
|||
|
||||
from uncloud.hack.vm import VM
|
||||
from uncloud.hack.config import Config
|
||||
from uncloud.hack.mac import MAC
|
||||
|
||||
arg_parser = argparse.ArgumentParser('hack', add_help=False)
|
||||
#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('--get-new-mac', action='store_true')
|
||||
|
||||
|
||||
def main(arguments):
|
||||
|
@ -16,3 +19,11 @@ def main(arguments):
|
|||
print("Creating VM")
|
||||
vm = VM(config)
|
||||
vm.create()
|
||||
|
||||
if arguments['last_used_mac']:
|
||||
m = MAC(config)
|
||||
print(m.last_used_mac())
|
||||
|
||||
if arguments['get_new_mac']:
|
||||
m = MAC(config).get_next()
|
||||
print(m.last_used())
|
||||
|
|
|
@ -25,48 +25,50 @@ import uuid
|
|||
import os
|
||||
|
||||
from uncloud.hack.db import DB
|
||||
from uncloud.hack.mac import MAC
|
||||
|
||||
class VM(object):
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.db = DB(config)
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.db = DB(config, prefix="/vm")
|
||||
|
||||
self.hackprefix="/home/nico/vcs/uncloud/uncloud/hack/hackcloud"
|
||||
self.qemu="/usr/bin/qemu-system-x86_64"
|
||||
self.hackprefix="/home/nico/vcs/uncloud/uncloud/hack/hackcloud"
|
||||
self.qemu="/usr/bin/qemu-system-x86_64"
|
||||
self.accel="kvm"
|
||||
|
||||
self.vm = {}
|
||||
self.vm = {}
|
||||
|
||||
self.accel="kvm"
|
||||
|
||||
# self.mac=$(./mac-gen.py)
|
||||
self.mac="42:00:00:00:00:42"
|
||||
self.owner="nico"
|
||||
self.bridge="br100"
|
||||
self.owner="nico"
|
||||
self.bridge="br100"
|
||||
|
||||
self.ifup = os.path.join(self.hackprefix, "ifup.sh")
|
||||
self.ifdown = os.path.join(self.hackprefix, "ifdown.sh")
|
||||
self.ifup = os.path.join(self.hackprefix, "ifup.sh")
|
||||
self.ifdown = os.path.join(self.hackprefix, "ifdown.sh")
|
||||
|
||||
self.uuid = uuid.uuid4()
|
||||
self.vm['uuid'] = str(self.uuid)
|
||||
self.vm['memory']=1024
|
||||
self.vm['cores']=2
|
||||
self.vm['os_image'] = os.path.join(self.hackprefix, "alpine-virt-3.11.2-x86_64.iso")
|
||||
def create(self):
|
||||
self.uuid = uuid.uuid4()
|
||||
self.vm['uuid'] = str(self.uuid)
|
||||
self.vm['memory'] = 1024
|
||||
self.vm['cores'] = 2
|
||||
self.vm['os_image'] = os.path.join(self.hackprefix, "alpine-virt-3.11.2-x86_64.iso")
|
||||
|
||||
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={}".format(self.ifup, self.ifdown),
|
||||
"-device", "virtio-net-pci,netdev=netmain,id=net0,mac={}".format(self.mac)
|
||||
]
|
||||
self.mac=MAC().next()
|
||||
|
||||
def create(self):
|
||||
self.db.set("vm/{}".format(str(self.vm['uuid'])),
|
||||
self.vm, store_as_json=True)
|
||||
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={}".format(self.ifup, self.ifdown),
|
||||
"-device", "virtio-net-pci,netdev=netmain,id=net0,mac={}".format(self.mac)
|
||||
]
|
||||
|
||||
print(" ".join(self.vm['commandline']))
|
||||
subprocess.run(self.vm['commandline'])
|
||||
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