Skip to content

Commit

Permalink
Run MySQL during config upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Willemsen committed Jul 11, 2024
1 parent 15677e2 commit 78afd2a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
7 changes: 6 additions & 1 deletion installation_and_upgrade/ibex_install_utils/install_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,12 @@ def run_vhd_creation(self):
self._mysql_tasks.install_mysql_for_vhd()
self._client_tasks.install_ibex_client()
self._server_tasks.setup_config_repository()
self._server_tasks.upgrade_instrument_configuration()

# Some config upgrade steps require MySQL to be running
# For the VHD build, we can always assume we have a MYSQL_PASSWORD env variable
with self._mysql_tasks.temporarily_run_mysql(os.getenv("MYSQL_PASSWORD")):
self._server_tasks.upgrade_instrument_configuration()

self._server_tasks.setup_calibrations_repository()
self._server_tasks.update_calibrations_repository()
self._vhd_tasks.initialize_var_dir()
Expand Down
81 changes: 46 additions & 35 deletions installation_and_upgrade/ibex_install_utils/tasks/mysql_tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import os
import shutil
import subprocess
Expand Down Expand Up @@ -159,45 +160,18 @@ def _initialize_mysql_data_area_for_vhd(self):
]
).run()

def _setup_database_users_and_tables(self, vhd_install=True):
@contextlib.contextmanager
def temporarily_run_mysql(self, sql_password: str):
mysqld = os.path.join(MYSQL8_INSTALL_DIR, "bin", "mysqld.exe")

sql_password = self.prompt.prompt("Enter the MySQL root password:", UserPrompt.ANY,
os.getenv("MYSQL_PASSWORD", "environment variable not set"),
show_automatic_answer=False)

if vhd_install: # Service won't have been started yet
# spawn service in background
subprocess.Popen(mysqld, creationflags=DETACHED_PROCESS)
# spawn service in background
subprocess.Popen(mysqld, creationflags=DETACHED_PROCESS)

sleep(5) # Chance for the service to spawn
sleep(5) # Chance for the service to spawn

RunProcess(
executable_directory=os.path.join(MYSQL8_INSTALL_DIR, "bin"),
working_dir=os.path.join(MYSQL8_INSTALL_DIR, "bin"),
executable_file="mysql.exe",
prog_args=[
'-u',
'root',
'-e',
f'ALTER USER \'root\'@\'localhost\' IDENTIFIED WITH mysql_native_password BY \'{sql_password}\';FLUSH '
f'privileges; '
,

],
log_command_args=False, # To make sure password doesn't appear in jenkins log.
).run()

RunProcess(
working_dir=SYSTEM_SETUP_PATH,
executable_directory=SYSTEM_SETUP_PATH,
executable_file="config_mysql.bat",
prog_args=[sql_password],
log_command_args=False, # To make sure password doesn't appear in jenkins log.
).run()

if vhd_install:
# For VHD install only, stop mysql when done
try:
yield
finally:
RunProcess(
executable_directory=os.path.join(MYSQL8_INSTALL_DIR, "bin"),
working_dir=os.path.join(MYSQL8_INSTALL_DIR, "bin"),
Expand All @@ -211,6 +185,43 @@ def _setup_database_users_and_tables(self, vhd_install=True):
log_command_args=False, # To make sure password doesn't appear in jenkins log.
).run()

def _setup_database_users_and_tables(self, vhd_install=True):
sql_password = self.prompt.prompt("Enter the MySQL root password:", UserPrompt.ANY,
os.getenv("MYSQL_PASSWORD", "environment variable not set"),
show_automatic_answer=False)

if vhd_install:
# In the VHD install, need to explicitly temporarily run MySQL.
cm = self.temporarily_run_mysql(sql_password)
else:
# In normal installs, MySQL is already running as a service so do nothing
cm = contextlib.nullcontext()

with cm:
RunProcess(
executable_directory=os.path.join(MYSQL8_INSTALL_DIR, "bin"),
working_dir=os.path.join(MYSQL8_INSTALL_DIR, "bin"),
executable_file="mysql.exe",
prog_args=[
'-u',
'root',
'-e',
f'ALTER USER \'root\'@\'localhost\' IDENTIFIED WITH mysql_native_password BY \'{sql_password}\';FLUSH '
f'privileges; '
,

],
log_command_args=False, # To make sure password doesn't appear in jenkins log.
).run()

RunProcess(
working_dir=SYSTEM_SETUP_PATH,
executable_directory=SYSTEM_SETUP_PATH,
executable_file="config_mysql.bat",
prog_args=[sql_password],
log_command_args=False, # To make sure password doesn't appear in jenkins log.
).run()

def _setup_mysql8_service(self):
mysqld = os.path.join(MYSQL8_INSTALL_DIR, "bin", "mysqld.exe")
admin_commands = AdminCommandBuilder()
Expand Down

0 comments on commit 78afd2a

Please sign in to comment.