Skip to content

Commit 50e8f56

Browse files
committed
Merge remote-tracking branch 'intgit/master'
2 parents 2e0c985 + 0359c8a commit 50e8f56

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

python/walmgr.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ def archive_mode(self):
208208
return "off"
209209
return None
210210

211+
def synchronous_standby_names(self):
212+
"""Return value for specified parameter"""
213+
# see if explicitly set
214+
m = re.search("^\s*synchronous_standby_names\s*=\s*'?([a-zA-Z01]+)'?\s*#?.*$", self.cf_buf, re.M | re.I)
215+
if m:
216+
return m.group(1)
217+
# also, it could be commented out as initdb leaves it
218+
# it'd probably be best to check from the database ...
219+
m = re.search("^#synchronous_standby_names\s*=.*$", self.cf_buf, re.M | re.I)
220+
if m:
221+
return ''
222+
return None
223+
211224
def wal_level(self):
212225
"""Return value for specified parameter"""
213226
# see if explicitly set
@@ -283,6 +296,8 @@ def init_optparse(self, parser=None):
283296
help = "slave: add public key to authorized_hosts", default=False)
284297
p.add_option("", "--ssh-remove-key", action="store", dest="ssh_remove_key",
285298
help = "slave: remove master key from authorized_hosts", default=False)
299+
p.add_option("", "--primary-conninfo", action="store", dest="primary_conninfo", default=None,
300+
help = "slave: connect string for streaming replication master")
286301
p.add_option("", "--init-slave", action="store_true", dest="init_slave",
287302
help = "Initialize slave walmgr.", default=False)
288303
return p
@@ -324,8 +339,6 @@ def load_config(self):
324339

325340
# slave configuration settings
326341
slave_opt_dict = {
327-
'slave_stop_cmd': '/etc/init.d/postgresql-%s stop' % DEFAULT_PG_VERSION,
328-
'slave_start_cmd': '/etc/init.d/postgresql-%s start' % DEFAULT_PG_VERSION,
329342
'completed_wals': '%%(walmgr_data)s/logs.complete',
330343
'partial_wals': '%%(walmgr_data)s/logs.partial',
331344
'full_backup': '%%(walmgr_data)s/data.master',
@@ -722,6 +735,11 @@ def master_configure_archiving(self, enable_archiving, can_restart):
722735

723736
cf_params['archive_command'] = '/bin/true'
724737

738+
# disable synchronous standbys, note that presently we don't care
739+
# if there is more than one standby.
740+
if cf.synchronous_standby_names():
741+
cf_params['synchronous_standby_names'] = ''
742+
725743
self.log.debug("modifying configuration: %s" % cf_params)
726744

727745
cf.modify(cf_params)
@@ -851,7 +869,6 @@ def guess_locations(self):
851869
for path in os.environ['PATH'].split(os.pathsep):
852870
path = os.path.expanduser(path)
853871
exe = os.path.join(path, "postgres")
854-
print "checking",exe
855872
if os.path.isfile(exe):
856873
self.postgres_bin = path
857874
break
@@ -872,6 +889,19 @@ def guess_locations(self):
872889
# postgres_conf is required for master
873890
die(1, "Configuration file not found: %s" % self.postgres_conf)
874891

892+
# Attempt to guess the init.d script name
893+
script_suffixes = [ "9.1", "9.0", "8.4", "8.3", "8.2", "8.1", "8.0" ]
894+
self.initd_script = "/etc/init.d/postgresql"
895+
if not os.path.exists(self.initd_script):
896+
for suffix in script_suffixes:
897+
try_file = "%s-%s" % (self.initd_script, suffix)
898+
if os.path.exists(try_file):
899+
self.initd_script = try_file
900+
break
901+
else:
902+
self.initd_script = "%s -m fast -D %s" % \
903+
(os.path.join(self.postgres_bin, "pg_ctl"), os.path.abspath(self.pgdata))
904+
875905
def write_walmgr_config(self, config_data):
876906
cf_name = os.path.join(os.path.expanduser(self.options.config_dir),
877907
self.cf.get("job_name") + ".ini")
@@ -964,6 +994,10 @@ def walmgr_init_slave(self):
964994
self.override_cf_option('slave_data', self.pgdata)
965995
self.override_cf_option('slave_config_dir', os.path.dirname(self.postgres_conf))
966996

997+
if self.initd_script:
998+
self.override_cf_option('slave_start_cmd', "%s start" % self.initd_script)
999+
self.override_cf_option('slave_stop_cmd', "%s stop" % self.initd_script)
1000+
9671001
slave_config = """[walmgr]
9681002
job_name = %(job_name)s
9691003
logfile = %(logfile)s
@@ -981,6 +1015,13 @@ def walmgr_init_slave(self):
9811015
full_backup = %(full_backup)s
9821016
config_backup = %(config_backup)s
9831017
"""
1018+
1019+
if self.options.primary_conninfo:
1020+
self.override_cf_option('primary_conninfo', self.options.primary_conninfo)
1021+
slave_config += """
1022+
primary_conninfo = %(primary_conninfo)s
1023+
"""
1024+
9841025
try:
9851026
opt_dict = dict([(k, self.cf.get(k)) for k in self.cf.options()])
9861027
slave_config = slave_config % opt_dict
@@ -997,7 +1038,7 @@ def walmgr_init_slave(self):
9971038
if not os.path.isdir(ssh_dir):
9981039
self.log.info("Creating directory: %s" % ssh_dir)
9991040
if not self.not_really:
1000-
os.path.mkdir(ssh_dir)
1041+
os.mkdir(ssh_dir)
10011042

10021043
self.log.debug("Reading public key from %s" % self.options.ssh_add_key)
10031044
master_pubkey = open(self.options.ssh_add_key).read()
@@ -1837,6 +1878,9 @@ def restore_database(self):
18371878
conf += "standby_mode = 'on'\n"
18381879
conf += "trigger_file = '%s'\n" % os.path.join(self.cf.getfile("completed_wals"), "STOP")
18391880
conf += "primary_conninfo = '%s'\n" % primary_conninfo
1881+
conf += "archive_cleanup_command = '%s %s %%r'\n" % \
1882+
(os.path.join(self.cf.getfile("slave_bin"), "pg_archivecleanup"),
1883+
self.cf.getfile("completed_wals"))
18401884

18411885
self.log.info("Write %s" % rconf)
18421886
if self.not_really:

0 commit comments

Comments
 (0)