forked from uncloud/uncloud
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
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue