@@ -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 """
@@ -277,14 +287,15 @@ def append_conf(self, filename, string):
277287
278288 return self
279289
280- def pg_ctl (self , command , params , command_options = []):
290+ def pg_ctl (self , command , params = {} , command_options = []):
281291 """Runs pg_ctl with specified params
282292
283- This function is a workhorse for start(), stop() and reload ()
293+ This function is a workhorse for start(), stop(), reload() and status ()
284294 functions"""
285295 pg_ctl = self .get_bin_path ("pg_ctl" )
286296
287297 arguments = [pg_ctl ]
298+ arguments .append (command )
288299 for key , value in six .iteritems (params ):
289300 arguments .append (key )
290301 if value :
@@ -294,7 +305,7 @@ def pg_ctl(self, command, params, command_options=[]):
294305 open (self .error_filename , "a" ) as file_err :
295306
296307 res = subprocess .call (
297- arguments + [ command ] + command_options ,
308+ arguments + command_options ,
298309 stdout = file_out ,
299310 stderr = file_err
300311 )
@@ -318,6 +329,55 @@ def start(self, params={}):
318329
319330 return self
320331
332+ def status (self ):
333+ """ Check cluster status
334+ If Running - return True
335+ If not Running - return False
336+ If there is no data in data_dir or data_dir do not exists - return None
337+ """
338+ try :
339+ res = subprocess .check_output ([
340+ self .get_bin_path ("pg_ctl" ), 'status' , '-D' , '{0}' .format (self .data_dir )
341+ ])
342+ return True
343+ except subprocess .CalledProcessError as e :
344+ if e .returncode == 3 :
345+ # Not Running
346+ self .working = False
347+ return False
348+ elif e .returncode == 4 :
349+ # No data or directory do not exists
350+ self .working = False
351+ return None
352+
353+ def get_pid (self ):
354+ """ Check that node is running and return postmaster pid
355+ Otherwise return None
356+ """
357+ if self .status ():
358+ file = open (os .path .join (self .data_dir , 'postmaster.pid' ))
359+ pid = int (file .readline ())
360+ return pid
361+ else :
362+ return None
363+
364+ def get_control_data (self ):
365+ """ Return pg_control content """
366+ out_data = {}
367+ pg_controldata = self .get_bin_path ("pg_controldata" )
368+ try :
369+ lines = subprocess .check_output (
370+ [pg_controldata ] + ["-D" , self .data_dir ],
371+ stderr = subprocess .STDOUT
372+ ).splitlines ()
373+ except subprocess .CalledProcessError as e :
374+ raise PgcontroldataException (e .output , e .cmd )
375+
376+ for line in lines :
377+ key , value = line .partition (':' )[::2 ]
378+ out_data [key .strip ()] = value .strip ()
379+ return out_data
380+
321381 def stop (self , params = {}):
322382 """ Stops cluster """
323383 _params = {
@@ -375,7 +435,7 @@ def psql(self, dbname, query=None, filename=None, username=None):
375435 """
376436 psql = self .get_bin_path ("psql" )
377437 psql_params = [
378- psql , "-XAtq" , "-p {}" .format (self .port ), dbname
438+ psql , "-XAtq" , "-h{}" . format ( self . host ) , "-p {}" .format (self .port ), dbname
379439 ]
380440
381441 if query :
0 commit comments