BootstrapMysql.py 4.7 KB

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