@@ -55,7 +55,6 @@ def __init__(self, name, port):
55
55
self .port = port
56
56
self .base_dir = tempfile .mkdtemp ()
57
57
os .makedirs (self .logs_dir )
58
- os .makedirs (self .data_dir )
59
58
self .working = False
60
59
self .config = {}
61
60
self .load_pg_config ()
@@ -94,10 +93,11 @@ def get_bin_path(self, filename):
94
93
else :
95
94
return "%s/%s" % (self .config ["BINDIR" ], filename )
96
95
97
- def init (self ):
96
+ def init (self , allows_streaming = False ):
98
97
""" Performs initdb """
99
98
100
99
# initialize cluster
100
+ os .makedirs (self .data_dir )
101
101
initdb = self .get_bin_path ("initdb" )
102
102
with open (self .output_filename , "a" ) as file_out , \
103
103
open (self .error_filename , "a" ) as file_err :
@@ -112,9 +112,41 @@ def init(self):
112
112
# add parameters to config file
113
113
config_name = "%s/postgresql.conf" % self .data_dir
114
114
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 " )
118
150
119
151
def append_conf (self , filename , string ):
120
152
"""Appends line to a config file like "postgresql.conf"
@@ -133,7 +165,7 @@ def pg_ctl(self, command, params):
133
165
functions"""
134
166
pg_ctl = self .get_bin_path ("pg_ctl" )
135
167
136
- arguments = [' pg_ctl' ]
168
+ arguments = [" pg_ctl" ]
137
169
for key , value in params .iteritems ():
138
170
arguments .append (key )
139
171
if value :
@@ -240,6 +272,24 @@ def execute(self, dbname, query):
240
272
241
273
return res
242
274
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
+
243
293
244
294
def get_username ():
245
295
""" Returns current user name """
0 commit comments