1+ import contextlib
12import os
23import shutil
34import subprocess
@@ -159,45 +160,18 @@ def _initialize_mysql_data_area_for_vhd(self):
159160 ]
160161 ).run ()
161162
162- def _setup_database_users_and_tables (self , vhd_install = True ):
163+ @contextlib .contextmanager
164+ def temporarily_run_mysql (self , sql_password : str ):
163165 mysqld = os .path .join (MYSQL8_INSTALL_DIR , "bin" , "mysqld.exe" )
164166
165- sql_password = self .prompt .prompt ("Enter the MySQL root password:" , UserPrompt .ANY ,
166- os .getenv ("MYSQL_PASSWORD" , "environment variable not set" ),
167- show_automatic_answer = False )
168-
169- if vhd_install : # Service won't have been started yet
170- # spawn service in background
171- subprocess .Popen (mysqld , creationflags = DETACHED_PROCESS )
167+ # spawn service in background
168+ subprocess .Popen (mysqld , creationflags = DETACHED_PROCESS )
172169
173- sleep (5 ) # Chance for the service to spawn
170+ sleep (5 ) # Chance for the service to spawn
174171
175- RunProcess (
176- executable_directory = os .path .join (MYSQL8_INSTALL_DIR , "bin" ),
177- working_dir = os .path .join (MYSQL8_INSTALL_DIR , "bin" ),
178- executable_file = "mysql.exe" ,
179- prog_args = [
180- '-u' ,
181- 'root' ,
182- '-e' ,
183- f'ALTER USER \' root\' @\' localhost\' IDENTIFIED WITH mysql_native_password BY \' { sql_password } \' ;FLUSH '
184- f'privileges; '
185- ,
186-
187- ],
188- log_command_args = False , # To make sure password doesn't appear in jenkins log.
189- ).run ()
190-
191- RunProcess (
192- working_dir = SYSTEM_SETUP_PATH ,
193- executable_directory = SYSTEM_SETUP_PATH ,
194- executable_file = "config_mysql.bat" ,
195- prog_args = [sql_password ],
196- log_command_args = False , # To make sure password doesn't appear in jenkins log.
197- ).run ()
198-
199- if vhd_install :
200- # For VHD install only, stop mysql when done
172+ try :
173+ yield
174+ finally :
201175 RunProcess (
202176 executable_directory = os .path .join (MYSQL8_INSTALL_DIR , "bin" ),
203177 working_dir = os .path .join (MYSQL8_INSTALL_DIR , "bin" ),
@@ -211,6 +185,43 @@ def _setup_database_users_and_tables(self, vhd_install=True):
211185 log_command_args = False , # To make sure password doesn't appear in jenkins log.
212186 ).run ()
213187
188+ def _setup_database_users_and_tables (self , vhd_install = True ):
189+ sql_password = self .prompt .prompt ("Enter the MySQL root password:" , UserPrompt .ANY ,
190+ os .getenv ("MYSQL_PASSWORD" , "environment variable not set" ),
191+ show_automatic_answer = False )
192+
193+ if vhd_install :
194+ # In the VHD install, need to explicitly temporarily run MySQL.
195+ cm = self .temporarily_run_mysql (sql_password )
196+ else :
197+ # In normal installs, MySQL is already running as a service so do nothing
198+ cm = contextlib .nullcontext ()
199+
200+ with cm :
201+ RunProcess (
202+ executable_directory = os .path .join (MYSQL8_INSTALL_DIR , "bin" ),
203+ working_dir = os .path .join (MYSQL8_INSTALL_DIR , "bin" ),
204+ executable_file = "mysql.exe" ,
205+ prog_args = [
206+ '-u' ,
207+ 'root' ,
208+ '-e' ,
209+ f'ALTER USER \' root\' @\' localhost\' IDENTIFIED WITH mysql_native_password BY \' { sql_password } \' ;FLUSH '
210+ f'privileges; '
211+ ,
212+
213+ ],
214+ log_command_args = False , # To make sure password doesn't appear in jenkins log.
215+ ).run ()
216+
217+ RunProcess (
218+ working_dir = SYSTEM_SETUP_PATH ,
219+ executable_directory = SYSTEM_SETUP_PATH ,
220+ executable_file = "config_mysql.bat" ,
221+ prog_args = [sql_password ],
222+ log_command_args = False , # To make sure password doesn't appear in jenkins log.
223+ ).run ()
224+
214225 def _setup_mysql8_service (self ):
215226 mysqld = os .path .join (MYSQL8_INSTALL_DIR , "bin" , "mysqld.exe" )
216227 admin_commands = AdminCommandBuilder ()
0 commit comments