mac address string generator

This commit is contained in:
deflax 2016-11-05 04:07:04 +02:00
parent 1c648824f2
commit 4cb5d21bad
2 changed files with 28 additions and 6 deletions

27
grid.py
View file

@ -33,15 +33,14 @@ def querydb(cubeid):
dbf = open(dbfile, 'r')
data = json.load(dbf)
dbf.close()
logger.info('grid> {} --> {}'.format(dbfile, data))
logger.info('{}> <-- {}'.format(dbfile, data))
return data
except Exception as e:
logger.critical('grid> {}'.format(e))
logger.critical('{}> '.format(e))
pass
return None
def writedb(src_data):
""" create new metadata file """
try:
@ -139,6 +138,26 @@ def generate_ipv4(region_name, how_many=1):
return requested_ips
def genmac(int_value):
""" convert kinda long enough int to MAC string """
prefix_sum = sum(int(digit) for digit in str(int_value))
if (prefix_sum > 255):
prefix_hex = 'ff'
else:
prefix_hex = format(prefix_sum, 'x')
suffix = int(str(int_value)[-12:])
suffix_hex = format(suffix, 'x')
length = len(suffix_hex)
suffix_hex = suffix_hex.zfill(length+length%2)
addr = prefix_hex + suffix_hex
#logger.info('grid> mac-string {} genrated from: {} ({}->{}) ({}->{}) '.format(addr, int_value, prefix, prefix_hex, suffix, suffix_hex))
print('grid> mac-string {} genrated from: {} (sum {}->{}) ({}->{}) '.format(addr, int_value, prefix_sum, prefix_hex, suffix, suffix_hex))
return ':'.join(addr[i:i+2] for i in range(0,len(addr),2))
def generate_vmid():
""" analyzes cached grid data and return proposed vmid for new machines """
grid_data = readcache()
@ -192,6 +211,8 @@ def findDiff(d1, d2, path=""):
### DEPRECATED
def readreal():
""" read the current state and return its contents """
try:

View file

@ -82,8 +82,8 @@ def vmcreate(req):
for ip in ipv4_list:
ipv4_dict[str(ipidx)] = str(ip)
ipidx += 1
#TODO: aa:aa:aa <- generate from slave name to hex bb:bb:bb <- generate from cubeid to hex
macaddr = 'E2:C6:DE:BE:98:F2'
macaddr = grid.genmac(cubeid)
#metadata
deploy = { 'cube': int(cubeid),
@ -262,7 +262,7 @@ def vmrrd(cubeid):
return response
def vmvnc(vm_id):
def vmvnc(cubeid):
""" invoke vnc ticket """
slave_name, vm_type, vm_id = grid.query_vm(cubeid)
proxobject = auth(slave_name)
@ -281,6 +281,7 @@ def vmvnc(vm_id):
slaveip = ioconfig.parser.get(str(slave_name), 'ipv4')
#slaveport = socket['port']
slaveport = ticket['port']
slave_id = 1 #TODO: fix this
listenport = str(int(slaveport) + 1000 + (int(slave_id) * 100)) #TODO: max 100 parallel connections/slave.
myip = getmyip()