@@ -64,6 +64,16 @@ class QueryException(Exception):
6464 pass
6565
6666
67+ class PgcontroldataException (Exception ):
68+ def __init__ (self , error_text , cmd ):
69+ self .error_text = error_text
70+ self .cmd = cmd
71+
72+ def __str__ (self ):
73+ self .error_text = repr (self .error_text )
74+ return '\n ERROR: {0}\n CMD: {1}' .format (self .error_text , self .cmd )
75+
76+
6777class NodeConnection (object ):
6878
6979 """
@@ -252,6 +262,26 @@ def init_from_backup(self, root_node, backup_name, has_streaming=False,
252262 if has_streaming :
253263 self .enable_streaming (root_node )
254264
265+ def set_archiving_conf (self , archive_dir ):
266+ self .append_conf (
267+ "postgresql.auto.conf" ,
268+ "wal_level = archive"
269+ )
270+ self .append_conf (
271+ "postgresql.auto.conf" ,
272+ "archive_mode = on"
273+ )
274+ if os .name == 'posix' :
275+ self .append_conf (
276+ "postgresql.auto.conf" ,
277+ "archive_command = 'test ! -f {0}/%f && cp %p {0}/%f'" .format (archive_dir )
278+ )
279+ elif os .name == 'nt' :
280+ self .append_conf (
281+ "postgresql.auto.conf" ,
282+ "archive_command = 'copy %p {0}\\ %f'" .format (archive_dir )
283+ )
284+
255285 def set_replication_conf (self ):
256286 hba_conf = os .path .join (self .data_dir , "pg_hba.conf" )
257287 with open (hba_conf , "a" ) as conf :
@@ -277,14 +307,15 @@ def append_conf(self, filename, string):
277307
278308 return self
279309
280- def pg_ctl (self , command , params , command_options = []):
310+ def pg_ctl (self , command , params = {} , command_options = []):
281311 """Runs pg_ctl with specified params
282312
283- This function is a workhorse for start(), stop() and reload ()
313+ This function is a workhorse for start(), stop(), reload() and status ()
284314 functions"""
285315 pg_ctl = self .get_bin_path ("pg_ctl" )
286316
287317 arguments = [pg_ctl ]
318+ arguments .append (command )
288319 for key , value in six .iteritems (params ):
289320 arguments .append (key )
290321 if value :
@@ -294,7 +325,7 @@ def pg_ctl(self, command, params, command_options=[]):
294325 open (self .error_filename , "a" ) as file_err :
295326
296327 res = subprocess .call (
297- arguments + [ command ] + command_options ,
328+ arguments + command_options ,
298329 stdout = file_out ,
299330 stderr = file_err
300331 )
@@ -318,6 +349,54 @@ def start(self, params={}):
318349
319350 return self
320351
352+ def status (self ):
353+ """ Check cluster status
354+ If Running - return True
355+ If not Running - return False
356+ If there is no data in data_dir or data_dir do not exists - return None
357+ """
358+ try :
359+ res = subprocess .check_output ([
360+ self .get_bin_path ("pg_ctl" ), 'status' , '-D' , '{0}' .format (self .data_dir )
361+ ])
362+ return True
363+ except subprocess .CalledProcessError as e :
364+ if e .returncode == 3 :
365+ # Not Running
366+ self .working = False
367+ return False
368+ elif e .returncode == 4 :
369+ # No data or directory do not exists
370+ self .working = False
371+ return None
372+
373+ def get_pid (self ):
374+ """ Check that node is running and return postmaster pid
375+ Otherwise return None
376+ """
377+ if self .status ():
378+ file = open (os .path .join (self .data_dir , 'postmaster.pid' ))
379+ pid = int (file .readline ())
380+ return pid
381+ else :
382+ return None
383+
384+ def get_control_data (self ):
385+ out_data = {}
386+ pg_controldata = self .get_bin_path ("pg_controldata" )
387+ try :
388+ lines = subprocess .check_output (
389+ [pg_controldata ] + ["-D" , self .data_dir ],
390+ stderr = subprocess .STDOUT
391+ ).splitlines ()
392+ except subprocess .CalledProcessError as e :
393+ raise PgcontroldataException (e .output , e .cmd )
394+
395+ for line in lines :
396+ key , value = line .partition (':' )[::2 ]
397+ out_data [key .strip ()] = value .strip ()
398+ return out_data
399+
321400 def stop (self , params = {}):
322401 """ Stops cluster """
323402 _params = {
@@ -361,6 +440,9 @@ def cleanup(self):
361440 pass
362441
363442 # remove data directory
443+ tblspace_list = os .listdir (os .path .join (self .data_dir , 'pg_tblspc' ))
444+ for i in tblspace_list :
445+ shutil .rmtree (os .readlink (os .path .join (self .data_dir , 'pg_tblspc' , i )))
364446 shutil .rmtree (self .data_dir )
365447
366448 return self
@@ -375,7 +457,7 @@ def psql(self, dbname, query=None, filename=None, username=None):
375457 """
376458 psql = self .get_bin_path ("psql" )
377459 psql_params = [
378- psql , "-XAtq" , "-p {}" .format (self .port ), dbname
460+ psql , "-XAtq" , "-h127.0.0.1 " , "-p {}" .format (self .port ), dbname
379461 ]
380462
381463 if query :
0 commit comments