@@ -53,11 +53,29 @@ class PostgresNode:
5353 def __init__ (self , name , port ):
5454 self .name = name
5555 self .port = port
56- self .data_dir = tempfile .mkdtemp ()
56+ self .base_dir = tempfile .mkdtemp ()
57+ os .makedirs (self .logs_dir )
58+ os .makedirs (self .data_dir )
5759 self .working = False
5860 self .config = {}
5961 self .load_pg_config ()
6062
63+ @property
64+ def data_dir (self ):
65+ return "%s/data" % self .base_dir
66+
67+ @property
68+ def logs_dir (self ):
69+ return "%s/logs" % self .base_dir
70+
71+ @property
72+ def output_filename (self ):
73+ return "%s/stdout.log" % self .logs_dir
74+
75+ @property
76+ def error_filename (self ):
77+ return "%s/stderr.log" % self .logs_dir
78+
6179 def load_pg_config (self ):
6280 """ Loads pg_config output into dict """
6381 pg_config = os .environ .get ("PG_CONFIG" ) \
@@ -81,9 +99,15 @@ def init(self):
8199
82100 # initialize cluster
83101 initdb = self .get_bin_path ("initdb" )
84- ret = subprocess .call ([initdb , self .data_dir , "-N" ])
85- if ret :
86- raise ClusterException ("Cluster initialization failed" )
102+ with open (self .output_filename , "a" ) as file_out , \
103+ open (self .error_filename , "a" ) as file_err :
104+ ret = subprocess .call (
105+ [initdb , self .data_dir , "-N" ],
106+ stdout = file_out ,
107+ stderr = file_err
108+ )
109+ if ret :
110+ raise ClusterException ("Cluster initialization failed" )
87111
88112 # add parameters to config file
89113 config_name = "%s/postgresql.conf" % self .data_dir
@@ -102,32 +126,57 @@ def append_conf(self, filename, string):
102126 with open (config_name , "a" ) as conf :
103127 conf .write (string )
104128
105- def start (self ):
106- """ Starts cluster """
129+ def pg_ctl (self , command , params ):
130+ """Runs pg_ctl with specified params
131+
132+ This function is a workhorse for start(), stop() and reload()
133+ functions"""
107134 pg_ctl = self .get_bin_path ("pg_ctl" )
108- logfile = self .data_dir + "/postgresql.log"
109- params = [
110- pg_ctl , "-D" , self .data_dir ,
111- "-w" , "start" , "-l" , logfile
112- ]
113135
114- ret = subprocess .call (params )
115- if ret :
136+ arguments = ['pg_ctl' ]
137+ for key , value in params .iteritems ():
138+ arguments .append (key )
139+ if value :
140+ arguments .append (value )
141+
142+ with open (self .output_filename , "a" ) as file_out , \
143+ open (self .error_filename , "a" ) as file_err :
144+ return subprocess .call (
145+ arguments + [command ],
146+ stdout = file_out ,
147+ stderr = file_err
148+ )
149+
150+ def start (self ):
151+ """ Starts cluster """
152+ logfile = self .logs_dir + "/postgresql.log"
153+ params = {
154+ "-D" : self .data_dir ,
155+ "-w" : None ,
156+ "-l" : logfile ,
157+ }
158+ if self .pg_ctl ("start" , params ):
116159 raise ClusterException ("Cluster startup failed" )
117160
118161 self .working = True
119162
120163 def stop (self ):
121164 """ Stops cluster """
122- pg_ctl = self . get_bin_path ( "pg_ctl" )
123- params = [ pg_ctl , "-D" , self .data_dir , "-w" , "stop" ]
124-
125- ret = subprocess . call ( params )
126- if ret :
165+ params = {
166+ "-D" : self .data_dir ,
167+ "-w" : None
168+ }
169+ if self . pg_ctl ( "stop" , params ) :
127170 raise ClusterException ("Cluster stop failed" )
128171
129172 self .working = False
130173
174+ def reload (self ):
175+ """Reloads config files"""
176+ params = {"-D" : self .data_dir }
177+ if self .pg_ctl ("reload" , params ):
178+ raise ClusterException ("Cluster reload failed" )
179+
131180 def cleanup (self ):
132181 """Stops cluster if needed and removes the data directory"""
133182
0 commit comments