From 4c5635cca33d391dadde2236cab82141dd28d8bc Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Fri, 19 Jan 2018 15:08:02 +0200 Subject: [PATCH] Refactored the runners --- .pylintrc | 2 +- certbot/main.py | 63 +-------------------------- certbot/renewal.py | 6 ++- certbot/tests/main_test.py | 3 +- certbot/tests/renewupdater_test.py | 13 ++---- certbot/updater.py | 69 ++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 74 deletions(-) create mode 100644 certbot/updater.py diff --git a/.pylintrc b/.pylintrc index 6b38b1d48..36d8c286f 100644 --- a/.pylintrc +++ b/.pylintrc @@ -225,7 +225,7 @@ single-line-if-stmt=no no-space-check=trailing-comma # Maximum number of lines in a module -max-module-lines=1300 +max-module-lines=1250 # String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 # tab). diff --git a/certbot/main.py b/certbot/main.py index ef5c83db4..1decb1a3d 100644 --- a/certbot/main.py +++ b/certbot/main.py @@ -29,6 +29,7 @@ from certbot import log from certbot import renewal from certbot import reporter from certbot import storage +from certbot import updater from certbot import util from certbot.display import util as display_util, ops as display_ops @@ -1075,67 +1076,7 @@ def renew_cert(config, plugins, lineage): notify("new certificate deployed with reload of {0} server; fullchain is {1}".format( config.installer, lineage.fullchain), pause=False) # Run deployer - _run_renewal_deployer(renewed_lineage, installer, config) - -def run_renewal_updaters(config, plugins, lineage): - """Run updaters that the plugin supports - - :param config: Configuration object - :type config: interfaces.IConfig - - :param plugins: List of plugins - :type plugins: `list` of `str` - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - :returns: `None` - :rtype: None - """ - try: - # installers are used in auth mode to determine domain names - installer, _ = plug_sel.choose_configurator_plugins(config, plugins, "certonly") - except errors.PluginSelectionError as e: - logger.info("Could not choose appropriate plugin: %s", e) - raise - _run_updaters(lineage, installer, config) - -def _run_renewal_deployer(lineage, installer, config): - """Helper function to run deployer interface method if supported by the used - installer plugin. - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - :param installer: Installer object - :type installer: interfaces.IInstaller - - :returns: `None` - :rtype: None - """ - if config.installer_updates and isinstance(installer, interfaces.RenewDeployer): - installer.renew_deploy(lineage) - -def _run_updaters(lineage, installer, config): - """Helper function to run the updater interface methods if supported by the - used installer plugin. - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - :param installer: Installer object - :type installer: interfaces.IInstaller - - :returns: `None` - :rtype: None - """ - for domain in lineage.names(): - if config.server_tls_updates: - if isinstance(installer, interfaces.ServerTLSUpdater): - installer.server_tls_updates(domain) - if config.installer_updates: - if isinstance(installer, interfaces.GenericUpdater): - installer.generic_updates(domain) + updater.run_renewal_deployer(renewed_lineage, installer, config) def certonly(config, plugins): """Authenticate & obtain cert, but do not install it. diff --git a/certbot/renewal.py b/certbot/renewal.py index efec9e5ef..b73fb71f0 100644 --- a/certbot/renewal.py +++ b/certbot/renewal.py @@ -12,13 +12,14 @@ import zope.component import OpenSSL from certbot import cli - from certbot import crypto_util from certbot import errors from certbot import interfaces from certbot import util from certbot import hooks from certbot import storage +from certbot import updater + from certbot.plugins import disco as plugins_disco logger = logging.getLogger(__name__) @@ -427,7 +428,8 @@ def handle_renewal_request(config): else: renew_skipped.append(renewal_candidate.fullchain) # Run updater interface methods - main.run_renewal_updaters(lineage_config, plugins, renewal_candidate) + updater.run_renewal_updaters(lineage_config, plugins, + renewal_candidate) except Exception as e: # pylint: disable=broad-except # obtain_cert (presumably) encountered an unanticipated problem. logger.warning("Attempting to renew cert (%s) from %s produced an " diff --git a/certbot/tests/main_test.py b/certbot/tests/main_test.py index 5ecdb15ac..2f5d55a51 100644 --- a/certbot/tests/main_test.py +++ b/certbot/tests/main_test.py @@ -22,6 +22,7 @@ from certbot import configuration from certbot import crypto_util from certbot import errors from certbot import main +from certbot import updater from certbot import util from certbot.plugins import disco @@ -1373,7 +1374,7 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met mock_choose.side_effect = errors.PluginSelectionError self.assertRaises(errors.PluginSelectionError, main.renew_cert, None, None, None) - self.assertRaises(errors.PluginSelectionError, main.run_renewal_updaters, + self.assertRaises(errors.PluginSelectionError, updater.run_renewal_updaters, None, None, None) class UnregisterTest(unittest.TestCase): diff --git a/certbot/tests/renewupdater_test.py b/certbot/tests/renewupdater_test.py index eabdf69ca..e1c46e2b6 100644 --- a/certbot/tests/renewupdater_test.py +++ b/certbot/tests/renewupdater_test.py @@ -5,6 +5,7 @@ import mock from certbot import errors from certbot import interfaces from certbot import main +from certbot import updater from certbot.plugins import selection @@ -87,39 +88,33 @@ class RenewUpdaterTest(unittest.TestCase): mock_select.return_value = (mock_tls_installer, None) with mock.patch('certbot.main._init_le_client'): main.renew_cert(config, None, mock.MagicMock()) - #self.assertEqual(mock_tls_installer.callcounter.call_count, 2) self.assertTrue(mock_tls_installer.restart.called) mock_tls_installer.restart.reset_mock() mock_tls_installer.callcounter.reset_mock() - mock_tls_installer.renewed = [] - main.run_renewal_updaters(config, None, lineage) - #self.assertEqual(mock_tls_installer.callcounter.call_count, 2) + updater.run_renewal_updaters(config, None, lineage) self.assertFalse(mock_tls_installer.restart.called) # Generic Updater mock_select.return_value = (mock_generic_updater, None) with mock.patch('certbot.main._init_le_client'): main.renew_cert(config, None, mock.MagicMock()) - #self.assertEqual(mock_generic_updater.callcounter.call_count, 2) self.assertTrue(mock_generic_updater.restart.called) mock_generic_updater.restart.reset_mock() mock_generic_updater.callcounter.reset_mock() - mock_generic_updater.renewed = [] - main.run_renewal_updaters(config, None, lineage) + updater.run_renewal_updaters(config, None, lineage) self.assertEqual(mock_generic_updater.callcounter.call_count, 2) self.assertFalse(mock_generic_updater.restart.called) - self.assertFalse(any(mock_generic_updater.renewed)) def test_renew_deployer(self): config = self.get_config({"installer_updates": True}) lineage = mock.MagicMock() lineage.names.return_value = ['firstdomain', 'seconddomain'] mock_deployer = self.renew_deployer - main._run_renewal_deployer(lineage, mock_deployer, config) + updater.run_renewal_deployer(lineage, mock_deployer, config) self.assertTrue(mock_deployer.callcounter.called_with(lineage)) diff --git a/certbot/updater.py b/certbot/updater.py new file mode 100644 index 000000000..4a864ca6a --- /dev/null +++ b/certbot/updater.py @@ -0,0 +1,69 @@ +"""Updaters run at renewal""" +import logging + +from certbot import errors +from certbot import interfaces + +from certbot.plugins import selection as plug_sel + +logger = logging.getLogger(__name__) + +def run_renewal_updaters(config, plugins, lineage): + """Run updaters that the plugin supports + + :param config: Configuration object + :type config: interfaces.IConfig + + :param plugins: List of plugins + :type plugins: `list` of `str` + + :param lineage: Certificate lineage object + :type lineage: storage.RenewableCert + + :returns: `None` + :rtype: None + """ + try: + # installers are used in auth mode to determine domain names + installer, _ = plug_sel.choose_configurator_plugins(config, plugins, "certonly") + except errors.PluginSelectionError as e: + logger.info("Could not choose appropriate plugin: %s", e) + raise + _run_updaters(lineage, installer, config) + +def run_renewal_deployer(lineage, installer, config): + """Helper function to run deployer interface method if supported by the used + installer plugin. + + :param lineage: Certificate lineage object + :type lineage: storage.RenewableCert + + :param installer: Installer object + :type installer: interfaces.IInstaller + + :returns: `None` + :rtype: None + """ + if config.installer_updates and isinstance(installer, interfaces.RenewDeployer): + installer.renew_deploy(lineage) + +def _run_updaters(lineage, installer, config): + """Helper function to run the updater interface methods if supported by the + used installer plugin. + + :param lineage: Certificate lineage object + :type lineage: storage.RenewableCert + + :param installer: Installer object + :type installer: interfaces.IInstaller + + :returns: `None` + :rtype: None + """ + for domain in lineage.names(): + if config.server_tls_updates: + if isinstance(installer, interfaces.ServerTLSUpdater): + installer.server_tls_updates(domain) + if config.installer_updates: + if isinstance(installer, interfaces.GenericUpdater): + installer.generic_updates(domain)