2020-01-12 12:41:54 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2012 Nico Schottelius (nico-cinv at schottelius.org)
|
|
|
|
#
|
|
|
|
# This file is part of cinv.
|
|
|
|
#
|
|
|
|
# cinv is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cinv is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cinv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import os.path
|
|
|
|
import os
|
|
|
|
import re
|
2020-01-14 18:02:15 +00:00
|
|
|
import json
|
2020-01-12 12:41:54 +00:00
|
|
|
|
2020-01-14 18:02:15 +00:00
|
|
|
from uncloud import UncloudException
|
|
|
|
from uncloud.hack.db import DB
|
2020-01-12 12:41:54 +00:00
|
|
|
|
2020-01-14 18:02:15 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2020-01-12 12:41:54 +00:00
|
|
|
|
|
|
|
|
2020-01-14 18:02:15 +00:00
|
|
|
class MAC(object):
|
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
|
|
|
self.db = DB(config, prefix="/mac")
|
2020-01-12 13:03:04 +00:00
|
|
|
|
2020-01-23 20:15:26 +00:00
|
|
|
self.prefix = 0x420000000000
|
|
|
|
self._number = 0 # Not set by default
|
2020-01-12 13:35:59 +00:00
|
|
|
|
2020-01-12 12:41:54 +00:00
|
|
|
@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)
|
|
|
|
|
2020-01-14 18:02:15 +00:00
|
|
|
def last_used_index(self):
|
|
|
|
value = self.db.get("last_used_index")
|
|
|
|
if not value:
|
2020-01-15 09:02:37 +00:00
|
|
|
self.db.set("last_used_index", "0")
|
|
|
|
value = self.db.get("last_used_index")
|
|
|
|
|
2020-01-14 18:02:15 +00:00
|
|
|
return int(value)
|
2020-01-12 12:41:54 +00:00
|
|
|
|
2020-01-14 18:02:15 +00:00
|
|
|
def last_used_mac(self):
|
|
|
|
return self.int_to_mac(self.prefix + self.last_used_index())
|
2020-01-12 12:41:54 +00:00
|
|
|
|
2020-01-23 20:15:26 +00:00
|
|
|
def to_colon_format(self):
|
|
|
|
b = self._number.to_bytes(6, byteorder="big")
|
2020-01-12 13:35:59 +00:00
|
|
|
return ':'.join(format(s, '02x') for s in b)
|
2020-01-12 12:41:54 +00:00
|
|
|
|
2020-01-23 20:15:26 +00:00
|
|
|
def to_str_format(self):
|
|
|
|
b = self._number.to_bytes(6, byteorder="big")
|
|
|
|
return ''.join(format(s, '02x') for s in b)
|
|
|
|
|
|
|
|
def create(self):
|
2020-01-14 18:02:15 +00:00
|
|
|
last_number = self.last_used_index()
|
2020-01-12 12:41:54 +00:00
|
|
|
|
2020-01-23 20:15:26 +00:00
|
|
|
if last_number == int('0xffffffff', 16):
|
2020-01-14 18:02:15 +00:00
|
|
|
raise UncloudException("Exhausted all possible mac addresses - try to free some")
|
2020-01-12 12:41:54 +00:00
|
|
|
|
2020-01-14 18:02:15 +00:00
|
|
|
next_number = last_number + 1
|
2020-01-23 20:15:26 +00:00
|
|
|
self._number = self.prefix + next_number
|
2020-01-12 12:41:54 +00:00
|
|
|
|
2020-01-23 20:15:26 +00:00
|
|
|
#next_number_string = "{:012x}".format(next_number)
|
|
|
|
#next_mac = self.int_to_mac(next_mac_number)
|
|
|
|
# db_entry = {}
|
|
|
|
# db_entry['vm_uuid'] = vmuuid
|
|
|
|
# db_entry['index'] = next_number
|
|
|
|
# db_entry['mac_address'] = next_mac
|
2020-01-12 12:41:54 +00:00
|
|
|
|
2020-01-15 09:02:37 +00:00
|
|
|
# should be one transaction
|
2020-01-23 20:15:26 +00:00
|
|
|
# self.db.increment("last_used_index")
|
|
|
|
# self.db.set("used/{}".format(next_mac),
|
|
|
|
# db_entry, as_json=True)
|
|
|
|
|
|
|
|
def __int__(self):
|
|
|
|
return self._number
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.to_str_format()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.to_colon_format()
|