proxmaster/grid.py

119 lines
3.7 KiB
Python
Raw Normal View History

2016-02-15 05:30:43 -05:00
#. -*- coding: utf-8
#
2017-11-05 15:21:06 -05:00
# vdc grid
2016-02-15 05:30:43 -05:00
#import site packages
import base64
import json
import re
import datetime
import random
2017-09-25 10:55:01 -04:00
import os
2016-02-15 05:30:43 -05:00
#import local packages
import utils
import plugin
import ioconfig
logger = ioconfig.logger
config = ioconfig.parser
2017-10-19 11:55:09 -04:00
def query(json):
data = read(json)
if json['type'] == 'kvm' or json['type'] == 'lxc':
2017-11-05 15:21:06 -05:00
return data['slave'], data['phy_id'], data['hostname'], data['clientemail']
if json['type'] == 'br':
return data['slave'], data['phy_id'], data['clientemail']
2016-02-15 05:30:43 -05:00
2017-10-19 11:55:09 -04:00
def read(data):
2017-11-05 15:21:06 -05:00
""" read a metadata file """
2016-11-03 00:05:51 -04:00
try:
2017-10-19 11:55:09 -04:00
dbfile = 'db/{}.{}.json'.format(data['type'], data['unit_id'])
2016-11-03 00:16:21 -04:00
dbf = open(dbfile, 'r')
data = json.load(dbf)
2016-11-03 00:08:43 -04:00
dbf.close()
2017-11-05 15:21:06 -05:00
#logger.info('grid> {}'.format(dbfile))
data['status'] = 'query_success'
2016-11-03 01:10:38 -04:00
return data
2016-11-03 00:05:51 -04:00
except Exception as e:
2017-11-05 15:21:06 -05:00
logger.critical('grid> read error: {}'.format(e))
2016-11-03 00:05:51 -04:00
pass
2016-11-03 01:10:38 -04:00
return None
2017-11-05 15:21:06 -05:00
def create(data):
""" write new metadata file """
2016-11-03 00:05:51 -04:00
try:
2017-10-19 11:55:09 -04:00
dbfile = 'db/{}.{}.json'.format(data['type'], data['unit_id'])
2017-11-05 15:21:06 -05:00
logger.info('{}'.format(data))
2016-11-03 00:16:21 -04:00
dbf = open(dbfile, 'w')
2017-10-19 11:55:09 -04:00
json.dump(data, dbf)
2016-11-03 00:05:51 -04:00
dbf.close()
2017-11-05 15:21:06 -05:00
logger.info('grid> {} successfully writen.'.format(dbfile))
2016-11-03 00:05:51 -04:00
except Exception as e:
2017-11-05 15:21:06 -05:00
logger.critical('grid> write error: {}'.format(e))
2016-02-15 05:30:43 -05:00
2017-11-05 15:21:06 -05:00
def delete(data):
2017-09-20 20:07:56 -04:00
""" remove metadata file """
2017-11-05 15:21:06 -05:00
dbfile = 'db/{}.{}.json'.format(data['type'], data['unit_id'])
2017-10-19 11:55:09 -04:00
#TODO: perhaps just move the datafile to an archive directory
2017-09-20 20:07:56 -04:00
os.remove(dbfile)
return None
2017-10-27 11:17:01 -04:00
def phyidgen(slave_name, unit_type):
""" scans all current db files and generate new id within a range between 1000 and 9999, and avoid any duplicates """
full_list = list(range(1000,10000))
exclude_list = []
directory = 'db/'
for dbfile in os.listdir(directory):
filename = os.fsdecode(dbfile)
if filename.startswith(str(unit_type)):
db_fullpath = os.path.join(directory, filename)
dbf = open(db_fullpath, 'r')
data = json.load(dbf)
if data['slave'] == str(slave_name):
2017-11-05 15:21:06 -05:00
exclude_list.append(data['phy_id'])
2017-10-27 11:17:01 -04:00
dbf.close()
valid_list = list(set(full_list) - set(exclude_list))
if len(valid_list) > 1:
choice = random.choice(valid_list)
2017-11-05 15:21:06 -05:00
logger.info('{}> physical id generated: {}'.format(slave_name, choice))
2017-10-27 11:17:01 -04:00
return choice
else:
2017-11-05 15:21:06 -05:00
logger.critical('{}> no free physical ids!'.format(slave_name))
2017-10-27 11:17:01 -04:00
return none
2017-10-19 11:55:09 -04:00
def analyze_happiness(region_id):
2016-02-15 05:30:43 -05:00
""" analyzes grid data for the reuqested region and returns proposed slave_id,
based on a "happiness" factor. happiness means alive and free :) """
grid_data = readcache()
grid_data = grid_data[str(region_id)]
all_slaves = []
for element in grid_data:
try:
if str(element) == grid_data[element]['id']:
all_slaves.append(element)
except:
continue
all_slaves = [ int(x) for x in all_slaves ] #convert values from str to int
alive_slaves = []
for slaveid in all_slaves:
if str(grid_data[str(slaveid)]['alive']) == 'up':
alive_slaves.append(slaveid)
2016-05-08 08:44:19 -04:00
logger.info('region[{}]> alive slaves {}'.format(str(region_id), str(alive_slaves)))
2016-02-15 05:30:43 -05:00
#happy_slave = random.choice(alive_slaves)
if len(alive_slaves) < 1:
2016-05-08 08:44:19 -04:00
logger.error('region[{}]> grid is full. add more slaves'.format(str(region_id)))
2016-02-15 05:30:43 -05:00
else:
2016-11-03 00:05:51 -04:00
happy_slave = 0 #TODO: analyze slaves and make informed decision.
2016-05-08 08:44:19 -04:00
logger.info('region[{}]> {} selected'.format(str(region_id), str(happy_slave)))
2016-02-15 05:30:43 -05:00
return happy_slave
if __name__ == '__main__':
#print(query_happiness(0))
2017-10-27 11:17:01 -04:00
print(phyidgen('warrior', 'kvm'))
2016-02-15 05:30:43 -05:00