66from shutil import copytree
77from six import raise_from
88
9+ from os_ops import OsOperations
910from .config import testgres_config
1011
1112from .consts import XLOG_CONTROL_FILE
2122 execute_utility
2223
2324
24- def cached_initdb (data_dir , logfile = None , params = None ):
25+ def cached_initdb (data_dir , logfile = None , hostname = 'localhost' , ssh_key = None , params = None ):
2526 """
2627 Perform initdb or use cached node files.
2728 """
29+ os_ops = OsOperations (hostname = hostname , ssh_key = ssh_key )
30+
2831 def call_initdb (initdb_dir , log = None ):
2932 try :
3033 _params = [get_bin_path ("initdb" ), "-D" , initdb_dir , "-N" ]
31- execute_utility (_params + (params or []), log )
34+ execute_utility (_params + (params or []), log , hostname = hostname , ssh_key = ssh_key )
3235 except ExecUtilException as e :
3336 raise_from (InitNodeException ("Failed to run initdb" ), e )
3437
@@ -39,26 +42,27 @@ def call_initdb(initdb_dir, log=None):
3942 cached_data_dir = testgres_config .cached_initdb_dir
4043
4144 # Initialize cached initdb
42- if not os .path .exists (cached_data_dir ) or \
43- not os .listdir (cached_data_dir ):
45+
46+ if not os_ops .path_exists (cached_data_dir ) or \
47+ not os_ops .listdir (cached_data_dir ):
4448 call_initdb (cached_data_dir )
4549
4650 try :
4751 # Copy cached initdb to current data dir
48- copytree (cached_data_dir , data_dir )
52+ os_ops . copytree (cached_data_dir , data_dir )
4953
5054 # Assign this node a unique system id if asked to
5155 if testgres_config .cached_initdb_unique :
5256 # XXX: write new unique system id to control file
5357 # Some users might rely upon unique system ids, but
5458 # our initdb caching mechanism breaks this contract.
5559 pg_control = os .path .join (data_dir , XLOG_CONTROL_FILE )
56- with io . open ( pg_control , "r+b" ) as f :
57- f .write (generate_system_id ()) # overwrite id
60+ system_id = generate_system_id ()
61+ os_ops .write (pg_control , system_id , truncate = True , binary = True , read_and_write = True )
5862
5963 # XXX: build new WAL segment with our system id
6064 _params = [get_bin_path ("pg_resetwal" ), "-D" , data_dir , "-f" ]
61- execute_utility (_params , logfile )
65+ execute_utility (_params , logfile , hostname = hostname , ssh_key = ssh_key )
6266
6367 except ExecUtilException as e :
6468 msg = "Failed to reset WAL for system id"
0 commit comments