Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

migrate.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #! /usr/bin/env python3
  2. import argparse
  3. import sys
  4. import os
  5. import os.path
  6. import re
  7. import distutils.version
  8. import subprocess
  9. import datetime
  10. def file_runner_pgsql(folder, file):
  11. subprocess.call(['psql', '-f', os.path.join(folder, file)])
  12. def file_runner_pgsql_gz(folder, file):
  13. p1 = subprocess.Popen(['gunzip', '-c', os.path.join(folder, file)], stdout=subprocess.PIPE)
  14. p2 = subprocess.Popen(["psql"], stdin=p1.stdout)
  15. p1.stdout.close()
  16. p2.communicate()
  17. def file_runner_exec(folder, file):
  18. subprocess.call([os.path.join(folder, file)])
  19. def list_files(folder):
  20. files = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
  21. files.sort()
  22. return files
  23. def sort_versions(versions):
  24. versions.sort(key=distutils.version.StrictVersion)
  25. def list_versions(files):
  26. versions = []
  27. for file in files:
  28. version = file.split('_')[0]
  29. if re.match('^[0-9]+\.[0-9]+\.[0-9]+$', version):
  30. if version not in versions:
  31. versions.append(version)
  32. sort_versions(versions)
  33. return versions
  34. def run_file(folder, file, no_run, file_runners):
  35. can_run = False
  36. for file_runner_ext in file_runners:
  37. if file.endswith(file_runner_ext):
  38. print('Running file %s' % (file))
  39. can_run = True
  40. if not no_run:
  41. try:
  42. sys.stdout.flush()
  43. file_runners[file_runner_ext](folder, file)
  44. except Exception as e:
  45. print('Failed to run file: %s' % (e))
  46. if not can_run:
  47. print('Ignoring file %s' % (file))
  48. def run_migration(folder, files, version, no_run, file_runners):
  49. for file in files:
  50. if file.startswith('%s_' % (version)):
  51. run_file(folder, file, no_run, file_runners)
  52. def run_migrations(folder, version_from, version_to, no_run, file_runners, log_file):
  53. files = list_files(folder)
  54. versions = list_versions(files)
  55. if not versions:
  56. print('No migration available. Exiting.')
  57. return 0
  58. if version_to is None:
  59. version_to = versions[-1]
  60. print('Defaulting VERSION_TO to %s' % (version_to))
  61. if version_to not in versions:
  62. print('Could not find VERSION_TO %s' % (version_to))
  63. return 1
  64. if version_from is not None and version_from not in versions:
  65. print('Could not find VERSION_FROM %s' % (version_from))
  66. return 1
  67. if no_run:
  68. print('Not running because --no-run was specified')
  69. if version_from is None:
  70. msg = 'Initialising from %s (inclusive) to %s (inclusive)' % (versions[0], version_to)
  71. else:
  72. msg = 'Migrating from %s (exclusive) to %s (inclusive)' % (version_from, version_to)
  73. print(msg)
  74. if not no_run:
  75. try:
  76. with open(log_file, "a") as f:
  77. f.write('%s: %s in %s\n' % (datetime.datetime.now(), msg, folder))
  78. except Exception as e:
  79. print('Failed to write logs in %: %s' % (log_file, e))
  80. version_from_obj = distutils.version.StrictVersion('0.0.0' if version_from is None else version_from)
  81. version_to_obj = distutils.version.StrictVersion(version_to)
  82. last_version = version_from
  83. for version in versions:
  84. version_obj = distutils.version.StrictVersion(version)
  85. if version_from_obj < version_obj and version_obj <= version_to_obj:
  86. if last_version is None:
  87. print('Initialising %s' % (version))
  88. else:
  89. print('Migrating from %s to %s' % (last_version, version))
  90. run_migration(folder, files, version, no_run, file_runners)
  91. last_version = version
  92. return 0
  93. def main():
  94. parser = argparse.ArgumentParser(description='Migrate container configuration/data')
  95. parser.add_argument('--folder', dest='folder', default='/docker-entrypoint-initdb.d/', help='Folder to use for migration')
  96. parser.add_argument('--init', dest='is_init', default=False, action='store_true', help='Run all migrations')
  97. parser.add_argument('--migrate', dest='is_migrate', default=False, action='store_true', help='Run all migrations between VERSION_FROM (exclusive) and VERSION_TO (inclusive)')
  98. parser.add_argument('--no-run', dest='no_run', default=False, action='store_true', help='Do not run migration, just print')
  99. parser.add_argument('--version-from', dest='version_from', default=None, help='Current version, required if using --migrate, can not be used --init')
  100. parser.add_argument('--version-to', dest='version_to', default=None, help='Final version, default to last available version is not given')
  101. parser.add_argument('--log-file', dest='log_file', default='/var/lib/postgresql/data/migrate.py.log', help='File to log migrations')
  102. args = parser.parse_args()
  103. file_runners = {
  104. 'sql': file_runner_pgsql,
  105. 'sql.gz': file_runner_pgsql_gz,
  106. 'sh': file_runner_exec
  107. }
  108. if args.is_init and args.is_migrate:
  109. print('--init and --migrate can not be used together')
  110. return 64
  111. if args.is_init:
  112. if args.version_from is not None:
  113. print('--version-from can not be used with --init')
  114. return 64
  115. return run_migrations(args.folder, None, args.version_to, args.no_run, file_runners, args.log_file)
  116. elif args.is_migrate:
  117. if args.version_from is None:
  118. print('--version-from is required. Use --init to run all migrations')
  119. return 64
  120. return run_migrations(args.folder, args.version_from, args.version_to, args.no_run, file_runners, args.log_file)
  121. else:
  122. print('Missing --init or --migrate')
  123. return 64
  124. if __name__ == '__main__':
  125. sys.exit(main())