Skip to content
Snippets Groups Projects
Commit 8687114a authored by Auke Klazema's avatar Auke Klazema
Browse files

SW-598: Add username, password, port options revertDefaultTemplates.py

parent c8d933bf
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python #!/usr/bin/env python
#coding: iso-8859-15 # coding: iso-8859-15
import os,sys,time,pg import sys
import time
import pg
from optparse import OptionParser from optparse import OptionParser
import getpass
# #
# MAIN # MAIN
...@@ -10,11 +14,11 @@ if __name__ == '__main__': ...@@ -10,11 +14,11 @@ if __name__ == '__main__':
""" """
revertDefaultTemplates reverts each default template in OTDB to a previous revertDefaultTemplates reverts each default template in OTDB to a previous
version, when the default template has a matching older one. version, when the default template has a matching older one.
Two templates match when the templatename, the processType, the processSubtype and the Strategy values Two templates match when the templatename, the processType, the processSubtype and the
only differ in a leading '#' Strategy values only differ in a leading '#'
""" """
parser = OptionParser("Usage: %prog [options]" ) parser = OptionParser("Usage: %prog [options]")
parser.add_option("-D", "--database", parser.add_option("-D", "--database",
dest="dbName", dest="dbName",
type="string", type="string",
...@@ -27,6 +31,18 @@ if __name__ == '__main__': ...@@ -27,6 +31,18 @@ if __name__ == '__main__':
default="sasdb", default="sasdb",
help="Hostname of OTDB database") help="Hostname of OTDB database")
parser.add_option("-P", "--port",
dest="dbPort",
type="int",
default="5432",
help="Port of StationCoordinates database")
parser.add_option("-U", "--user",
dest="dbUser",
type="string",
default="postgres",
help="Username of StationCoordinates database")
# parse arguments # parse arguments
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
...@@ -39,62 +55,74 @@ if __name__ == '__main__': ...@@ -39,62 +55,74 @@ if __name__ == '__main__':
dbName = options.dbName dbName = options.dbName
dbHost = options.dbHost dbHost = options.dbHost
dbPort = options.dbPort
dbUser = options.dbUser
dbPassword = getpass.getpass()
# calling stored procedures only works from the pg module for some reason. # calling stored procedures only works from the pg module for some reason.
otdb = pg.connect(user="postgres", host=dbHost, dbname=dbName) otdb = pg.connect(user=dbUser, host=dbHost, dbname=dbName, port=dbPort, passwd=dbPassword)
# Give user escape possibility # Give user escape possibility
print "About to REVERT the default templates in database %s on host %s. Starting in 5 seconds..." % (dbName, dbHost) print "About to REVERT the default templates in database %s on host %s." % (dbName, dbHost)
print "Starting in 5 seconds..."
time.sleep(5) time.sleep(5)
# Wrap all modifications in a transaction, to avoid leaving behind a broken database # Wrap all modifications in a transaction, to avoid leaving behind a broken database
otdb.query("BEGIN") otdb.query("BEGIN")
print "=> Collecting info about default templates..." print "=> Collecting info about default templates..."
# built dictionary with componentID, nodeID, nodeName, version and treeName of the default templates like: # built dictionary with componentID, nodeID, nodeName, version and treeName of the
# {6171: (412, 2589, 'LOFAR', 40506, 'master template 4.5.6'), # default templates like:
# {6171: (412, 2589, 'LOFAR', 40506, 'master template 4.5.6'),
# 6121: (203, 426, 'LOFAR', 40000, 'test template')} # 6121: (203, 426, 'LOFAR', 40000, 'test template')}
oldTrees = {} oldTrees = {}
newTrees = {} newTrees = {}
dfltTmplInfo = {} dfltTmplInfo = {}
dfltTemplateIDs = otdb.query("select * from getDefaultTemplates()").dictresult() dfltTemplateIDs = otdb.query("select * from getDefaultTemplates()").dictresult()
for dfltTemplate in dfltTemplateIDs: for dfltTemplate in dfltTemplateIDs:
state = otdb.query("select state from getTreeInfo(%s, 'false')" % dfltTemplate['treeid']).getresult()[0][0] state = otdb.query("select state from getTreeInfo(%s, 'false')" %
dfltTemplate['treeid']).getresult()[0][0]
if state == 1200: if state == 1200:
oldTrees[dfltTemplate['name']] = { \ oldTrees[dfltTemplate['name']] = {
'processType' : dfltTemplate['processtype'], \ 'processType': dfltTemplate['processtype'],
'processSubtype' : dfltTemplate['processsubtype'], \ 'processSubtype': dfltTemplate['processsubtype'],
'strategy' : dfltTemplate['strategy'], \ 'strategy': dfltTemplate['strategy'],
'treeID' : dfltTemplate['treeid'] } 'treeID': dfltTemplate['treeid']}
else: else:
newTrees[dfltTemplate['name']] = { \ newTrees[dfltTemplate['name']] = {
'processType' : dfltTemplate['processtype'], \ 'processType': dfltTemplate['processtype'],
'processSubtype' : dfltTemplate['processsubtype'], \ 'processSubtype': dfltTemplate['processsubtype'],
'strategy' : dfltTemplate['strategy'], \ 'strategy': dfltTemplate['strategy'],
'treeID' : dfltTemplate['treeid']} 'treeID': dfltTemplate['treeid']}
# for each old default template make a new template # for each old default template make a new template
for treeName in newTrees: for treeName in newTrees:
if '#'+treeName in oldTrees: if '#'+treeName in oldTrees:
oTreeName = '#'+treeName oTreeName = '#'+treeName
if oldTrees[oTreeName]['processType'] == '#'+newTrees[treeName]['processType'] and \ if(oldTrees[oTreeName]['processType'] == '#'+newTrees[treeName]['processType'] and
oldTrees[oTreeName]['processSubtype'] == '#'+newTrees[treeName]['processSubtype'] and \ oldTrees[oTreeName]['processSubtype'] ==
oldTrees[oTreeName]['strategy'] == '#'+newTrees[treeName]['strategy']: '#'+newTrees[treeName]['processSubtype'] and
print newTrees[treeName]['treeID'],": ",treeName, newTrees[treeName]['processSubtype'], " <==> ", \ oldTrees[oTreeName]['strategy'] == '#'+newTrees[treeName]['strategy']):
oldTrees[oTreeName]['treeID'],": ",oTreeName, oldTrees[oTreeName]['processSubtype'] print newTrees[treeName]['treeID'], ": ", treeName, \
newTrees[treeName]['processSubtype'], " <==> ", \
oldTrees[oTreeName]['treeID'], ": ", oTreeName, \
oldTrees[oTreeName]['processSubtype']
# delete new tree # delete new tree
#print ("select * from deleteTree(1, %s)" % newTrees[treeName]['treeID']) # print ("select * from deleteTree(1, %s)" % newTrees[treeName]['treeID'])
otdb.query("select * from deleteTree(1, %s)" % newTrees[treeName]['treeID']) otdb.query("select * from deleteTree(1, %s)" % newTrees[treeName]['treeID'])
# set the old default template state to described (1200) # set the old default template state to described (1200)
oldTreeID = oldTrees[oTreeName]['treeID'] oldTreeID = oldTrees[oTreeName]['treeID']
#print ("select * from settreestate(1, %s, '100')" % (oldTreeID)) # print ("select * from settreestate(1, %s, '100')" % (oldTreeID))
otdb.query("select * from settreestate(1, %s, '100')" % (oldTreeID)) otdb.query("select * from settreestate(1, %s, '100')" % (oldTreeID))
# rename the old template with a '# ' before its original name # rename the old template with a '# ' before its original name
#print ("select * from assignTemplateName(1, %s, '%s')" % (oldTreeID, oTreeName[1:])) # print ("select * from assignTemplateName(1, %s, '%s')" % (oldTreeID, oTreeName[1:]))
otdb.query("select * from assignTemplateName(1, %s, '%s')" % (oldTreeID, oTreeName[1:])) otdb.query("select * from assignTemplateName(1, %s, '%s')" % (oldTreeID, oTreeName[1:]))
#print ("select * from assignProcessType (1, %s, '%s', '%s', '%s')" % (oldTreeID, oldTrees[oTreeName]['processType'][1:], oldTrees[oTreeName]['processSubtype'][1:], oldTrees[oTreeName]['strategy'][1:])) # print ("select * from assignProcessType (1, %s, '%s', '%s', '%s')" % (oldTreeID, oldTrees[oTreeName]['processType'][1:], oldTrees[oTreeName]['processSubtype'][1:], oldTrees[oTreeName]['strategy'][1:]))
otdb.query("select * from assignProcessType (1, %s, '%s', '%s', '%s')" % (oldTreeID, oldTrees[oTreeName]['processType'][1:], oldTrees[oTreeName]['processSubtype'][1:], oldTrees[oTreeName]['strategy'][1:])) otdb.query("select * from assignProcessType (1, %s, '%s', '%s', '%s')" %
(oldTreeID, oldTrees[oTreeName]['processType'][1:],
oldTrees[oTreeName]['processSubtype'][1:], oldTrees[oTreeName]['strategy'][1:]))
# Write all changes to the database # Write all changes to the database
otdb.query("COMMIT") otdb.query("COMMIT")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment