diff --git a/installation_and_upgrade/ibex_install_utils/install_tasks.py b/installation_and_upgrade/ibex_install_utils/install_tasks.py index 47de75d..8d3a300 100644 --- a/installation_and_upgrade/ibex_install_utils/install_tasks.py +++ b/installation_and_upgrade/ibex_install_utils/install_tasks.py @@ -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() diff --git a/installation_and_upgrade/ibex_install_utils/tasks/mysql_tasks.py b/installation_and_upgrade/ibex_install_utils/tasks/mysql_tasks.py index 8cf6d96..31bfb48 100644 --- a/installation_and_upgrade/ibex_install_utils/tasks/mysql_tasks.py +++ b/installation_and_upgrade/ibex_install_utils/tasks/mysql_tasks.py @@ -1,3 +1,4 @@ +import contextlib import os import shutil import subprocess @@ -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"), @@ -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()