From 94dad7c9b6ae8bd5e5bf4b035b582ce7d4a44a01 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 12 Jan 2020 14:35:59 +0100 Subject: [PATCH] Add script to generate mac addresses --- uncloud/hack/hackcloud/mac-gen.py | 96 +++++++++---------------------- uncloud/hack/hackcloud/mac-last | 1 + 2 files changed, 28 insertions(+), 69 deletions(-) mode change 100644 => 100755 uncloud/hack/hackcloud/mac-gen.py create mode 100644 uncloud/hack/hackcloud/mac-last diff --git a/uncloud/hack/hackcloud/mac-gen.py b/uncloud/hack/hackcloud/mac-gen.py old mode 100644 new mode 100755 index f2a5db0..e2b4bc5 --- a/uncloud/hack/hackcloud/mac-gen.py +++ b/uncloud/hack/hackcloud/mac-gen.py @@ -36,7 +36,8 @@ class Mac(object): def __init__(self): self.base_dir = "." - self._prefix = "02:00" + self.prefix = 0x002000000000 + #self.prefix = "{:012x}".format(self._prefix) self.free = self.read_file("mac-free") self.last = self.read_file("mac-last") @@ -53,6 +54,11 @@ class Mac(object): 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): @@ -62,30 +68,24 @@ class Mac(object): if mac in self.free: raise Error("Mac already in free database: %s" % mac) - self._init_base_dir() - self.free.append(mac) + self.append_to_file(mac, "mac-free") + self.free = self.read_file("mac-free") + @staticmethod - def get_base_dir(): - return cinv.get_base_dir("db/mac") + def int_to_mac(number): + b = number.to_bytes(6, byteorder="big") + return ':'.join(format(s, '02x') for s in b) - @classmethod - def exists(cls): - return os.path.exists(cls.get_base_dir()) + def getnext(self): +# if self.free: +# return self.free.pop() - def get_next(self): - self._init_base_dir() - - if self.free: - return self.free.pop() - - if not self.prefix: - raise Error("Cannot generate address without prefix - use prefix-set") +# if not self.prefix: +# raise Error("Cannot generate address without prefix - use prefix-set") if self.last: - suffix = re.search(r'([0-9A-F]{2}[-:]){2}[0-9A-F]{2}$', self.last, re.I) - last_number_hex = "0x%s" % suffix.group().replace(":", "") - last_number = int(last_number_hex, 16) + last_number = int(self.last[0], 16) if last_number == int('0xffffff', 16): raise Error("Exhausted all possible mac addresses - try to free some") @@ -94,60 +94,16 @@ class Mac(object): else: next_number = 0 - next_number_hex = "%0.6x" % next_number - next_suffix = "%s:%s:%s" % (next_number_hex[0:2], next_number_hex[2:4], next_number_hex[4:6]) + next_number_string = "{:012x}".format(next_number) - next_mac = "%s:%s" % (self.prefix, next_suffix) + next_mac_number = self.prefix + next_number + next_mac = self.int_to_mac(next_mac_number) - self.last = next_mac + with open(os.path.join(self.base_dir, "mac-last"), "w+") as fd: + fd.write("{}\n".format(next_number_string)) return next_mac - - @property - def prefix(self): - return self._prefix - - @prefix.setter - def prefix(self, prefix): - if not re.match(r'([0-9A-F]{2}[-:]){2}[0-9A-F]{2}$', prefix, re.I): - raise Error("Wrong mac address format - use 00:11:22") - - self._init_base_dir() - self._prefix = prefix - - @classmethod - def commandline_generate(cls, args): - mac = Mac() - print(mac.get_next()) - - @classmethod - def commandline_free_add(cls, args): - mac = Mac() - mac.validate_mac(args.address) - mac.free_append(args.address) - - @classmethod - def commandline_free_list(cls, args): - mac = Mac() - for mac in mac.free: - print(mac) - - @classmethod - def commandline_prefix_set(cls, args): - mac = Mac() - mac.prefix = args.prefix - - @classmethod - def commandline_prefix_get(cls, args): - mac = cls() - print(mac.prefix) - - @classmethod - def commandline_add(cls, args): - host = cls(fqdn=args.fqdn) - host.host_type = args.type - @classmethod def commandline(cls): pass @@ -156,4 +112,6 @@ class Mac(object): if __name__ == '__main__': m = Mac() m.commandline() - print(m.free) + # print(m.free) + #print(m.last) + print(m.getnext()) diff --git a/uncloud/hack/hackcloud/mac-last b/uncloud/hack/hackcloud/mac-last new file mode 100644 index 0000000..df32b47 --- /dev/null +++ b/uncloud/hack/hackcloud/mac-last @@ -0,0 +1 @@ +000000000006