diff --git a/extras/startup-scripts/patroni b/extras/startup-scripts/patroni index d48dff6f..8d90ea1c 100644 --- a/extras/startup-scripts/patroni +++ b/extras/startup-scripts/patroni @@ -1,144 +1,144 @@ -#!/bin/sh -# -### BEGIN INIT INFO -# Provides: patroni -# Required-Start: $remote_fs $syslog -# Required-Stop: $remote_fs $syslog -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: Patroni init script -# Description: Runners to orchestrate a high-availability PostgreSQL -### END INIT INFO - -### BEGIN USER CONFIGURATION - -CONF="/etc/patroni/postgres.yml" -LOGFILE="/var/log/patroni.log" -USER="postgres" -GROUP="postgres" - -NAME=patroni -PATRONI="/opt/patroni/$NAME.py" -PIDFILE="/var/run/$NAME.pid" - -# Set this parameter, if you have several Postgres versions installed -# POSTGRES_VERSION="9.4" -POSTGRES_VERSION="" - -### END USER CONFIGURATION - -. /lib/lsb/init-functions - -# Loading this library for get_versions() function -if test ! -e /usr/share/postgresql-common/init.d-functions; then - log_failure_msg "Probably postgresql-common does not installed." - exit 1 -else - . /usr/share/postgresql-common/init.d-functions -fi - -# Is there Patroni executable? -if test ! -e $PATRONI; then - log_failure_msg "Patroni executable $PATRONI does not exist." - exit 1 -fi - -# Is there Patroni configuration file? -if test ! -e $CONF; then - log_failure_msg "Patroni configuration file $CONF does not exist." - exit 1 -fi - -# Create logfile if doesn't exist -if test ! -e $LOGFILE; then - log_action_msg "Creating logfile for Patroni..." - touch $LOGFILE - chown $USER:$GROUP $LOGFILE -fi - -prepare_pgpath() { - if [ "$POSTGRES_VERSION" != "" ]; then - if [ -x /usr/lib/postgresql/$POSTGRES_VERSION/bin/pg_ctl ]; then - PGPATH="/usr/lib/postgresql/$POSTGRES_VERSION/bin" - else - log_failure_msg "Postgres version incorrect, check POSTGRES_VERSION variable." - exit 0 - fi - else - get_versions - if echo $versions | grep -q -e "\s"; then - log_warning_msg "You have several Postgres versions installed. Please, use POSTGRES_VERSION to define correct environment." - else - versions=`echo $versions | sed -e 's/^[ \t]*//'` - PGPATH="/usr/lib/postgresql/$versions/bin" - fi - fi -} - -get_pid() { - if test -e $PIDFILE; then - PID=`cat $PIDFILE` - CHILDPID=`ps --ppid $PID -o %p --no-headers` - else - log_failure_msg "Could not find PID file. Patroni probably down." - exit 1 - fi -} - - -case "$1" in - start) - prepare_pgpath - PGPATH=$PATH:$PGPATH - log_success_msg "Starting Patroni\n" - exec start-stop-daemon --start --quiet \ - --background \ - --pidfile $PIDFILE --make-pidfile \ - --chuid $USER:$GROUP \ - --chdir `eval echo ~$USER` \ - --exec $PATRONI \ - --startas /bin/sh -- \ - -c "/usr/bin/env PATH=$PGPATH /usr/bin/python $PATRONI $CONF >> $LOGFILE 2>&1" - ;; - - stop) - log_success_msg "Stopping Patroni" - get_pid - start-stop-daemon --stop --pid $CHILDPID - start-stop-daemon --stop --pidfile $PIDFILE --remove-pidfile --quiet - ;; - - reload) - log_success_msg "Reloading Patroni configuration" - get_pid - kill -HUP $CHILDPID - ;; - - status) - get_pid - if start-stop-daemon -T --pid $CHILDPID; then - log_success_msg "Patroni is running\n" - exit 0 - else - log_warning_msg "Patroni in not running\n" - fi - ;; - - restart) - $0 stop - $0 start - ;; - - *) - echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload|status}" - exit 1 - ;; -esac - -if [ $? -eq 0 ]; then - echo . - exit 0 -else - echo " failed" - exit 1 -fi +#!/bin/sh +# +### BEGIN INIT INFO +# Provides: patroni +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Patroni init script +# Description: Runners to orchestrate a high-availability PostgreSQL +### END INIT INFO + +### BEGIN USER CONFIGURATION + +CONF="/etc/patroni/postgres.yml" +LOGFILE="/var/log/patroni.log" +USER="postgres" +GROUP="postgres" + +NAME=patroni +PATRONI="/opt/patroni/$NAME.py" +PIDFILE="/var/run/$NAME.pid" + +# Set this parameter, if you have several Postgres versions installed +# POSTGRES_VERSION="9.4" +POSTGRES_VERSION="" + +### END USER CONFIGURATION + +. /lib/lsb/init-functions + +# Loading this library for get_versions() function +if test ! -e /usr/share/postgresql-common/init.d-functions; then + log_failure_msg "Probably postgresql-common does not installed." + exit 1 +else + . /usr/share/postgresql-common/init.d-functions +fi + +# Is there Patroni executable? +if test ! -e $PATRONI; then + log_failure_msg "Patroni executable $PATRONI does not exist." + exit 1 +fi + +# Is there Patroni configuration file? +if test ! -e $CONF; then + log_failure_msg "Patroni configuration file $CONF does not exist." + exit 1 +fi + +# Create logfile if doesn't exist +if test ! -e $LOGFILE; then + log_action_msg "Creating logfile for Patroni..." + touch $LOGFILE + chown $USER:$GROUP $LOGFILE +fi + +prepare_pgpath() { + if [ "$POSTGRES_VERSION" != "" ]; then + if [ -x /usr/lib/postgresql/$POSTGRES_VERSION/bin/pg_ctl ]; then + PGPATH="/usr/lib/postgresql/$POSTGRES_VERSION/bin" + else + log_failure_msg "Postgres version incorrect, check POSTGRES_VERSION variable." + exit 0 + fi + else + get_versions + if echo $versions | grep -q -e "\s"; then + log_warning_msg "You have several Postgres versions installed. Please, use POSTGRES_VERSION to define correct environment." + else + versions=`echo $versions | sed -e 's/^[ \t]*//'` + PGPATH="/usr/lib/postgresql/$versions/bin" + fi + fi +} + +get_pid() { + if test -e $PIDFILE; then + PID=`cat $PIDFILE` + CHILDPID=`ps --ppid $PID -o %p --no-headers` + else + log_failure_msg "Could not find PID file. Patroni probably down." + exit 1 + fi +} + + +case "$1" in + start) + prepare_pgpath + PGPATH=$PATH:$PGPATH + log_success_msg "Starting Patroni\n" + exec start-stop-daemon --start --quiet \ + --background \ + --pidfile $PIDFILE --make-pidfile \ + --chuid $USER:$GROUP \ + --chdir `eval echo ~$USER` \ + --exec $PATRONI \ + --startas /bin/sh -- \ + -c "/usr/bin/env PATH=$PGPATH /usr/bin/python $PATRONI $CONF >> $LOGFILE 2>&1" + ;; + + stop) + log_success_msg "Stopping Patroni" + get_pid + start-stop-daemon --stop --pid $CHILDPID + start-stop-daemon --stop --pidfile $PIDFILE --remove-pidfile --quiet + ;; + + reload) + log_success_msg "Reloading Patroni configuration" + get_pid + kill -HUP $CHILDPID + ;; + + status) + get_pid + if start-stop-daemon -T --pid $CHILDPID; then + log_success_msg "Patroni is running\n" + exit 0 + else + log_warning_msg "Patroni in not running\n" + fi + ;; + + restart) + $0 stop + $0 start + ;; + + *) + echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload|status}" + exit 1 + ;; +esac + +if [ $? -eq 0 ]; then + echo . + exit 0 +else + echo " failed" + exit 1 +fi diff --git a/features/environment.py b/features/environment.py index 3f622e49..d82a95ff 100644 --- a/features/environment.py +++ b/features/environment.py @@ -228,7 +228,7 @@ class PatroniController(AbstractController): if not os.path.exists(pidfile): return None return int(open(pidfile).readline().strip()) - except: + except Exception: return None def database_is_running(self): diff --git a/patroni/async_executor.py b/patroni/async_executor.py index dc9d1896..f1de6490 100644 --- a/patroni/async_executor.py +++ b/patroni/async_executor.py @@ -85,7 +85,7 @@ class AsyncExecutor(object): # if the func returned something (not None) - wake up main HA loop wakeup = func(*args) if args else func() return wakeup - except: + except Exception: logger.exception('Exception during execution of long running task %s', self.scheduled_action) finally: with self: diff --git a/patroni/dcs/__init__.py b/patroni/dcs/__init__.py index 7afbc995..fb97eedc 100644 --- a/patroni/dcs/__init__.py +++ b/patroni/dcs/__init__.py @@ -424,7 +424,7 @@ class AbstractDCS(object): with self._cluster_thread_lock: try: self._load_cluster() - except: + except Exception: self._cluster = None raise return self._cluster diff --git a/patroni/dcs/consul.py b/patroni/dcs/consul.py index 9cca6041..56cf63de 100644 --- a/patroni/dcs/consul.py +++ b/patroni/dcs/consul.py @@ -262,7 +262,7 @@ class Consul(AbstractDCS): self._cluster = Cluster(initialize, config, leader, last_leader_operation, members, failover, sync) except NotFound: self._cluster = Cluster(None, None, None, None, [], None, None) - except: + except Exception: logger.exception('get_cluster') raise ConsulError('Consul is not responding properly') diff --git a/patroni/dcs/zookeeper.py b/patroni/dcs/zookeeper.py index 82cdff4b..43bcb42b 100644 --- a/patroni/dcs/zookeeper.py +++ b/patroni/dcs/zookeeper.py @@ -206,7 +206,7 @@ class ZooKeeper(AbstractDCS): try: self._client.retry(self._client.create, path, value.encode('utf-8'), **kwargs) return True - except: + except Exception: return False def attempt_to_acquire_leader(self, permanent=False): @@ -221,7 +221,7 @@ class ZooKeeper(AbstractDCS): return True except NoNodeError: return value == '' or (index is None and self._create(self.failover_path, value)) - except: + except Exception: logging.exception('set_failover_value') return False @@ -248,7 +248,7 @@ class ZooKeeper(AbstractDCS): self._client.delete_async(self.member_path).get(timeout=1) except NoNodeError: pass - except: + except Exception: return False member = None @@ -268,7 +268,7 @@ class ZooKeeper(AbstractDCS): self._client.set_async(self.member_path, data).get(timeout=1) self._my_member_data = data return True - except: + except Exception: logger.exception('touch_member') return False @@ -285,9 +285,9 @@ class ZooKeeper(AbstractDCS): try: self._client.create_async(self.leader_optime_path, last_operation, makepath=True).get(timeout=1) return True - except: + except Exception: logger.exception('Failed to create %s', self.leader_optime_path) - except: + except Exception: logger.exception('Failed to update %s', self.leader_optime_path) return False @@ -307,7 +307,7 @@ class ZooKeeper(AbstractDCS): def cancel_initialization(self): try: self._client.retry(self._cancel_initialization) - except: + except Exception: logger.exception("Unable to delete initialize key") def delete_cluster(self): @@ -322,7 +322,7 @@ class ZooKeeper(AbstractDCS): return True except NoNodeError: return value == '' or (index is None and self._create(self.sync_path, value)) - except: + except Exception: logging.exception('set_sync_state_value') return False diff --git a/patroni/ha.py b/patroni/ha.py index 730bec56..9a50dfea 100644 --- a/patroni/ha.py +++ b/patroni/ha.py @@ -91,7 +91,7 @@ class Ha(object): if write_leader_optime: try: self.dcs.write_leader_optime(self.state_handler.last_operation()) - except: + except Exception: pass return ret @@ -124,7 +124,7 @@ class Ha(object): if not self._async_executor.busy and data['state'] in ['running', 'restarting', 'starting']: try: data['xlog_location'] = self.state_handler.wal_position(retry=False) - except: + except Exception: pass if self.patroni.scheduled_restart: scheduled_restart_data = self.patroni.scheduled_restart.copy() diff --git a/patroni/utils.py b/patroni/utils.py index 3ebd1f7a..bdf663ab 100644 --- a/patroni/utils.py +++ b/patroni/utils.py @@ -95,17 +95,17 @@ def strtol(value, strict=True): True """ value = str(value).strip() - l = len(value) + ln = len(value) i = 0 # skip sign: - if i < l and value[i] in ('-', '+'): + if i < ln and value[i] in ('-', '+'): i += 1 # we always expect to get digit in the beginning - if i < l and value[i].isdigit(): + if i < ln and value[i].isdigit(): if value[i] == '0': i += 1 - if i < l and value[i] in ('x', 'X'): # '0' followed by 'x': HEX + if i < ln and value[i] in ('x', 'X'): # '0' followed by 'x': HEX base = 16 i += 1 else: # just starts with '0': OCT @@ -114,7 +114,7 @@ def strtol(value, strict=True): base = 10 ret = None - while i <= l: + while i <= ln: try: # try to find maximally long number i += 1 # by giving to `int` longer and longer strings ret = int(value[:i], base) diff --git a/setup.py b/setup.py index a610b8fb..424d4d90 100644 --- a/setup.py +++ b/setup.py @@ -87,7 +87,7 @@ class PyTest(TestCommand): def run_tests(self): try: import pytest - except: + except Exception: raise RuntimeError('py.test is not installed, run: pip install pytest') params = {'args': self.test_args} if self.cov: diff --git a/tests/test_ha.py b/tests/test_ha.py index fa3d0cec..3f4a40a4 100644 --- a/tests/test_ha.py +++ b/tests/test_ha.py @@ -35,7 +35,7 @@ def get_cluster_not_initialized_without_leader(): def get_cluster_initialized_without_leader(leader=False, failover=None, sync=None): m1 = Member(0, 'leader', 28, {'conn_url': 'postgres://replicator:rep-pass@127.0.0.1:5435/postgres', 'api_url': 'http://127.0.0.1:8008/patroni', 'xlog_location': 4}) - l = Leader(0, 0, m1) if leader else None + leader = Leader(0, 0, m1) if leader else None m2 = Member(0, 'other', 28, {'conn_url': 'postgres://replicator:rep-pass@127.0.0.1:5436/postgres', 'api_url': 'http://127.0.0.1:8011/patroni', 'state': 'running', @@ -43,7 +43,7 @@ def get_cluster_initialized_without_leader(leader=False, failover=None, sync=Non 'scheduled_restart': {'schedule': "2100-01-01 10:53:07.560445+00:00", 'postgres_version': '99.0.0'}}) syncstate = SyncState(0 if sync else None, sync and sync[0], sync and sync[1]) - return get_cluster(True, l, [m1, m2], failover, syncstate) + return get_cluster(True, leader, [m1, m2], failover, syncstate) def get_cluster_initialized_with_leader(failover=None, sync=None): @@ -51,8 +51,8 @@ def get_cluster_initialized_with_leader(failover=None, sync=None): def get_cluster_initialized_with_only_leader(failover=None): - l = get_cluster_initialized_without_leader(leader=True, failover=failover).leader - return get_cluster(True, l, [l], failover, None) + leader = get_cluster_initialized_without_leader(leader=True, failover=failover).leader + return get_cluster(True, leader, [leader], failover, None) def get_node_status(reachable=True, in_recovery=True, wal_position=10, nofailover=False, watchdog_failed=False):