X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=monitor%2Fwrapper%2Fplccache.py;h=ac23f1b92aee663c56658ff0326587cf38bfe5fc;hb=5c41a475f7d198274e46f2c5e6f033f06794288b;hp=0645b186840161674f878bf1606fcf9500c5e0f7;hpb=c9d06f3b274ecbc092a0b3eb1f5ceb6c0f734aad;p=monitor.git diff --git a/monitor/wrapper/plccache.py b/monitor/wrapper/plccache.py index 0645b18..ac23f1b 100755 --- a/monitor/wrapper/plccache.py +++ b/monitor/wrapper/plccache.py @@ -3,6 +3,7 @@ import sys from monitor.wrapper import plc from monitor.database.info.model import * +import profile def dsites_from_lsites(l_sites): d_sites = {} @@ -61,23 +62,39 @@ plcdb_lb2hn = None plcdb_id2lb = None def init(): + import traceback + #print "IMPORTING PLCCACHE: ", + #traceback.print_stack() global l_sites global l_nodes global l_pcus global plcdb_hn2lb global plcdb_lb2hn global plcdb_id2lb + print "initing plccache" + print "collecting plcsites" dbsites = PlcSite.query.all() l_sites = [ s.plc_site_stats for s in dbsites ] + print "collecting plcnodes" dbnodes = PlcNode.query.all() l_nodes = [ s.plc_node_stats for s in dbnodes ] - dbpcus = PlcPCU.query.all() - l_pcus = [ s.plc_pcu_stats for s in dbpcus ] - + print "collecting plcpcus" + dbpcus = PlcPCU2.query.all() + l_pcus = [] + for s in dbpcus: + pcu = {} + for k in ['username', 'protocol', 'node_ids', 'ip', + 'pcu_id', 'hostname', 'site_id', 'notes', + 'model', 'password', 'ports']: + pcu[k] = getattr(s, k) + l_pcus.append(pcu) + + print "building id2lb" (d_sites,id2lb) = dsites_from_lsites(l_sites) + print "building lb2hn" (plcdb, hn2lb, lb2hn) = dsn_from_dsln(d_sites, id2lb, l_nodes) plcdb_hn2lb = hn2lb @@ -108,15 +125,32 @@ def GetSitesByName(sitelist): ret.append(site.plc_site_stats) return ret +def GetSitesById(idlist): + ret = [] + for site_id in idlist: + site = PlcSite.get_by(site_id=site_id) + ret.append(site.plc_site_stats) + return ret + +def deleteExtra(l_plc, objectClass=PlcSite, dbKey='loginbase', plcKey='login_base'): + dbobjs = objectClass.query.all() + dbobj_key = [ getattr(s, dbKey) for s in dbobjs ] + plcobj_key = [ s[plcKey] for s in l_plc ] + extra_key = set(dbobj_key) - set(plcobj_key) + for obj in extra_key: + print "deleting %s" % obj + dbobj = objectClass.get_by(**{dbKey : obj}) + dbobj.delete() + def sync(): l_sites = plc.api.GetSites({'peer_id':None}, ['login_base', 'site_id', 'abbreviated_name', 'latitude', 'longitude', 'max_slices', 'slice_ids', 'node_ids', 'enabled', 'date_created' ]) l_nodes = plc.api.GetNodes({'peer_id':None}, - ['hostname', 'node_id', 'ports', 'site_id', - 'version', 'last_updated', 'date_created', - 'last_contact', 'pcu_ids', 'nodenetwork_ids']) + ['hostname', 'node_id', 'ports', 'site_id', 'boot_state', + 'version', 'last_updated', 'date_created', 'key', + 'last_contact', 'pcu_ids', 'interface_ids']) l_pcus = plc.api.GetPCUs() print "sync sites" @@ -125,8 +159,21 @@ def sync(): dbsite.loginbase = site['login_base'] dbsite.date_checked = datetime.now() dbsite.plc_site_stats = site - #dbsite.flush() - # TODO: delete old records. + deleteExtra(l_sites, PlcSite, 'loginbase', 'login_base') + deleteExtra(l_sites, HistorySiteRecord, 'loginbase', 'login_base') + session.flush() + + print "sync pcus" + for pcu in l_pcus: + dbpcu = PlcPCU2.findby_or_create(pcu_id=pcu['pcu_id']) + dbpcu.date_checked = datetime.now() + for key in pcu.keys(): + print "setting %s = %s" % (key, pcu[key]) + setattr(dbpcu, key, pcu[key]) + + deleteExtra(l_pcus, PlcPCU2, 'pcu_id', 'pcu_id') + deleteExtra(l_pcus, HistoryPCURecord, 'plc_pcuid', 'pcu_id') + deleteExtra(l_pcus, FindbadPCURecord, 'plc_pcuid', 'pcu_id') session.flush() print "sync nodes" @@ -135,17 +182,9 @@ def sync(): dbnode.hostname = node['hostname'] dbnode.date_checked = datetime.now() dbnode.plc_node_stats = node - #dbnode.flush() - # TODO: delete old records. - session.flush() - - print "sync pcus" - for pcu in l_pcus: - dbpcu = PlcPCU.findby_or_create(pcu_id=pcu['pcu_id']) - dbpcu.date_checked = datetime.now() - dbpcu.plc_pcu_stats = pcu - #dbpcu.flush() - # TODO: delete old records. + deleteExtra(l_nodes, PlcNode, 'hostname', 'hostname') + deleteExtra(l_nodes, HistoryNodeRecord, 'hostname', 'hostname') + deleteExtra(l_nodes, FindbadNodeRecord, 'hostname', 'hostname') session.flush() init()