Guest User

Untitled

a guest
May 2nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # script to migrate Nimbus 2.2/2.3 accounting db to 2.4 db
  4. import os
  5. import re
  6. import sys
  7. import time
  8. import shutil
  9. import tempfile
  10. from subprocess import Popen, PIPE
  11.  
  12. def update_db(ij_path, old_db, new_db):
  13. """
  14. ij_path -- assumes that this exists
  15. old_db -- path to 2.2 or 2.3 accounting db
  16. new_db -- path to 2.4 accounting db
  17. """
  18.  
  19. def _run_sql_on_db(ij_path, database, sql, user=None, password=None):
  20. script = "connect 'jdbc:derby:%s' " % database
  21. if user and password:
  22. script += "user '%s' " % user
  23. script += "password '%s' " % password
  24. script += ";"
  25. script += sql
  26. script += "disconnect; exit;"
  27.  
  28. (script_file, script_filename) = tempfile.mkstemp()
  29. os.write(script_file, script)
  30. os.close(script_file)
  31.  
  32. ij_args = ij_path.split()
  33. ij_args.append(script_filename)
  34. output = Popen(ij_args, stdout=PIPE).communicate()[0]
  35. os.remove(script_filename)
  36.  
  37. return output
  38.  
  39. # Check that the databases exist
  40. if not os.path.exists(old_db):
  41. print >> sys.stderr, "Error in db-update: %s doesn't exist" % old_db
  42. return 1
  43.  
  44. if not os.path.exists(new_db):
  45. print >> sys.stderr, "Error in db-update: %s doesn't exist" % new_db
  46. return 1
  47.  
  48.  
  49. # determine schema of deployments table
  50. tables_sql = "SHOW TABLES;"
  51. output = _run_sql_on_db(ij_path, old_db, tables_sql)
  52. find_schema = re.compile(".*(APP|NIMBUS)\s*\|DEPLOYMENTS", re.DOTALL)
  53. schema = find_schema.match(output).group(1)
  54.  
  55. # Pull data out of old database
  56. select_sql = """
  57. SELECT uuid, workspaceid, creator_dn, creation_time,\
  58. requested_duration, active, elapsed_minutes\
  59. FROM %s.deployments;
  60. """ % schema
  61. output = _run_sql_on_db(ij_path, old_db, select_sql)
  62. if re.match(".*error", output, re.IGNORECASE | re.DOTALL):
  63. print >> sys.stderr, "Error in db-update: Problem getting old data"
  64. print >> sys.stderr, output
  65. return 1
  66.  
  67.  
  68. insert_sql = ""
  69. for line in output.splitlines():
  70. # disgard lines that aren't data
  71. if line.startswith("ij") or line.startswith("UUID ") \
  72. or line.startswith("-------") or line.endswith("selected")\
  73. or line == "":
  74. continue
  75.  
  76. elements = line.split("|")
  77. elements = [element.strip() for element in elements]
  78.  
  79. insert_sql += "INSERT INTO nimbus.deployments (uuid, workspaceid, creator_dn,"\
  80. "creation_time, requested_duration, active, elapsed_minutes) "\
  81. "VALUES ('%s', %s, '%s', %s, %s, %s, %s);\n" %\
  82. (elements[0], elements[1], elements[2], elements[3],
  83. elements[4], elements[5], elements[6])
  84.  
  85. output = _run_sql_on_db(ij_path, new_db, insert_sql)
  86. if re.match(".*error", output, re.IGNORECASE | re.DOTALL):
  87. print >> sys.stderr, "Error in db-update: Problem inserting data"
  88. print >> sys.stderr, output
  89. return 1
  90.  
  91.  
  92.  
  93. if __name__ == "__main__":
  94.  
  95. ij_path = "java -jar /usr/local/nimbus/lib/derbyrun.jar ij"
  96. old_db = "/tmp/WorkspaceAccountingDB/"
  97. new_db = "/tmp/new/WorkspaceAccountingDB/"
  98. update_db(ij_path, old_db, new_db)
Add Comment
Please, Sign In to add comment