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):
|
2017-10-24 11:47:37 -04:00
|
|
|
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):
|
2017-12-14 18:00:17 -05:00
|
|
|
""" scans all current db files and generate new id within a range between
|
|
|
|
1000 - 1999 for bridges,
|
|
|
|
2000 - 2999 for routers,
|
|
|
|
and 7000 - 7999 for application servers
|
|
|
|
Avoid any duplicates. """
|
|
|
|
if str(unit_type) == 'br':
|
|
|
|
full_list = list(range(1000,2999))
|
|
|
|
if str(unit_type) == 'lxc':
|
|
|
|
full_list = list(range(2000,2999))
|
|
|
|
if str(unit_type) == 'kvm':
|
|
|
|
full_list = list(range(7000,7999))
|
2017-10-27 11:17:01 -04:00
|
|
|
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-12-14 18:00:17 -05:00
|
|
|
#logger.info('{}[{}]> unit type: {} physical id: {}'.format(str(json['clientemail']), str(slave_name), str(unit_type), str(choice)))
|
|
|
|
logger.info('[{}]> unit type: {} physical id: {}'.format(str(slave_name), str(unit_type), str(choice)))
|
|
|
|
print(str(exclude_list))
|
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
|
|
|
|