refactoring

This commit is contained in:
deflax 2016-11-03 06:16:21 +02:00
parent 57dfc8f8ba
commit f0a1efe454

24
grid.py
View file

@ -29,8 +29,8 @@ def querydb(cubeid):
""" open a metadata file """ """ open a metadata file """
try: try:
dbfile = 'db/vm.{}.json'.format(src_data['cube']) dbfile = 'db/vm.{}.json'.format(src_data['cube'])
with open(dbfile, 'r' as dbf): dbf = open(dbfile, 'r')
data = json.load(dbf) data = json.load(dbf)
dbf.close() dbf.close()
logger.info('grid> {} --> {}'.format(dbfile, data)) logger.info('grid> {} --> {}'.format(dbfile, data))
except Exception as e: except Exception as e:
@ -43,8 +43,8 @@ def writedb(src_data):
""" create new metadata file """ """ create new metadata file """
try: try:
dbfile = 'db/vm.{}.json'.format(src_data['cube']) dbfile = 'db/vm.{}.json'.format(src_data['cube'])
with open(dbfile, 'w' as dbf: dbf = open(dbfile, 'w')
json.dump(src_data, dbf) json.dump(src_data, dbf)
dbf.close() dbf.close()
#TODO: send mail #TODO: send mail
logger.info('grid> {} <-- {}'.format(dbfile, src_data)) logger.info('grid> {} <-- {}'.format(dbfile, src_data))
@ -86,8 +86,8 @@ def query_happiness(region_id):
def generate_ipv4(region_name, how_many=1): def generate_ipv4(region_name, how_many=1):
""" this function should check the range, exclude deployed machines and return a list of available ips """ """ this function should check the range, exclude deployed machines and return a list of available ips """
ip_range_min = ioconfig.parser.get(region_name), 'ipv4_min') ip_range_min = ioconfig.parser.get(str(region_name), 'ipv4_min')
ip_range_max = ioconfig.parser.get(region_name), 'ipv4_max') ip_range_max = ioconfig.parser.get(str(region_name), 'ipv4_max')
region_ipset = netaddr.IPSet(netaddr.IPRange(ip_range_min, ip_range_max)) region_ipset = netaddr.IPSet(netaddr.IPRange(ip_range_min, ip_range_max))
region_ips = [] region_ips = []
@ -104,7 +104,7 @@ def generate_ipv4(region_name, how_many=1):
counter = 0 counter = 0
while True: while True:
if counter == 50: if counter == 50:
logger.error('region[{}]> ip range full'.format(str(region_id))) logger.error('region[{}]> ip range full'.format(str(region_name)))
return None return None
else: else:
counter += 1 counter += 1
@ -113,26 +113,26 @@ def generate_ipv4(region_name, how_many=1):
requested_ip = str(region_ips[requested_ip_index]) requested_ip = str(region_ips[requested_ip_index])
if requested_ip in tested_ips: if requested_ip in tested_ips:
logger.warning('region[{}]> ip addres {} already tested. cache: {}'.format(str(region_id), str(requested_ip), str(tested_ips))) logger.warning('region[{}]> ip addres {} already tested. cache: {}'.format(str(region_name), str(requested_ip), str(tested_ips)))
continue continue
if requested_ip in requested_ips: if requested_ip in requested_ips:
logger.warning('region[{}]> ip address {} already generated.'.format(str(region_id), str(requested_ip))) logger.warning('region[{}]> ip address {} already generated.'.format(str(region_name), str(requested_ip)))
tested_ips.append(requested_ip) tested_ips.append(requested_ip)
continue continue
if requested_ip in all_ips: if requested_ip in all_ips:
position = used_ips.index(requested_ip) position = used_ips.index(requested_ip)
logger.warning('region[{}]> ip address {} already exist. location: {}'.format(str(region_id), str(position))) logger.warning('region[{}]> ip address {} already exist. location: {}'.format(str(region_name), str(position)))
tested_ips.append(requested_ip) tested_ips.append(requested_ip)
continue continue
else: else:
tested_ips = [] #clear ip cache tested_ips = [] #clear ip cache
break break
logger.info('region[{}]> ip address {} selected.'.format(str(region_id), str(requested_ip))) logger.info('region[{}]> ip address {} selected.'.format(str(region_name), str(requested_ip)))
requested_ips.append(requested_ip) requested_ips.append(requested_ip)
logger.info('region[{}]> ip addresses {} selected.'.format(str(region_id), str(requested_ips))) logger.info('region[{}]> ip addresses {} selected.'.format(str(region_name), str(requested_ips)))
return requested_ips return requested_ips