Skip to content

Commit

Permalink
If restart fails, try alternative restart command if available (#5500)
Browse files Browse the repository at this point in the history
* Use alternative restart command if available in distro overrides
  • Loading branch information
joohoi authored and bmw committed Apr 3, 2018
1 parent 2c502e6 commit 9996730
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
19 changes: 18 additions & 1 deletion certbot-apache/certbot_apache/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2000,10 +2000,27 @@ def _reload(self):
:raises .errors.MisconfigurationError: If reload fails
"""
error = ""
try:
util.run_script(self.constant("restart_cmd"))
except errors.SubprocessError as err:
raise errors.MisconfigurationError(str(err))
logger.info("Unable to restart apache using %s",
self.constant("restart_cmd"))
alt_restart = self.constant("restart_cmd_alt")
if alt_restart:
logger.debug("Trying alternative restart command: %s",
alt_restart)
# There is an alternative restart command available
# This usually is "restart" verb while original is "graceful"
try:
util.run_script(self.constant(
"restart_cmd_alt"))
return
except errors.SubprocessError as secerr:
error = str(secerr)
else:
error = str(err)
raise errors.MisconfigurationError(error)

def config_test(self): # pylint: disable=no-self-use
"""Check the configuration of Apache for errors.
Expand Down
1 change: 1 addition & 0 deletions certbot-apache/certbot_apache/override_centos.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CentOSConfigurator(configurator.ApacheConfigurator):
version_cmd=['apachectl', '-v'],
apache_cmd="apachectl",
restart_cmd=['apachectl', 'graceful'],
restart_cmd_alt=['apachectl', 'restart'],
conftest_cmd=['apachectl', 'configtest'],
enmod=None,
dismod=None,
Expand Down
1 change: 1 addition & 0 deletions certbot-apache/certbot_apache/override_gentoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class GentooConfigurator(configurator.ApacheConfigurator):
version_cmd=['/usr/sbin/apache2', '-v'],
apache_cmd="apache2ctl",
restart_cmd=['apache2ctl', 'graceful'],
restart_cmd_alt=['apache2ctl', 'restart'],
conftest_cmd=['apache2ctl', 'configtest'],
enmod=None,
dismod=None,
Expand Down
14 changes: 14 additions & 0 deletions certbot-apache/certbot_apache/tests/centos_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import mock

from certbot import errors

from certbot_apache import obj
from certbot_apache import override_centos
from certbot_apache.tests import util
Expand Down Expand Up @@ -121,5 +123,17 @@ def test_get_sysconfig_vars(self, mock_cfg):
self.assertTrue("MOCK_NOSEP" in self.config.parser.variables.keys())
self.assertEqual("NOSEP_VAL", self.config.parser.variables["NOSEP_TWO"])

@mock.patch("certbot_apache.configurator.util.run_script")
def test_alt_restart_works(self, mock_run_script):
mock_run_script.side_effect = [None, errors.SubprocessError, None]
self.config.restart()
self.assertEquals(mock_run_script.call_count, 3)

@mock.patch("certbot_apache.configurator.util.run_script")
def test_alt_restart_errors(self, mock_run_script):
mock_run_script.side_effect = [None,
errors.SubprocessError,
errors.SubprocessError]
self.assertRaises(errors.MisconfigurationError, self.config.restart)
if __name__ == "__main__":
unittest.main() # pragma: no cover
8 changes: 8 additions & 0 deletions certbot-apache/certbot_apache/tests/gentoo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import mock

from certbot import errors

from certbot_apache import override_gentoo
from certbot_apache import obj
from certbot_apache.tests import util
Expand Down Expand Up @@ -123,5 +125,11 @@ def mock_get_cfg(command):
self.assertEquals(len(self.config.parser.modules), 4)
self.assertTrue("mod_another.c" in self.config.parser.modules)

@mock.patch("certbot_apache.configurator.util.run_script")
def test_alt_restart_works(self, mock_run_script):
mock_run_script.side_effect = [None, errors.SubprocessError, None]
self.config.restart()
self.assertEquals(mock_run_script.call_count, 3)

if __name__ == "__main__":
unittest.main() # pragma: no cover

0 comments on commit 9996730

Please sign in to comment.