# -*- coding: utf-8 # # manage clientsdb.json #import site packages import json #import local packages import ioconfig import utils def addclient(vmid, vmname, clientid, clientname): """ add new client to the clientsdb.json """ clientsdb = readclientsdb() if str(clientid) in clientsdb: ioconfig.logger.info('clients> client ' + clientid + ' already exists. merging.') else: ioconfig.logger.info('clients> client ' + clientid + ' does not exist. creating.') vcard = { 'name':str(clientname) } newclient = { str(clientid):vcard } clientsdb.update(newclient) ioconfig.logger.info('clients> vmid ' + vmid + ' will be owned by ' + clientid + ' (' + clientname + ')') vmdata = { 'name':str(vmname), 'vmid':str(vmid), 'ownerid':str(clientid) } clientsdb[str(clientid)][str(vmid)] = vmdata writeclientsdb(clientsdb) def vmowner(vmid, vmname, verbose): """ find the owner of the vm """ clientsdb = readclientsdb() try: clientid = utils.get_rec(clientsdb, str(vmid))[0]['ownerid'] clientname = clientsdb[str(clientid)]['name'] except: raise clientid = '0' #unknown owner clientname = 'unknown' if verbose: ioconfig.logger.info('clients> the owner of ' + str(vmid) + ' (' + vmname + ') is ' + str(clientid) + ' (' + clientname + ')') return clientid def readclientsdb(): """ read client db """ try: with open('clients.json') as dbr: clientsdb = json.load(dbr) dbr.close() except: clientsdb = {} ioconfig.logger.warning('clients> initializing...') #writeclientsdb(clientsdb) return clientsdb def writeclientsdb(clientsdb): """ write db """ with open('clients.json', 'w') as dbw: json.dump(clientsdb, dbw) dbw.close()