@@ -53,11 +53,29 @@ class PostgresNode:
53
53
def __init__ (self , name , port ):
54
54
self .name = name
55
55
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 )
57
59
self .working = False
58
60
self .config = {}
59
61
self .load_pg_config ()
60
62
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
+
61
79
def load_pg_config (self ):
62
80
""" Loads pg_config output into dict """
63
81
pg_config = os .environ .get ("PG_CONFIG" ) \
@@ -81,9 +99,15 @@ def init(self):
81
99
82
100
# initialize cluster
83
101
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" )
87
111
88
112
# add parameters to config file
89
113
config_name = "%s/postgresql.conf" % self .data_dir
@@ -102,32 +126,57 @@ def append_conf(self, filename, string):
102
126
with open (config_name , "a" ) as conf :
103
127
conf .write (string )
104
128
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"""
107
134
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
- ]
113
135
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 ):
116
159
raise ClusterException ("Cluster startup failed" )
117
160
118
161
self .working = True
119
162
120
163
def stop (self ):
121
164
""" 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 ) :
127
170
raise ClusterException ("Cluster stop failed" )
128
171
129
172
self .working = False
130
173
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
+
131
180
def cleanup (self ):
132
181
"""Stops cluster if needed and removes the data directory"""
133
182
0 commit comments