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
2122execute_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+
2831def call_initdb (initdb_dir ,log = None ):
2932try :
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 )
3235except ExecUtilException as e :
3336raise_from (InitNodeException ("Failed to run initdb" ),e )
3437
@@ -39,26 +42,27 @@ def call_initdb(initdb_dir, log=None):
3942cached_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 ):
4448call_initdb (cached_data_dir )
4549
4650try :
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
5155if 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.
5559pg_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
6367except ExecUtilException as e :
6468msg = "Failed to reset WAL for system id"