BootstrapMysql.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from jinja2 import Environment, FileSystemLoader
  2. from modules.BootstrapBase import BootstrapBase
  3. import os
  4. import time
  5. import subprocess
  6. class BootstrapMysql(BootstrapBase):
  7. def bootstrap(self):
  8. dbuser = "root"
  9. dbpass = os.getenv("MYSQL_ROOT_PASSWORD", "")
  10. socket = "/tmp/mysql-temp.sock"
  11. print("Starting temporary mysqld for upgrade...")
  12. self.start_temporary(socket)
  13. self.connect_mysql(socket)
  14. print("Running mysql_upgrade...")
  15. self.upgrade_mysql(dbuser, dbpass, socket)
  16. print("Checking timezone support with CONVERT_TZ...")
  17. self.check_and_import_timezone_support(dbuser, dbpass, socket)
  18. print("Shutting down temporary mysqld...")
  19. self.close_mysql()
  20. self.stop_temporary(dbuser, dbpass, socket)
  21. # Setup Jinja2 Environment and load vars
  22. self.env = Environment(
  23. loader=FileSystemLoader([
  24. '/service_config/custom_templates',
  25. '/service_config/config_templates'
  26. ]),
  27. keep_trailing_newline=True,
  28. lstrip_blocks=True,
  29. trim_blocks=True
  30. )
  31. extra_vars = {
  32. }
  33. self.env_vars = self.prepare_template_vars('/service_config/overwrites.json', extra_vars)
  34. print("Set Timezone")
  35. self.set_timezone()
  36. print("Render config")
  37. self.render_config("/service_config")
  38. def start_temporary(self, socket):
  39. """
  40. Starts a temporary mysqld process in the background using the given UNIX socket.
  41. The server is started with networking disabled (--skip-networking).
  42. Args:
  43. socket (str): Path to the UNIX socket file for MySQL to listen on.
  44. Returns:
  45. subprocess.Popen: The running mysqld process object.
  46. """
  47. return subprocess.Popen([
  48. "mysqld",
  49. "--user=mysql",
  50. "--skip-networking",
  51. f"--socket={socket}"
  52. ])
  53. def stop_temporary(self, dbuser, dbpass, socket):
  54. """
  55. Shuts down the temporary mysqld instance gracefully.
  56. Uses mariadb-admin to issue a shutdown command to the running server.
  57. Args:
  58. dbuser (str): The MySQL username with shutdown privileges (typically 'root').
  59. dbpass (str): The password for the MySQL user.
  60. socket (str): Path to the UNIX socket the server is listening on.
  61. """
  62. self.run_command([
  63. "mariadb-admin",
  64. "shutdown",
  65. f"--socket={socket}",
  66. "-u", dbuser,
  67. f"-p{dbpass}"
  68. ])
  69. def upgrade_mysql(self, dbuser, dbpass, socket, max_retries=5, wait_interval=3):
  70. """
  71. Executes mysql_upgrade to check and fix any schema or table incompatibilities.
  72. Retries the upgrade command if it fails, up to a maximum number of attempts.
  73. Args:
  74. dbuser (str): MySQL username with privilege to perform the upgrade.
  75. dbpass (str): Password for the MySQL user.
  76. socket (str): Path to the MySQL UNIX socket for local communication.
  77. max_retries (int): Maximum number of attempts before giving up. Default is 5.
  78. wait_interval (int): Number of seconds to wait between retries. Default is 3.
  79. Returns:
  80. bool: True if upgrade succeeded, False if all attempts failed.
  81. """
  82. retries = 0
  83. while retries < max_retries:
  84. result = self.run_command([
  85. "mysql_upgrade",
  86. "-u", dbuser,
  87. f"-p{dbpass}",
  88. f"--socket={socket}"
  89. ], check=False)
  90. if result.returncode == 0:
  91. print("mysql_upgrade completed successfully.")
  92. break
  93. else:
  94. print(f"mysql_upgrade failed (try {retries+1}/{max_retries})")
  95. retries += 1
  96. time.sleep(wait_interval)
  97. else:
  98. print("mysql_upgrade failed after all retries.")
  99. return False
  100. def check_and_import_timezone_support(self, dbuser, dbpass, socket):
  101. """
  102. Checks if MySQL supports timezone conversion (CONVERT_TZ).
  103. If not, it imports timezone info using mysql_tzinfo_to_sql piped into mariadb.
  104. """
  105. try:
  106. cursor = self.mysql_conn.cursor()
  107. cursor.execute("SELECT CONVERT_TZ('2019-11-02 23:33:00','Europe/Berlin','UTC')")
  108. result = cursor.fetchone()
  109. cursor.close()
  110. if not result or result[0] is None:
  111. print("Timezone conversion failed or returned NULL. Importing timezone info...")
  112. # Use mysql_tzinfo_to_sql piped into mariadb
  113. tz_dump = subprocess.Popen(
  114. ["mysql_tzinfo_to_sql", "/usr/share/zoneinfo"],
  115. stdout=subprocess.PIPE
  116. )
  117. self.run_command([
  118. "mariadb",
  119. "--socket", socket,
  120. "-u", dbuser,
  121. f"-p{dbpass}",
  122. "mysql"
  123. ], input_stream=tz_dump.stdout)
  124. tz_dump.stdout.close()
  125. tz_dump.wait()
  126. print("Timezone info successfully imported.")
  127. else:
  128. print(f"Timezone support is working. Sample result: {result[0]}")
  129. except Exception as e:
  130. print(f"Failed to verify or import timezone info: {e}")