diff --git a/os_apply_config/apply_config.py b/os_apply_config/apply_config.py index fb55a9f..a1d97e1 100755 --- a/os_apply_config/apply_config.py +++ b/os_apply_config/apply_config.py @@ -29,12 +29,35 @@ from os_apply_config import renderers from os_apply_config import value_types from os_apply_config import version -TEMPLATES_DIR = os.environ.get('OS_CONFIG_APPLIER_TEMPLATES', None) -if TEMPLATES_DIR is None: - TEMPLATES_DIR = '/opt/stack/os-apply-config/templates' - if not os.path.isdir(TEMPLATES_DIR): - # Backwards compat with the old name. - TEMPLATES_DIR = '/opt/stack/os-config-applier/templates' +DEFAULT_TEMPLATES_DIR = '/usr/libexec/os-apply-config/templates' + + +def templates_dir(): + """Determine the default templates directory path + + If the OS_CONFIG_APPLIER_TEMPLATES environment variable has been set, + use its value. + Otherwise, select a default path based on which directories exist on the + system, preferring the newer paths but still allowing the old ones for + backwards compatibility. + """ + templates_dir = os.environ.get('OS_CONFIG_APPLIER_TEMPLATES', None) + if templates_dir is None: + templates_dir = '/opt/stack/os-apply-config/templates' + if not os.path.isdir(templates_dir): + # Backwards compat with the old name. + templates_dir = '/opt/stack/os-config-applier/templates' + if (os.path.isdir(templates_dir) and + not os.path.isdir(DEFAULT_TEMPLATES_DIR)): + logging.warning('Template directory %s is deprecated. The ' + 'recommended location for template files is %s', + templates_dir, DEFAULT_TEMPLATES_DIR) + else: + templates_dir = DEFAULT_TEMPLATES_DIR + return templates_dir + + +TEMPLATES_DIR = templates_dir() OS_CONFIG_FILES_PATH = os.environ.get( 'OS_CONFIG_FILES_PATH', '/var/lib/os-collect-config/os_config_files.json') OS_CONFIG_FILES_PATH_OLD = '/var/run/os-collect-config/os_config_files.json' diff --git a/os_apply_config/tests/test_apply_config.py b/os_apply_config/tests/test_apply_config.py index 1645f05..4009259 100644 --- a/os_apply_config/tests/test_apply_config.py +++ b/os_apply_config/tests/test_apply_config.py @@ -19,6 +19,7 @@ import os import tempfile import fixtures +import mock import testtools from os_apply_config import apply_config @@ -283,3 +284,25 @@ class OSConfigApplierTestCase(testtools.TestCase): err = self.assertRaises(ValueError, load_list, path) self.assertEqual( "No list defined in json file: %s" % path, str(err)) + + def test_default_templates_dir_current(self): + default = '/usr/libexec/os-apply-config/templates' + with mock.patch('os.path.isdir', lambda x: x == default): + self.assertEqual(default, apply_config.templates_dir()) + + def test_default_templates_dir_deprecated(self): + default = '/opt/stack/os-apply-config/templates' + with mock.patch('os.path.isdir', lambda x: x == default): + self.assertEqual(default, apply_config.templates_dir()) + + def test_default_templates_dir_old_deprecated(self): + default = '/opt/stack/os-config-applier/templates' + with mock.patch('os.path.isdir', lambda x: x == default): + self.assertEqual(default, apply_config.templates_dir()) + + def test_default_templates_dir_both(self): + default = '/usr/libexec/os-apply-config/templates' + deprecated = '/opt/stack/os-apply-config/templates' + with mock.patch('os.path.isdir', lambda x: (x == default or + x == deprecated)): + self.assertEqual(default, apply_config.templates_dir()) diff --git a/test-requirements.txt b/test-requirements.txt index ade0b3c..742be56 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -3,6 +3,7 @@ hacking>=0.8.0,<0.9 coverage>=3.6 discover fixtures>=0.3.14 +mock>=1.0 python-subunit>=0.0.18 sphinx>=1.1.2,<1.2 testrepository>=0.0.18