@@ -55,7 +55,6 @@ def __init__(self, name, port):
5555 self .port = port
5656 self .base_dir = tempfile .mkdtemp ()
5757 os .makedirs (self .logs_dir )
58- os .makedirs (self .data_dir )
5958 self .working = False
6059 self .config = {}
6160 self .load_pg_config ()
@@ -94,10 +93,11 @@ def get_bin_path(self, filename):
9493 else :
9594 return "%s/%s" % (self .config ["BINDIR" ], filename )
9695
97- def init (self ):
96+ def init (self , allows_streaming = False ):
9897 """ Performs initdb """
9998
10099 # initialize cluster
100+ os .makedirs (self .data_dir )
101101 initdb = self .get_bin_path ("initdb" )
102102 with open (self .output_filename , "a" ) as file_out , \
103103 open (self .error_filename , "a" ) as file_err :
@@ -112,9 +112,41 @@ def init(self):
112112 # add parameters to config file
113113 config_name = "%s/postgresql.conf" % self .data_dir
114114 with open (config_name , "a" ) as conf :
115- conf .write ("fsync = off\n " )
116- conf .write ("log_statement = all\n " )
117- conf .write ("port = %s\n " % self .port )
115+ conf .write (
116+ "fsync = off\n "
117+ "log_statement = all\n "
118+ "port = %s\n " % self .port )
119+
120+ if allows_streaming :
121+ conf .write (
122+ "wal_level = replica\n "
123+ "max_wal_senders = 5\n "
124+ "wal_keep_segments = 20\n "
125+ "max_wal_size = 128MB\n "
126+ "shared_buffers = 1MB\n "
127+ "wal_log_hints = on\n "
128+ "hot_standby = on\n "
129+ "max_connections = 10\n " )
130+ self .set_replication_conf ()
131+
132+ def init_from_backup (self , root_node , backup_name ):
133+ """Initializes cluster from backup, made by another node"""
134+
135+ # Copy data from backup
136+ backup_path = "%s/%s" % (root_node .base_dir , backup_name )
137+ shutil .copytree (backup_path , self .data_dir )
138+ os .chmod (self .data_dir , 0o0700 )
139+
140+ # Change port in config file
141+ self .append_conf (
142+ "postgresql.conf" ,
143+ "port = %s" % self .port
144+ )
145+
146+ def set_replication_conf (self ):
147+ hba_conf = "%s/pg_hba.conf" % self .data_dir
148+ with open (hba_conf , "a" ) as conf :
149+ conf .write ("local replication all trust\n " )
118150
119151 def append_conf (self , filename , string ):
120152 """Appends line to a config file like "postgresql.conf"
@@ -133,7 +165,7 @@ def pg_ctl(self, command, params):
133165 functions"""
134166 pg_ctl = self .get_bin_path ("pg_ctl" )
135167
136- arguments = [' pg_ctl' ]
168+ arguments = [" pg_ctl" ]
137169 for key , value in params .iteritems ():
138170 arguments .append (key )
139171 if value :
@@ -240,6 +272,24 @@ def execute(self, dbname, query):
240272
241273 return res
242274
275+ def backup (self , name ):
276+ """Performs pg_basebackup"""
277+ pg_basebackup = self .get_bin_path ("pg_basebackup" )
278+ backup_path = self .base_dir + "/" + name
279+ os .makedirs (backup_path )
280+ params = [pg_basebackup , "-D" , backup_path , "-p %s" % self .port , "-x" ]
281+ with open (self .output_filename , "a" ) as file_out , \
282+ open (self .error_filename , "a" ) as file_err :
283+ ret = subprocess .call (
284+ params ,
285+ stdout = file_out ,
286+ stderr = file_err
287+ )
288+ if ret :
289+ raise ClusterException ("Base backup failed" )
290+
291+ return backup_path
292+
243293
244294def get_username ():
245295 """ Returns current user name """
0 commit comments