proxmaster/journaldb.py

71 lines
2.1 KiB
Python
Raw Normal View History

2016-02-15 05:30:43 -05:00
# -*- 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
2016-02-19 21:26:04 -05:00
def createjnode(vmid, regionid, slaveid, vmpasswd):
2016-02-15 05:30:43 -05:00
""" 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)
2016-02-19 21:26:04 -05:00
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) }
2016-02-15 05:30:43 -05:00
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']
2016-02-19 21:26:04 -05:00
ioconfig.logger.info('journal> read: id[{}] => r[{}]s[{}]'.format(vmid, regionid, slaveid))
2016-02-15 05:30:43 -05:00
except:
ioconfig.logger.error('journal> invalid id[{}] !'.format(vmid))
else:
return regionid, slaveid
2016-02-19 21:26:04 -05:00
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
2016-02-15 05:30:43 -05:00
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()