simplifying stuff by removing the journal

This commit is contained in:
deflax 2016-03-30 05:50:03 +03:00
parent f8d7b32d0f
commit db5225b11d
7 changed files with 32 additions and 98 deletions

View file

@ -33,7 +33,7 @@ def validate(vmname, srvpass):
""" return vmid or false if credentials match something in clientdb. useful for authing extrnal admin panels """
try:
clientsdb = readclientsdb()
path = utils.find_key(clientsdb, vmname)
path = utils.get_path(clientsdb, vmname)
c_id = str(path[0])
v_id = str(path[1])
#check the returned path with forward query
@ -63,7 +63,7 @@ def validate(vmname, srvpass):
response = { 'vpsid':v_id, 'ticket':generated_ticket }
return response
else:
ioconfig.logger.warning('clients> {} (clientid: {}, vmid: {}) ACCESS DENIED!'.format(query, c_id, v_id)
ioconfig.logger.warning('clients> {} (clientid: {}, vmid: {}) ACCESS DENIED!'.format(query, c_id, v_id))
#cant compare password
return None
return None
@ -78,7 +78,7 @@ def setencpasswd(vmname, newpass):
try:
clientsdb = readclientsdb()
#print(clientsdb)
path = utils.find_key(clientsdb, vmname)
path = utils.get_path(clientsdb, vmname)
#print(path)
c_id = str(path[0])
v_id = str(path[1])
@ -100,7 +100,7 @@ def vmowner(vmid, vmname, verbose):
""" find the owner of the vm """
clientsdb = readclientsdb()
try:
clientid = utils.get_rec(clientsdb, str(vmid))[0]['ownerid']
clientid = utils.find_rec(clientsdb, str(vmid))[0]['ownerid']
clientname = clientsdb[str(clientid)]['name']
except:
raise

View file

@ -16,13 +16,13 @@ ipv4_min = 192.168.0.4
ipv4_max = 192.168.0.254
[slave_0]
name = CHANGEME
name = host.0CHANGEME
masterip = 192.168.0.2
password = CHANGEME
regionid = 0
[slave_1]
name = CHANGEME
name = host1.CHANGEME
masterip = 192.168.0.3
password = CHANGEME
regionid = 0

32
grid.py
View file

@ -15,7 +15,6 @@ import utils
import plugin
import ioconfig
import clientsdb
import journaldb
logger = ioconfig.logger
config = ioconfig.parser
@ -218,7 +217,7 @@ def generate_ipv4(region_id, how_many=1):
ip_max = len(region_ips) - 1
tested_ips = [] #initialize ip cache
requested_ips = []
all_ips = utils.get_rec(grid_data, 'ipaddr')
all_ips = utils.find_rec(grid_data, 'ipaddr')
for ips in range(int(how_many)):
counter = 0
@ -262,7 +261,7 @@ def generate_vmid():
tested_vmids = [] #initialize id cache
id_min = grid_data['vmid_min']
id_max = grid_data['vmid_max']
all_vmid = utils.get_rec(grid_data, 'vmid') #get all vmid values from the nested grid
all_vmid = utils.find_rec(grid_data, 'vmid') #get all vmid values from the nested grid
all_vmid = [ int(x) for x in all_vmid ] #convert values from str to int (its a vmid right?)
counter = 0
while True:
@ -318,27 +317,33 @@ def query_vm(req_vmid):
sync(False)
grid_data = readreal()
#compare requested vmid to all vmid's from the grid
#TODO: maybe we should also check the owner somehow
all_vmid = utils.get_rec(grid_data, 'vmid')
#search for the requested vmid
#TODO: maybe we should also check if the owner has permissions to manage it,
###### if for some reason the admin panel is compromised.
all_vmid = utils.find_rec(grid_data, 'vmid')
target = int(0)
for running_vmid in all_vmid:
if str(req_vmid) == str(running_vmid):
target = req_vmid #=runn?
target = running_vmid
break
else:
continue
if target == 0:
logger.error('grid> vmid {} cannot be found.' + str(req_vmid))
return "-1"
logger.error('grid> vmid {} cannot be found!' + str(req_vmid))
return int(-1), "None"
path = utils.get_path(grid_data, target)
region_id = path[0]
slave_id = path[1]
region_id, slave_id = journaldb.getjnode(target)
try:
vm_type = grid_data[str(region_id)][str(slave_id)][str(target)]['type']
except:
logger.error('{}> type is unknown!'.format(vm_id))
raise
#we should know them by now.
logger.info('{}> region={}, slave={}, type={} found.'.format(target, region_id, slave_id, vm_type))
return slave_id, vm_type
@ -386,11 +391,12 @@ def readreal():
if __name__ == '__main__':
print(sync())
#print(sync())
#print(query_region('Plovdiv, Bulgaria'))
#print(query_happiness(0))
#print(generate_ipv4(0,3))
#print(generate_vmid())
#print(query_slave_data(0))
#print(query_vm(483039))
print(query_vm(483039))
print(query_vm(147344))

View file

@ -4,8 +4,8 @@
#TODO: scan for all jsons and rewrite them.
echo " "
echo "JOURNAL:"
cat journal.json | python3 -m json.tool
echo "GRID:"
cat grid-real.json | python3 -m json.tool
echo " "
echo "CLIENTS:"
cat clients.json | python3 -m json.tool

View file

@ -1,70 +0,0 @@
# -*- coding: utf-8
#
# manage journaldb.json which is a table of vmid's as indexes update on vmcreate and
# values of region_id and slave_id. should support deletion of unused id's and be
# properly updated on vm migrations
#site
import json
#local
import ioconfig
import utils
def createjnode(vmid, regionid, slaveid, vmpasswd):
""" create new record into the journal. invoked on vm creation """
journaldb = readjournal()
if str(vmid) in journaldb:
ioconfig.logger.warning('journal> overwriting id[{}] !'.format(vmid))
else:
jnode = { str(vmid):{} }
journaldb.update(jnode)
ioconfig.logger.info ('journal> write: r[{}]s[{}]id[{}] => {}'.format(regionid, slaveid, vmid, vmpasswd))
jdata = { 'vmid':str(vmid), 'slaveid':str(slaveid), 'regionid':str(regionid), 'passwd':str(vmpasswd) }
journaldb[str(vmid)] = jdata
writedb(journaldb)
def getjnode(vmid):
""" query the database for records with requested vmid. invoked on user commands """
journaldb = readjournal()
try:
regionid = journaldb[str(vmid)]['regionid']
slaveid = journaldb[str(vmid)]['slaveid']
ioconfig.logger.info('journal> read: id[{}] => r[{}]s[{}]'.format(vmid, regionid, slaveid))
except:
ioconfig.logger.error('journal> invalid id[{}] !'.format(vmid))
else:
return regionid, slaveid
def getpass(vmid):
""" query the database for the recorded vmid password. """
journaldb = readjournal()
try:
vmpasswd = journaldb[str(vmid)]['passwd']
ioconfig.logger.info('journal> read: id[{}] => {}'.format(vmid, vmpasswd))
except:
ioconfig.logger.error('journal> invalid id[{}] !'.format(vmid))
else:
return vmpasswd
def readjournal():
""" read journal """
try:
with open('journal.json') as dbr:
journaldb = json.load(dbr)
dbr.close()
except:
journaldb = {}
ioconfig.logger.warning('journal> initializing...')
return journaldb
def writedb(journaldb):
""" write journal """
with open('journal.json', 'w') as dbw:
json.dump(journaldb, dbw)
dbw.close()

View file

@ -13,7 +13,6 @@ import socket
#local
import grid
import clientsdb
import journaldb
import utils
import ioconfig
import novnc
@ -116,12 +115,11 @@ def vmcreate(req):
onboot=1,
description=description)
#populate the client db and vm journal
#populate the client db
client_id = req['clientid']
client_name = req['clientname']
srv_pass = req['password']
clientsdb.addclient(vm_id, vm_name, client_id, client_name, srv_pass)
journaldb.createjnode(vm_id, region_id, slave_id)
#start the machihe
time.sleep(7) #wait few seconds for the slave to prepare the machine for initial run

View file

@ -24,7 +24,7 @@ def dict_merge(target, *args):
target[k] = deepcopy(v)
return target
def get_rec(search_dict, field):
def find_rec(search_dict, field):
"""
Takes a dict with nested lists and dicts,
and searches all dicts for a key of the field
@ -37,7 +37,7 @@ def get_rec(search_dict, field):
fields_found.append(value)
elif isinstance(value, dict):
results = get_rec(value, field)
results = find_rec(value, field)
for result in results:
fields_found.append(result)
@ -51,11 +51,11 @@ def get_rec(search_dict, field):
return fields_found
def find_key(search_dict, key):
def get_path(search_dict, key):
""" takes a nested dict and returns the path for the searched value """
for k,v in search_dict.items():
if isinstance(v,dict):
p = find_key(v,key)
p = get_path(v,key)
if p:
return [k] + p
elif v == key: